<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: jiaxiaolong   
    $Date: 2016-05-20 下午03:41:29  
    $Revision: 1.0  
    $Purpose: 
-->
<bm:model xmlns:e="leaf.service.exception" xmlns:bm="http://www.leaf-framework.org/schema/bm" xmlns:f="leaf.database.features">
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
                SELECT
                    t.*,
                    (SELECT
                        'Y'
                    FROM
                        dual
                    WHERE
                        EXISTS
                        (SELECT
                            1
                        FROM
                            aut_unit_user_batch_tmp temp
                        WHERE
                            temp.session_id = ${/session/@session_id} AND
                            temp.user_id    = t.user_id AND
                            t.type          = 'USER'
                        )
                    ) checked
                FROM
                    (SELECT
                        user_id,
                        u.employee_id,
                        'USER' type,
                        to_number(TO_CHAR(u.user_id)
                        ||'.'
                        ||TO_CHAR(un.company_id)) * -1 children,
                        u.user_name,
                        u.description,
                        e.unit_id parent,
                        e.unit_code,
                        e.unit_name,
                        e.position_code,
                        e.position_name,
                        3 order_number
                    FROM
                        sys_user u,
                        exp_emp_position_unit_v e,
                        exp_org_unit un
                    WHERE
                        u.employee_id        = e.employee_id AND
                        e.unit_id            = un.unit_id AND
                        e.emp_assign_enabled = 'Y' AND
                        e.position_enabled   = 'Y' AND
                        e.unit_enabled       = 'Y'
                    UNION ALL
                    SELECT
                        user_id,
                        NULL,
                        'USER' type,
                        to_number(TO_CHAR(u.user_id)) * -1 children,
                        u.user_name,
                        u.description,
                        NULL parent,
                        NULL,
                        NULL,
                        NULL,
                        NULL,
                        3 order_number
                    FROM
                        sys_user u
                    WHERE
                        u.employee_id IS NULL
                    UNION ALL
                    SELECT
                        unit_id,
                        0,
                        'UNIT' type,
                        u.unit_id children,
                        NULL,
                        NULL,
                        NVL(u.parent_unit_id, u.company_id + 0.1) parent,
                        NULL,
                        u.unit_name,
                        NULL,
                        NULL,
                        2 order_number
                    FROM
                        exp_org_unit_v u
                    UNION ALL
                    SELECT
                        company_id,
                        0,
                        'COMPANY',
                        company_id + 0.1,
                        NULL,
                        NULL,
                        0.2,
                        NULL,
                        f.company_Short_name,
                        NULL,
                        NULL,
                        1 order_number
                    FROM
                        fnd_companies f
                    ) t
                ORDER BY
                    t.order_number ,
                    t.user_id
            ]]></bm:query-sql>
        </bm:operation>
        <bm:operation name="update">
            <bm:query-sql><![CDATA[
                begin
					aut_authority_save_pkg.init_aut_unit_batch_tmp(p_session_id => ${/session/@session_id},
					                                               p_user_id => ${@user_id},
					                                               p_o_user_id => ${/session/@user_id});
				end;
			]]></bm:query-sql>
        </bm:operation>
        <bm:operation name="delete">
            <bm:query-sql><![CDATA[
              delete from AUT_UNIT_USER_BATCH_TMP t where session_id = ${/session/@session_id}
              or t.creation_date  - trunc(sysdate) >1
			]]></bm:query-sql>
        </bm:operation>
    </bm:operations>
    <e:exception-descriptor-config>
        <e:exception-descriptor exception="java.sql.SQLException" handleClass="leaf.database.SQLExceptionDescriptor">
            <e:error-message code="1" message="AUT106.USER_ID_UNIQUE_ERROR"/>
        </e:exception-descriptor>
    </e:exception-descriptor-config>
</bm:model>
