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 java.util.concurrent.ConcurrentMap;                                                      // 6.4.3.3 (2016/03/04)
019
020import org.opengion.fukurou.util.Shell;
021import org.opengion.hayabusa.common.HybsSystem;
022import org.opengion.hayabusa.db.DBTableModel;
023import static org.opengion.fukurou.system.HybsConst.CR;                         // 6.1.0.0 (2014/12/26) refactoring
024import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE;      // 6.1.0.0 (2014/12/26) refactoring
025
026/**
027 * RFIDPrintPoint インターフェース のデフォルト実装クラスです。
028 * execute() をオーバーライドして、各種RFIDシステムに対応して下さい。
029 *
030 * @og.group 帳票システム
031 *
032 * @version  4.0
033 * @author       Masakazu Takahashi
034 * @since    JDK6.0,
035 */
036public abstract class AbstractRFIDPrintPointService implements RFIDPrintPointService {
037
038        protected final StringBuilder errMsg = new StringBuilder( BUFFER_MIDDLE );      // エラーメッセージ
039
040        protected String                ykno            ;
041        protected String                systemId        ;
042        protected String                fgrun           ;
043        protected String                hostName        ;
044        protected String                prtName         ;
045        protected DBTableModel  table           ;
046        protected String                prgdir          ;
047        protected String                prgfile         ;
048        protected String                outdir          ;
049        /** 6.4.3.1 (2016/02/12) 作成元のMapを、HashMap から ConcurrentHashMap に置き換え。  */
050        protected ConcurrentMap<String, String[]>       rfidLayout;             // 6.4.3.3 (2016/03/04)
051        protected String                prtid           ;                                               // 5.4.3.0 (2011/12/26)
052        protected String                portnm          ;                                               // 5.4.3.1 (2011/12/27)
053        protected String                listid          ;                                               // 5.4.3.4 (2012/01/12)
054        protected String                layoutFile      ;                                               // 5.4.3.9 (2012/01/25)
055
056        protected String                fgkan           = GE50Access.FG_ERR2;   // 初期値はアプリエラー
057        protected String                shellCmd        ;
058
059        private static final int TIMEOUT = HybsSystem.sysInt( "REPORT_DAEMON_TIMEOUT" ); //Shellタイムアウト 6.3.9.1 (2015/11/27) private static final化
060
061        /**
062         * デフォルトコンストラクター
063         *
064         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
065         */
066        protected AbstractRFIDPrintPointService() { super(); }          // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
067
068        /**
069         * RFID発行処理
070         * RFIDのシステムに応じてこのメソッドをオーバーライドします
071         * 実行後はfgkanの値を正しい値でセットしなおして下さい。
072         *
073         * @return 結果 [true:正常/false:異常]
074         */
075        public abstract boolean execute();
076
077        /**
078         * 帳票起動された要求番号をセットします。
079         *
080         * @param   no 要求NO
081         */
082        public void setYkno( final String no ) {
083                ykno = no;
084        }
085
086        /**
087         * システムIDをセットします。
088         *
089         * @param   id システムID
090         */
091        public void setSystemId( final String id ) {
092                systemId = id;
093        }
094
095        /**
096         * 実行方法をセットします。
097         *
098         * @param   flag 実行方法
099         */
100        public void setFgrun( final String flag ) {
101                fgrun = flag;
102        }
103
104        /**
105         * 帳票デーモンが実行されているホスト名をセットします。
106         *
107         * @param   host ホスト名
108         */
109        public void setHostName( final String host ) {
110                hostName = host;
111        }
112
113        /**
114         * プリンター名をセットします。
115         *
116         * @param   printerName プリンタ名
117         */
118        public void setPrinterName( final String printerName ) {
119                prtName = printerName;
120        }
121
122        /**
123         * DBTableModel をセットします。
124         *
125         * @param   tbl DBTableModelオブジェクト
126         */
127        public void setTable( final DBTableModel tbl ) {
128                table = tbl;
129        }
130
131        /**
132         * REPORT_RFID_SERVICE_CLASSから起動するバッチ等のプログラムをセットします。
133         *
134         * @param dir バッチプログラムディレクトリ
135         */
136        public void setPrgDir( final String dir ){
137                prgdir = dir;
138        }
139
140        /**
141         * REPORT_RFID_SERVICE_CLASSから起動するバッチ等のプログラムをセットします。
142         *
143         * @param file バッチプログラム名
144         */
145        public void setPrgFile( final String file ){
146                prgfile = file;
147        }
148
149        /**
150         * ファイル出力時のディレクトリを指定します。
151         *
152         * @og.rev 4.3.2.2 (2008/09/22) 板金 RFID対応
153         * @param dir ファイル出力ディレクトリ
154         */
155        public void setOutDir( final String dir ){
156                outdir = dir;
157        }
158
159        /**
160         * ファイル出力時のディレクトリを指定します。
161         *
162         * @og.rev 5.4.3.0 (2011/12/26) _DEFAULT対応
163         * @param id プリンタID
164         */
165        public void setPrtId( final String id ){
166                prtid = id;
167        }
168
169        /**
170         * プリンタのポート名。
171         *
172         * @og.rev 5.4.3.1 (2011/12/27)
173         * @param port ポート名
174         */
175        public void setPortnm( final String port ){
176                portnm = port;
177        }
178
179        /**
180         * RFIDのレイアウトデータをセットします。
181         *
182         * キーはカラム名です。
183         * 値はGE58で定義された出力区分及びパラメーターの配列です。
184         * 配列の各インデックス番号の意味は、RFIDPrintRequestを参照して下さい。
185         *
186         * @og.rev 6.4.3.1 (2016/02/12) 実態を、HashMap → ConcurrentHashMap に置き換えたので、key,val ともに not null 制限です。
187         * @og.rev 6.4.3.3 (2016/03/04) ConcurrentHashMap を受け取ることを明確にするため、I/FをConcurrentMapに変更します。
188         *
189         * @param   layout RFIDレイアウト
190         */
191        public void setLayout( final ConcurrentMap<String, String[]> layout ) {
192                rfidLayout = layout;
193        }
194
195        /**
196         * 帳票IDをセットします。
197         *
198         * @param   id 帳票ID
199         */
200        public void setListId( final String id ) {
201                listid = id;
202        }
203
204        /**
205         * 帳票IDをセットします。
206         *
207         * @og.rev 5.4.3.9 (2012/01/25)
208         * @param file 雛形ファイル名
209         */
210        public void setLayoutFile( final String file ) {
211                layoutFile = file;
212        }
213
214        /**
215         * 完成フラグを返します。
216         *
217         * @return 完成フラグ String
218         */
219        public String getFgkan(){
220                return fgkan;
221        }
222
223        /**
224         * エラーメッセージを返します。
225         *
226         * @return エラーメッセージ String
227         * @og.rtnNotNull
228         */
229        public String getErrMsg(){
230                return errMsg.toString();
231        }
232
233        /**
234         * シェルの実行を行います。
235         *
236         * @og.rev 5.4.3.0 (2011/12/26)
237         *
238         * @return 結果 [true:正常/false:異常]
239         */
240        protected boolean programRun(){
241                final Shell shell = new Shell();
242                shell.setCommand( shellCmd,true );              // BATCHプロセスで実行する
243                shell.setWait( true );                                  // プロセスの終了を待つ
244                shell.setTimeout( TIMEOUT );
245
246                final int rtnCode = shell.exec();                               // 0 は正常終了を示す
247
248                if( rtnCode != 0 ) {
249                        errMsg.append( "Shell Command exequte Error." ).append( CR )
250                                .append( "==============================" ).append( CR )
251                                .append( shellCmd ).append( CR )
252                                .append( shell.getStdoutData() ).append( CR )
253                                .append( shell.getStderrData() ).append( CR )
254                                .append( CR );
255                        return false;
256                }
257
258                return true;
259        }
260
261}