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.hayabusa.report;
017
018import org.opengion.fukurou.util.Shell;
019import org.opengion.hayabusa.common.HybsSystem;
020import org.opengion.hayabusa.db.DBTableModel;
021
022/**
023 * CSVPrintPoint インターフェース のデフォルト実装クラスです。
024 * execute() をオーバーライドして、各種CSV取込み方式のシステムに対応して下さい。
025 *
026 * @og.group 帳票システム
027 *
028 * @version  5.0
029 * @author       Masakazu Takahashi
030 * @since    JDK6.0,
031 */
032public abstract class AbstractCSVPrintPointService implements CSVPrintPointService {
033
034        protected String                ykno            = null;
035        protected String                systemId        = null;
036        protected String                fgrun           = null;
037        protected String                hostName        = null;
038        protected String                prtName         = null;
039        protected DBTableModel  table           = null;
040        protected DBTableModel  tableH          = null;
041        protected DBTableModel  tableF          = null;
042        protected String                prgdir          = null;
043        protected String                prgfile         = null;
044        protected String                outdir          = null;
045        protected String                prtid           = null; 
046        protected String                portnm          = null; 
047        protected String                listid          = null; 
048        protected String                modelname               = null; 
049        protected String                csvOutdir       = null;
050
051        protected final StringBuilder   errMsg  = new StringBuilder();  // エラーメッセージ
052        protected               String                  fgkan   = GE50Access.FG_ERR2;   // 初期値はアプリエラー
053
054        protected int TIMEOUT = HybsSystem.sysInt( "REPORT_DAEMON_TIMEOUT" ); //Shellタイムアウト
055        protected String                shellCmd        = null;
056        
057        protected final static String FGRUN_EXCEL = "H";
058        protected final static String FGRUN_PDF = "I";
059        
060        private static final String CR          = System.getProperty("line.separator");
061
062        /**
063         * 発行処理
064         * 対象のシステムに応じてこのメソッドをオーバーライドします
065         * 実行後はfgkanの値を正しい値でセットしなおして下さい。
066         *
067         * @return 結果 [true:正常/false:異常]
068         */
069        public abstract boolean execute();
070
071        /**
072         * 帳票起動された要求番号をセットします。
073         *
074         * @param   no 要求NO
075         */
076        public void setYkno( final String no ) {
077                ykno = no;
078        }
079
080        /**
081         * システムIDをセットします。
082         *
083         * @param   id システムID
084         */
085        public void setSystemId( final String id ) {
086                systemId = id;
087        }
088
089        /**
090         * 実行方法をセットします。
091         *
092         * @param   flag 実行方法
093         */
094        public void setFgrun( final String flag ) {
095                fgrun = flag;
096        }
097
098        /**
099         * 帳票デーモンが実行されているホスト名をセットします。
100         *
101         * @param   host ホスト名
102         */
103        public void setHostName( final String host ) {
104                hostName = host;
105        }
106
107        /**
108         * プリンター名をセットします。
109         *
110         * @param   printerName プリンタ名
111         */
112        public void setPrinterName( final String printerName ) {
113                prtName = printerName;
114        }
115
116        /**
117         * DBTableModel をセットします。
118         *
119         * @param   tbl DBTableModelオブジェクト
120         */
121        public void setTable( final DBTableModel tbl ) {
122                table = tbl;
123        }
124        
125        /**
126         * DBTableModel をセットします。
127         *
128         * @param   tbl DBTableModelオブジェクト
129         */
130        public void setTableH( final DBTableModel tbl ) {
131                tableH = tbl;
132        }
133        
134        /**
135         * DBTableModel をセットします。
136         *
137         * @param   tbl DBTableModelオブジェクト
138         */
139        public void setTableF( final DBTableModel tbl ) {
140                tableF = tbl;
141        }
142
143        /**
144         * 起動するバッチ等のプログラム(ディレクトリ)をセットします。
145         *
146         * @param dir バッチプログラムディレクトリ
147         */
148        public void setPrgDir( final String dir ){
149                prgdir = dir;
150        }
151
152        /**
153         * 起動するバッチ等のプログラムをセットします。
154         * 空の場合は起動しません。
155         *
156         * @param file バッチプログラム名
157         */
158        public void setPrgFile( final String file ){
159                prgfile = file;
160        }
161
162        /**
163         * ファイル出力時のディレクトリを指定します
164         *
165         * @param dir ファイル出力ディレクトリ
166         */
167        public void setOutDir( final String dir ){
168                outdir = dir;
169        }
170
171        /**
172         * プリンタIDを指定します
173         *
174         * @param id プリンタID
175         */
176        public void setPrtId( final String id ){
177                prtid = id;
178        }
179
180        /**
181         * プリンタのポート名
182         *
183         * @param port ポート名
184         */
185        public void setPortnm( final String port ){
186                portnm = port;
187        }
188
189        /**
190         * 帳票IDをセットします
191         *
192         * @param   id 帳票ID
193         */
194        public void setListId( final String id ) {
195                listid = id;
196        }
197        
198        /**
199         * 雛形ファイル名をセットします
200         *
201         * @param   name 雛形ファイル名
202         */
203        public void setModelname( final String name ) {
204                modelname = name;
205        }
206
207        /**
208         * 完成フラグを返します。
209         *
210         * @return 完成フラグ String
211         */
212        public String getFgkan(){
213                return fgkan;
214        }
215
216        /**
217         * エラーメッセージを返します。
218         *
219         * @return エラーメッセージ String
220         */
221        public String getErrMsg(){
222                return errMsg.toString();
223        }
224
225        /**
226         * シェルの実行を行います
227         *
228         * @og.rev 5.4.3.0 (2011/12/26)
229         *
230         * @return 結果 [true:正常/false:異常]
231         */
232        protected boolean programRun(){
233                Shell shell = new Shell();
234                shell.setCommand( shellCmd,true );              // BATCHプロセスで実行する
235                shell.setWait( true );                                  // プロセスの終了を待つ
236                shell.setTimeout( TIMEOUT );
237
238                int rtnCode = shell.exec();                             // 0 は正常終了を示す
239
240                if( rtnCode != 0 ) {
241                        errMsg.append( "Shell Command exequte Error." ).append( CR );
242                        errMsg.append( "==============================" ).append( CR );
243                        errMsg.append( shellCmd ).append( CR );
244                        errMsg.append( shell.getStdoutData() ).append( CR );
245                        errMsg.append( shell.getStderrData() ).append( CR );
246                        errMsg.append( CR );
247                        return false;
248                }
249
250                return true;
251        }
252
253}