<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:s="leaf.plugin.script" xmlns:a="http://www.leaf-framework.org/application" xmlns:p="uncertain.proc" trace="true">
    <a:init-procedure>
        <s:server-script><![CDATA[
            var para = $ctx.current_parameter || $ctx.parameter;
            var validation_sql_bm = $bm('hls.HLS500N.hls_fin_calc_config_get_validation_sql');
            var validation_sql_map = validation_sql_bm.queryAsMap();
            var validation_sql_childs = validation_sql_map.getChildren();
            var validation_sql = validation_sql_childs[0].validation_sql;
            para.validation_sql = validation_sql;
            if (para.url) {
                var url = decodeURIComponent(para.url);
                var matches = url.match(/validation_sql=([^&]*)&?(.*$)/);
                validation_sql = matches[1];
                var para_list = {};
                if (matches[2]) {
                    var paras = matches[2].split('&');
                    for (var i = 0;i < paras.length;i++) {
                        var p = paras[i].split('=');
                        para_list[p[0]] = p[1];
                    }
                }
                validation_sql = validation_sql.replace(/\$\{@([^\}]*)\}/g, function(a, b) {
                    return "'" + (para_list[b] || '') + "'";
                });
            } else if (para.validation_sql) {
                validation_sql = para.validation_sql;
                validation_sql = validation_sql.replace(/\$\{@([^\}]*)\}/g, function(a, b) {
                    return "'" + (para[b] || '') + "'";
                });
            }
            validation_sql = validation_sql.replace(/\$\{\/session\/@([^\}]*)\}/g, function(a, b) {
                return "'" + $ctx.session[b] + "'";
            });
            var dsf = $instance('leaf.database.service.IDatabaseServiceFactory');
            var conn = dsf.getDataSource().getConnection();
            var stmt = conn.createStatement();
            var rs = stmt.executeQuery(validation_sql);
            var rsmd = rs.getMetaData();
            var count = rsmd.getColumnCount();
            for (var i = 1;i <= count;i++) {
                var fname = rsmd.getColumnName(i);
                var r = para.createChild("record");
                r.name = String(fname);
            }
            stmt.close();
            conn.close();
        ]]></s:server-script>
    </a:init-procedure>
    <a:service-output output="/parameter"/>
</a:service>
