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 */
016package org.opengion.plugin.table;
017
018import java.io.File;
019import java.io.PrintWriter;
020import java.util.Map;
021
022import org.opengion.fukurou.util.ErrorMessage;
023import org.opengion.fukurou.util.FileUtil;
024import org.opengion.fukurou.util.FixLengthData;
025import org.opengion.fukurou.util.StringUtil;
026import org.opengion.hayabusa.common.HybsSystem;
027import org.opengion.hayabusa.common.HybsSystemException;
028import org.opengion.hayabusa.db.AbstractTableFilter;
029import 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 *      A <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 */
069public 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    1行分のデータ配列
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    1行分のデータ配列
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}