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 } 078 catch( final Throwable ex ) { 079 errMsg.append( "CSV Print Request Execution Error. " ).append( CR ) 080 .append( "==============================" ).append( CR ) 081 .append( "SYSTEM_ID=[" ).append( systemId ).append( "] , " ) 082 .append( "YKNO=[" ).append( ykno ).append( "] , " ) 083 .append( ex.toString() ) 084 .append( CR ); 085 throw new OgRuntimeException( errMsg.toString(), ex ); 086 } 087 finally { 088 Closer.ioClose( bw ); // 6.3.8.0 (2015/09/11) 089 } 090 return true; // 6.3.8.0 (2015/09/11) catch 以外は、フラグにtrue がセットされるので、ここでは、true しか返さない。 091 } 092 093 /** 094 * ヘッダの出力。 095 * 096 */ 097 private void makeheader(){ 098 //ヘッダデータを出力する場合はここで指定する。 099 //strCSV.append( listid ).append( CR ); 100 101 //1行目にカラム名を出力します。 102 // メインテーブルはNULLではない 103 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) { 104 // 先頭以外はカンマを付ける 105 if( clmNo > 0 ){ strCSV.append( ',' ); } 106 strCSV.append('"').append( table.getColumnName( clmNo )).append( '"' ); 107 } 108 if( tableH != null){ 109 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) { 110 strCSV.append( ",\"H_" ).append( tableH.getColumnName( clmNo )).append('"'); 111 } 112 } 113 if( tableF != null){ 114 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) { 115 strCSV.append(",\"F_").append( tableF.getColumnName( clmNo )).append('"'); 116 } 117 } 118 strCSV.append( CR ); 119 } 120 121 /** 122 * 本体の出力を行います。 123 */ 124 private void makebody(){ 125 126 for( int rowNo=0; rowNo<table.getRowCount(); rowNo++ ) { 127 // カラム単位の処理 128 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) { 129 // 先頭以外はカンマを付ける 130 if( clmNo > 0 ){ strCSV.append( ',' ); } 131 // 全てダブルクウォートで囲う 132 strCSV.append('"').append( StringUtil.replace(table.getValue( rowNo, clmNo ),"\"","\"\"" ) ).append('"'); 133 } 134 135 //ヘッダ、フッタは毎行に必ず付加します。 136 //例え複数行あったとしても先頭行のみ有効です 137 //ヘッダ 138 if( tableH != null){ 139 final int rowNoH=0; 140 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) { 141 // 必ずカンマを付ける 142 // 全てダブルクウォートで囲う 143 strCSV.append( ",\"" ).append( StringUtil.replace(tableH.getValue( rowNoH, clmNo ),"\"","\"\"" ) ).append('"'); 144 } 145 } 146 147 //フッタ 148 if( tableF != null ){ 149 final int rowNoF=0; 150 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) { 151 // 必ずカンマを付ける 152 // 全てダブルクウォートで囲う 153 strCSV.append( ",\"" ).append( StringUtil.replace(table.getValue( rowNoF, clmNo ),"\"","\"\"" ) ).append('"'); 154 } 155 } 156 157 strCSV.append( CR ); 158 } 159 } 160 161}