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