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.Locale; 021import java.util.Map; 022 023import org.opengion.fukurou.util.ErrorMessage; 024import org.opengion.fukurou.util.FileUtil; 025import org.opengion.fukurou.util.FixLengthData; 026import org.opengion.fukurou.util.StringUtil; 027import org.opengion.hayabusa.common.HybsSystem; 028import org.opengion.hayabusa.common.HybsSystemException; 029import org.opengion.hayabusa.db.AbstractTableFilter; 030import org.opengion.hayabusa.db.DBTableModel; 031 032/** 033 * TableFilter_CMNT_TBL は、TableFilter インターフェースを継承した、DBTableModel 処理用の 034 * 実装クラスです。 035 * 036 * ここでは、テーブル一覧の検索結果より、ORACLE のテーブルにコメントを作成します。 037 * 構文は、「COMMENT ON TABLE テーブル名 IS 'コメント'」です。 038 * このコメントを取り出す場合は、 039 *「SELECT COMMENTS FROM USER_TAB_COMMENTS WHERE TABLE_NAME = 'テーブル名'」 040 * とします。 041 * 042 * SQLのツール類には、このコメントを使用して、テーブルの日本語名を表示させたりします。 043 * 044 * 検索では、(TABLE_NAME,NAME_JA) の項目を取得する必要があります。 045 * 046 * パラメータは、tableFilterタグの keys, vals にそれぞれ記述するか、BODY 部にCSS形式で記述します。 047 * 出力ファイル名は、通常、テーブル で1つ、カラムで1つにまとめて作成されます。 048 * 【パラメータ】 049 * { 050 * DIR : {@BASE_DIR}/sql/install/08_CMNT ; 出力ファイルの基準フォルダ(必須) 051 * FILE : false ; 出力ファイル名(初期値:CMNT_TBL[.sql|.xml]) 052 * XML : false ; XML出力を行うかどうか[true/false]を指定します(初期値:false) 053 * } 054 * 055 * @og.formSample 056 * ●形式: 057 * select SYSTEM_ID,TABLE_NAME,NAME_JA from GF02 058 * 059 * ① <og:tableFilter classId="CMNT_TBL" keys="DIR" vals='"{@BASE_DIR}/sql/install/08_CMNT"' /> 060 * 061 * ② <og:tableFilter classId="CMNT_TBL" > 062 * { 063 * DIR : {@BASE_DIR}/sql/install/08_CMNT ; 064 * FILE : CMNT_TBL ; 065 * XML : false ; 066 * } 067 * </og:tableFilter> 068 * 069 * @og.rev 4.0.0.0 (2005/08/31) 新規作成 070 * 071 * @version 0.9.0 2000/10/17 072 * @author Kazuhiko Hasegawa 073 * @since JDK1.6, 074 */ 075public class TableFilter_CMNT_TBL extends AbstractTableFilter { 076 //* このプログラムのVERSION文字列を設定します。 {@value} */ 077 private static final String VERSION = "5.6.6.2 (2013/07/19)" ; 078 079 /** 080 * keys の整合性チェックを行うための初期設定を行います。 081 * 082 * @og.rev 5.6.6.1 (2013/07/12) keys の整合性チェック対応 083 * 084 * @param keysMap keys の整合性チェックを行うための Map 085 */ 086 @Override 087 protected void init( final Map<String,String> keysMap ) { 088 keysMap.put( "DIR" , "出力ファイルの基準フォルダ(必須)" ); 089 keysMap.put( "FILE" , "出力ファイル名(初期値:CMNT_TBL[.sql|.xml])" ); 090 keysMap.put( "XML" , "XML出力を行うかどうか[true/false]を指定(初期値:false)" ); 091 } 092 093 private static final String[] DBKEY = {"TABLE_NAME","NAME_JA"}; 094 095 /** データのアクセス用の配列番号 {@value} */ 096 protected static final int TABLE_NAME = 0; 097 /** データのアクセス用の配列番号 {@value} */ 098 protected static final int NAME_JA = 1; 099 100 // private static final String ENCODE = "Windows-31J" ; 101 private static final String ENCODE = "UTF-8" ; 102 103 private static final int X = FixLengthData.X ; // type 定数 104 private static final int K = FixLengthData.K ; // type 定数 105 106 /** 各種定数 */ 107 protected static final String XML_START_TAG = "<?xml version='1.0' encoding='UTF-8'?>" + CR + "<ROWSET>"; 108 protected static final String XML_END_TAG = "</ROWSET>"; 109 protected static final String EXEC_START_TAG= "<EXEC_SQL>"; 110 protected static final String EXEC_END_TAG = "</EXEC_SQL>"; 111 112 /** XML形式かどうか */ 113 protected boolean isXml = false; 114 115 /** ファイル名(拡張子なし) */ 116 private String fileName = "CMNT_TBL"; 117 118 /** 119 * DBTableModel処理を実行します。 120 * 121 * @return 実行結果のテーブルモデル 122 */ 123 public DBTableModel execute() { 124 DBTableModel table = getDBTableModel(); 125 126 isXml = StringUtil.nval( getValue( "XML" ), isXml ); 127 128 int[] clmNo = getTableColumnNo( DBKEY ); 129 int rowCnt = table.getRowCount(); 130 131 File dir = new File( getValue( "DIR" ) ); 132 133 if( ! dir.exists() && ! dir.mkdirs() ) { 134 String errMsg = "所定のフォルダが作成できませんでした。[" + dir + "]" ; 135 // 4.3.4.4 (2009/01/01) 136 throw new HybsSystemException( errMsg ); 137 } 138 139 fileName = StringUtil.nval( getValue( "FILE" ), fileName ); 140 141 // COMMENT ON TABLE テーブル名 IS 'コメント' 142 int[] addLen = new int[] { 0,0,0,0 }; // 各データ間のスペース 143 int[] type = new int[] { X,X,X,K }; // 各データの種別 X:半角 S:空白前埋め K:全角混在 144 FixLengthData fixData = new FixLengthData( addLen,type ); 145 146 String[] data = null; 147 int row = 0; 148 try { 149 PrintWriter writer = FileUtil.getPrintWriter( new File( dir,fileName + ( isXml ? ".xml" : ".sql" ) ),ENCODE ); 150 151 if( isXml ) { writer.println( XML_START_TAG ); } 152 153 // 全データを読み込んで、最大長の計算を行う。 154 for( row=0; row<rowCnt; row++ ) { 155 data = table.getValues( row ); 156 String tableName = data[clmNo[TABLE_NAME]]; 157 String name_ja = "'" + data[clmNo[NAME_JA]] + "'"; 158 159 String[] outData = new String[] { "COMMENT ON TABLE " , tableName , " IS " , name_ja } ; 160 fixData.addListData( outData ); 161 } 162 163 // データの出力 164 for( row=0; row<rowCnt; row++ ) { 165 if( isXml ) { writer.print( EXEC_START_TAG ); } 166 writer.print( fixData.getFixData( row ) ); 167 if( isXml ) { writer.println( EXEC_END_TAG ); } 168 else { writer.println( ";" ); } 169 } 170 171 if( isXml ) { writer.println( XML_END_TAG ); } 172 writer.close(); 173 } 174 catch( RuntimeException ex ) { 175 ErrorMessage errMessage = makeErrorMessage( "TableFilter_CMNT_TBL Error",ErrorMessage.NG ); 176 data = table.getValues( row ); 177 errMessage.addMessage( row+1,ErrorMessage.NG,"TABLE",StringUtil.array2csv( data ) ); 178 // BAT から呼び出す場合があるため、標準エラー出力にも情報を出しておきます。 179 System.out.println( errMessage ); 180 } 181 182 return table; 183 } 184}