OSDN Git Service

Ver8.5.2.0
[opengion/opengionV8.git] / uap / webapps / gf / src / org / opengion / plugin / query / Query_JDBCErrMsg.java
1 /*
2  * Copyright (c) 2009 The openGion Project.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific language
14  * governing permissions and limitations under the License.
15  */
16 package org.opengion.plugin.query;
17
18 import org.opengion.hayabusa.db.AbstractQuery;
19 import org.opengion.hayabusa.db.DBErrMsg;
20 import org.opengion.hayabusa.common.HybsSystemException;
21 import org.opengion.fukurou.util.ErrorMessage;
22 import org.opengion.fukurou.util.StringUtil;
23
24 import java.sql.Connection;
25 import java.sql.CallableStatement;
26 import java.sql.ResultSet;
27 import java.sql.SQLException;
28 import java.sql.Types;
29 import java.sql.Array;                                                          // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ) 対応。oracle.sql.ARRAY の置き換え
30 import oracle.jdbc.OracleConnection;                            // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ) 対応
31
32 import oracle.jdbc.OracleTypes;                                         // CURSOR が残る
33 import oracle.jdbc.OracleCallableStatement;                     // CURSOR が残る
34
35 import java.util.Map;
36
37 /**
38  * エントリ系 PL/SQL をコールして、結果カーソルから、DBTableModel を作成します。
39  *
40  * java.sql.CallableStatement を用いて、データベース検索処理を行います。
41  * 引数を配列指定で渡すことが出来、エラー時には、DBErrMsg オブジェクトにエラー情報を
42  * 格納して返すことが可能です。
43  * 内部変数の受け渡しのデフォルト実装は、AbstractQuery クラスを継承している
44  * ため、ここでは、execute() メソッドを実装しています。
45  * このクラスでは、ステートメント文を execute() する事により、データベースを
46  * 検索した結果を DBTableModel に割り当てます。
47  *
48  * @og.formSample
49  * 例:jsp/TYPESB/result.jsp  (検索系:カーソル返し)
50  * 例:jsp/TYPE3B/entry.jsp   (エントリ系)
51  *     names には、ARG_ARRAY 配列に順次セットされます。
52  *     使用する場合は、一旦わかり易い変数に受けて利用してください。
53  *     呼び出す PL/SQL では、検索系PL/SQL です。(下記の例は、エントリ系)
54  *
55  * <og:query
56  *     command    = "NEW"
57  *     names      = "SYSTEM_ID,LANG,CLM,NAME_JA,LABEL_NAME,KBSAKU,USER.ID"
58  *     queryType  = "JDBCErrMsg"
59  *     displayMsg = "" >
60  *        {call TYPE3B01.TYPE3B01(?,?,?,?)}
61  * </og:query>
62  *
63  *    CREATE OR REPLACE PACKAGE TYPE3B01 AS
64  *        TYPE CUST_CURSOR IS REF CURSOR;
65  *        PROCEDURE TYPE3B01(
66  *            P_KEKKA      OUT    NUMBER,
67  *            P_ERRMSGS    OUT    ERR_MSG_ARRAY,
68  *            P_RC1        OUT    CUST_CURSOR,
69  *            P_ARGS       IN     ARG_ARRAY     );
70  *    END;
71  *
72  *     P_SYSTEM_ID     GEA08.SYSTEM_ID%TYPE     := P_ARGS(1);    --システムID
73  *     P_LANG          GEA08.LANG%TYPE          := P_ARGS(2);    --言語
74  *     P_CLM           GEA08.CLM%TYPE           := P_ARGS(3);    --項目
75  *     P_NAME_JA       GEA08.NAME_JA%TYPE       := P_ARGS(4);    --名称(漢字)
76  *     P_LABEL_NAME    GEA08.LABEL_NAME%TYPE    := P_ARGS(5);    --表示名称
77  *     P_KBSAKU        GEA08.KBSAKU%TYPE        := P_ARGS(6);    --作成区分
78  *     P_USRSET        GEA08.USRSET%TYPE        := P_ARGS(7);    --登録者
79  *
80  * @og.group データ表示
81  * @og.group データ編集
82  *
83  * @version  4.0
84  * @author   Kazuhiko Hasegawa
85  * @since    JDK5.0,
86  */
87 public class Query_JDBCErrMsg extends AbstractQuery {
88         /** このプログラムのVERSION文字列を設定します。       {@value} */
89         private static final String VERSION = "7.0.5.0 (2019/09/24)" ;
90
91         /**
92          * デフォルトコンストラクター
93          *
94          * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
95          */
96         public Query_JDBCErrMsg() { super(); }          // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
97
98         /**
99          * 引数配列付のクエリーを実行します。
100          * 処理自体は、#execute() と同様に、各サブクラスの実装に依存します。
101          * これは、CallableStatement を用いて、データベース検索処理を行います。
102          * {call TYPE3B01.TYPE3B01(?,?,?,?)} で、4番目の引数には、
103          * names で指定したリクエスト情報が、ARG_ARRAY 配列に順次セットされます。
104          * 使用する場合は、一旦わかり易い変数に受けて利用してください。
105          * 呼び出す PL/SQL では、検索系PL/SQL です。
106          *
107          * @og.rev 2.3.1.3 (2003/01/28) Open Cursor が、大量に残る件の対応。ResultSet を close()
108          * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
109          * @og.rev 3.3.3.1 (2003/07/18) DB登録時の後ろスペースを削除する。
110          * @og.rev 3.5.2.0 (2003/10/20) 内部オブジェクトタイプ名を システムパラメータ で定義します。
111          * @og.rev 3.5.6.0 (2004/06/18) nullに対する無駄な比較を削除します。
112          * @og.rev 3.8.0.8 (2005/10/03) エラーメッセージの出力順をメッセージ+Queryに変更します。
113          * @og.rev 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
114          * @og.rev 6.3.6.1 (2015/08/28) close(),realClose() 廃止。Queryはキャッシュしません。
115          * @og.rev 6.4.2.1 (2016/02/05) try-with-resources 文で記述。
116          * @og.rev 6.9.3.0 (2018/03/26) DB_FETCH_SIZE追加。
117          * @og.rev 7.0.5.0 (2019/09/24) カーソルが null の場合チェックとエラーメッセージを追加。
118          *
119          * @param   args オブジェクトの引数配列(可変長引数)
120          */
121         @Override
122         public void execute( final String... args ) {                   // 6.1.1.0 (2015/01/17) refactoring
123                 try {
124                         final Connection conn = getConnection();
125                         // 6.4.2.1 (2016/02/05) try-with-resources 文
126                         try( CallableStatement callStmt = conn.prepareCall( getStatement() ) ) {
127                                 callStmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
128                                 callStmt.setFetchSize( DB_FETCH_SIZE );                 // 6.9.3.0 (2018/03/26)
129
130                                 final Map<String,Class<?>> map = conn.getTypeMap();
131                                 map.put( ERR_MSG,DBErrMsg.class );      // 4.0.0 (2005/01/31)
132
133                                 // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応 http://docs.oracle.com/cd/E28389_01/web.1111/b60995/thirdparty.htm
134                                 final Array newArray = ((OracleConnection)conn).createOracleArray( ARG_ARRAY, StringUtil.rTrims( args ));               // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
135
136                                 callStmt.registerOutParameter(1, Types.INTEGER);
137                                 callStmt.registerOutParameter(2, Types.ARRAY,ERR_MSG_ARRAY);                    // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
138                                 callStmt.registerOutParameter(3, OracleTypes.CURSOR);
139                                 callStmt.setArray( 4,newArray );                                                                                // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
140
141                                 callStmt.execute();
142
143                                 final int rtnCode = callStmt.getInt(1);
144                                 setErrorCode( rtnCode );
145                                 if( rtnCode < ErrorMessage.NG ) {               // 異常以外の場合
146                                         try( ResultSet resultSet = ((OracleCallableStatement)callStmt).getCursor(3) ) {
147                                                 // 7.0.5.0 (2019/09/24) カーソルが null の場合チェックとエラーメッセージを追加。
148                                                 if( resultSet == null ) {
149                                                         setErrorCode( ErrorMessage.EXCEPTION );
150                                                         final String errMsg = "Query_JDBCErrMsg Return CURSOR Not Found !!" + CR;
151                                                         throw new HybsSystemException( errMsg );
152                                                 }
153                                                 else {
154                                                         createTableModel( resultSet );
155                                                 }
156                                         }
157                                 }
158                                 if( rtnCode > ErrorMessage.OK ) {               // 正常以外の場合
159                                         final Array rtn3 = callStmt.getArray(2);                                                                        // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
160                                         final Object[] rtnval3 = (Object[])rtn3.getArray();
161                                         final ErrorMessage errMessage = new ErrorMessage( "Query_JDBCErrMsg Error!!" );
162                                         for( int i=0; i<rtnval3.length; i++ ) {
163                                                 final DBErrMsg er = (DBErrMsg)rtnval3[i];
164                                                 if( er == null ) { break; }
165                                                 errMessage.addMessage( er.getErrMsg() );
166                                         }
167                                         setErrorMessage( errMessage );
168                                 }
169                         }
170                 }
171                 catch( final SQLException ex) {
172                         setErrorCode( ErrorMessage.EXCEPTION );
173                         final String errMsg = ex.getMessage() + ":" + ex.getSQLState() + CR
174                                                 + getStatement() + CR;
175                         throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び順変更
176                 }
177         }
178 }