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