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