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.hayabusa.taglib;
017    
018    import static org.opengion.fukurou.util.StringUtil.nval;
019    
020    import java.io.File;
021    
022    import org.opengion.fukurou.db.ConnectionFactory;
023    import org.opengion.fukurou.db.DatabaseConfig;
024    import org.opengion.fukurou.util.StringUtil;
025    import org.opengion.hayabusa.common.HybsSystem;
026    import org.opengion.hayabusa.common.HybsSystemException;
027    import org.opengion.hayabusa.db.DBTableModel;
028    import org.opengion.hayabusa.db.DBTableModelUtil;
029    
030    /**
031     * DBConfig.xmlの編é›?‚’行うためのタグですã?
032     *
033     * 自åˆ??身のコンãƒ?‚­ストã?DBConfig.xmlの編é›?�Šび、ファイルパスを指定して他ã?
034     * コンãƒ?‚­ストã?DBConfig.xmlの編é›?‚’行うことができますã?
035     *
036     * 編é›?‚¿イプã?、type属æ?により、ã?DBドライバã?クラス」と「DBIDの設定情報」ã?2つã‚?
037     * åˆ?‚Š替えることができますã?
038     *
039     * またã?こã?タグ自体ã?機è?としては、ã?取得ã?、ã?保存ã?、ã?æƒ??」にåˆ?›¢されてã�?�¾すã?
040     *
041     * (1)取å¾?command="GET")
042     *  type属æ?でæŒ?®šされたæƒ??を読み取り、DBTableModel形式に変換しã?セãƒ?‚·ョンに登録しますã?
043     *  (tableIdは"DEFAULT"でã�?
044     *
045     * (2)保å­?command="SET")
046     *  セãƒ?‚·ョンからDBTableModelを読み取り、type属æ?でæŒ?®šされたæƒ??の形式にオブジェクト化しã?
047     *  DBConfig.xmlに保存しますã?
048     *  (DBTableModelを取得するtableIdは"DEFAULT"でã�?
049     *
050     *  保存を実行すると、å?のDBConfig.xmlファイルと同じ場æ‰?�«、タイプスタンプ付きのバックアãƒ??
051     *  ファイルがè?動的に作æ?されますã?
052     *
053     *  またã?xmlFile(DBConfig.xml)を指定しなã�?ж態で、保å­?command="SET")を行うとã€?
054     *  å†?ƒ¨にキャãƒ?‚·ュしてã�?‚‹接続情報は、ä¸?—¦全てクリアされますã?
055     *
056     * (3)�?(command="INFO")
057     *  DBIDæƒ??のキーとタイトルから、ã?ルãƒ?‚¦ンメニューを作æ?するためのæƒ??を画面出力しますã?
058     *  こã?値をã?valueタグで取込み、columnタグのcodeKeyVal変数にセãƒ?ƒˆすれば、DBIDの選択メニューã‚?
059     *  作æ?できますã?開発用に接続å?をå?り替える処ç�?­‰に使ã�?�¾すã?
060     *
061     * @og.formSample
062     * ●形式ï¼?lt;dbConfigEditor command="..." type="..." xmlFile="..." />
063     * ●body?šなã�?
064     *
065     * ●Tag定義??
066     *   <og:dbConfigEditor
067     *       command            【TAG】DBConfig.xmlに対する操作ã?コマンãƒ?GET/SET/INFO)を指定しまã�?初期値:GET)
068     *       type               【TAG】DBConfig.xmlに対する操作ã?タイãƒ?DBID/CLASS)を指定しまã�?初期値:DBID)
069     *       xmlFile            【TAG】編é›?‚’行うDBConfig.xmlのファイルのパスを指定しまã�?初期値:自身のDBConfig.xml)
070     *       debug              【TAG】デバッグæƒ??をå?力するかどã�?�‹[true/false]を指定しまã�?初期値:false)
071     *   />
072     *
073     * ●使用ä¾?
074     *    ・取å¾?
075     *        <dbConfigEditor command="GET" type="{@TYPE}" xmlFile="{@DBCXML}" />
076     *
077     *    ・保å­?
078     *        <dbConfigEditor command="SET" type="{@MEM.TYPE}" xmlFile="{@MEM.DBCXML}" />
079     *
080     *    ・æƒ??
081     *        <value command="SET" key="DBID_INFO" >
082     *          <dbConfigEditor command="INFO" />
083     *        </value>
084     *
085     *        <column name="DBID_INFO" codeKeyVal="{@DBID_INFO}" />
086     *
087     * @og.group そã?他部å“?
088     *
089     * @version  4.0
090     * @author       Hiroki Nakamura
091     * @since    JDK5.0,
092     */
093    public class DBConfigEditorTag extends CommonTagSupport {
094            //* こã?プログラãƒ??VERSIONæ–?­—å?を設定しますã?       {@value} */
095            private static final String VERSION = "5.6.7.0 (2013/07/27)" ;
096    
097            private static final long serialVersionUID = 567020130727L ;
098    
099            private transient DBTableModel  table   = null;         // 5.1.9.0 (2010/08/01) transient 追åŠ?
100            private String                  command = "GET";
101            private String                  type    = "DBID";
102            private String                  xmlFile = null;
103    
104            /**
105             * Taglibの開始タグが見つかったときに処ç�?�™ã‚?doStartTag() ã‚?オーバã?ライドしますã?
106             *
107             * @return      後続å?ç�??æŒ?¤º( SKIP_BODY )
108             */
109            @Override
110            public int doStartTag() {
111                    startQueryTransaction( HybsSystem.TBL_MDL_KEY );
112                    return ( SKIP_BODY );
113            }
114    
115            /**
116             * Taglibの終äº?‚¿グが見つかったときに処ç�?�™ã‚?doEndTag() ã‚?オーバã?ライドしますã?
117             *
118             * @og.rev 5.6.7.0 (2013/07/27) command="INFO" 対å¿?
119             *
120             * @return      後続å?ç�??æŒ?¤º
121             */
122            @Override
123            public int doEndTag() {
124                    debugPrint();
125    
126                    // DatabaseConfigオブジェクトを取得しますã?
127                    DatabaseConfig dbc = null;
128                    if( xmlFile == null ) {
129                            dbc = new DatabaseConfig();
130                    }
131                    else {
132                            if( !( new File( xmlFile ) ).exists() ) {
133                                    throw new HybsSystemException( "編é›?¯¾象のDBConfig.xmlが存在しませんã€?ile=[" + xmlFile + "]" );
134                            }
135                            dbc = new DatabaseConfig( xmlFile );
136                    }
137    
138                    // 編é›?‚¿イプに応じてキーæƒ??をセãƒ?ƒˆしますã?
139                    String[] keys = null;
140                    if( "DBID".equals( type ) ) {
141                            keys = DatabaseConfig.getDbidInfoKeys();
142                    }
143                    else if( "CLASS".equals( type ) ) {
144                            keys = new String[1];
145                            keys[0] = DatabaseConfig.getDriverKey();
146                    }
147    
148                    // DBConfig.xmlからæƒ??を取得しますã?
149                    if( "GET".equals( command ) ) {
150                            table = getDBConfig( dbc, keys );
151    
152                            if( table != null && !commitTableObject( HybsSystem.TBL_MDL_KEY, table ) ) {
153                                    jspPrint( "QueryTag Query処ç�?�Œ割り込まれましたã€?BTableModel は登録しませんã€? );
154                                    return(SKIP_PAGE);
155                            }
156                    }
157                    // DBConfig.xmlにæƒ??を保存しますã?
158                    else if( "SET".equals( command ) ) {
159                            setDBConfig( dbc, keys );
160    
161                            // xmlFileが指定されてã�?�ªã�?=自信のDBConfig.xml)を書き換えた場合ã?、キャãƒ?‚·ュをクリアしますã?
162                            if( xmlFile == null ) {
163                                    ConnectionFactory.realClose();
164                                    ConnectionFactory.init( HybsSystem.getContextName(), null );
165                            }
166                    }
167                    // 5.6.7.0 (2013/07/27) command="INFO" 対å¿?
168                    else if( "INFO".equals( command ) ) {
169                            jspPrint( dbc.getCodeKeyVal() );
170                    }
171    
172                    return( EVAL_PAGE );
173            }
174    
175            /**
176             * DBConfig.xmlからæƒ??を取得しますã?
177             *
178             * @param       dbc             DatabaseConfigオブジェクãƒ?
179             * @param       keys    編é›?�™る情報のカラãƒ?‚­ー配å?
180             *
181             * @return      ãƒ??ブルモãƒ?ƒ«
182             */
183            private DBTableModel getDBConfig( final DatabaseConfig dbc, final String[] keys ) {
184                    String[][] vals = null;
185                    if( "DBID".equals( type ) ) {
186                            vals = dbc.getDbidInfo();
187                    }
188                    else if( "CLASS".equals( type ) ) {
189                            String[] tmpVals = dbc.getDriverList();
190                            vals = new String[tmpVals.length][1];
191                            for( int i=0; i<tmpVals.length; i++ ) {
192                                    vals[i][0] = tmpVals[i];
193                            }
194                    }
195    
196                    return DBTableModelUtil.makeDBTable( keys, vals, getResource() );
197            }
198    
199            /**
200             * DBConfig.xmlにæƒ??を保存しますã?
201             *
202             * @param dbc DatabaseConfigオブジェクãƒ?
203             * @param keys 編é›?�™る情報のカラãƒ?‚­ー配å?
204             */
205            private void setDBConfig( final DatabaseConfig dbc, final String[] keys ) {
206                    table = (DBTableModel)getObject( HybsSystem.TBL_MDL_KEY );
207    
208                    String tmpKeys = StringUtil.array2csv( table.getNames() );
209                    if( !StringUtil.array2csv( keys ).equals( tmpKeys ) ) {
210                            throw new RuntimeException( "DBTableModelのキーが不正でã�? );
211                    }
212    
213                    int row = table.getRowCount();
214                    String[][] vals = new String[row][keys.length];
215                    for( int i=0; i<row; i++ ) {
216                            vals[i] = table.getValues( i );
217                    }
218    
219                    if( "DBID".equals( type ) ) {
220                            dbc.setDbidInfo( vals );
221                    }
222                    else if( "CLASS".equals( type ) ) {
223                            String[] tmpVals = new String[vals.length];
224                            for( int i=0; i<vals.length; i++ ) {
225                                    tmpVals[i] = vals[i][0];
226                            }
227                            dbc.setDriverList( tmpVals );
228                    }
229            }
230    
231            /**
232             * タグリブオブジェクトをリリースしますã?
233             * キャãƒ?‚·ュされて再利用されるã?で、フィールドã?初期設定を行いますã?
234             *
235             */
236            @Override
237            protected void release2() {
238                    super.release2();
239                    command                 = "GET";
240                    type                    = "DBID";
241                    xmlFile                 = null;
242            }
243    
244            /**
245             * 【TAG】DBConfig.xmlに対する操作ã?コマンãƒ?GET/SET/INFO)を指定しまã�?初期値:GET)ã€?
246             *
247             * @og.tag
248             *  GET  â‡?typeに応じた情報をDBConfig.xmlから読み出しã?DBTableModelに登録しますã?
249             *  SET  â‡?typeに応じた情報をDBTableModelから読み出しã?DBConfig.xmlに登録しますã?
250             *  INFO â‡?DBConfig.xmlに登録されてã�?‚‹ DBID のキーとタイトルをé?結したコードリソースæƒ??を画面出力しますã?
251             *
252             * なおã?保å­?SET)を実行すると、å?のDBConfig.xmlファイルと同じ場æ‰?�«、タイプスタンプ付きの
253             * バックアãƒ??ファイルがè?動的に作æ?されますã?
254             *
255             * またã?xmlFile(DBConfig.xml)を指定しなã�?ж態で、保å­?command="SET")を行うとã€?
256             * å†?ƒ¨にキャãƒ?‚·ュしてã�?‚‹接続情報は、ä¸?—¦全てクリアされますã?
257             *
258             * @og.rev 5.6.7.0 (2013/07/27) command として使用できるæ–?­—å?をチェãƒ?‚¯しますã?
259             *
260             * @param       cmd コマンãƒ?
261             */
262            public void setCommand( final String cmd ) {
263                    command = nval( getRequestParameter( cmd ),command );
264    
265                    // 5.6.7.0 (2013/07/27) command として使用できるæ–?­—å?をチェãƒ?‚¯しますã?
266                    if( "|GET|SET|INFO|".indexOf( "|" + command + "|" ) < 0 ) {
267                                    String errMsg = "commandは GET,SET,INFO のどれかですã?"
268                                                            + "command=" + command ;
269                                    throw new HybsSystemException( errMsg );
270                    }
271            }
272    
273            /**
274             * 【TAG】DBConfig.xmlに対する操作ã?タイãƒ?DBID/CLASS)を指定しまã�?初期値:DBID)ã€?
275             *
276             * @og.tag
277             *  DBID  â‡?DBIDのå�?¨®æƒ??を読み取り/書き込みしますã?
278             *  CLASS â‡?ロードするDBドライバã?のä¸?¦§を読み取り/書き込みしますã?
279             *
280             * @og.rev 5.6.7.0 (2013/07/27) type として使用できるæ–?­—å?をチェãƒ?‚¯しますã?
281             *
282             * @param       tp タイãƒ?
283             */
284            public void setType( final String tp ) {
285                    type = nval( getRequestParameter( tp ),type );
286    
287                    // 5.6.7.0 (2013/07/27) type として使用できるæ–?­—å?をチェãƒ?‚¯しますã?
288                    if( "|DBID|CLASS|".indexOf( "|" + type + "|" ) < 0 ) {
289                                    String errMsg = "typeは DBID,CLASS のどれかですã?"
290                                                            + "type=" + type ;
291                                    throw new HybsSystemException( errMsg );
292                    }
293            }
294    
295            /**
296             * 【TAG】編é›?‚’行うDBConfig.xmlのファイルのパスを指定しまã�?初期値:自身のDBConfig.xml)ã€?
297             *
298             * @og.tag
299             * 何もæŒ?®šされなã�??合ã?、è?身のコンãƒ?‚­ストã?DBConfig.xmlに対する編é›?�¨なりますã?
300             *
301             * @param       file DBConfig.xmlのファイルパス
302             */
303            public void setXmlFile( final String file ) {
304                    xmlFile = nval( getRequestParameter( file ),xmlFile );
305            }
306    
307            /**
308             * こã?オブジェクトã?æ–?­—å?表現を返しますã?
309             * 基本çš?�«ãƒ?ƒ�ãƒ?‚°目çš?�«使用しますã?
310             *
311             * @return こã?クラスのæ–?­—å?表現
312             */
313            @Override
314            public String toString() {
315                    return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
316                                    .println( "VERSION"             ,VERSION        )
317                                    .println( "command"             ,command        )
318                                    .println( "type"                ,type           )
319                                    .println( "xmlFile"             ,xmlFile        )
320                                    .println( "Other..."    ,getAttributes().getAttribute() )
321                                    .fixForm().toString() ;
322            }
323    }