<?xml version="1.0" encoding="UTF-8"?>
<bm:model xmlns:e="leaf.service.exception" xmlns:s="leaf.plugin.script" xmlns:o="leaf.database.local.oracle" xmlns:bm="http://www.leaf-framework.org/schema/bm" xmlns:f="leaf.database.features" alias="e">
    <bm:fields/>
    <bm:primary-key/>
    <bm:features>
        <f:standard-who/>
        <!-- make sure bm-script must be the LAST feature -->
        <s:bm-script><![CDATA[
            function new_bm_map(name) {
                return new CompositeMap("bm", 'http://www.leaf-framework.org/schema/bm', name);
            }
            
            function create_query_field(query_field) {
                if (query_field) {
                    var qf = new_bm_map('query-field');
                    qf.field = query_field;
                    qf.queryoperator = '='; //attribute name must in lowercase
                    $this.addQueryField(qf.getData()); //get it's java type...
                }
            }
            var table_name = $ctx.dynamic_base_table || $ctx.parameter.dynamic_base_table;
            // println("table_name:"+table_name);
            if (!table_name) raise_app_error("table name not found");
            $this.setBaseTable(table_name); //get base table from somewhere...
            var dsf = $instance('leaf.database.service.DatabaseServiceFactory');
            var it = new Packages.leaf.plugin.entity.OracleTableFields();
            var tableName = $this.getBaseTable();
            var fMap = new CompositeMap(it.getTableFields(dsf, tableName));
            // println(fMap.toXML());
            if (fMap.msg) raise_app_error('error while get table fields:' + fMap.msg + " :" + table_name);
            var flds = fMap.getChildren();
            for (var i = 0;i < flds.length;i++) {
                var r = flds[i];
                var f = Packages.leaf.bm.Field().createField(r.name.toLowerCase());
                f.setPhysicalName(r.name);
                if (r.dbtype == 'NUMBER') {
                    f.setDataType('java.lang.Double');
                } else if (r.dbtype == 'CLOB') {
                    f.setDataType('java.sql.Clob');
                } else {
                    f.setDataType(r.javatype);
                }
                f.setDatabaseType(r.dbtype);
                f.setPrompt(r.prompt);
                $this.addField(f);
                if (r.ispk) {
                    //dynamic primary-key
                    if (!$ctx.pkmap) $ctx.pkmap = {};
                    $ctx.pkmap[table_name] = r.name.toLowerCase();
                    var pk = new_bm_map('pk-field');
                    // println("pk:" + r.toXML());
                    //f.setInsertExpression(table_name+'_s.nextval');
                    pk.name = r.name.toLowerCase();
                    $this.getObjectContext().getChild('primary-key').addChild(pk.getData()); //get it's java type...
                }
            }
            var qf_root = $ctx.model.getChild('rootpath_query_field');
            // println("pk:" + qf_root.toXML());
            var qfs = ( !! qf_root) ? qf_root.getChildren() : [];
            var model = new CompositeMap($this.getObjectContext());
            var datafilters = model.createChildNS('data-filters');
            if (table_name == 'HLS_FIN_CALCULATOR_LN' || table_name == 'HLS_FIN_CALCULATOR_LN_FORMULA') {
                create_query_field('calc_line_id');
            }
            var dataf = datafilters.createChildNS('data-filter');
            dataf.name = 'calc_session_id';
            dataf.enforceoperations = 'query';
            dataf.expression = 'calc_session_id=${@calc_session_id}';
            // println($this.getObjectContext().toXML());
        ]]></s:bm-script>
    </bm:features>
    <bm:relations/>
    <bm:query-fields/>
</bm:model>
