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     */
016    package org.opengion.plugin.column;
017    
018    import org.opengion.fukurou.db.DBUtil;
019    import org.opengion.fukurou.util.ApplicationInfo;
020    import org.opengion.hayabusa.common.HybsSystem;
021    import org.opengion.hayabusa.db.AbstractRenderer;
022    import org.opengion.hayabusa.db.CellRenderer;
023    import org.opengion.hayabusa.db.DBColumn;
024    import 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 オブジェクト毎に?‘つ作æ?されますã?
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     */
047    public 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,操ä½?プログラãƒ?D
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    }