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 * CSVPrintPointService_RFWとの違いは出力ファイル名のみです。 033 * CSVはシステムリソースRFW_CSV_OUTPUTDIRで指定した場所に[GRPID]_[YKNO]_[LISTID].csvで出力されます。 034 * 035 * @og.group 帳票システム 036 * 037 * @og.rev 5.9.27.1 (2017/12/08) 新規作成 038 * 039 * @version 5.9.0.0 040 * @author Masakazu Takahashi 041 * @since JDK6.0, 042 */ 043public class CSVPrintPointService_RFW2 extends AbstractCSVPrintPointService { 044 045 private static final String CR = System.getProperty("line.separator"); 046 private final StringBuilder strCSV = new StringBuilder(); // CSVはこれに吐く 047 048 private static final String csvEncode = HybsSystem.sys("REPORT_CSV_TEXT_ENCODE"); 049 050 private static final String RFW_CSV_OUTPUTDIR = HybsSystem.sys("RFW_CSV_OUTPUTDIR"); 051 052 private static final String RFW_EXCEL_TYPE = StringUtil.nval( HybsSystem.sys("RFW_EXCEL_TYPE"), "XLS" ) ; 053 054 /** 055 * 発行処理 056 * ファイル出力 057 * 058 * 059 * @return 結果 [true:正常/false:異常] 060 */ 061 @Override 062 public boolean execute(){ 063 System.out.print( "CSV create ... " ); 064 BufferedWriter bw = null; 065 boolean flg = false; 066 067 try { 068 // outdirが\\から開始される場合に、次の\もしくは/までの文字列を出力フォルダに付け足す 069 // かつ、outdirからはサーバ名は削除する 070 String nasName = ""; 071 if( outdir != null && outdir.startsWith( "\\\\" ) ){ 072 int spl = outdir.indexOf( "\\", 2 ); 073 int spl2 = outdir.indexOf( "/", 2 ); 074 spl = spl<0 ? outdir.length() : spl; 075 spl2 = spl2<0 ? outdir.length() : spl2; 076 spl = spl < spl2 ? spl : spl2; 077 nasName = "_" + outdir.substring( 2, spl ); 078 outdir = outdir.substring(spl+1); 079 } 080 081 makeheader(); 082 makebody(); 083 084 085// bw = getWriter( RFW_CSV_OUTPUTDIR + File.separator + listid + "_" + ykno + ".csv" ,false,csvEncode); 086 087 // 汎用化も考えたが、予期せぬ出力があると困るのでBIG決め打ち。フォルダ存在しない場合はエラー 088 if( dmngrp != null && dmngrp.indexOf( "BIG" ) >= 0 ){ 089 bw = getWriter( RFW_CSV_OUTPUTDIR + nasName + "_BIG" + File.separator + grpid + "_" + ykno + "_" + listid + ".csv" ,false,csvEncode); 090 } 091 else{ 092 bw = getWriter( RFW_CSV_OUTPUTDIR + nasName + File.separator + grpid + "_" + ykno + "_" + listid + ".csv" ,false,csvEncode); 093 } 094 bw.write( strCSV.toString() ); 095 bw.flush(); 096 bw.close(); 097 098 flg = true; 099 100 101 // 先頭が*のデーモングループの場合は約7秒スリープさせる=このスレッドでの連続処理をわざと遅延させる 102 // 特殊対応なので決め打ち 103 if( dmngrp != null && dmngrp.indexOf( "*" ) == 0 ){ 104 Thread.sleep(7000); 105 } 106 107 } 108 catch ( Throwable ex ) { 109 errMsg.append( "CSV Print Request Execution Error. " ).append( CR ); 110 errMsg.append( "==============================" ).append( CR ); 111 errMsg.append( "SYSTEM_ID=[" ).append( systemId ).append( "] , " ); 112 errMsg.append( "YKNO=[" ).append( ykno ).append( "] , " ); 113 errMsg.append( ex.toString() ); 114 errMsg.append( CR ); 115 throw new RuntimeException( errMsg.toString(), ex ); 116 } 117 return flg; 118 } 119 120 /** 121 * ヘッダの出力 122 * 123 */ 124 private void makeheader(){ 125 //ヘッダデータを出力する場合はここで指定する。 126 strCSV.append( "<rdstart>" ).append( CR ); 127 128 strCSV.append( "RDSetForm=\"" ).append(modelname).append("\"").append( CR ); 129 130 //5.9.3.1 (2015/12/16) 131 strCSV.append( "RDSetUserName=\"" ).append(systemId).append("\"").append( CR ); 132 strCSV.append( "RDSetComputer=\"" ).append( listid + "_" + grpid + "_" + ykno ).append("\"").append( CR ); 133 strCSV.append( "RDSetDocName=\"" ).append(listid).append("\"").append( CR ); 134 135 String suffix = ""; // 5.9.6.0 136 137 // 5.9.6.0 拡張子を自動で付ける対応を入れておく 138 // PDFの場合 139 if( FGRUN_PDF.equals( fgrun ) ){ 140 if( outdir != null && outdir.indexOf(".") < 0 ){ 141 suffix = ".pdf"; 142 } 143 144 strCSV.append( "RDSetOutputMode=PDF" ).append( CR ); 145 strCSV.append( "RDSetOutputFileName=\"" ).append( outdir ).append( suffix ).append("\"").append( CR ); 146 } 147 // Excel(XLS) 148 else if( FGRUN_EXCEL.equals(fgrun) ){ 149 if( outdir != null && outdir.indexOf(".") < 0 ){ 150 suffix = ".xls"; 151 } 152 strCSV.append( "RDSetOutputMode=" + RFW_EXCEL_TYPE ).append( CR ); 153 strCSV.append( "RDSetOutputFileName=\"" ).append( outdir ).append( suffix ).append("\"").append( CR ); 154 } 155 else if( FGRUN_EXCEL2.equals(fgrun) ){ 156 if( outdir != null && outdir.indexOf(".") < 0 ){ 157 suffix = ".xlsx"; 158 } 159 strCSV.append( "RDSetOutputMode=XLSX" ).append( CR ); 160 strCSV.append( "RDSetOutputFileName=\"" ).append( outdir ).append( suffix ).append("\"").append( CR ); 161 } 162 // 印刷 163 else{ 164 strCSV.append( "RDSetOutputMode=SPOOL" ).append( CR ); 165 strCSV.append( "RDSetOutputPrinter=\"" ).append(prtid).append( "\"" ).append( CR ); 166 } 167 168 if( option != null && option.length() > 0 ){ 169 strCSV.append( option ).append( CR ); // 5.9.3.0 (2015/12/04) 170 } 171 172 strCSV.append( "<rdend>" ).append( CR ); 173 174 //1行目にカラム名を出力します。クウォートで囲わない。 175 // メインテーブルはNULLではない 176 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) { 177 // 先頭以外はカンマを付ける 178 if( clmNo > 0 ){ strCSV.append( "," ); } 179 strCSV.append( table.getColumnName( clmNo )); 180 } 181 if( tableH != null){ 182 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) { 183 strCSV.append( "," ); 184 strCSV.append("H_").append( tableH.getColumnName( clmNo )); 185 } 186 } 187 if( tableF != null){ 188 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) { 189 strCSV.append( "," ); 190 strCSV.append("F_").append( tableF.getColumnName( clmNo )); 191 } 192 } 193 strCSV.append( CR ); 194 } 195 196 197 198 /** 199 * 本体の出力を行います 200 * HTMLエスケープされている場合は戻します 201 */ 202 private void makebody(){ 203 204 for( int rowNo=0; rowNo<table.getRowCount(); rowNo++ ) { 205 // カラム単位の処理 206 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) { 207 // 先頭以外はカンマを付ける 208 if( clmNo > 0 ){ strCSV.append( "," ); } 209 // 原則全てダブルクウォートで囲う 210 // 5.9.8.2 (2016/05/16) 但し、先頭カラムが制御コードである//EOR//の場合のみ囲わない 211 if( clmNo == 0 && "//EOR//".equals( table.getValue( rowNo, clmNo )) ){ 212 strCSV.append( table.getValue( rowNo, clmNo ) ); 213 } 214 else{ 215 strCSV.append("\"").append( StringUtil.replace( StringUtil.getReplaceEscape( table.getValue( rowNo, clmNo )) ,"\"","\"\"" ) ).append("\""); 216 } 217 } 218 219 //ヘッダ、フッタは毎行に必ず付加します。 220 //例え複数行あったとしても先頭行のみ有効です 221 //ヘッダ 222 if( tableH != null){ 223 int rowNoH=0; 224 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) { 225 // 必ずカンマを付ける 226 strCSV.append( "," ); 227 // 全てダブルクウォートで囲う 228 strCSV.append("\"").append( StringUtil.replace( StringUtil.getReplaceEscape( tableH.getValue( rowNoH, clmNo )) ,"\"","\"\"" ) ).append("\""); 229 } 230 } 231 232 //フッタ 233 if( tableF != null ){ 234 int rowNoF=0; 235 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) { 236 // 必ずカンマを付ける 237 strCSV.append( "," ); 238 // 全てダブルクウォートで囲う 239 strCSV.append("\"").append( StringUtil.replace( StringUtil.getReplaceEscape( tableF.getValue( rowNoF, clmNo )) ,"\"","\"\"" ) ).append("\""); 240 } 241 } 242 243 strCSV.append( CR ); 244 } 245 } 246 247 248 /** 249 * ファイル書き込み用のライターを返します。 250 * 251 * @param fileName ファイル名 252 * @param append アベンドするか 253 * @param encode エンコード 254 * 255 * @return ライター 256 */ 257 private BufferedWriter getWriter( final String fileName, final boolean append, final String encode) { 258 File file = new File ( fileName ); 259 BufferedWriter bw; 260 261 try { 262 bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file, append ), encode ) ); 263 } 264 catch ( UnsupportedEncodingException ex ) { 265 errMsg.append( "[ERROR] Input File is written by Unsupported Encoding" ); 266 throw new HybsSystemException( ex ); 267 } 268 catch ( FileNotFoundException ex ) { 269 errMsg.append( "[ERROR] File not Found" ); 270 throw new HybsSystemException( ex ); 271 } 272 return bw; 273 } 274 275}