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.db.ApplicationInfo;
024import org.opengion.fukurou.db.DBUtil;
025
026/**
027 * QUERY レンデラーは、表示パラメータで指定された SQL文の実行結果を表示する
028 * クラスで、元のValue を、$1 として使用可能です。
029 * 又、$Cで自身のカラム名が使用可能です。
030 *
031 * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
032 * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
033 * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
034 * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
035 * 変数は、""(ゼロ文字列)として、扱われます。
036 * 又、$Cには自分自身のカラム名を割り当てます。
037 *
038 *  カラムの表示に必要な属性は、DBColumn オブジェクト より取り出します。
039 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
040 *
041 * @og.rev 2.1.1.1 (2002/11/15) QUERY レンデラーを新規追加しました。
042 * @og.group データ表示
043 *
044 * @version  4.0
045 * @author   Kazuhiko Hasegawa
046 * @since    JDK5.0,
047 */
048public class Renderer_QUERY extends AbstractRenderer {
049        /** このプログラムのVERSION文字列を設定します。   {@value} */
050        private static final String VERSION = "5.7.9.0 (2014/08/08)" ;
051
052        private final String query ;
053        private final String dbid ;
054        private final String name ; // 4.3.4.0 (2008/12/01)
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.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
068         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為、ApplicationInfoオブジェクトを設定
069         *
070         */
071        public Renderer_QUERY() {
072                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
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.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
083         * @og.rev 3.4.0.0 (2003/09/01) 表示パラメータ、編集パラメータ、文字パラメータの追加。
084         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為、ApplicationInfoオブジェクトを設定
085         *
086         * @param       clm     DBColumnオブジェクト
087         */
088        private Renderer_QUERY( final DBColumn clm ) {
089                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
090                query = clm.getRendererParam();
091                dbid  = clm.getDbid();
092                appInfo = makeApplicationInfo( clm.getName() );
093                name  = clm.getName();
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( "Renderer_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         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
130         * @og.rev 3.1.2.1 (2003/04/10) synchronized を、削除します。
131         *
132         * @param       clm     DBColumnオブジェクト
133         *
134         * @return      CellRendererオブジェクト
135         * @og.rtnNotNull
136         */
137        public CellRenderer newInstance( final DBColumn clm ) {
138                return new Renderer_QUERY( clm );
139        }
140
141        /**
142         * データの表示用文字列を返します。
143         *
144         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
145         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
146         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
147         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
148         * 変数は、""(ゼロ文字列)として、扱われます。
149         * 又、$Cには自分自身のカラム名を割り当てます。
150         *
151         * @og.rev 3.4.0.2 (2003/09/05) AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てます。
152         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為、ApplicationInfoオブジェクトを設定
153         * @og.rev 4.3.4.0 (2008/12/01) $C対応
154         * @og.rev 5.7.9.0 (2014/08/08) DBUtil.dbExecute 実行時エラーを回避
155         *
156         * @param   value 入力値
157         *
158         * @return  データの表示用文字列
159         * @og.rtnNotNull
160         */
161        @Override
162        public String getValue( final String value ) {
163                // StringFormat format = new StringFormat( query,value );
164                final StringFormat format = new StringFormat( query, value, name ); // 4.3.4.0 (2008/12/01)
165                final String str = format.format();
166
167                // 5.7.9.0 (2014/08/08) DBUtil.dbExecute 実行時エラーを回避
168
169                String rtnVal = value == null ? "" : value ;            // 本来、QUERYで変換すべきだが、元の値を設定する。
170                try {
171                        final String[][] rtn = DBUtil.dbExecute( str,null,appInfo,dbid );
172                        rtnVal = rtn == null || rtn[0] == null || rtn[0][0] == null ? "" : rtn[0][0];                   // 6.4.2.1 (2016/02/05) PMD refactoring. Useless parentheses.
173                }
174                catch( final RuntimeException ex ) {
175                        final String errMsg = "SQL文の処理に失敗しました。" + CR
176                                                                + "query=" + query + " , inValue=" + value + " , name=" + name
177                                                                + CR
178                                                                + ex.getMessage() ;
179                        System.err.println( errMsg );
180                }
181
182                return rtnVal ;
183        }
184}