<?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>
        <a:model-update model="hls.HLS811.hls_doc_file_templet_get_atm"/>
        <s:server-script import="contract_print_path.js"><![CDATA[
        importPackage(java.io);
        importPackage(Packages.hls.plugin.docx4j);
        importPackage(Packages.leaf.plugin.word2pdf);
        importPackage(Packages.org.apache.commons.io);

        function write_os_to_file(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();
        }

            $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");
            resp.setDateHeader("Expires", 0);
            resp.setContentType("application/x-msdownload");

            try {
				var sys_url_bm=$bm('cont.CON301N.get_file_address');
				var FileQuery = sys_url_bm.queryAsMap({
									 record_id: $ctx.parameter.record_id
									});

                var arr = FileQuery.getChildren();
				var file_name = arr[0].file_name;
				var file_path = arr[0].file_path;
				 println('file_name = '+ file_name);
				 println('file_path = '+ file_path);

				resp.setHeader("Content-disposition", "attachment; filename=" + encodeURI(file_name, 'UTF-8'));
				var os = resp.getOutputStream();
				//transfer(file_path, os);

				write_os_to_file(file_path, os);
				os.flush();
            } catch (e) {
                println(e);
            }

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