<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script" trace="true">
    <a:init-procedure>
        <s:server-script import="con_print_path.js"><![CDATA[
            set_parameter_file_path();
        ]]></s:server-script>
        <s:server-script><![CDATA[
            importPackage(java.io);
            importPackage(Packages.hls.plugin.docx4j);
            importPackage(Packages.leaf.plugin.word2pdf);
            
            function copyFile(fOld, fNew) {
                var fis = new java.io.FileInputStream(fOld);
                var fos = new java.io.FileOutputStream(fNew);
                var b = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 4);
                var len = -1;
                while ((len = fis.read(b)) != -1) {
                    fos.write(b, 0, len);
                }
                fis.close();
                fos.close();
            }

            function get_file_length(path){
                var file=new File(String(path));
                if(file.exists() && file.isFile()){
                    //println(f.length());
                    length=file.length();
                    return length;}
                else{
                     return 0 ;}
            }

                var logger = $logger('server-script');
            var instance_id = $ctx.parameter.instance_id;
            var  user_id=$ctx.parameter.user_id;
            $bm('zjwfl.ZJWFL1060.wfl_instance_pdf').update({
                    instance_id: instance_id,
                     user_id :user_id
            });

            var file_templet_get_atm_path = $bm('zjwfl.ZJWFL1060.wfl_instance_pdf').queryAsMap({
                    instance_id: instance_id,
                    user_id :user_id
            });

            var from_file_data = file_templet_get_atm_path.getChildren();
            for (var i = 0;i < from_file_data.length;i++) {
                var to_file_path = $ctx.parameter.file_path;
                var record_data = from_file_data[i];
            
                var to_file_name = record_data.to_file_name ;
                var from_file_path = record_data.file_path;
                var guid_file_name_path = $bm('cont.CON500.con_contract_get_guid_file_name').queryAsMap();
                var guid_file_name_tables = guid_file_name_path.getChildren();
                to_file_path = to_file_path + guid_file_name_tables[0].guid_file_name + 'wfl' + record_data.content_id;
                copyFile(from_file_path, to_file_path);
                //放入实例id方便表单参数取值
                $ctx.current_parameter.document_id = instance_id;
                $ctx.current_parameter.user_id = user_id;

                try {
                    var brwt = new BookmarksReplaceWithText($instance('leaf.database.service.IDatabaseServiceFactory'), $instance('uncertain.ocm.IObjectRegistry'), $ctx.getData());
                    brwt.replaceBookmarkFromContent(to_file_path.toString(), record_data.content_id,'COMMON');
                } catch (e) {
                    raise_app_error(e);
                }
                word_to_pdf_sign(record_data.content_id,to_file_path.toString());

                //得到生成的pdf文件的大小
                var file_size=get_file_length(to_file_path.toString() + '.pdf');
                //关联附件表
                $bm('zjwfl.ZJWFL1060.wfl_instance_pdf').execute({
                    table_name: 'ZJ_WFL_WORKFLOW_INSTANCE',
                    instance_id: instance_id,
                    file_name: to_file_name.toString(),
                    file_path: to_file_path.toString(),
                    file_size: file_size,
                    user_id :user_id
                });

                var attachment_update = $bm('zjwfl.ZJWFL1060.wfl_instance_pdf');
                //更新转换的pdf附件,其附件表的字段
                //这里的附件关联表是工作流监控表
                    attachment_update.insert({
                        instance_id: instance_id,
                        table_name: 'ZJ_WFL_WORKFLOW_INSTANCE'
                    });
            
            }
            
            function word_to_pdf_sign(content_id,file_path) {
                importPackage(Packages.com.hl.signatures);
                importPackage(Packages.java.io);
                importPackage(Packages.java.util);

                var outputfilepath = file_path + '.pdf'; // 由于wordToPdf转换后pdf文件已经带有.pdf
                var wordToPdf = new WordToPdf($instance('uncertain.ocm.IObjectRegistry'), "jacob-1.19-x64.dll");
                wordToPdf.word2pdf(file_path, outputfilepath);

            }
        ]]></s:server-script>
    </a:init-procedure>
</a:service>