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) 020 021import java.io.File; 022 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) 026import org.opengion.hayabusa.common.HybsSystem; 027import org.opengion.hayabusa.report.AbstractCSVPrintPointService; 028 029import static org.opengion.fukurou.system.HybsConst.CR ; // 5.9.0.0 (2015/09/04) 030import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE ; // 5.9.0.0 (2015/09/04) 031 032/** 033 * 標準的なCSV形式でデータを作成します。 034 * CSVの出力先はGE50系テーブルで指定した場所です。 035 * 036 * @og.group 帳票システム 037 * 038 * @version 5.9.0.0 039 * @author Masakazu Takahashi 040 * @since JDK6.0, 041 */ 042public class CSVPrintPointService_DEFAULT extends AbstractCSVPrintPointService { 043 044 private final StringBuilder strCSV = new StringBuilder( BUFFER_MIDDLE ); // CSVはこれに吐く 045 046 private final String csvEncode = HybsSystem.sys("REPORT_CSV_TEXT_ENCODE"); 047 048 /** 049 * デフォルトコンストラクター 050 * 051 * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor. 052 */ 053 public CSVPrintPointService_DEFAULT() { super(); } // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。 054 055 /** 056 * 発行処理。 057 * ファイル出力 058 * 059 * @og.rev 6.3.8.0 (2015/09/11) FileUtil#getPrintWriter( File,String ) を使用。 060 * 061 * @return 結果 [true:正常/false:異常] 062 */ 063 @Override 064 public boolean execute(){ 065 System.out.print( "CSV create ... " ); 066 PrintWriter bw = null; // 6.3.8.0 (2015/09/11) 067 068 try { 069 makeheader(); 070 makebody(); 071 072 // 6.3.8.0 (2015/09/11) FileUtil#getPrintWriter( File,String ) を使用。 073 bw = FileUtil.getPrintWriter( new File( outdir ),csvEncode ) ; // 6.3.8.0 (2015/09/11) 074 bw.write( strCSV.toString() ); 075 bw.flush(); 076 } 077 catch( final Throwable ex ) { 078 errMsg.append( "CSV Print Request Execution Error. " ).append( CR ) 079 .append( "==============================" ).append( CR ) 080 .append( "SYSTEM_ID=[" ).append( systemId ).append( "] , " ) 081 .append( "YKNO=[" ).append( ykno ).append( "] , " ) 082 .append( ex.toString() ) 083 .append( CR ); 084 throw new OgRuntimeException( errMsg.toString(), ex ); 085 } 086 finally { 087 Closer.ioClose( bw ); // 6.3.8.0 (2015/09/11) 088 } 089 return true; // 6.3.8.0 (2015/09/11) catch 以外は、フラグにtrue がセットされるので、ここでは、true しか返さない。 090 } 091 092 /** 093 * ヘッダの出力。 094 * 095 */ 096 private void makeheader(){ 097 //ヘッダデータを出力する場合はここで指定する。 098 //strCSV.append( listid ).append( CR ); 099 100 //1行目にカラム名を出力します。 101 // メインテーブルはNULLではない 102 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) { 103 // 先頭以外はカンマを付ける 104 if( clmNo > 0 ){ strCSV.append( ',' ); } 105 strCSV.append('"').append( table.getColumnName( clmNo )).append( '"' ); 106 } 107 if( tableH != null){ 108 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) { 109 strCSV.append( ",\"H_" ).append( tableH.getColumnName( clmNo )).append('"'); 110 } 111 } 112 if( tableF != null){ 113 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) { 114 strCSV.append(",\"F_").append( tableF.getColumnName( clmNo )).append('"'); 115 } 116 } 117 strCSV.append( CR ); 118 } 119 120 /** 121 * 本体の出力を行います。 122 */ 123 private void makebody(){ 124 125 for( int rowNo=0; rowNo<table.getRowCount(); rowNo++ ) { 126 // カラム単位の処理 127 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) { 128 // 先頭以外はカンマを付ける 129 if( clmNo > 0 ){ strCSV.append( ',' ); } 130 // 全てダブルクウォートで囲う 131 strCSV.append('"').append( StringUtil.replace(table.getValue( rowNo, clmNo ),"\"","\"\"" ) ).append('"'); 132 } 133 134 //ヘッダ、フッタは毎行に必ず付加します。 135 //例え複数行あったとしても先頭行のみ有効です 136 //ヘッダ 137 if( tableH != null){ 138 final int rowNoH=0; 139 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) { 140 // 必ずカンマを付ける 141 // 全てダブルクウォートで囲う 142 strCSV.append( ",\"" ).append( StringUtil.replace(tableH.getValue( rowNoH, clmNo ),"\"","\"\"" ) ).append('"'); 143 } 144 } 145 146 //フッタ 147 if( tableF != null ){ 148 final int rowNoF=0; 149 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) { 150 // 必ずカンマを付ける 151 // 全てダブルクウォートで囲う 152 strCSV.append( ",\"" ).append( StringUtil.replace(table.getValue( rowNoF, clmNo ),"\"","\"\"" ) ).append('"'); 153 } 154 } 155 156 strCSV.append( CR ); 157 } 158 } 159 160}