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.queue;
017
018// import org.apache.commons.lang3.StringUtils;                                 // 7.0.6.0 (2019/10/07)
019import org.opengion.fukurou.db.ConnectionFactory;
020import org.opengion.fukurou.db.DBFunctionName;
021import org.opengion.fukurou.db.DBUtil;
022import org.opengion.fukurou.db.Transaction;
023import org.opengion.fukurou.db.TransactionReal;
024// import org.opengion.fukurou.util.ApplicationInfo;
025import org.opengion.fukurou.db.ApplicationInfo;                                 // 7.0.6.0 (2019/10/07) util → db に移動のため
026import org.opengion.hayabusa.common.HybsSystem;
027import org.opengion.fukurou.system.DateSet;                                             // 7.0.6.0 (2019/10/07) HybsSystem.getDate( String )
028
029/**
030 * キュー処理用のDBアクセスクラス
031 * キューの受信と送信処理で、
032 * データベースにアクセスして処理を行います。
033 *
034 * 実際のMQ,SQSとの通信そのものはfukurouで行います。
035 *
036 * @og.group メッセージ連携
037 *
038 * @og.rev 5.10.15.2 (2019/09/20) 新規作成
039 *
040 * @version 5
041 * @author oota
042 * @since JDK7
043 *
044 */
045public class DBAccessQueue {
046        private static final String DYSET_YMD  = "yyyyMMddHHmmss";
047//      private static ApplicationInfo appInfo ;
048        private static final String DBID = HybsSystem.sys( "RESOURCE_DBID" );
049        private final String SYSTEM_ID;
050        private final String USRSET;
051        private final String PGUPD;
052        private final String DMN_NAME;
053        private final ApplicationInfo appInfo ;                 // 7.2.9.4 (2020/11/20) PMD:Possible unsafe assignment to a non-final static field in a constructor.
054
055        /** 処理中 {@value} */
056        public static final String  FGKAN_PROCESS = "2";
057        /** 完了 {@value} */
058        public static final String  FGKAN_END = "3";
059        /** エラー{@value} */
060        public static final String  FGKAN_ERROR = "4";
061
062        /** コネクションにアプリケーション情報を追記するかどうか指定 */
063        // パラメータ編集必要
064        private static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
065
066        /**
067         * コンストラクター
068         * 初期処理を行います。
069         *
070         * @param systemid システムID
071         * @param usrset ユーザID
072         * @param prpupd プログラムID
073         * @param dmnName デーモン名
074         */
075        public DBAccessQueue(final String systemid, final String usrset, final String prpupd, final String dmnName) {
076                SYSTEM_ID = systemid;
077//              USRSET = StringUtils.defaultString(usrset, "UNNONE") ;                  // 7.0.6.0 (2019/10/07)
078//              PGUPD  = StringUtils.defaultString( prpupd, "UNNONE" ) ;                // 7.0.6.0 (2019/10/07)
079//              DMN_NAME = StringUtils.defaultString( dmnName, "UNNONE" ) ;             // 7.0.6.0 (2019/10/07)
080                USRSET          = usrset  == null ? "UNNONE" : usrset ;                         // "" の時、"" を返す必要がある。StringUtil.nval は、"" と null を同値に扱う
081                PGUPD           = prpupd  == null ? "UNNONE" : prpupd ;
082                DMN_NAME        = dmnName == null ? "UNNONE" : dmnName ;
083
084                if (USE_DB_APPLICATION_INFO) {
085                        appInfo = new ApplicationInfo();
086
087                        // ユーザーID,IPアドレス,ホスト名
088                        appInfo.setClientInfo(USRSET, HybsSystem.HOST_ADRS, HybsSystem.HOST_NAME);
089                        // 画面ID,操作,プログラムID
090                        appInfo.setModuleInfo("DBAccessQueue", null, DMN_NAME);
091                }
092                else {
093                        appInfo = null;
094                }
095        }
096
097        /**
098         * GE65検索
099         * GE65の処理対象リストを取得します。
100         *
101         * @return 処理対象リスト
102         */
103        public String[][] selectGE65() {
104                final String sql = "SELECT A.YKNO, A.QUEUEID, A.MESSAGE, A.SFDUPID, B.QUESYU, B.JMSURL FROM GE66 A"
105                                + " INNER JOIN GE65 B ON A.SYSTEM_ID = B.SYSTEM_ID AND A.QUEUEID = B.QUEUEID AND B.FGJ = '1'"
106                                + " WHERE A.SYSTEM_ID = ? AND A.FGKAN = '1' AND A.FGJ = '1'";
107
108                // 7.2.9.4 (2020/11/20) PMD:Consider simply returning the value vs storing it in local variable 'vals'
109                return DBUtil.dbExecute(sql, new String[] {SYSTEM_ID}, appInfo, DBID);
110//              final String[][] vals = DBUtil.dbExecute(sql, new String[] {SYSTEM_ID}, appInfo, DBID);
111//              return vals;
112        }
113
114        /**
115         * エラー状態に更新
116         * 完了フラグをエラー状態に更新して、
117         * エラー情報を格納します。
118         *
119         * @param ykno 要求番号
120         * @param errMsg エラーメッセージ
121         */
122        public void updateGE66Error(final String ykno, final String errMsg) {
123                final String updSql = "UPDATE GE66 SET FGKAN = ?, ERRMSG = ?"
124                                + "WHERE SYSTEM_ID = ? AND FGJ = '1' AND YKNO = ?";
125                DBUtil.dbExecute(updSql, new String[] {
126                                FGKAN_ERROR, errMsg, SYSTEM_ID, ykno
127                }, appInfo, DBID);
128        }
129
130        /**
131         * 完了フラグの更新
132         * 完了フラグを指定された値に更新します。
133         *
134         * @param ykno 要求番号
135         * @param fgkan 完了フラグ
136         */
137        public void updateGE66(final String ykno, final String fgkan) {
138//              final String dyset = HybsSystem.getDate(DYSET_YMD);
139                final String dyset = DateSet.getDate(DYSET_YMD);                        // 7.0.6.0 (2019/10/07)
140                final String updSql = "UPDATE GE66 SET FGKAN = ?, DYUPD = ?, USRUPD = ?, PGUPD = ?"
141                                + "WHERE SYSTEM_ID = ? AND FGJ = '1' AND YKNO = ?";
142                DBUtil.dbExecute(updSql, new String[] {
143                                fgkan, dyset, USRSET, PGUPD, SYSTEM_ID, ykno}, appInfo, DBID);
144        }
145        /**
146         * 受信管理データ取得
147         * 受信管理テーブルから、キューIDとbizlogic名を取得します。
148         *
149         * @return 受信管理リスト
150         */
151        public String[][] setlectGE67() {
152                // 対象 キュー名(グループ名)とbizlogic名の取得処理
153                final String sql = "SELECT QUEUEID, BIZLOGICID FROM GE67"
154                                + " WHERE SYSTEM_ID = ? AND FGJ = '1'";
155
156                // 7.2.9.4 (2020/11/20) PMD:Consider simply returning the value vs storing it in local variable 'vals'
157                return DBUtil.dbExecute(sql, new String[] {SYSTEM_ID}, appInfo, DBID);
158//              final String[][] vals = DBUtil.dbExecute(sql, new String[] {SYSTEM_ID}, appInfo, DBID);
159//              return vals;
160        }
161
162        /**
163         * 処理番号生成
164         * GE68_SEQUENCEからシーケンス番号を生成します。
165         *
166         * @og.rev 7.0.6.4 (2019/11/29) TransactionRealのclose漏れ対応
167         *
168         * @return 処理番号
169         */
170        public String generateSyoriNo() {
171                String syoriNo = "";
172
173//              final Transaction tran = new TransactionReal( appInfo );
174
175                // 処理番号生成
176                try( Transaction tran = new TransactionReal( appInfo ) ) {                              // 7.0.6.4 (2019/11/29) try-with-resources文
177//              try {
178                        final DBFunctionName dbName = DBFunctionName.getDBName( ConnectionFactory.getDBName( DBID ) );
179                        syoriNo = Integer.toString(dbName.getSequence("GE68S01", tran, DBID));
180//              }finally {
181//                      tran.close();
182                }
183
184                return syoriNo;
185        }
186
187        /**
188         * GE68(キュー受信結果テーブル)更新
189         * キュー受信結果テーブルを指定された完了状態に更新します。
190         *
191         * @param syno 処理番号
192         * @param fgkan 完了フラグ
193         */
194        public void updateGE68(final String syno,  final String fgkan) {
195//              final String dyset = HybsSystem.getDate(DYSET_YMD);
196                final String dyset = DateSet.getDate(DYSET_YMD);                        // 7.0.6.0 (2019/10/07)
197
198                final String sql = "UPDATE GE68 SET FGKAN = ?, DYUPD = ?, USRUPD = ?, PGUPD = ?"
199                                + "WHERE FGJ = '1'"
200                                + " AND SYSTEM_ID = ?"
201                                + " AND SYNO = ?";
202
203                final String[] data = new String[] {fgkan, dyset, USRSET, PGUPD, SYSTEM_ID, syno};
204
205                // sql実行
206                DBUtil.dbExecute(sql, data, appInfo, DBID);
207        }
208
209        /**
210         *  GE68(キュー受信結果テーブル)登録
211         * キュー受信結果テーブルに受信データを登録します。
212         *
213         * @param queueNm キュー名
214         * @param syno 処理NO
215         * @param bizlogicId ビズロジックID
216         * @param messageText メッセージ
217         */
218        public void insertGE68(final String queueNm, final String syno, final String bizlogicId
219                        ,final String messageText) {
220//              final String dyset = HybsSystem.getDate(DYSET_YMD);
221                final String dyset = DateSet.getDate(DYSET_YMD);                        // 7.0.6.0 (2019/10/07)
222
223                final String sql = "INSERT INTO GE68(SYSTEM_ID, QUEUEID, SYNO, BIZLOGICID, MESSAGE, FGKAN, FGJ,"
224                                + "DYSET, DYUPD, USRSET, USRUPD, PGSET,  PGUPD)"
225                                + " VALUES(?,?,?,?,?,?,'1'"
226                                + ",?,?,?,?,?,?)";
227
228                final String[] data = new String[] {
229                                SYSTEM_ID, queueNm, syno, bizlogicId, messageText, DBAccessQueue.FGKAN_PROCESS
230                                ,dyset, dyset, USRSET,  USRSET, PGUPD, PGUPD
231                };
232
233                // sql実行
234                DBUtil.dbExecute(sql,  data, appInfo, DBID);
235        }
236
237        /**
238         * GE68(キュー受信結果テーブル)エラー更新
239         * キュー受信結果テーブルをエラー状態に更新します。
240         *
241         * @param syoriNo 処理NO
242         * @param errMsg エラーメッセージ
243         */
244        public void updateGE68Error(final String syoriNo, final String errMsg) {
245                // エラーテーブルに登録
246//              final String dyset = HybsSystem.getDate(DYSET_YMD);
247                final String dyset = DateSet.getDate(DYSET_YMD);                        // 7.0.6.0 (2019/10/07)
248
249                final String sql = "UPDATE GE68"
250                                + " SET ERRMSG = ?, FGKAN = ?"
251                                + " ,DYUPD = ?, USRUPD = ?, PGUPD = ?"
252                                + " WHERE SYSTEM_ID = ? AND FGJ = '1' AND SYNO = ? ";
253
254                final String[] data = new String[] {
255                                  errMsg                                                        // エラーメッセージ
256                                , DBAccessQueue.FGKAN_ERROR
257                                , dyset
258                                , USRSET
259                                , PGUPD
260                                , SYSTEM_ID
261                                , syoriNo
262                };
263
264                // sql実行
265                DBUtil.dbExecute(sql, data, appInfo, DBID);
266        }
267}