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