con_atm_batch_pdf.lsc 13.2 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 186 187 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 232 233 234 235 236 237 238 239 240 241 242
<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:s="leaf.plugin.script" xmlns:a="http://www.leaf-framework.org/application" trace="true">
    <a:init-procedure>
        <a:model-query fetchAll="true" model="cont.CON500.con_contract_sign_flag" rootPath="sign_flag_path"/>
        <s:server-script import="seal_util.js;jacob/jacob.js"><![CDATA[
            importPackage(java.util.zip);
            importPackage(java.io);
            importPackage(Packages.hls.plugin.docx4j);
            importPackage(Packages.hls.plugin.jacob.engine);
            importPackage(Packages.hls.plugin.jacobservice.servlet);
            //importPackage(org.apache.tools.zip); /*可以传入参数*/
            //importPackage(java.util.zip);
            
            var record_data = $ctx.get('/model/sign_flag_path').getChildren();
            var sign_flag = record_data[0].sign_flag;
            
            function writeFile(zos, fn, fp) {
                var ze = new ZipEntry(fn);
                //zos.setEncoding("UTF-8");//如果是org.apache.tools.zip需要追加字符集
                zos.putNextEntry(ze);
                var fis = new FileInputStream(fp);
                var b = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 64);
                var len = -1;
                while ((len = fis.read(b)) != -1) {
                    zos.write(b, 0, len);
                }
                fis.close();
            }
            
            
            function transfer(file, os) {
                var fis = new FileInputStream(file);
                var b = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 64);
                var len = -1;
                while ((len = fis.read(b)) != -1) {
                    os.write(b, 0, len);
                }
                fis.close();
            }
            
            
            function getdate() {
                var now = new Date()
                y = now.getFullYear()
                m = now.getMonth() + 1
                d = now.getDate()
                m = m < 10 ? "0" + m : m
                d = d < 10 ? "0" + d : d
                return y + "" + m + "" + d
            }
            $ctx["__request_type__"] = 'file'; //to indicate this request is not a JSON_REQUEST
            //var resp = $ctx['_instance.javax.servlet.http.HttpServletResponse'];
            //resp.setHeader("Pragma", "No-cache");
            //resp.setHeader("Cache-Control", "no-cache, must-revalidate");
            var date_str = getdate();
            var doc_code = '合同文本附件'
            var type = $ctx.parameter.type;
            var zip_filename = doc_code + '-' + date_str + ".zip";
            var file_path;
            var fnd_atm_flag = $ctx.parameter.fnd_atm_flag;
            //
            //resp.setDateHeader("Expires", 0);
            //resp.setContentType("application/x-msdownload");
            
            try {
                var attachment_batch_dl = $bm('cont.CON500.con_contract_content_download');
                var result = attachment_batch_dl.queryAsMap({
                    content_id: $ctx.parameter.content_id,
                    //842
                    contract_id: $ctx.parameter.contract_id,
                    //'3597'
                });
                var arr = result.getChildren();
                if (arr.length >= 1) {
                    if (type == 'SIN') {
                        //单个下载
                        var file_name = arr[0].file_name2;
                        if (fnd_atm_flag == 'Y') {
                            file_path = arr[0].file_path;
                        } else {
                            file_path = arr[0].file_path + file_name;
                        }
            			var outputfilepath = file_path + '_pdf';
            			var file_type = new File(outputfilepath);
            			 if (file_type.exists()&&file_type.length()>0){
            			     null;
            			 }else{
            			 	var pe = new PdfConvertEngine($instance('leaf.database.service.IDatabaseServiceFactory'), $instance('uncertain.ocm.IObjectRegistry'), $ctx.getData());
	                        var attachment_id = arr[0].attachment_id;
	                        var server = jacob_config.jacobUrl + attachment_id;
	                        var target_path = jacob_config.target_path;
	                        var url = jacob_config.jacobserviceUrl + '?target_url=' + server + '&target_path=' + target_path + '&type=wps';
	                        try {
	                            pe.run(url);
	                        } catch (e) {
	                            raise_app_error('转化PDF失败:' + e);
	                        }    
            			 }
                        
                        
                        
                        if (file_name.substring(file_name.length - 4) == '.doc') {
                            file_name = file_name.substring(0, file_name.length - 4);
                        } else if (file_name.substring(file_name.length - 5) == '.docx') {
                            file_name = file_name.substring(0, file_name.length - 5);
                        }
            			$ctx.parameter.sign_status = 'N';
                        var sign_path = null;
                        var file_sign = null;
                        
                        //签章开启标志
                       if (sign_flag == 'Y'){
                        /*电子签章先不做*/
                        /* if (arr[0].templet_code == 'HY_RISK' || arr[0].templet_code == 'HY_RES_RELEVANCE' || arr[0].templet_code == 'GH_WITHHOLD_AGREE') {
                            sign_path = outputfilepath;
                        } else {
                            sign_path = seal(file_name, outputfilepath);
                        } */
                        if (arr[0].templet_code == 'CON_LEASE'||arr[0].templet_code == 'CON_LEASE_ORG'||arr[0].templet_code == 'CON_LEASE1'||arr[0].templet_code == 'CON_LEASE2' ){
                            //盖骑缝章包含签章
                            sign_path = outputfilepath+'_sign';
	                        file_sign = new File(sign_path);
	                        if (file_sign.exists()&&file_sign.length()>0){
	                            null;
	                        }else{
	                            sign_path = seal(file_name, outputfilepath,'Y');    
	                        }
                            //sign_path = seal(file_name, outputfilepath,'Y');
                        }else if(arr[0].templet_code == 'CON_ITEM_INFORMATION'||arr[0].templet_code == 'CON_PAY_CCB'||arr[0].templet_code=='PAY_PLAN1'||arr[0].templet_code=='PAY_PLAN'||
                        arr[0].templet_code=='CON_PAY_BOC'||arr[0].templet_code=='CON_PAY_PSBC'||arr[0].templet_code=='CON_HZTZ'||arr[0].templet_code=='CON_GUARANT'||arr[0].templet_code=='CON_MORTGAGE'||arr[0].templet_code=='CON_PURCHASE'){
                            //盖签章
                            sign_path = outputfilepath+'_sign';
	                        file_sign = new File(sign_path);
	                        if (file_sign.exists()&&file_sign.length()>0){
	                            null;
	                        }else{
	                            sign_path = seal(file_name, outputfilepath,'N'); 
	                        }
                            //sign_path = seal(file_name, outputfilepath,'N');
                        }
                        else{
                        	sign_path = outputfilepath;    
                        }
                            }
            
                        //resp.setHeader("Content-disposition", "attachment; filename=" + encodeURI(file_name + '.pdf', 'UTF-8'));
                        //var os = resp.getOutputStream();
                        //transfer(sign_path, os);
                        //os.flush();
                    } else if (type == 'ZIP') {
                        //打包下载
                        zip_filename = arr[0].bp_id_tenant_n + '-' + arr[0].contract_number + '-' + arr[0].project_number + ".zip";
                        //resp.setHeader("Content-disposition", "attachment; filename=" + encodeURI(zip_filename, 'UTF-8'));
                        //var zos = new ZipOutputStream(resp.getOutputStream());
            
                        //zos.setEncoding("GBK"); //如果是org.apache.tools.zip需要追加字符集
            
                        for (var i = 0;i < arr.length;i++) {
                            var f = arr[i];
                            var temp_file_name = f.file_name2,
                                temp_file_path;
                            if (fnd_atm_flag == 'Y') {
                                temp_file_path = f.file_path;
                            } else {
                                temp_file_path = f.file_path + temp_file_name;
                            }
                            if (temp_file_name.substring(temp_file_name.length - 4) == '.doc') {
                                temp_file_name = temp_file_name.substring(0, temp_file_name.length - 4);
                            } else if (temp_file_name.substring(temp_file_name.length - 5) == '.docx') {
                                temp_file_name = temp_file_name.substring(0, temp_file_name.length - 5);
                            }
                            // var brwt = new ConvertOutPDF();
                            // var outputfilepath = brwt.convertPdf(temp_file_path);
                            var outputfilepath = temp_file_path + '_pdf';
                            var file_type = new File(outputfilepath);
	            			if (file_type.exists()&&file_type.length()>0){
	            			     null;
	            			}else{
	            				var pe = new PdfConvertEngine($instance('leaf.database.service.IDatabaseServiceFactory'), $instance('uncertain.ocm.IObjectRegistry'), $ctx.getData());
	                            var attachment_id = f.attachment_id;
	                            var server = jacob_config.jacobUrl + attachment_id;
	                            var target_path = jacob_config.target_path;
	                            var url = jacob_config.jacobserviceUrl + '?target_url=' + server + '&target_path=' + target_path + '&type=wps';
	                            try {
	                                pe.run(url);
	                            } catch (e) {
	                                raise_app_error('转化PDF失败:' + e);
	                            }     
	            			}
                            
                            var sign_path = null;
                            var file_sign = null;
                            $ctx.parameter.sign_status = 'N';
                            
                            //签章开启标志
                            if (sign_flag == 'Y'){
                            /*电子签章先不做*/
                            /* if (f.templet_code == 'HY_RISK' || f.templet_code == 'HY_RES_RELEVANCE' || f.templet_code == 'GH_WITHHOLD_AGREE') {
                                sign_path = outputfilepath;
                            } else {
                                sign_path = seal(file_name, outputfilepath);
                            } */
                            //sign_path = outputfilepath;
                            if (f.templet_code == 'CON_LEASE'||f.templet_code == 'CON_LEASE_ORG'||f.templet_code == 'CON_LEASE1'||f.templet_code == 'CON_LEASE2' ){
	                            //盖骑缝章包含签章
	                            sign_path = outputfilepath+'_sign';
	                            file_sign = new File(sign_path);
	                            if (file_sign.exists()&&file_sign.length()>0){
	                                null
	                            }else{
	                            	sign_path = seal(file_name, outputfilepath,'Y');    
	                            }
	                        }else if(f.templet_code == 'CON_GUR_ORG' || f.templet_code == 'CON_ITEM_INFORMATION'||f.templet_code == 'CON_PAY_CCB'||f.templet_code=='PAY_PLAN1'||f.templet_code=='PAY_PLAN'||
	                        f.templet_code=='CON_PAY_BOC'||f.templet_code=='CON_PAY_PSBC'||arr[0].templet_code=='CON_HZTZ'||arr[0].templet_code=='CON_GUARANT'||arr[0].templet_code=='CON_MORTGAGE'||arr[0].templet_code=='CON_PURCHASE'){
	                            //盖签章
	                            sign_path = outputfilepath+'_sign';
	                            file_sign = new File(sign_path);
	                            if (file_sign.exists()&&file_sign.length()>0){
	                                null
	                            }else{
	                            	sign_path = seal(file_name, outputfilepath,'N');
	                            }
	                        }
	                        else{
	                        	sign_path = outputfilepath;    
	                        }
                            if (sign_path && temp_file_name) {
                                //writeFile(zos, temp_file_name + '.pdf', sign_path);
                            }
                            }
                        }
                        //zos.close();
                    }
                }
            } catch (e) {
                println(e);
                var logger = $logger("server-script");
                logger.severe(e.message);
            }
        ]]></s:server-script>
    </a:init-procedure>
</a:service>