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 org.opengion.fukurou.system.OgRuntimeException ; // 6.4.2.0 (2016/01/29) 019import java.io.PrintWriter; // 6.3.8.0 (2015/09/11) 020import java.io.File; 021import org.opengion.hayabusa.common.HybsSystem; 022import org.opengion.hayabusa.report.AbstractCSVPrintPointService; 023import org.opengion.fukurou.util.StringUtil; 024import org.opengion.fukurou.util.FileUtil; // 6.3.8.0 (2015/09/11) 025import org.opengion.fukurou.system.Closer ; // 6.3.8.0 (2015/09/11) 026 027import static org.opengion.fukurou.system.HybsConst.CR ; // 5.9.0.0 (2015/09/04) 028import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE ; // 5.9.0.0 (2015/09/04) 029 030/** 031 * ユニリタ「Report & Form Warehouse」に対応したCSV形式でデータを作成します。 032 * 033 * CSVはシステムリソースRFW_CSV_OUTPUTDIRで指定した場所に[LISTID]_[GRPID]_[YKNO].csvで出力されます。 034 * 又、RFWはNASに出力する場合はJOB単位にNASサーバを指定する必要があるため、出力先ディレクトリの先頭文字が「\\」 035 * となっていた際には「_NASサーバ名」を出力先ディレクトリとします。 036 * 特殊な動作として、デーモングループに"BIG"の文字が入っている場合はCSV出力先ディレクトリ末尾に"_BIG"を付加します。 037 * 2つのフォルダは予め作成しておきます。 038 * 039 * データに関しては、全てダブルクウォートで囲って出力されます。 040 * ダブルクウォートそのものは二重化でエスケープします。 041 * ヘッダ、フッタが存在する場合、ボディ、ヘッダ、フッタの順番に連結して出力し、カラム名はヘッダはH_、フッタはF_を先頭に追加します。 042 * 043 * 区分Excelの場合にどの文字列でヘッダーを出すかはシステムリソースRFW_EXCEL_TYPEで決めます。 044 * 指定なしの場合はXLSとなります。 045 * 区分Excel(XLSX)の場合はXLSX固定です。 046 * 047 * なお、デーモングループ名の先頭文字が*の場合には最後に約7秒待ってから終了します。 048 * (プリンタによっては並列処理に対応していない場合があるため、Excel帳票と同等まで発行速度を落とす) 049 * 050 * @og.group 帳票システム 051 * 052 * @version 5.9.0.0 053 * @author Masakazu Takahashi 054 * @since JDK6.0, 055 */ 056public class CSVPrintPointService_RFW extends AbstractCSVPrintPointService { 057 058 private final StringBuilder strCSV = new StringBuilder( BUFFER_MIDDLE ); // CSVはこれに吐く 059 060 private static final String CSV_ENCODE = HybsSystem.sys("REPORT_CSV_TEXT_ENCODE"); // 6.4.1.1 (2016/01/16) csvEncode → CSV_ENCODE refactoring 061 062 private static final String RFW_CSV_OUTPUTDIR = HybsSystem.sys("RFW_CSV_OUTPUTDIR"); 063 064 // 5.9.3.3 (2015/12/26) 新規追加 065 private static final String RFW_EXCEL_TYPE = StringUtil.nval( HybsSystem.sys("RFW_EXCEL_TYPE"), "XLS" ) ; 066 067 // 5.9.17.3 (2017/02/24) 先頭が*のデーモングループの場合は約7秒スリープさせる=このスレッドでの連続処理をわざと遅延させる 068 /** デーモンスリープ時間 {@value} */ 069 public static final int DMN_GRP_SLEEP_TIME = 7000 ; // (ms) 070 071 /** 072 * デフォルトコンストラクター 073 * 074 * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor. 075 */ 076 public CSVPrintPointService_RFW() { super(); } // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。 077 078 /** 079 * 発行処理。 080 * ファイル出力 081 * 082 * @og.rev 6.3.8.0 (2015/09/11) FileUtil#getPrintWriter( File,String ) を使用。 083 * @og.rev 5.9.2.2 (2015/11/20) ファイル名に標準OrderBy同様にGRPIDを付ける。デーモングループに「BIG」が入っている場合は出力先変更 084 * @og.rev 5.9.6.2 (2016/03/11) RFWのNAS出力対応に伴う修正。outdirが\\から開始される場合に、次の\もしくは/までの文字列を出力フォルダに付け足す。 085 * @og.rev 5.9.6.3 (2016/03/18) outdirからはサーバ名は削除する。 086 * @og.rev 5.9.17.3 (2017/02/24) デーモングループの先頭文字が*の場合は最後に7秒スリープする 087 * 088 * @return 結果 [true:正常/false:異常] 089 */ 090 @Override 091 public boolean execute(){ 092 System.out.print( "CSV create ... " ); 093 PrintWriter bw = null; // 6.3.8.0 (2015/09/11) 094 095 try { 096 // 5.9.6.2 (2016/03/11) RFWのNAS出力対応に伴う修正 097 // outdirが\\から開始される場合に、次の\もしくは/までの文字列を出力フォルダに付け足す 098 // 5.9.6.3 (2016/03/18) かつ、outdirからはサーバ名は削除する 099 String nasName = ""; 100 if( outdir != null && outdir.startsWith( "\\\\" ) ){ 101 int spl = outdir.indexOf( "\\", 2 ); 102 int spl2 = outdir.indexOf( "/", 2 ); 103 spl = spl<0 ? outdir.length() : spl; 104 spl2 = spl2<0 ? outdir.length() : spl2; 105 spl = spl < spl2 ? spl : spl2; 106 nasName = "_" + outdir.substring( 2, spl ); 107 outdir = outdir.substring(spl+1); // 5.9.6.3 108 } 109 110 makeheader(); 111 makebody(); 112 113 // 6.3.8.0 (2015/09/11) FileUtil#getPrintWriter( File,String ) を使用。 114 // 5.9.2.2 (2015/11/20) 汎用化も考えたが、予期せぬ出力があると困るのでBIG決め打ち。フォルダ存在しない場合はエラー 115 final String dir = dmngrp != null && dmngrp.contains( "BIG" ) ? RFW_CSV_OUTPUTDIR + nasName + "_BIG" : RFW_CSV_OUTPUTDIR + nasName ; 116 final String csv = listid + "_" + grpid + "_" + ykno + ".csv" ; 117 bw = FileUtil.getPrintWriter( new File( dir , csv ),CSV_ENCODE ) ; // 6.3.8.0 (2015/09/11) 118 119 bw.write( strCSV.toString() ); 120 bw.flush(); 121 122 // 5.9.17.3 (2017/02/24) 先頭が*のデーモングループの場合は約7秒スリープさせる=このスレッドでの連続処理をわざと遅延させる 123 // 特殊対応なので決め打ち 124 if( dmngrp != null && dmngrp.indexOf( "*" ) == 0 ){ 125 Thread.sleep( DMN_GRP_SLEEP_TIME ); 126 } 127 } 128 catch( final Throwable ex ) { 129 errMsg.append( "CSV Print Request Execution Error. " ).append( CR ) 130 .append( "==============================" ).append( CR ) 131 .append( "SYSTEM_ID=[" ).append( systemId ).append( "] , " ) 132 .append( "YKNO=[" ).append( ykno ).append( "] , " ) 133 .append( ex.toString() ) 134 .append( CR ); 135 throw new OgRuntimeException( errMsg.toString(), ex ); 136 } 137 finally { 138 Closer.ioClose( bw ); // 6.3.8.0 (2015/09/11) 139 } 140 return true; // 6.3.8.0 (2015/09/11) catch 以外は、フラグにtrue がセットされるので、ここでは、true しか返さない。 141 } 142 143 /** 144 * ヘッダの出力。 145 * 146 * @og.rev 5.9.1.2 (2015/10/23) RDSetOutputPrinterの値をIDに変更 147 * @og.rev 5.9.3.0 (2015/12/04) option追加 148 * @og.rev 5.9.3.2 (2015/12/21) XLSX対応 149 * @og.rev 5.9.4.2 (2016/01/13) XLSXは区分に持たせるようにする 150 * @og.rev 5.9.6.0 (2016/03/01) 拡張子対応 151 */ 152 private void makeheader(){ 153 // ヘッダデータを出力する場合はここで指定する。 154 strCSV.append( "<rdstart>" ).append( CR ) 155 .append( "RDSetForm=\"" ).append(modelname).append('"').append( CR ) 156 // 5.9.3.1 (2015/12/16) 157 .append( "RDSetUserName=\"" ).append(systemId).append('"').append( CR ) 158 .append( "RDSetComputer=\"" ).append(listid).append('_').append( grpid ).append('_').append(ykno).append('"').append( CR ) // 6.4.1.1 (2016/01/16) refactoring 159 .append( "RDSetDocName=\"" ).append(listid).append('"').append( CR ); 160 161 // 5.9.6.0 拡張子を自動で付ける対応を入れておく 162 String suffix = ""; // 5.9.6.0 163 164 // PDFの場合 165 if( FGRUN_PDF.equals( fgrun ) ){ 166 if( outdir != null && outdir.indexOf(".") < 0 ) { 167 suffix = ".pdf"; 168 } 169 strCSV.append( "RDSetOutputMode=PDF" ).append( CR ) 170 .append( "RDSetOutputFileName=\"" ).append( outdir ).append( suffix ).append('"').append( CR ); 171 } 172 // Excel(XLS) 173 else if( FGRUN_EXCEL.equals(fgrun) ){ 174 if( outdir != null && outdir.indexOf(".") < 0 ){ 175 suffix = ".xls"; 176 } 177 // 5.9.3.2 (2015/12/21) XLSX対応 178 strCSV.append( "RDSetOutputMode=" ).append( RFW_EXCEL_TYPE ).append( CR ) 179 .append( "RDSetOutputFileName=\"" ).append( outdir ).append( suffix ).append('"').append( CR ); 180 } 181 // Excel(XLSX) 5.9.4.2 (2016/01/13) 182 else if( FGRUN_EXCEL2.equals(fgrun) ){ 183 if( outdir != null && outdir.indexOf(".") < 0 ){ 184 suffix = ".xlsx"; 185 } 186 strCSV.append( "RDSetOutputMode=XLSX" ).append( CR ) 187 .append( "RDSetOutputFileName=\"" ).append( outdir ).append( suffix ).append('"').append( CR ); 188 } 189 // 印刷 190 else{ 191 strCSV.append( "RDSetOutputMode=SPOOL" ).append( CR ) 192 // プリンタ名ではなく、プリンタIDを出力するように変更 193 .append( "RDSetOutputPrinter=\"" ).append(prtid).append( '"' ).append( CR ); 194 } 195 196 if( option != null && option.length() > 0 ){ 197 strCSV.append( option ).append( CR ); // 5.9.3.0 (2015/12/04) 198 } 199 200 strCSV.append( "<rdend>" ).append( CR ); 201 202 //1行目にカラム名を出力します。クウォートで囲わない。 203 // メインテーブルはNULLではない 204 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) { 205 // 先頭以外はカンマを付ける 206 if( clmNo > 0 ){ strCSV.append( ',' ); } 207 strCSV.append( table.getColumnName( clmNo )); 208 } 209 if( tableH != null){ 210 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) { 211 strCSV.append(",H_").append( tableH.getColumnName( clmNo )); 212 } 213 } 214 if( tableF != null){ 215 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) { 216 strCSV.append(",F_").append( tableF.getColumnName( clmNo )); 217 } 218 } 219 strCSV.append( CR ); 220 } 221 222 /** 223 * 本体の出力を行います。 224 * HTMLエスケープされている場合は戻します 225 * 226 * @og.rev 5.9.8.2 (2016/05/16) EOR対応 227 */ 228 private void makebody(){ 229 230 for( int rowNo=0; rowNo<table.getRowCount(); rowNo++ ) { 231 // カラム単位の処理 232 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) { 233 // 先頭以外はカンマを付ける 234 if( clmNo > 0 ){ strCSV.append( ',' ); } 235 // 原則全てダブルクウォートで囲う 236 // 5.9.8.2 (2016/05/16) 但し、先頭カラムが制御コードである//EOR//の場合のみ囲わない 237 if( clmNo == 0 && "//EOR//".equals( table.getValue( rowNo, clmNo )) ){ 238 strCSV.append( table.getValue( rowNo, clmNo ) ); 239 } 240 else{ 241 strCSV.append('"').append( StringUtil.replace( StringUtil.getReplaceEscape( table.getValue( rowNo, clmNo )) ,"\"","\"\"" ) ).append('"'); 242 } 243 } 244 245 //ヘッダ、フッタは毎行に必ず付加します。 246 //例え複数行あったとしても先頭行のみ有効です 247 //ヘッダ 248 if( tableH != null){ 249 final int rowNoH=0; 250 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) { 251 // 必ずカンマを付ける 252 // 全てダブルクウォートで囲う 253 strCSV.append( ",\"" ).append( StringUtil.replace( StringUtil.getReplaceEscape( tableH.getValue( rowNoH, clmNo )) ,"\"","\"\"" ) ).append('"'); 254 } 255 } 256 257 //フッタ 258 if( tableF != null ){ 259 final int rowNoF=0; 260 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) { 261 // 必ずカンマを付ける 262 // 全てダブルクウォートで囲う 263 strCSV.append( ",\"" ).append( StringUtil.replace( StringUtil.getReplaceEscape( tableF.getValue( rowNoF, clmNo )) ,"\"","\"\"" ) ).append('"'); 264 } 265 } 266 267 strCSV.append( CR ); 268 } 269 } 270 271}