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    }