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