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.table;
017    
018    import java.io.File;
019    import java.io.PrintWriter;
020    import java.util.Map;
021    
022    import org.opengion.fukurou.util.ErrorMessage;
023    import org.opengion.fukurou.util.FileUtil;
024    import org.opengion.fukurou.util.FixLengthData;
025    import org.opengion.fukurou.util.StringUtil;
026    import org.opengion.hayabusa.common.HybsSystem;
027    import org.opengion.hayabusa.common.HybsSystemException;
028    import org.opengion.hayabusa.db.AbstractTableFilter;
029    import org.opengion.hayabusa.db.DBTableModel;
030    
031    /**
032     * TableFilter_SEQUENCE は、TableUpda インターフェースを継承した、DBTableModel 処?の
033     * 実?ラスです?
034     *
035     * ここでは、シーケンス?の検索結果より、GF09 のシーケンス定義??ブルから
036     * ?な??を取得し、シーケンス作?スクリプトを作?します?
037     *
038     * こ?処?実行するには、DBTableModelのカラ?して?
039     *  SYSTEM_ID,TBLSYU,SEQNAME,INCREBY,STARTVAL,MINVAL,MAXVAL,FGCYCLE,SUCACHE
040     * が?です?
041     *
042     * パラメータは、tableFilterタグの keys, vals にそれぞれ記述するか?BODY 部にCSS形式で記述します?
043     * 【パラメータ?
044     *  {
045     *       DIR : {@BASE_DIR}/sql/install/03_SEQ ;    出力ファイルの基準フォル???)
046     *       XML : false ;                                  XML出力を行うかど?[true/false]を指定しま?初期値:false)?
047     *  }
048     *
049     * @og.formSample
050     * ●形式?
051     *      select SYSTEM_ID,TBLSYU,SEQNAME,INCREBY,STARTVAL,MINVAL,MAXVAL,FGCYCLE,SUCACHE from GF09
052     * 
053     *      ?<og:tableFilter classId="SEQUENCE" keys="DIR,XML" vals='"{@BASE_DIR}/sql/install/03_SEQ,"' />
054     *
055     *      ② <og:tableFilter classId="SEQUENCE" >
056     *               {
057     *                   DIR : {@BASE_DIR}/sql/install/03_SEQ ;
058     *                   XML : false ;
059     *               }
060     *         </og:tableFilter>
061     *
062     * @og.rev 5.1.9.0 (2010/08/01) DB定義DB・シーケンス定義追?
063     * @og.rev 5.6.6.0 (2013/07/05) keys の整合?チェ?を追?
064     *
065     * @version  0.9.0  2010/08/01
066     * @author   Hiroki Nakamura
067     * @since    JDK1.1,
068     */
069    public class TableFilter_SEQUENCE extends AbstractTableFilter {
070            //* こ?プログラ??VERSION??を設定します?       {@value} */
071            private static final String VERSION = "5.6.6.1 (2013/07/12)" ;
072    
073            /**
074             * keys の整合?チェ?を行うための初期設定を行います?
075             *
076             * @og.rev 5.6.6.1 (2013/07/12) keys の整合?チェ?対?
077             *
078             * @param       keysMap keys の整合?チェ?を行うための Map
079             */
080            @Override
081            protected void init( final Map<String,String> keysMap ) {
082                    keysMap.put( "DIR"      , "出力ファイルの基準フォル???)"                                                );
083                    keysMap.put( "XML"      , "XML出力を行うかど?[true/false]を指?初期値:false)"      );
084            }
085    
086            private static final String[] DBKEY = {"SYSTEM_ID","TBLSYU","SEQNAME","INCREBY","STARTVAL","MINVAL","MAXVAL","FGCYCLE","SUCACHE" };
087    
088            /** ??タのアクセス用の配?番号 {@value} */
089            protected static final int SYSTEM_ID            = 0;
090            /** ??タのアクセス用の配?番号 {@value} */
091            protected static final int TBLSYU                       = 1;
092            /** ??タのアクセス用の配?番号 {@value} */
093            protected static final int SEQNAME                      = 2;
094            /** ??タのアクセス用の配?番号 {@value} */
095            protected static final int INCREBY                      = 3;
096            /** ??タのアクセス用の配?番号 {@value} */
097            protected static final int STARTVAL                     = 4;
098            /** ??タのアクセス用の配?番号 {@value} */
099            protected static final int MINVAL                       = 5;
100            /** ??タのアクセス用の配?番号 {@value} */
101            protected static final int MAXVAL                       = 6;
102            /** ??タのアクセス用の配?番号 {@value} */
103            protected static final int FGCYCLE                      = 7;
104            /** ??タのアクセス用の配?番号 {@value} */
105            protected static final int SUCACHE                      = 8;
106    
107            private static final String ENCODE = "UTF-8" ; // 4.3.6.6 (2009/05/15)
108    
109            private static final String CMNT  = "************************************************************************" ;
110    
111            private static final int X = FixLengthData.X ;
112            private static final int K = FixLengthData.K ;
113    
114            /** ?定数 */
115            protected static final String XML_START_TAG     = "<?xml version='1.0' encoding='UTF-8'?>" + CR + "<ROWSET tableName='xxx'>";
116            protected static final String XML_END_TAG       = "</ROWSET>";
117            protected static final String EXEC_START_TAG= "<EXEC_SQL>";
118            protected static final String EXEC_END_TAG      = "</EXEC_SQL>";
119    
120            /** XML形式かど?  */
121            protected boolean               isXml                           = false; // 4.3.7.0 (2009/06/01)
122    
123            /**
124             * DBTableModel処?実行します?
125             *
126             * @og.rev 5.5.2.6 (2012/05/25) protected変数を?private化したため?getterメソ?で取得するよ?変更
127             *
128             * @return      実行結果の??ブルモ?
129             */
130            public DBTableModel execute() {
131                    DBTableModel table = getDBTableModel();         // 5.5.2.6 (2012/05/25) インターフェースにgetterメソ?追?
132    
133                    isXml = StringUtil.nval( getValue( "XML" ), false );
134    
135                    int[] clmNo = getTableColumnNo( DBKEY );
136                    int rowCnt = table.getRowCount();
137    
138                    File dir = new File( getValue( "DIR" ) );
139                    if( ! dir.exists() && ! dir.mkdirs() ) {
140                            String errMsg = "??フォル?作?できませんでした?" + dir + "]" ;
141                            // 4.3.4.4 (2009/01/01)
142                            throw new HybsSystemException( errMsg );
143                    }
144    
145                    String[] data  = null;
146                    PrintWriter writer = null;
147                    for( int row=0; row<rowCnt; row++ ) {
148                            data  = table.getValues( row );
149                            String seqName = data[clmNo[SEQNAME]];
150                            try {
151                                    writer = FileUtil.getPrintWriter( new File( dir,seqName + ( isXml ? ".xml" : ".sql" ) ),ENCODE );
152                                    if( isXml ) { writer.println( XML_START_TAG ); }
153                                    writer.print( makeHeadLine( clmNo,data ) );
154                                    writer.println( makeLineList( clmNo,data ) );
155                            }
156                            catch( RuntimeException ex ) {
157                                    ErrorMessage errMessage = makeErrorMessage( "TableFilter_SEQNENCE Error",ErrorMessage.NG );
158                                    errMessage.addMessage( row+1,ErrorMessage.NG,"SEQUENCE",ex.getMessage() );
159                                    errMessage.addMessage( row+1,ErrorMessage.NG,"SEQUENCE",StringUtil.array2csv( data ) );
160                                    errMessage.addMessage( row+1,ErrorMessage.NG,"SEQUENCE","SEQ=[" + seqName + "]" );
161                                    System.out.println( errMessage );
162                            }
163                            if( isXml ) { writer.println( XML_END_TAG ); }
164                            if( writer != null ) { writer.close(); }
165                    }
166    
167                    return table;
168            }
169    
170            /**
171             * ヘッ??部??処?実行します?
172             *
173             * @og.rev 5.6.6.0 (2013/07/05) FixLengthData の簡易コンストラクタを使用
174             *
175             * @param       clmNo   カラ?号配?
176             * @param       data    ?行?の??タ配?
177             *
178             * @return      ヘッ??部????
179             */
180            protected String makeHeadLine( final int[] clmNo,final String[] data ) {
181                    String seqName = data[clmNo[SEQNAME]];
182                    String LINE1 = seqName;
183                    String LINE2 = "Created : " + HybsSystem.getDate() ;
184    
185                    // 5.6.6.0 (2013/07/05) FixLengthData の簡易コンストラクタを使用
186                    int[] addLen = new int[] { 0,0,0 };     // ?ータ間?スペ?ス
187                    int[] type   = new int[] { X,K,X };     // ?ータの種別 X:半?S:空白前埋?K:全角混在
188                    FixLengthData fixData = new FixLengthData( addLen,type );
189    
190                    String[][] outData = new String[][] {
191                            { "/**",        CMNT ,  "**/" },
192                            { "/* ",        LINE1,  " */" },
193                            { "/* ",        LINE2,  " */" },
194                            { "/**",        CMNT ,  "**/" },
195                    };
196    
197                    fixData.addAllListData( outData );
198    
199                    return fixData.getAllFixData();
200            }
201    
202            /**
203             * シーケンス作?の処?実行します?
204             *
205             * @param       clmNo   カラ?号配?
206             * @param       data    ?行?の??タ配?
207             *
208             * @return      シーケンス作?
209             */
210            protected String makeLineList( final int[] clmNo,final String[] data ) {
211                    StringBuilder buf = new StringBuilder();
212    
213                    if( isXml ) { buf.append( EXEC_START_TAG ).append( CR ); }
214    
215                    buf.append( "CREATE SEQUENCE " ).append( data[clmNo[SEQNAME]] ).append( CR );
216                    buf.append( "  START WITH " ).append( data[clmNo[STARTVAL]] );
217                    buf.append( " INCREMENT BY " ).append( data[clmNo[INCREBY]] );
218                    buf.append( " MINVALUE " ).append( data[clmNo[MINVAL]] );
219                    buf.append( " MAXVALUE " ).append( data[clmNo[MAXVAL]] );
220    
221                    if( "1".equals( data[clmNo[FGCYCLE]] ) ) {
222                            buf.append( " CYCLE" );
223                    }
224                    else {
225                            buf.append( " NOCYCLE" );
226                    }
227    
228                    if( Integer.valueOf( data[clmNo[SUCACHE]] ) > 0 ) {
229                            buf.append( " CACHE " ).append( data[clmNo[SUCACHE]] );
230                    }
231                    else {
232                            buf.append( " NOCACHE" );
233                    }
234    
235                    if( isXml )     { buf.append( CR ).append( EXEC_END_TAG ); }
236                    else            { buf.append( " ;" ); }
237    
238                    return buf.toString();
239            }
240    }