001/* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016package org.opengion.plugin.report; 017 018import java.io.BufferedWriter; 019import java.io.File; 020import java.io.FileNotFoundException; 021import java.io.FileOutputStream; 022import java.io.OutputStreamWriter; 023import java.io.UnsupportedEncodingException; 024import org.opengion.hayabusa.common.HybsSystemException; 025import org.opengion.hayabusa.common.HybsSystem; 026import org.opengion.hayabusa.report.AbstractCSVPrintPointService; 027import org.opengion.fukurou.util.StringUtil; 028 029/** 030 * ユニリタ「Report & Form Warehouse」に対応したCSV形式でデータを作成します。 031 * 032 * CSVはシステムリソースRFW_CSV_OUTPUTDIRで指定した場所に[LISTID]_[GRPID]_[YKNO].csvで出力されます。 033 * 特殊な動作として、デーモングループに"BIG"の文字が入っている場合はCSV出力先ディレクトリ末尾に"_BIG"を付加します。 034 * 2つのフォルダは予め作成しておきます。 035 * 036 * PDF等の最終的な出力先、つまりCSVのコントロールヘッダのRDSetOutputFileNameはGE50で指定します。 037 * (Defaultのプラグインと出力が異なるので注意が必要です) 038 * 039 * データに関しては、全てダブルクウォートで囲って出力されます。 040 * ダブルクウォートそのものは二重化でエスケープします。 041 * ヘッダ、フッタが存在する場合、ボディ、ヘッダ、フッタの順番に連結して出力し、カラム名はヘッダはH_、フッタはF_を先頭に追加します。 042 * 043 * 044 * @og.group 帳票システム 045 * 046 * @version 5.9.0.0 047 * @author Masakazu Takahashi 048 * @since JDK6.0, 049 */ 050public class CSVPrintPointService_RFW extends AbstractCSVPrintPointService { 051 052 private static final String CR = System.getProperty("line.separator"); 053 private final StringBuilder strCSV = new StringBuilder(); // CSVはこれに吐く 054 055 private static final String csvEncode = HybsSystem.sys("REPORT_CSV_TEXT_ENCODE"); 056 057 private static final String RFW_CSV_OUTPUTDIR = HybsSystem.sys("RFW_CSV_OUTPUTDIR"); 058 059 private static final String RFW_EXCEL_TYPE = StringUtil.nval( HybsSystem.sys("RFW_EXCEL_TYPE"), "XLS" ) ; 060 061 /** 062 * 発行処理 063 * ファイル出力 064 * 065 * @og.rev 5.9.2.2 ファイル名に標準OrderBy同様にGRPIDを付ける。デーモングループに「BIG」が入っている場合は出力先変更 066 * 067 * @return 結果 [true:正常/false:異常] 068 */ 069 @Override 070 public boolean execute(){ 071 System.out.print( "CSV create ... " ); 072 BufferedWriter bw = null; 073 boolean flg = false; 074 075 try { 076 makeheader(); 077 makebody(); 078 079// bw = getWriter( RFW_CSV_OUTPUTDIR + File.separator + listid + "_" + ykno + ".csv" ,false,csvEncode); 080 // 汎用化も考えたが、予期せぬ出力があると困るのでBIG決め打ち。フォルダ存在しない場合はエラー 081 if( dmngrp != null && dmngrp.indexOf( "BIG" ) >= 0 ){ // 5.9.2.2 082 bw = getWriter( RFW_CSV_OUTPUTDIR + "_BIG" + File.separator + listid + "_" + grpid + "_" + ykno + ".csv" ,false,csvEncode); 083 } 084 else{ 085 bw = getWriter( RFW_CSV_OUTPUTDIR + File.separator + listid + "_" + grpid + "_" + ykno + ".csv" ,false,csvEncode); 086 } 087 bw.write( strCSV.toString() ); 088 bw.flush(); 089 bw.close(); 090 091 flg = true; 092 093// if( prgfile != null && prgfile.length() > 0){ 094// makeShellCommand(); 095// flg = programRun(); 096// } 097 098 } 099 catch ( Throwable ex ) { 100 errMsg.append( "CSV Print Request Execution Error. " ).append( CR ); 101 errMsg.append( "==============================" ).append( CR ); 102 errMsg.append( "SYSTEM_ID=[" ).append( systemId ).append( "] , " ); 103 errMsg.append( "YKNO=[" ).append( ykno ).append( "] , " ); 104 errMsg.append( ex.toString() ); 105 errMsg.append( CR ); 106// throw new RuntimeException( errMsg.toString() ); 107 throw new RuntimeException( errMsg.toString(), ex ); 108 } 109 return flg; 110 } 111 112 /** 113 * ヘッダの出力 114 * 115 * @og.rev 5.9.1.2 (2015/10/23) RDSetOutputPrinterの値をIDに変更 116 * @og.rev 5.9.3.0 (2015/12/04) option追加 117 * @og.rev 5.9.3.2 (2015/12/21) XLSX対応 118 */ 119 private void makeheader(){ 120 //ヘッダデータを出力する場合はここで指定する。 121 strCSV.append( "<rdstart>" ).append( CR ); 122 123 strCSV.append( "RDSetForm=\"" ).append(modelname).append("\"").append( CR ); 124 125 //5.9.3.1 (2015/12/16) 126 strCSV.append( "RDSetUserName=\"" ).append(systemId).append("\"").append( CR ); 127 strCSV.append( "RDSetComputer=\"" ).append( listid + "_" + grpid + "_" + ykno ).append("\"").append( CR ); 128 strCSV.append( "RDSetDocName=\"" ).append(listid).append("\"").append( CR ); 129 130 131 // PDFの場合 132 if( FGRUN_PDF.equals( fgrun ) ){ 133 strCSV.append( "RDSetOutputMode=PDF" ).append( CR ); 134 strCSV.append( "RDSetOutputFileName=\"" ).append( outdir ).append("\"").append( CR ); 135 } 136 // Excel 137 else if( FGRUN_EXCEL.equals(fgrun) ){ 138// strCSV.append( "RDSetOutputMode=XLS" ).append( CR ); 139 if( option != null && option.indexOf("RDSetOutputMode") < 0 ){ 140 strCSV.append( "RDSetOutputMode=" + RFW_EXCEL_TYPE ).append( CR ); 141 } 142 strCSV.append( "RDSetOutputFileName=\"" ).append( outdir ).append("\"").append( CR ); 143 } 144 // 印刷 145 else{ 146 strCSV.append( "RDSetOutputMode=SPOOL" ).append( CR ); 147 //strCSV.append( "RDSetOutputPrinter=\"" ).append(prtName).append( "\"" ).append( CR ); 148 // プリンタ名ではなく、プリンタIDを出力するように変更 149 strCSV.append( "RDSetOutputPrinter=\"" ).append(prtid).append( "\"" ).append( CR ); 150 } 151 152 if( option != null && option.length() > 0 ){ 153 strCSV.append( option ).append( CR ); // 5.9.3.0 (2015/12/04) 154 } 155 156 strCSV.append( "<rdend>" ).append( CR ); 157 158 //1行目にカラム名を出力します。クウォートで囲わない。 159 // メインテーブルはNULLではない 160 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) { 161 // 先頭以外はカンマを付ける 162 if( clmNo > 0 ){ strCSV.append( "," ); } 163 strCSV.append( table.getColumnName( clmNo )); 164 } 165 if( tableH != null){ 166 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) { 167 strCSV.append( "," ); 168 strCSV.append("H_").append( tableH.getColumnName( clmNo )); 169 } 170 } 171 if( tableF != null){ 172 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) { 173 strCSV.append( "," ); 174 strCSV.append("F_").append( tableF.getColumnName( clmNo )); 175 } 176 } 177 strCSV.append( CR ); 178 } 179 180 181 182 /** 183 * 本体の出力を行います 184 * HTMLエスケープされている場合は戻します 185 */ 186 private void makebody(){ 187 188 for( int rowNo=0; rowNo<table.getRowCount(); rowNo++ ) { 189 // カラム単位の処理 190 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) { 191 // 先頭以外はカンマを付ける 192 if( clmNo > 0 ){ strCSV.append( "," ); } 193 // 全てダブルクウォートで囲う 194 strCSV.append("\"").append( StringUtil.replace( StringUtil.getReplaceEscape( table.getValue( rowNo, clmNo )) ,"\"","\"\"" ) ).append("\""); 195 } 196 197 //ヘッダ、フッタは毎行に必ず付加します。 198 //例え複数行あったとしても先頭行のみ有効です 199 //ヘッダ 200 if( tableH != null){ 201 int rowNoH=0; 202 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) { 203 // 必ずカンマを付ける 204 strCSV.append( "," ); 205 // 全てダブルクウォートで囲う 206 strCSV.append("\"").append( StringUtil.replace( StringUtil.getReplaceEscape( tableH.getValue( rowNoH, clmNo )) ,"\"","\"\"" ) ).append("\""); 207 } 208 } 209 210 //フッタ 211 if( tableF != null ){ 212 int rowNoF=0; 213 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) { 214 // 必ずカンマを付ける 215 strCSV.append( "," ); 216 // 全てダブルクウォートで囲う 217 strCSV.append("\"").append( StringUtil.replace( StringUtil.getReplaceEscape( tableF.getValue( rowNoF, clmNo )) ,"\"","\"\"" ) ).append("\""); 218 } 219 } 220 221 strCSV.append( CR ); 222 } 223 } 224 225 226 /** 227 * ファイル書き込み用のライターを返します。 228 * 229 * @param fileName ファイル名 230 * @param append アベンドするか 231 * @param encode エンコード 232 * 233 * @return ライター 234 */ 235 private BufferedWriter getWriter( final String fileName, final boolean append, final String encode) { 236 File file = new File ( fileName ); 237 BufferedWriter bw; 238 239 try { 240 bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file, append ), encode ) ); 241 } 242 catch ( UnsupportedEncodingException ex ) { 243 errMsg.append( "[ERROR] Input File is written by Unsupported Encoding" ); 244 throw new HybsSystemException( ex ); 245 } 246 catch ( FileNotFoundException ex ) { 247 errMsg.append( "[ERROR] File not Found" ); 248 throw new HybsSystemException( ex ); 249 } 250 return bw; 251 } 252 253}