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    }