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 java.io.BufferedWriter; 019import java.io.File; 020import java.io.FileNotFoundException; 021import java.io.FileOutputStream; 022import java.io.OutputStreamWriter; 023import java.io.UnsupportedEncodingException; 024 025import org.opengion.fukurou.util.StringUtil; 026import org.opengion.hayabusa.common.HybsSystemException; 027import org.opengion.hayabusa.common.HybsSystem; 028import org.opengion.hayabusa.report.AbstractCSVPrintPointService; 029 030/** 031 * 標準的なCSV形式でデータを作成します。 032 * CSVの出力先はGE50系テーブルで指定した場所です。 033 * 034 * @og.group 帳票システム 035 * 036 * @version 5.9.0.0 037 * @author Masakazu Takahashi 038 * @since JDK6.0, 039 */ 040public class CSVPrintPointService_DEFAULT extends AbstractCSVPrintPointService { 041 042 private static final String CR = System.getProperty("line.separator"); 043 private final StringBuilder strCSV = new StringBuilder(); // CSVはこれに吐く 044 045 private final String csvEncode = HybsSystem.sys("REPORT_CSV_TEXT_ENCODE"); 046 047 /** 048 * 発行処理 049 * ファイル出力 050 * 051 * @return 結果 [true:正常/false:異常] 052 */ 053 @Override 054 public boolean execute(){ 055 System.out.print( "CSV create ... " ); 056 BufferedWriter bw = null; 057 String filename= outdir; 058 boolean flg = false; 059 060 try { 061 makeheader(); 062 makebody(); 063 064 bw = getWriter(filename,false,csvEncode); 065 bw.write( strCSV.toString() ); 066 bw.flush(); 067 bw.close(); 068 069 flg = true; 070 071// if( prgfile != null && prgfile.length() > 0){ 072// makeShellCommand(); 073// flg = programRun(); 074// } 075 076 } 077 catch ( Throwable ex ) { 078 errMsg.append( "CSV Print Request Execution Error. " ).append( CR ); 079 errMsg.append( "==============================" ).append( CR ); 080 errMsg.append( "SYSTEM_ID=[" ).append( systemId ).append( "] , " ); 081 errMsg.append( "YKNO=[" ).append( ykno ).append( "] , " ); 082 errMsg.append( ex.toString() ); 083 errMsg.append( CR ); 084// throw new RuntimeException( errMsg.toString() ); 085 throw new RuntimeException( errMsg.toString(), ex ); 086 } 087 return flg; 088 } 089 090 /** 091 * ヘッダの出力 092 * 093 */ 094 private void makeheader(){ 095 //ヘッダデータを出力する場合はここで指定する。 096 //strCSV.append( listid ).append( CR ); 097 098 //1行目にカラム名を出力します。 099 // メインテーブルはNULLではない 100 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) { 101 // 先頭以外はカンマを付ける 102 if( clmNo > 0 ){ strCSV.append( "," ); } 103 strCSV.append("\"").append( table.getColumnName( clmNo )).append( "\"" ); 104 } 105 if( tableH != null){ 106 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) { 107 strCSV.append( "," ); 108 strCSV.append("\"H_").append( tableH.getColumnName( clmNo )).append("\""); 109 } 110 } 111 if( tableF != null){ 112 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) { 113 strCSV.append( "," ); 114 strCSV.append("\"F_").append( tableF.getColumnName( clmNo )).append("\""); 115 } 116 } 117 strCSV.append( CR ); 118 } 119 120 121 122 /** 123 * 本体の出力を行います 124 */ 125 private void makebody(){ 126 127 for( int rowNo=0; rowNo<table.getRowCount(); rowNo++ ) { 128 // カラム単位の処理 129 for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) { 130 // 先頭以外はカンマを付ける 131 if( clmNo > 0 ){ strCSV.append( "," ); } 132 // 全てダブルクウォートで囲う 133 strCSV.append("\"").append( StringUtil.replace(table.getValue( rowNo, clmNo ),"\"","\"\"" ) ).append("\""); 134 } 135 136 //ヘッダ、フッタは毎行に必ず付加します。 137 //例え複数行あったとしても先頭行のみ有効です 138 //ヘッダ 139 if( tableH != null){ 140 int rowNoH=0; 141 for( int clmNo=0; clmNo<tableH.getColumnCount(); clmNo++ ) { 142 // 必ずカンマを付ける 143 strCSV.append( "," ); 144 // 全てダブルクウォートで囲う 145 strCSV.append("\"").append( StringUtil.replace(tableH.getValue( rowNoH, clmNo ),"\"","\"\"" ) ).append("\""); 146 } 147 } 148 149 //フッタ 150 if( tableF != null ){ 151 int rowNoF=0; 152 for( int clmNo=0; clmNo<tableF.getColumnCount(); clmNo++ ) { 153 // 必ずカンマを付ける 154 strCSV.append( "," ); 155 // 全てダブルクウォートで囲う 156 strCSV.append("\"").append( StringUtil.replace(table.getValue( rowNoF, clmNo ),"\"","\"\"" ) ).append("\""); 157 } 158 } 159 160 strCSV.append( CR ); 161 } 162 } 163 164 165 /** 166 * ファイル書き込み用のライターを返します。 167 * 168 * @param fileName ファイル名 169 * @param append アベンドするか 170 * @param encode エンコード 171 * 172 * @return ライター 173 */ 174 private BufferedWriter getWriter( final String fileName, final boolean append, final String encode) { 175 File file = new File ( fileName ); 176 BufferedWriter bw; 177 178 try { 179 bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file, append ), encode ) ); 180 } 181 catch ( UnsupportedEncodingException ex ) { 182 errMsg.append( "[ERROR] Input File is written by Unsupported Encoding" ); 183 throw new HybsSystemException( ex ); 184 } 185 catch ( FileNotFoundException ex ) { 186 errMsg.append( "[ERROR] File not Found" ); 187 throw new HybsSystemException( ex ); 188 } 189 return bw; 190 } 191 192 193}