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.AbstractRenderer;
020import org.opengion.hayabusa.db.CellRenderer;
021import org.opengion.hayabusa.db.DBColumn;
022import org.opengion.fukurou.util.StringFormat;
023import org.opengion.fukurou.util.ApplicationInfo;
024import org.opengion.fukurou.db.DBUtil;
025
026/**
027 * QUERY レンデラーは、表示パラメータで指定された SQL文の実行結果を表示する
028 * クラスで、元のValue を、$1 として使用可能です。
029 * 又、$Cで自身のカラム名が使用可能です。
030 *
031 *  カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
032 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
033 *
034 * @og.rev 2.1.1.1 (2002/11/15) QUERY レンデラーを新規追加しました。
035 * @og.group データ表示
036 *
037 * @version  4.0
038 * @author   Kazuhiko Hasegawa
039 * @since    JDK5.0,
040 */
041public class Renderer_QUERY extends AbstractRenderer {
042        //* このプログラムのVERSION文字列を設定します。   {@value} */
043        private static final String VERSION = "5.7.9.0 (2014/08/08)" ;
044
045        private final String query ;
046        private final String dbid ;
047        private final String name ; // 4.3.4.0 (2008/12/01)
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.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
061         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
062         *
063         */
064        public Renderer_QUERY() {
065                query   = null;
066                dbid    = null;
067                appInfo = makeApplicationInfo( null );
068                name    = null; // 4.3.4.0 (2008/12/01)
069        }
070
071        /**
072         * デフォルトコンストラクター。
073         *
074         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
075         * @og.rev 3.4.0.0 (2003/09/01) 表示パラメータ、編集パラメータ、文字パラメータの追加。
076         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
077         *
078         * @param       clm     DBColumnオブジェクト
079         */
080        private Renderer_QUERY( final DBColumn clm ) {
081                query = clm.getRendererParam();
082                dbid  = clm.getDbid();
083                appInfo = makeApplicationInfo( clm.getName() );
084                name  = clm.getName();
085        }
086
087        /**
088         * アクセスログ取得の為,ApplicationInfoオブジェクトを作成します。
089         *
090         * @og.rev 3.8.7.0 (2006/12/15) 新規作成
091         *
092         * @param       name    オブジェクト
093         *
094         * @return      ApplicationInfoオブジェクト
095         */
096        private ApplicationInfo makeApplicationInfo( final String name ) {
097                ApplicationInfo infoTemp = null;
098
099                // 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
100                if( USE_DB_APPLICATION_INFO ) {
101                        infoTemp = new ApplicationInfo();
102                        // ユーザーID,IPアドレス,ホスト名
103                        infoTemp.setClientInfo( SYSTEM_ID,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
104                        // 画面ID,操作,プログラムID
105                        infoTemp.setModuleInfo( "Renderer_QUERY",null,name );
106                }
107
108                return infoTemp ;
109        }
110
111        /**
112         * 各オブジェクトから自分のインスタンスを返します。
113         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
114         * まかされます。
115         *
116         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
117         * @og.rev 3.1.2.1 (2003/04/10) synchronized を、削除します。
118         *
119         * @param       clm     DBColumnオブジェクト
120         *
121         * @return      CellRendererオブジェクト
122         */
123        public CellRenderer newInstance( final DBColumn clm ) {
124                return new Renderer_QUERY( clm );
125        }
126
127        /**
128         * データの表示用文字列を返します。
129         *
130         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
131         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
132         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
133         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
134         * 変数は、""(ゼロ文字列)として、扱われます。
135         * 又、$Cには自分自身のカラム名を割り当てます。
136         *
137         * @og.rev 3.4.0.2 (2003/09/05) AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てます。
138         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
139         * @og.rev 4.3.4.0 (2008/12/01) $C対応
140         * @og.rev 5.7.9.0 (2014/08/08) DBUtil.dbExecute 実行時エラーを回避
141         *
142         * @param   value 入力値
143         *
144         * @return  データの表示用文字列
145         */
146        @Override
147        public String getValue( final String value ) {
148                // StringFormat format = new StringFormat( query,value );
149                StringFormat format = new StringFormat( query, value, name ); // 4.3.4.0 (2008/12/01)
150                String str = format.format();
151
152                // 5.7.9.0 (2014/08/08) DBUtil.dbExecute 実行時エラーを回避
153//              String[][] rtn = DBUtil.dbExecute( str, null, appInfo, dbid );
154//              return ( rtn == null || rtn[0] == null ) ? "" : rtn[0][0];
155
156                String rtnVal = (value == null) ? "" : value ;          // 本来、QUERYで変換すべきだが、元の値を設定する。
157                try {
158                        String[][] rtn = DBUtil.dbExecute( str,null,appInfo,dbid );
159                        rtnVal = ( rtn == null || rtn[0] == null || rtn[0][0] == null ) ? "" : rtn[0][0];
160                }
161                catch( RuntimeException ex ) {
162                        String errMsg = "SQL文の処理に失敗しました。" + HybsSystem.CR
163                                                                + "query=" + query + " , inValue=" + value + " , name=" + name
164                                                                + HybsSystem.CR
165                                                                + ex.getMessage() ;
166                        System.err.println( errMsg );
167                }
168
169                return rtnVal ;
170        }
171}