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 }