server_layout_save.lsc 16.1 KB
Newer Older
Spencer Chang's avatar
Spencer Chang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:ns1="leaf.application.action" 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[
           //  println($ctx.parameter.toXML());
            function parseErrorMessage(e) {
                var err = $ctx.getChild('error');
                if (err) {
                    return err.message;
                }
                if (e.javaException) e = e.javaException;
                else return e.message;
                while (e && e.getCause && e.getCause())
                e = e.getCause();
                var serviceContext = Packages.leaf.service.ServiceContext.createServiceContext($ctx.getData());
                var ed = $instance('leaf.service.exception.IExceptionDescriptor')
                var map = new CompositeMap(ed.process(serviceContext, e));
                return map.message;
            }
            
            function recursive_dynamic_repeat(sub_child, sub_base_table, pk, q) {
                if (sub_base_table.base_table) {
                    var bind_name = q + $ctx.parameter.layout_code + '_' + sub_base_table.tab_code + '_' + sub_base_table.base_table.toLowerCase();
                    var sub_root = sub_child.getChild(bind_name);
                    if (sub_root) {
                        var sub_children = sub_root.getChildren();
                        if (bind_name.indexOf('prj_cdd_item_doc_ref') == -1) {
                            for (var m = 0;m < sub_children.length;m++) {
                                if (pk) {
                                    sub_children[m][pk] = sub_child[pk];
                                }
                                if (sub_base_table.base_table.toLowerCase() == 'prj_quotation' && !sub_children[m]['document_id']) {
                                    sub_children[m]['document_id'] = sub_child[pk];
                                }
                            }
                            recursive_dynamic_save(sub_root, sub_base_table.base_table, sub_base_table.query_only, sub_base_table.parent_table, sub_base_table.parent_base_table_pk, sub_base_table.check_before_delete_bm, sub_base_table.check_after_save_bm, sub_base_table.tab_code);
                        } else {
                            var spec_bm = $bm('layout.server_layout_doc_cdd_item');
                            for (var m = 0;m < sub_children.length;m++) {
                                sub_children[m]['document_id'] = sub_child[pk];
                                spec_bm[sub_children[m]['_status']](sub_children[m]);
                            }
                        }
                    }
                }
            }
            
            function document_check_before_delete(record, check_before_delete_bm) {
                var check_before_delete_bm = $bm(check_before_delete_bm);
                try {
                    check_before_delete_bm.update(record);
                } catch (e) {
                    raise_app_error(parseErrorMessage(e));
                }
            }
            
            function layout_field_bak_save(_status, detail, base_table, base_table_pk, parent_table, parent_base_table_pk, layout_code, tab_code) {
                for (var name in detail) {
                    var bak_name = name + '_bak';
                    if (name == '_status' || name == '_id' || !detail[bak_name]) {
                        continue;
                    }
                    if (detail[bak_name] == 'LAYOUT_FIELD_VALUE_INIT_NULL') {
                        if (_status == 'insert') {
                            detail[bak_name] = '此记录为新增记录';
                        } else if (_status == 'delete') {
                            detail[bak_name] = '此记录为删除记录';
                        }
                    }
                    var value = detail[name];
                    var old_value = detail[bak_name];
                    var field_bak_bm = $bm('layout.server_layout_field_value_bak');
                    var parent_table_pk_value = '';
                    if (parent_table) {
                        parent_table_pk_value = detail[parent_base_table_pk];
                    }
                    if (value != old_value && old_value) {
                        if (old_value == 'LAYOUT_FIELD_VALUE_INIT_NULL') {
                            old_value = '';
                        }
                        field_bak_bm.update({
                            layout_code: layout_code,
                            tab_code: tab_code,
                            table_name: base_table,
                            table_pk_value: detail[base_table_pk],
                            parent_table: parent_table,
                            parent_table_pk_value: parent_table_pk_value,
                            source_table: dynamic_source_table,
                            source_table_pk_value: dynamic_source_table_pk_value,
                            column_name: name,
                            from_column_value: old_value,
                            to_column_value: value,
                            confirm_flag: 'N'
                        });
                    }
                }
            }
            
            var dynamic_source_table = $ctx.parameter.dynamic_source_table || '';
            var dynamic_source_table_pk_value = $ctx.parameter.dynamic_source_table_pk_value || '';

            //add by chenlingfeng
            var  check_document_id = $ctx.parameter.document_id ;
              var  check_document_category =  $ctx.parameter.document_category ;
            //
            function recursive_dynamic_save(root, base_table, query_only, parent_table, parent_base_table_pk, check_before_delete_bm, check_after_save_bm, tab_code) {
                println('-------');
                 println(root);
                 println(base_table);
                 println( query_only);
                 println( parent_table);
                 println( parent_base_table_pk);
                 println( check_before_delete_bm);
                 println( check_after_save_bm);
                 println( tab_code);
                if (!root) return;
                var children = root.getChildren();
                if (!children.length) return;
                var query_base_table_bm = '';
                if ($ctx.parameter.tree_code) {
                    query_base_table_bm = $bm('layout.server_doc_layout_tree_tab_query');
                } else {
                    query_base_table_bm = $bm('layout.server_doc_layout_tab_query');
                }
                var sub_tn_map = '';
                var pk = '';
                var bm = '';
                var sub_base_tables = '';

                if (base_table) {
                    sub_tn_map = query_base_table_bm.queryAsMap({
                        "parent_table": base_table.toUpperCase(),
                        'layout_code': $ctx.parameter.layout_code,
                        'enabled_flag': 'Y'
                    });
                    $ctx.dynamic_base_table = base_table;
                    sub_base_tables = sub_tn_map.getChildren();
                    if (query_only == 'Y' && sub_base_tables.length == 0) {
                        return;
                    }
                    bm = $bm('layout.server_layout_common');
                    pk = $ctx.pkmap[base_table];
                } else {
                    sub_tn_map = query_base_table_bm.queryAsMap({
                        "virtual_parent_flag": 'Y',
                        'layout_code': $ctx.parameter.layout_code,
                        'enabled_flag': 'Y'
                    });
                    sub_base_tables = sub_tn_map.getChildren();
                    if (query_only == 'Y' && sub_base_tables.length == 0) {
                        return;
                    }
                }

                for (var i = 0;i < children.length;i++) {
                    var _status = children[i]._status;
                    if (base_table) {
                        var parent_error_code = '';
                        if (!parent_table && !dynamic_source_table) {
                            dynamic_source_table = base_table;
                            dynamic_source_table_pk_value = children[i][pk];
                        }
                        if (_status == 'delete') {
                            if (!check_before_delete_bm) {
                                var delete_map = query_base_table_bm.queryAsMap({
                                    'layout_code': $ctx.parameter.layout_code,
                                    'tab_code': $ctx.parameter.tab_code,
                                    'enabled_flag': 'Y'
                                });
                                var delete_map_tables = delete_map.getChildren();
                                if (delete_map_tables.length && delete_map_tables[0].check_before_delete_bm) {
                                    document_check_before_delete(children[i], delete_map_tables[0].check_before_delete_bm);
                                }
                            } else {
                                document_check_before_delete(children[i], check_before_delete_bm);
                            }
                        }
                        try {
                            if (parent_table && parent_base_table_pk && !children[i][parent_base_table_pk]) {
                                parent_error_code = 'HLS_TABLE_UNIQUE.SAVE_PARENT_FIRST';
                                raise_app_error(parent_error_code);
                            }

                            //add by chenlingfeng 在没有传document_category和document_id的情况下
                            if( !check_document_category){
18083's avatar
18083 committed
186 187
                                      if(dynamic_source_table == 'PRJ_PROJECT'||dynamic_source_table == 'prj_project'){
                                        if(!check_document_category && $ctx.parameter.layout_code!='PUR_ORDER_DETEAIL'){
Spencer Chang's avatar
Spencer Chang committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                                        check_document_category ='PROJECT';
                                        }
                                    }
                                    if(dynamic_source_table == 'CON_CONTRACT'||dynamic_source_table == 'con_contract'){
                                        if(!check_document_category){
                                        check_document_category ='CONTRACT';
                                        }
                                    }
                                    if(dynamic_source_table == 'HLS_BP_MASTER'||dynamic_source_table == 'hls_bp_master'){
                                        if(!check_document_category){
                                        check_document_category ='BP';
                                        }
                                    }
                                }
                                if (!check_document_id){
                                         check_document_id=dynamic_source_table_pk_value;
                                }

                            if (query_only != 'Y') {
                                if (_status != 'insert') {
                                    layout_field_bak_save(_status, children[i], base_table, pk, parent_table, parent_base_table_pk, $ctx.parameter.layout_code, tab_code);
                                }
                                bm.option = {
                                    updatePassedFieldOnly: 'true'
                                }
                                bm[_status](children[i]);
                                if (_status == 'insert') {
                                    layout_field_bak_save(_status, children[i], base_table, pk, parent_table, parent_base_table_pk, $ctx.parameter.layout_code, tab_code);
                                }
                                if (check_after_save_bm && i == children.length - 1) {
                                    document_check_after_save(children[i], check_after_save_bm);
                                }

                                //add by chenlingfeng
                                if(!check_document_id&&!parent_table&&parent_base_table_pk){
                                check_document_id =  children[i][parent_base_table_pk];
                                }

                            }
                        } catch (e) {
                            if (e.javaException) e = e.javaException;
                            while (e && e.getCause && e.getCause()) e = e.getCause();
                            var error_code = e.getErrorCode();
                            if (error_code == 1) {
232 233
                            if(base_table.toUpperCase()=='HLS_LEASE_ITEM'){
                            raise_app_error('机种代码和型号不能重复!');
niminmin's avatar
niminmin committed
234 235 236
                            }
                            else if(base_table.toUpperCase()=='HLS_BP_MASTER'){
                            raise_app_error('商业伙伴信息不能重复!');
237
                            }else
Spencer Chang's avatar
Spencer Chang committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
                                raise_app_error('HLS_TABLE_UNIQUE.' + base_table.toUpperCase());
                            }
                            if (parent_error_code) {
                                raise_app_error(parent_error_code);
                            } else {
                                raise_app_error($ctx.get('/error/@message') || String(e));
                            }
                        }
                    }

                    for (var j = 0;j < sub_base_tables.length;j++) {

                        if (sub_base_tables[j].repeat_bindname_object == 'TENANT') {
                            for (var q = 1;q <= $ctx.parameter.number_of_tenant;q++) {
                                recursive_dynamic_repeat(children[i], sub_base_tables[j], pk, q);
                            }
                        } else if (sub_base_tables[j].repeat_bindname_object == 'GUARANTOR') {
                            for (var q = 1;q <= $ctx.parameter.number_of_guarantor;q++) {
                                recursive_dynamic_repeat(children[i], sub_base_tables[j], pk, q);
                            }
                        } else if (sub_base_tables[j].repeat_bindname_object == 'ACTUAL_CONTROLLER') {
                            for (var q = 1;q <= $ctx.parameter.number_of_actual_controller;q++) {
                                recursive_dynamic_repeat(children[i], sub_base_tables[j], pk, q);
                            }
                        } else if (sub_base_tables[j].repeat_bindname_object == 'QUOTATION') {
                            for (var q = 1;q <= $ctx.parameter.number_of_quotation;q++) {
                                recursive_dynamic_repeat(children[i], sub_base_tables[j], pk, q);
                            }
                        } else {
                            recursive_dynamic_repeat(children[i], sub_base_tables[j], pk, '');
                        }
                    }
                }
            }

            function finnal_check_after_save() {
            var finnal_check_bm = $bm('layout.server_layout_finnal_check_after_save');
            finnal_check_bm.update({
                document_id: $ctx.parameter.document_id||check_document_id,
                document_category: $ctx.parameter.document_category||check_document_category,
                document_type: $ctx.parameter.document_type,
                function_code: $ctx.parameter.function_code,
                function_usage: $ctx.parameter.function_usage
                });
            }
            
            function document_check_after_save(record, check_after_save_bm) {
                var last_check_bm = $bm(check_after_save_bm);
                last_check_bm.update(record);
            }
            recursive_dynamic_save($ctx.parameter, $ctx.parameter.base_table, $ctx.parameter.query_only, $ctx.parameter.parent_table, $ctx.parameter.parent_base_table_pk);

            if (($ctx.parameter.document_id||check_document_id) && ($ctx.parameter.document_category||check_document_category) && (!$ctx.parameter.delete_flag || $ctx.parameter.delete_flag == 'N')) {
                finnal_check_after_save();
            }


        ]]></s:server-script>
    </a:init-procedure>
    <a:service-output output="/parameter"/>
</a:service>