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