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     */
016    package org.opengion.hayabusa.report;
017    
018    import java.io.BufferedWriter;
019    import java.io.File;
020    import java.io.FileNotFoundException;
021    import java.io.FileOutputStream;
022    import java.io.OutputStreamWriter;
023    import java.io.UnsupportedEncodingException;
024    import org.opengion.hayabusa.common.HybsSystemException;
025    import org.opengion.hayabusa.common.HybsSystem;
026    
027    /**
028     * CSVå½¢å¼ã§ãƒ??タを作æ?ã—ã¾ã™ã?
029     * ãƒãƒƒãƒã?コールもå?部ã§è¡Œã„ã¾ã™ã?
030     *
031     * @og.group 帳票シスãƒ?ƒ 
032     *
033     * @version  5.9.0.0
034     * @author       Masakazu Takahashi
035     * @since    JDK6.0,
036     */
037    public class CSVPrintPointService_DEFAULT extends AbstractCSVPrintPointService {
038    
039            private static final String CR          = System.getProperty("line.separator");
040            private final StringBuilder strCSV      = new StringBuilder();  // CSVã¯ã“れã«åã
041    
042            private final String    csvEncode       = HybsSystem.sys("REPORT_CSV_TEXT_ENCODE");
043    
044            /**
045             * 発行å?ç?
046             * ファイル出�
047             *
048             * @return çµæžœ [true:正常/false:異常]
049             */
050            @Override
051            public boolean execute(){
052                    System.out.print( "CSV create ... " );
053                    BufferedWriter bw = null;
054                    String filename= outdir;
055                    boolean flg = false;
056    
057                    try {
058                            makeheader();
059                            makebody();
060    
061                            bw = getWriter(filename,false,csvEncode);
062                            bw.write( strCSV.toString() );
063                            bw.flush();
064                            bw.close();
065    
066                            flg = true;
067                            
068    //                      if( prgfile != null && prgfile.length() > 0){
069    //                              makeShellCommand();
070    //                              flg = programRun();
071    //                      }
072    
073                    }
074                    catch ( Throwable ex ) {
075                            errMsg.append( "CSV Print Request Execution Error. " ).append( CR );
076                            errMsg.append( "==============================" ).append( CR );
077                            errMsg.append( "SYSTEM_ID=[" ).append( systemId ).append( "] , " );
078                            errMsg.append( "YKNO=["    ).append( ykno    ).append( "] , " );
079                            errMsg.append( ex.toString() );
080                            errMsg.append( CR );
081    //                      throw new RuntimeException( errMsg.toString() );
082                            throw new RuntimeException( errMsg.toString(), ex );
083                    }
084                    return flg;
085            }
086    
087            /**
088             * ヘッ�?出�
089             *
090             */
091            private void makeheader(){
092                    //ヘッãƒ?ƒ‡ãƒ¼ã‚¿ã‚’å?力ã™ã‚‹å?åˆã?ã“ã“ã§æŒ?®šã™ã‚‹ã?
093                    //strCSV.append( listid ).append( CR );
094            }
095    
096    
097    
098            /**
099             * 本体ã?出力を行ã„ã¾ã?
100             */
101            private void makebody(){
102    
103                    for( int rowNo=0; rowNo<table.getRowCount(); rowNo++ ) {
104                            // カラãƒ?˜ä½ã?処ç?
105                            for( int clmNo=0; clmNo<table.getColumnCount(); clmNo++ ) {
106                                    // å…ˆé?以外ã?カンマを付ã‘ã‚?
107                                    if( clmNo > 0 ){ strCSV.append( "," ); } 
108                                    // å…¨ã¦ãƒ?ƒ–ルクウォートã§å›²ã?
109                                    strCSV.append("\"").append( table.getValue( rowNo, clmNo ) ).append("\"");
110                            }
111    
112                            strCSV.append( CR );
113                    }
114            }
115    
116    
117            /**
118             * ファイル書ãè¾¼ã¿ç”¨ã®ãƒ©ã‚¤ã‚¿ãƒ¼ã‚’è¿”ã—ã¾ã™ã?
119             *
120             * @param fileName ファイルå?
121             * @param append アベンドã™ã‚‹ã‹
122             * @param encode エンコー�
123             *
124             * @return ライター
125             */
126            private BufferedWriter getWriter( final String fileName, final boolean append, final String encode) {
127                    File file = new File ( fileName );
128                    BufferedWriter bw;
129    
130                    try {
131                            bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file, append ), encode ) );
132                    }
133                    catch ( UnsupportedEncodingException ex ) {
134                            errMsg.append( "[ERROR] Input File is written by Unsupported Encoding" );
135                            throw new HybsSystemException( ex );
136                    }
137                    catch ( FileNotFoundException ex ) {
138                            errMsg.append( "[ERROR] File not Found" );
139                            throw new HybsSystemException( ex );
140                    }
141                    return bw;
142            }
143    
144            /**
145             * シェルコマンドã?æ–?­—å?を作æ?ã—ã¾ã™ã?
146             *
147             * @og.rev 5.4.3.9 引数変更
148             *
149             * @return çµæžœ [true:正常/false:異常]
150             */
151            private boolean makeShellCommand() {
152                    StringBuilder buf = new StringBuilder( HybsSystem.BUFFER_SMALL );
153    
154                    buf.append( prgdir + HybsSystem.FS + prgfile ).append( " " );           // 実行ã™ã‚‹ã‚³ãƒžãƒ³ãƒ?
155                    buf.append( "\"" ).append( ykno ).append( "\" " );                      // 引数?‘ï¼šè¦æ±?O
156                    buf.append( "\"" ).append( prtid ).append( "\" " );                     // 引数?’:ã?リンタID
157                    buf.append( "\"" ).append( prtName ).append( "\" " );           // 引数?“:ã?リンタID
158                    buf.append( "\"" ).append( hostName ).append( "\" " );          // 引数?”:ã?ストå
159                    buf.append( "\"" ).append( portnm ).append( "\" " );                    // 引数?•:ã?リンタãƒã?ãƒ?5.4.3.1(2011/12/27)
160    
161                    shellCmd = buf.toString();
162                    System.out.println( CR + shellCmd + CR );
163    
164                    return true;
165            }
166    }