<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"https://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <query name="findPermissionByUserId">
        <![CDATA[
            SELECT P
            FROM Permission P
            INNER JOIN UserHasPermission UHP ON P.id = UHP.permission
            WHERE 
                UHP.user = :user AND
                P.active =  :active AND
                P.generalAccess = false
            ORDER BY P.leftToRightOrder, P.topToBottomOrder ASC
        ]]>
    </query>
    
    <query name="findPermissionThatUserDoesNotHave">
        <![CDATA[
            SELECT P
            FROM Permission P
            WHERE 
                P.id NOT IN (
                    SELECT UHP.permission 
                    FROM User U
                    INNER JOIN UserHasPermission UHP ON U.id = UHP.user
                    WHERE UHP.user = :user
                    ) AND
                P.active =  :active AND
                P.generalAccess = false
            ORDER BY P.leftToRightOrder, P.topToBottomOrder ASC
        ]]>
    </query>
    
    <query name="findPermissionsByPublicAccess">
        <![CDATA[
            SELECT P
            FROM Permission P
            WHERE 
                P.generalAccess = :generalAccess AND
                P.active = true
            ORDER BY P.leftToRightOrder, P.topToBottomOrder ASC    
        ]]>
    </query>
    
    <query name="findApplicationMenuPermissions">
        <![CDATA[
            SELECT P
            FROM Permission P
            WHERE 
                P.generalAccess = :generalAccess AND
                P.active = :active AND
                P.applicationMenu = :applicationMenu
            ORDER BY P.leftToRightOrder, P.topToBottomOrder ASC    
        ]]>
    </query>
    
    <query name="findPermissions">
        <![CDATA[
            SELECT P
            FROM Permission P
            WHERE 
                P.active = true
            ORDER BY P.leftToRightOrder, P.topToBottomOrder ASC    
        ]]>
    </query>
</hibernate-mapping>