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.hayabusa.common.HybsSystem;
019import org.opengion.fukurou.util.LogWriter;
020import org.opengion.fukurou.util.StringUtil;
021import org.opengion.fukurou.util.ApplicationInfo;
022import org.opengion.fukurou.db.DBUtil;
023
024/**
025 * 【DB登録】EXCEL取込機能の最終処理である、テンポラリテーブルから各種アプリ側の
026 * 本番テーブルへのデータ取込処理のための、PL/SQL をコールします。
027 * 実際の呼び出しは、{ call 帳票IDP.帳票ID( 結果(STATUS),内容(ERR_CODE),PGID,要求番号 ) }
028 * という PL/SQL を Call します。
029 * 第1引数、第2引数は、OUT属性で、結果(STATUS)とエラー時の内容(ERR_CODE)を返します。
030 * 第3引数は、起動元PGIDです。 第4引数は、処理を行う要求番号です。
031 * 結果(STATUS)は、正常ならば、0 を返してください。
032 *
033 * @og.rev 3.8.0.0 (2005/06/07) 新規追加
034 * @og.group 帳票システム
035 *
036 * @version  4.0
037 * @author   Kazuhiko Hasegawa
038 * @since    JDK5.0,
039 */
040public class ProgramRun {
041
042        private final StringBuilder errMsg = new StringBuilder();
043        private static final String CR     = HybsSystem.CR ;
044
045        private final String    SYSTEM_ID       ;
046        private final String    YKNO            ;
047        private final String    LISTID          ;
048        private final boolean   DEBUG           ;       // 3.8.5.0 (2006/03/06) デバッグ用のフラグを追加
049
050        private String          sqlCmd  = null;
051
052        /** コネクションにアプリケーション情報を追記するかどうか指定 */
053        public static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
054
055        // 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
056        private final ApplicationInfo appInfo;
057
058        /**
059         * コンストラクター
060         *
061         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
062         *
063         * @param system_id システムID
064         * @param ykno      要求番号
065         * @param listId    帳票ID
066         * @param debug デバッグフラグ言語
067         */
068        public ProgramRun( final String system_id, final String ykno, final String listId, final boolean debug ) {
069                SYSTEM_ID       = system_id;
070                YKNO            = ykno;
071                LISTID          = listId;
072                DEBUG           = debug;
073
074                // 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
075                if( USE_DB_APPLICATION_INFO ) {
076                        appInfo = new ApplicationInfo();
077                        // ユーザーID,IPアドレス,ホスト名
078                        appInfo.setClientInfo( SYSTEM_ID,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
079                        // 画面ID,操作,プログラムID
080                        appInfo.setModuleInfo( "ProgramRun",YKNO,LISTID );
081                }
082                else {
083                        appInfo = null;
084                }
085        }
086
087        /**
088         * レポート出力処理を実行します。
089         *
090         * @return 結果 [true:正常/false:異常]
091         */
092        public boolean execute() {
093                System.out.print( "ProgramRun [" + SYSTEM_ID + "]... " );
094                boolean flag;
095
096                try {
097                        flag = makeSQLCommand();
098                        if( flag ) { System.out.print( " MK SQL," ); }
099
100                        if( flag ) {
101                                flag = programRun();
102                                if( flag ) { System.out.print( " PG RUN," ); }
103                        }
104                        System.out.println( " End." );
105                }
106                catch( Throwable ex ) {
107                        errMsg.append( "ProgramRun Execute Error! " ).append( CR ) ;
108                        errMsg.append( "==============================" ).append( CR );
109                        errMsg.append( StringUtil.stringStackTrace( ex ) ) ;
110                        errMsg.append( CR ) ;
111                        flag = false;
112                }
113
114                return flag ;
115        }
116
117        /**
118         * PLSQL の call コマンドの文字列を作成します。
119         * { call 帳票ID+P.帳票ID( 結果(STATUS),内容(ERR_CODE),PGID,要求番号 ) } に対応する
120         * { call 帳票IDP.帳票ID( ?,?,?,? ) } 文字列を作成します。
121         *
122         * @return 結果 [true:正常/false:異常]
123         */
124        private boolean makeSQLCommand() {
125                StringBuilder buf = new StringBuilder( HybsSystem.BUFFER_SMALL );
126
127                buf.append( "{ call " ).append( LISTID );
128                buf.append( "P." ).append( LISTID );
129                buf.append( "( ?,?,?,? ) }" );
130
131                sqlCmd = buf.toString();
132                if( DEBUG ) {
133                        System.out.println();
134                        System.out.println( sqlCmd );
135                }
136
137                return true;
138        }
139
140        /**
141         * 実際のPL/SQL コール処理を行います。
142         * { call 帳票IDP.帳票ID( 結果(STATUS),内容(ERR_CODE),PGID,要求番号 ) }
143         * という PL/SQL を Call します。
144         * 第1引数、第2引数は、OUT属性で、結果(STATUS)とエラー時の内容(ERR_CODE)を
145         * 返してください。第3引数は、起動元PGIDです。
146         * 結果(STATUS)は、正常ならば、0 を返してください。
147         *
148         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
149         *
150         * @return 結果 [true:正常/false:異常]
151         */
152        private boolean programRun() {
153
154                String[] args = new String[] { "ProgRUN",YKNO };
155                String[] rtn = DBUtil.dbCallExecute( sqlCmd,args,appInfo );     // 3.8.7.0 (2006/12/15)
156
157                boolean flag = false;
158                if( rtn != null && rtn.length == 2 ) {
159                        String rtnCode = rtn[0];
160                        String rtnMsg  = rtn[1];
161                        if( "0".equals( rtnCode ) ) {           // 正常
162                                flag = true;
163                        }
164                        else {
165                                errMsg.append( "PL/SQL=[" ).append( sqlCmd ).append( "] " );
166                                errMsg.append( "YKNO=[" ).append( YKNO ).append( "] " );
167                                errMsg.append( "LISTID=[" ).append( LISTID ).append( "] " );
168                                errMsg.append( rtnCode ).append( ":" ).append( rtnMsg );
169                                errMsg.append( CR );
170                                LogWriter.log( errMsg.toString() );
171                        }
172                }
173                return flag;
174        }
175
176        /**
177         * エラーが存在した場合に、エラーメッセージを返します。
178         *
179         * @return エラーメッセージ String
180         */
181        public String getErrMsg() {
182                return errMsg.toString();
183        }
184}