<?xml version="1.0" encoding="UTF-8"?>
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm" needAccessControl="fasle">
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
                SELECT
                    fm.table_name,
                    fm.table_pk_value,
                    fm.record_id,
                    f.attachment_id,
                    f.file_path,
                    pci.description
                    || '-'
                    || f.file_name file_name
                FROM
                    (SELECT
                        pcf.check_id,
                        pcf.document_id,
                        pcf.document_table,
                        pck.cdd_item_id,
                        pck.send_flag,
                        pck.paper_required,
                        pck.attachment_required,
                        pck.not_applicable
                    FROM
                        prj_cdd_item_doc_ref pcf,
                        prj_cdd_item_check pck
                    WHERE
                        pcf.document_table = ${@table_name} AND
                        pcf.document_id    = ${@table_pk_value} AND
                        pck.check_id       = pcf.check_id
                    ) pp,
                    prj_cdd_item pci,
                    prj_cdd_item_list_grp_tab lgt,
                    prj_cdd_item_tab_group tg,
                    (SELECT
                        sg.tab_group_id
                    FROM
                        PRJ_CDD_ITEM_TAB_SUB_GROUP sg START
                    WITH sg.parent_tab_group_id in (86) CONNECT BY prior sg.tab_group_id = sg.parent_tab_group_id
                    UNION
                    SELECT to_number('86') FROM dual
                    ) sg,
                    fnd_atm_attachment_multi fm,
                    fnd_atm_attachment f
                WHERE
                    pci.cdd_item_id   = lgt.cdd_item_id AND
                    lgt.TAB_GROUP_ID  = tg.TAB_GROUP_ID AND
                    lgt.TAB_GROUP_ID  = sg.tab_group_id AND
                    lgt.enabled_flag  = 'Y' AND
                    tg.enabled_flag   = 'Y' AND
                    pci.cdd_item_id   = pp.cdd_item_id(+) AND
                    pci.cdd_list_id   = ${@cdd_list_id} AND
                    lgt.cdd_list_id   = pci.cdd_list_id AND
                    pci.enabled_flag  = 'Y' AND
                    fm.attachment_id  = f.attachment_id AND
                    fm.table_name     = 'PRJ_CDD_ITEM_CHECK' AND
                    fm.table_pk_value = pp.check_id
                ORDER BY
                    lgt.seq_num ASC
            ]]></bm:query-sql>
            <bm:parameters>
                <bm:parameter name="table_pk_value"/>
                <bm:parameter name="table_name"/>
            </bm:parameters>
        </bm:operation>
    </bm:operations>
    <bm:fields>
        <bm:field name="table_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="TABLE_NAME" required="true"/>
        <bm:field name="table_pk_value" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="TABLE_PK_VALUE" required="true"/>
        <bm:field name="record_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="RECORD_ID" required="true"/>
        <bm:field name="attachment_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="ATTACHMENT_ID" required="true"/>
        <bm:field name="file_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="FILE_NAME"/>
        <bm:field name="file_path" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="FILE_PATH"/>
    </bm:fields>
</bm:model>
