2 * Copyright (c) 2009 The openGion Project.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13 * either express or implied. See the License for the specific language
14 * governing permissions and limitations under the License.
16 package org.opengion.plugin.report;
18 import java.io.BufferedWriter;
20 import java.io.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.OutputStreamWriter;
23 import java.io.UnsupportedEncodingException;
24 import org.opengion.hayabusa.common.HybsSystemException;
25 import org.opengion.hayabusa.common.HybsSystem;
26 import org.opengion.hayabusa.report.AbstractCSVPrintPointService;
27 import org.opengion.fukurou.util.StringUtil;
28 import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE ;
31 * ユニリタ「Report & Form Warehouse」に対応したCSV形式でデータを作成します。
33 * CSVPrintPointService_RFWとの違いは出力ファイル名のみです。
34 * CSVはシステムリソースRFW_CSV_OUTPUTDIRで指定した場所に[GRPID]_[YKNO]_[LISTID].csvで出力されます。
38 * @og.rev 5.9.27.1 (2017/12/08) 新規作成
41 * @author Masakazu Takahashi
44 public class CSVPrintPointService_RFW2 extends AbstractCSVPrintPointService {
46 private static final String CR = System.getProperty("line.separator");
47 private final StringBuilder strCSV = new StringBuilder( BUFFER_MIDDLE ); // CSVはこれに吐く
49 // 6.8.5.0 (2018/01/09) PMD Variables that are final and static should be all capitals。csvEncode → CSV_ENCODE
50 private static final String CSV_ENCODE = HybsSystem.sys("REPORT_CSV_TEXT_ENCODE");
52 private static final String RFW_CSV_OUTPUTDIR = HybsSystem.sys("RFW_CSV_OUTPUTDIR");
54 private static final String RFW_EXCEL_TYPE = StringUtil.nval( HybsSystem.sys("RFW_EXCEL_TYPE"), "XLS" ) ;
59 * @og.rev 6.9.7.0 (2018/05/14) PMD Each class should declare at least one constructor
61 public CSVPrintPointService_RFW2() { super(); } // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
68 * @og.rev 6.8.5.0 (2018/01/09) PMD Variables that are final and static should be all capitals。csvEncode → CSV_ENCODE
70 * @return 結果 [true:正常/false:異常]
73 public boolean execute(){
74 System.out.print( "CSV create ... " );
75 BufferedWriter bw = null;
79 // outdirが\\から開始される場合に、次の\もしくは/までの文字列を出力フォルダに付け足す
80 // かつ、outdirからはサーバ名は削除する
82 if( outdir != null && outdir.startsWith( "\\\\" ) ){
83 int spl = outdir.indexOf( "\\", 2 );
84 int spl2 = outdir.indexOf( "/", 2 );
85 spl = spl<0 ? outdir.length() : spl;
86 spl2 = spl2<0 ? outdir.length() : spl2;
87 spl = spl < spl2 ? spl : spl2;
88 nasName = "_" + outdir.substring( 2, spl );
89 outdir = outdir.substring(spl+1);
95 // 汎用化も考えたが、予期せぬ出力があると困るのでBIG決め打ち。フォルダ存在しない場合はエラー
96 final String fnm = File.separator + grpid + "_" + ykno + "_" + listid + ".csv"; // 6.8.5.0 (2018/01/09)
97 if( dmngrp != null && dmngrp.indexOf( "BIG" ) >= 0 ){
98 bw = getWriter( RFW_CSV_OUTPUTDIR + nasName + "_BIG" + fnm ,false,CSV_ENCODE);
101 bw = getWriter( RFW_CSV_OUTPUTDIR + nasName + fnm ,false,CSV_ENCODE);
103 bw.write( strCSV.toString() );
109 // 先頭が*のデーモングループの場合は約7秒スリープさせる=このスレッドでの連続処理をわざと遅延させる
111 if( dmngrp != null && dmngrp.indexOf( "*" ) == 0 ){
115 catch ( Throwable ex ) {
116 errMsg.append( "CSV Print Request Execution Error. " ).append( CR );
117 errMsg.append( "==============================" ).append( CR );
118 errMsg.append( "SYSTEM_ID=[" ).append( systemId ).append( "] , " );
119 errMsg.append( "YKNO=[" ).append( ykno ).append( "] , " );
120 errMsg.append( ex.toString() );
122 throw new RuntimeException( errMsg.toString(), ex );
131 private void makeheader(){
132 //ヘッダデータを出力する場合はここで指定する。
133 strCSV.append( "<rdstart>" ).append( CR )
134 .append( "RDSetForm=\"" ).append( modelname ).append( '"' ).append( CR )
135 .append( "RDSetUserName=\"" ).append( systemId ).append( '"' ).append( CR ) //5.9.3.1 (2015/12/16)
136 .append( "RDSetComputer=\"" ).append( listid ).append( '_' ).append( grpid ).append( '_' ).append( ykno ).append( '"' ).append( CR )
137 .append( "RDSetDocName=\"" ).append( listid ).append( '"' ).append( CR );
139 String suffix = ""; // 5.9.6.0
141 // 5.9.6.0 拡張子を自動で付ける対応を入れておく
143 if( FGRUN_PDF.equals( fgrun ) ){
144 if( outdir != null && outdir.indexOf( '.' ) < 0 ){
148 strCSV.append( "RDSetOutputMode=PDF" ).append( CR )
149 .append( "RDSetOutputFileName=\"" ).append( outdir ).append( suffix ).append( '"' ).append( CR );
152 else if( FGRUN_EXCEL.equals(fgrun) ){
153 if( outdir != null && outdir.indexOf( '.' ) < 0 ){
156 strCSV.append( "RDSetOutputMode=" + RFW_EXCEL_TYPE ).append( CR )
157 .append( "RDSetOutputFileName=\"" ).append( outdir ).append( suffix ).append( '"' ).append( CR );
159 else if( FGRUN_EXCEL2.equals(fgrun) ){
160 if( outdir != null && outdir.indexOf( '.' ) < 0 ){
163 strCSV.append( "RDSetOutputMode=XLSX" ).append( CR )
164 .append( "RDSetOutputFileName=\"" ).append( outdir ).append( suffix ).append( '"' ).append( CR );
168 strCSV.append( "RDSetOutputMode=SPOOL" ).append( CR )
169 .append( "RDSetOutputPrinter=\"" ).append(prtid).append( '"' ).append( CR );
172 if( option != null && option.length() > 0 ){
173 strCSV.append( option ).append( CR ); // 5.9.3.0 (2015/12/04)
176 strCSV.append( "<rdend>" ).append( CR );
178 //1行目にカラム名を出力します。クウォートで囲わない。
180 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) {
182 if( clmNo > 0 ){ strCSV.append( ',' ); }
183 strCSV.append( table.getColumnName( clmNo ));
186 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) {
187 strCSV.append( ",H_" ).append( tableH.getColumnName( clmNo ) );
191 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) {
192 strCSV.append( ",F_" ).append( tableF.getColumnName( clmNo ) );
200 * HTMLエスケープされている場合は戻します
202 private void makebody(){
204 for( int rowNo=0; rowNo<table.getRowCount(); rowNo++ ) {
206 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) {
208 if( clmNo > 0 ){ strCSV.append( ',' ); } // 6.9.7.0 (2018/05/14) PMD Avoid appending characters as strings in StringBuffer.append.
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 ) );
216 .append( StringUtil.replace( StringUtil.getReplaceEscape( table.getValue( rowNo, clmNo )) ,"\"","\"\"" ) )
221 //ヘッダ、フッタは毎行に必ず付加します。
222 //例え複数行あったとしても先頭行のみ有効です
225 final int rowNoH=0; // 先頭行のみ有効
226 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) {
228 strCSV.append( ",\"" ) // 全てダブルクウォートで囲う
229 .append( StringUtil.replace( StringUtil.getReplaceEscape( tableH.getValue( rowNoH, clmNo )) ,"\"","\"\"" ) )
235 if( tableF != null ){
236 final int rowNoF=0; // 先頭行のみ有効
237 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) {
239 strCSV.append( ",\"" ) // 全てダブルクウォートで囲う
240 .append( StringUtil.replace( StringUtil.getReplaceEscape( tableF.getValue( rowNoF, clmNo )) ,"\"","\"\"" ) )
249 * ファイル書き込み用のライターを返します。
251 * @param fileName ファイル名
252 * @param append アベンドするか
253 * @param encode エンコード
257 private BufferedWriter getWriter( final String fileName, final boolean append, final String encode) {
258 final File file = new File ( fileName );
262 bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file, append ), encode ) );
264 catch ( final UnsupportedEncodingException ex ) {
265 errMsg.append( "[ERROR] Input File is written by Unsupported Encoding" );
266 throw new HybsSystemException( ex );
268 catch ( final FileNotFoundException ex ) {
269 errMsg.append( "[ERROR] File not Found" );
270 throw new HybsSystemException( ex );