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.db.DBUtil; 023import org.opengion.fukurou.db.Transaction; 024import org.opengion.fukurou.util.ErrorMessage; 025import org.opengion.fukurou.util.FileUtil; 026import org.opengion.fukurou.util.FixLengthData; 027import org.opengion.fukurou.util.StringUtil; 028 029import org.opengion.hayabusa.common.HybsSystem; 030import org.opengion.hayabusa.common.HybsSystemException; 031import org.opengion.hayabusa.db.AbstractTableFilter; 032import org.opengion.hayabusa.db.DBTableModel; 033 034/** 035 * TableFilter_DBSRC_OUT は、TableFilter インターフェースを継承した、DBTableModel 処理用の 036 * 実装クラスです。 037 * 038 * ここでは、オブジェクト一覧(GF82)の検索結果より、オブジェクト明細テーブル(GF83)から 039 * 必要な情報を取得し、各種オブジェクトソースを抜き出します。 040 * 出力ファイルは、オブジェクト名+".sql" という命名規則で作成します。 041 * 042 * ここで、PACKAGE と、PACKAGE BODY が同じオブジェクト名の場合、同じファイルに追加(append=true)されます。 043 * 本来は、処理フォルダを先に削除しておかないと、上書きされてしまいます。 044 * ここでは、フォルダ削除ではなく、できるだけローカル処理するように、PACKAGE の場合だけ、 045 * 先に、ファイルを削除する処理を実行します。 046 * 047 * また、オブジェクトタイプによって、出力フォルダを変える場合は、個々に指定してください。 048 * 以下のコメントは参考ですので、詳細は、jsp 側の抜出プログラムの仕様をご確認ください。 049 * 050 * view 04_VIEW 051 * function 05_SRC 052 * package 05_SRC 053 * package body 05_SRC 054 * procedure 05_SRC 055 * trigger 06_TRG 056 * 057 * オブジェクト一覧(GF82)の検索では、(SYSTEM_ID,TBLSYU,OBJ_TYPE,OBJ_NAME,NAME_JA) 058 * の項目を取得する必要があります。 059 * 060 * パラメータは、tableFilterタグの keys, vals にそれぞれ記述するか、BODY 部にCSS形式で記述します。 061 * 【パラメータ】 062 * { 063 * DIR : {@BASE_DIR}/sql/install/05_SRC ; 出力ファイルの基準フォルダ(必須) 064 * XML : false ; XML出力を行うかどうか[true/false]を指定します(初期値:false)。 065 * } 066 * 067 * @og.formSample 068 * ●形式: 069 * select SYSTEM_ID,TBLSYU,OBJ_TYPE,OBJ_NAME,NAME_JA from GF82 070 * ① <og:tableFilter classId="DBSRC_OUT" keys="DIR" vals="{@BASE_DIR}/sql/install/05_SRC" /> 071 * 072 * ② <og:tableFilter classId="DBSRC_OUT" > 073 * { 074 * DIR : {@BASE_DIR}/sql/install/05_SRC ; 075 * XML : false ; 076 * } 077 * </og:tableFilter> 078 * 079 * @og.rev 5.6.7.0 (2013/07/27) 新規作成 080 * 081 * @version 0.9.0 2000/10/17 082 * @author Kazuhiko Hasegawa 083 * @since JDK1.1, 084 */ 085public class TableFilter_DBSRC_OUT extends AbstractTableFilter { 086 //* このプログラムのVERSION文字列を設定します。 {@value} */ 087 private static final String VERSION = "5.7.2.0 (2014/01/10)" ; 088 089 /** 090 * keys の整合性チェックを行うための初期設定を行います。 091 * 092 * @param keysMap keys の整合性チェックを行うための Map 093 */ 094 @Override 095 protected void init( final Map<String,String> keysMap ) { 096 keysMap.put( "DIR" , "出力ファイルの基準フォルダ(必須)" ); 097 keysMap.put( "XML" , "XML出力を行うかどうか[true/false]を指定(初期値:false)" ); 098 } 099 100 private static final String[] KEYS = new String[] { "SYSTEM_ID","TBLSYU","OBJ_TYPE","OBJ_NAME","NAME_JA" }; 101 102 private static final int SYSTEM_ID = 0; 103 private static final int TBLSYU = 1; 104 private static final int OBJ_TYPE = 2; 105 private static final int OBJ_NAME = 3; 106 // private static final int NAME_JA = 4; 107 108 private static final String ENCODE = "UTF-8" ; 109 110 // オブジェクト明細テーブル(GF83) の検索SQL 111 private static final String GF83_SEL = "select NOLINE,SRC_TEXT" 112 + " from GF83" 113 + " where SYSTEM_ID=? and TBLSYU=? and OBJ_TYPE=? and OBJ_NAME=?" 114 + " and FGJ='1'" 115 + " order by NOLINE" ; 116 117 // 5.6.6.0 (2013/07/05) ヘッダー部作成用 118 private static final String CMNT = "************************************************************************" ; 119 120 private static final int X = FixLengthData.X ; // type 定数 121 private static final int K = FixLengthData.K ; // type 定数 122 123 /** 5.6.7.0 (2013/07/27) 各種定数 */ 124 protected static final String XML_START_TAG = "<?xml version='1.0' encoding='UTF-8'?>" + CR + "<ROWSET tableName='xxx'>"; 125 protected static final String XML_END_TAG = "</ROWSET>"; 126 protected static final String EXEC_START_TAG= "<EXEC_SQL>"; 127 protected static final String EXEC_END_TAG = "</EXEC_SQL>"; 128 129 /** XML形式かどうか */ 130 protected boolean isXml = false; // 5.6.7.0 (2013/07/27) 131 132 /** 133 * DBTableModel処理を実行します。 134 * 135 * @return 処理結果のDBTableModel 136 */ 137 public DBTableModel execute() { 138 DBTableModel table = getDBTableModel(); 139 140 isXml = StringUtil.nval( getValue( "XML" ), false ); 141 142 int[] clmNo = getTableColumnNo( KEYS ); 143 int rowCnt = table.getRowCount(); 144 145 File dir = new File( getValue( "DIR" ) ); 146 if( ! dir.exists() && !dir.mkdirs() ) { 147 String errMsg = "所定のフォルダが作成できませんでした。[" + dir + "]" ; 148 throw new HybsSystemException( errMsg ); 149 } 150 151 String[] data = null; 152 PrintWriter writer = null; 153 Transaction tran = getTransaction(); 154 155 for( int row=0; row<rowCnt; row++ ) { 156 String objType = null; 157 String objName = null; 158 try { 159 data = table.getValues( row ); 160 String systemId = data[clmNo[SYSTEM_ID]]; 161 String tblsyu = data[clmNo[TBLSYU]]; 162 objType = data[clmNo[OBJ_TYPE]]; 163 objName = data[clmNo[OBJ_NAME]]; 164 165 // パッケージの場合は、既存のファイルを削除します。(PACKAGE BODY がappendされるため) 166 File objFile = new File( dir,objName + ( isXml ? ".xml" : ".sql" ) ); 167 // 6.0.0.1 (2014/04/25) These nested if statements could be combined 168 if( "PACKAGE".equalsIgnoreCase( objType ) && objFile.exists() && !objFile.delete() ) { 169 // このExceptionは、catchでErrorMessageにセットされます。 170 String errMsg = "所定のファイルが削除できませんでした。[" + objFile + "]" ; 171 throw new HybsSystemException( errMsg ); 172 } 173 174 // 出力ファイル名は、オブジェクト名と同じ 175 // PACKAGE と、PACKAGE BODY が同じオブジェクト名の場合、同じファイルに追加(append=true)されます。 176 writer = FileUtil.getPrintWriter( objFile,ENCODE,true ); 177 178 if( isXml ) { writer.println( XML_START_TAG ); } 179 writer.println( makeHeadLine( clmNo,data ) ); 180 181 // オブジェクト明細テーブル(GF83) の検索 182 String[] vals = new String[] { systemId,tblsyu,objType,objName }; 183 String[][] gf83 = DBUtil.dbExecute( GF83_SEL,vals,tran ); 184 if( gf83.length == 0 ) { 185 System.out.println( "OBJ_TYPE=[" + objType + "], OBJ_NAME=[" + objName + "] is Not Found!" ); 186 continue; 187 } 188 189 // ソースの出力 190 StringBuilder buf = new StringBuilder() ; 191 for( int j=0; j<gf83.length; j++ ) { 192 buf.append( gf83[j][1] ).append( CR ); 193 } 194 195 writer.print( buf.toString() ); 196 writer.println( makeEndLine() ); 197 if( isXml ) { writer.println( XML_END_TAG ); } 198 if( writer != null ) { writer.close(); } 199 } 200 catch( RuntimeException ex ) { 201 ErrorMessage errMessage = makeErrorMessage( "TableFilter_DBSRC_OUT Error",ErrorMessage.NG ); 202 errMessage.addMessage( row+1,ErrorMessage.NG,"SRC",ex.getMessage() ); 203 errMessage.addMessage( row+1,ErrorMessage.NG,"SRC",StringUtil.array2csv( data ) ); 204 errMessage.addMessage( row+1,ErrorMessage.NG,"SRC","OBJ_TYPE=[" + objType + "], OBJ_NAME=[" + objName + "]" ); 205 } 206 finally { 207 if( writer != null ) { writer.close(); } 208 } 209 } 210 211 return table; 212 } 213 214 /** 215 * ヘッダーとして使用する文字列を作成します。 216 * 217 * @og.rev 5.7.2.0 (2014/01/10) 構文の見直し 218 * 219 * @param clmNo カラム番号配列 220 * @param data 1行分のデータ配列 221 * 222 * @return ヘッダーとして使用する文字列 223 */ 224 protected String makeHeadLine( final int[] clmNo,final String[] data ) { 225 // 5.7.2.0 (2014/01/10) objType,objName の再利用と、VIEWの場合は、AS を追加します。 226 String objType = data[clmNo[OBJ_TYPE]]; 227 String objName = data[clmNo[OBJ_NAME]]; 228 String as = "VIEW".equalsIgnoreCase( objType ) ? " AS " : " " ; 229 230 String LINE1 = "SYSTEM_ID : " + data[clmNo[SYSTEM_ID]] ; 231 String LINE2 = objName + " ( " + objType + " )" ; // 5.7.2.0 (2014/01/10) 232 String LINE3 = "Created : " + HybsSystem.getDate() ; 233 234 int[] addLen = new int[] { 0,0,0 }; // 各データ間のスペース 235 int[] type = new int[] { X,K,X }; // 各データの種別 X:半角 S:空白前埋め K:全角混在 236 FixLengthData fixData = new FixLengthData( addLen,type ); 237 238 String[][] outData = new String[][] { 239 { "/**", CMNT , "**/" }, 240 { "/* ", LINE1, " */" }, 241 { "/* ", LINE2, " */" }, 242 { "/* ", LINE3, " */" }, 243 { "/**", CMNT , "**/" }, 244 }; 245 246 // 5.6.6.0 (2013/07/05) 簡易メソッドを利用 247 fixData.addAllListData( outData ); 248 249 StringBuilder buf = new StringBuilder(); 250 fixData.getAllFixData( buf ); 251 252 if( isXml ) { buf.append( EXEC_START_TAG ).append( CR ); } 253 buf.append( "CREATE " ).append( objType ).append( " " ).append( objName ).append( as ); // 5.7.2.0 (2014/01/10) 254 255 return buf.toString() ; 256 } 257 258 /** 259 * 最後の行に相当する文字列を作成します。 260 * 261 * @return 最後の行に相当する文字列 262 */ 263 private String makeEndLine() { 264 265 StringBuilder buf = new StringBuilder(); 266 267 if( isXml ) { buf.append( CR ).append( EXEC_END_TAG ); } 268 else { buf.append( "/" ); } 269 270 return buf.toString() ; 271 } 272}