	importPackage(java.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();
	}
	
	function update_file_size(file_path,file_name,tmp_id){
		var file = new File(file_path + file_name);
		$bm('prj.PRJ501N.bgfl_generate_word_tmp').update({
			file_size	   :file.length(),
            tmp_id         :tmp_id
		});
		
	}
	
	function download_file(file_path,file_name){
		$ctx["__request_type__"] = 'file'; // request file not JSON
	    var resp = $ctx['_instance.javax.servlet.http.HttpServletResponse'];
	    resp.setHeader("Pragma", "No-cache");
	    resp.setHeader("Cache-Control", "no-cache, must-revalidate");
	    resp.setHeader("Content-disposition", "attachment; filename=" + encodeURI(file_name,'utf-8'));
	    resp.setDateHeader("Expires", 0);
	    resp.setContentType("application/x-msdownload");
	    try{
	    	var os = resp.getOutputStream();
	    	write_os_to_file(file_path+file_name, os);
		    os.flush();
	    }catch(e){
	    	$logger("server-script").severe(e.message);
	    }
	}
	
	function download(tmp_id){
		var tmp = $bm('prj.PRJ501N.bgfl_generate_word_tmp');
        var result = tmp.queryAsMap({
        	tmp_id:tmp_id
        });
        var arr = result.getChildren();
        download_file(arr[0].file_path,arr[0].file_name);
	}
	
	function upload_atm(tmp_id,document_category,document_table,document_id,tab_group){
		$bm('prj.PRJ501N.bgfl_generate_word_tmp').execute({
			document_category:document_category,
			document_table :document_table,
            document_id    :document_id,
            tmp_id         :tmp_id,
            tab_group	   :tab_group
		});
	}
	
	
