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.plugin.column;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.hayabusa.db.AbstractEditor;
020import org.opengion.hayabusa.db.CellEditor;
021import org.opengion.hayabusa.db.DBColumn;
022import org.opengion.fukurou.util.StringFormat;
023import org.opengion.fukurou.util.XHTMLTag;
024import org.opengion.fukurou.db.ApplicationInfo;
025import org.opengion.fukurou.db.DBUtil;
026
027/**
028 * QUERY エディターは、編集パラメータで指定された SQL文の実行結果をテキストエリアに表示する
029 * クラスで、元のValue を、$1 として使用可能です。
030 * 又、$Cで自身のカラム名が使用可能です。
031 *
032 * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
033 * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
034 * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
035 * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
036 * 変数は、""(ゼロ文字列)として、扱われます。
037 * 又、$Cには自分自身のカラム名を割り当てます。
038 *
039 *  カラムの表示に必要な属性は、DBColumn オブジェクト より取り出します。
040 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
041 *
042 * @og.rev 4.0.0.0 (2006/04/01) 新規追加
043 * @og.group データ編集
044 *
045 * @version  4.0
046 * @author       Kazuhiko Hasegawa
047 * @since    JDK5.0,
048 */
049public class Editor_QUERY extends AbstractEditor {
050        /** このプログラムのVERSION文字列を設定します。   {@value} */
051        private static final String VERSION = "6.4.5.3 (2016/05/13)" ;
052
053        private final String query ;
054        private final String dbid ;
055
056        /** コネクションにアプリケーション情報を追記するかどうか指定 */
057        public static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
058
059        // 3.8.7.0 (2006/12/15) アクセスログ取得の為、ApplicationInfoオブジェクトを設定
060        private final ApplicationInfo appInfo;
061        private static final String SYSTEM_ID  = HybsSystem.sys( "SYSTEM_ID" ) ;
062
063        /**
064         * デフォルトコンストラクター。
065         * このコンストラクターで、基本オブジェクトを作成します。
066         *
067         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為、ApplicationInfoオブジェクトを設定
068         * @og.rev 6.4.4.2 (2016/04/01) nameのfinal化
069         */
070        public Editor_QUERY() {
071                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
072                // 4.3.4.4 (2009/01/01)
073                query   = null;
074                dbid    = null;
075                appInfo = makeApplicationInfo( null );
076        //      name    = null; // 4.3.4.0 (2008/12/01)
077        }
078
079        /**
080         * DBColumnオブジェクトを指定したprivateコンストラクター。
081         *
082         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為、ApplicationInfoオブジェクトを設定
083         * @og.rev 6.4.4.2 (2016/04/01) nameのfinal化
084         *
085         * @param       clm     DBColumnオブジェクト
086         */
087        private Editor_QUERY( final DBColumn clm ) {
088                super( clm );
089                query = clm.getEditorParam();
090                dbid  = clm.getDbid();
091                tagBuffer.add( XHTMLTag.inputAttri( attributes ) );
092                appInfo = makeApplicationInfo( clm.getName() );
093        //      name  = clm.getName(); //4.3.4.0 (2008/12/01)
094        }
095
096        /**
097         * アクセスログ取得の為、ApplicationInfoオブジェクトを作成します。
098         *
099         * @og.rev 3.8.7.0 (2006/12/15) 新規作成
100         *
101         * @param       name    オブジェクト
102         *
103         * @return ApplicationInfoオブジェクト
104         */
105        private ApplicationInfo makeApplicationInfo( final String name ) {
106                // 6.3.9.1 (2015/11/27) Found 'DD'-anomaly for variable(PMD)
107                final ApplicationInfo infoTemp ;
108
109                // 3.8.7.0 (2006/12/15) アクセスログ取得の為、ApplicationInfoオブジェクトを設定
110                if( USE_DB_APPLICATION_INFO ) {
111                        infoTemp = new ApplicationInfo();
112                        // ユーザーID,IPアドレス,ホスト名
113                        infoTemp.setClientInfo( SYSTEM_ID,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
114                        // 画面ID,操作,プログラムID
115                        infoTemp.setModuleInfo( "Editor_QUERY",null,name );
116                }
117                else {
118                        infoTemp = null;                                // 6.3.9.1 (2015/11/27)
119                }
120
121                return infoTemp ;
122        }
123
124        /**
125         * 各オブジェクトから自分のインスタンスを返します。
126         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
127         * まかされます。
128         *
129         * @param       clm     DBColumnオブジェクト
130         *
131         * @return      CellEditorオブジェクト
132         * @og.rtnNotNull
133         */
134        public CellEditor newInstance( final DBColumn clm ) {
135                return new Editor_QUERY( clm );
136        }
137
138        /**
139         * データの編集用文字列を返します。
140         *
141         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
142         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
143         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
144         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
145         * 変数は、""(ゼロ文字列)として、扱われます。
146         * 又、$Cには自分自身のカラム名を割り当てます。
147         *
148         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為、ApplicationInfoオブジェクトを設定
149         * @og.rev 4.3.4.0 (2008/12/01) $C対応
150         * @og.rev 5.7.9.0 (2014/08/08) DBUtil.dbExecute 実行時エラーを回避
151         * @og.rev 6.4.5.3 (2016/05/13) value は、コロン区切りの先頭だけ分離する。
152         *
153         * @param       value 入力値
154         *
155         * @return      データの編集用文字列
156         */
157        @Override
158        public String getValue( final String value ) {
159                // StringFormat format = new StringFormat( query,value );
160                final StringFormat format = new StringFormat( query,value, name ); // 4.3.4.0 (2008/12/01)
161                final String str = format.format();
162
163                // 5.7.9.0 (2014/08/08) DBUtil.dbExecute 実行時エラーを回避
164                String rtnVal = (value == null) ? "" : StringFormat.getValue( value ) ;         // 6.4.5.3 (2016/05/13) 本来、QUERYで変換すべきだが、元の値を設定する。
165                try {
166                        final String[][] rtn = DBUtil.dbExecute( str,null,appInfo,dbid );
167                        rtnVal = rtn == null || rtn[0] == null || rtn[0][0] == null ? "" : rtn[0][0];           // 6.4.2.1 (2016/02/05) PMD refactoring. Useless parentheses.
168                }
169                catch( final RuntimeException ex ) {
170                        final String errMsg = "SQL文の処理に失敗しました。" + CR
171                                                                + "query=" + query + " , inValue=" + value + " , name=" + name
172                                                                + CR
173                                                                + ex.getMessage() ;
174                        System.err.println( errMsg );
175                }
176
177                return super.getValue( rtnVal );
178        }
179
180        /**
181         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
182         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し、
183         * リクエスト情報を1つ毎のフィールドで処理できます。
184         *
185         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
186         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
187         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
188         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
189         * 変数は、""(ゼロ文字列)として、扱われます。
190         * 又、$Cには自分自身のカラム名を割り当てます。
191         *
192         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為、ApplicationInfoオブジェクトを設定
193         * @og.rev 4.3.4.0 (2008/12/01) $C対応
194         * @og.rev 5.7.9.0 (2014/08/08) DBUtil.dbExecute 実行時エラーを回避
195         * @og.rev 6.4.5.3 (2016/05/13) value は、コロン区切りの先頭だけ分離する。
196         *
197         * @param       row   行番号
198         * @param       value 入力値
199         *
200         * @return      データ表示/編集用の文字列
201         */
202        @Override
203        public String getValue( final int row,final String value ) {
204                // StringFormat format = new StringFormat( query,value );
205                final StringFormat format = new StringFormat( query,value,name ); // 4.3.4.0 (2008/12/01)
206                final String str = format.format();
207
208                // 5.7.9.0 (2014/08/08) DBUtil.dbExecute 実行時エラーを回避
209                String rtnVal = (value == null) ? "" : StringFormat.getValue( value ) ;         // 6.4.5.3 (2016/05/13) 本来、QUERYで変換すべきだが、元の値を設定する。
210                try {
211                        final String[][] rtn = DBUtil.dbExecute( str,null,appInfo,dbid );
212                        rtnVal = rtn == null || rtn[0] == null || rtn[0][0] == null ? "" : rtn[0][0];           // 6.4.2.1 (2016/02/05) PMD refactoring. Useless parentheses.
213                }
214                catch( final RuntimeException ex ) {
215                        final String errMsg = "SQL文の処理に失敗しました。" + CR
216                                                                + "row=" + row + " , query=" + query + " , inValue=" + value + " , name=" + name
217                                                                + CR
218                                                                + ex.getMessage() ;
219                        System.err.println( errMsg );
220                }
221
222                return super.getValue( row,rtnVal );
223        }
224}