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.Locale;
021    import java.util.Map;
022    
023    import org.opengion.fukurou.util.ErrorMessage;
024    import org.opengion.fukurou.util.FileUtil;
025    import org.opengion.fukurou.util.FixLengthData;
026    import org.opengion.fukurou.util.StringUtil;
027    import org.opengion.hayabusa.common.HybsSystem;
028    import org.opengion.hayabusa.common.HybsSystemException;
029    import org.opengion.hayabusa.db.AbstractTableFilter;
030    import 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     * 出力ファイル名???常、テーブル で?つ、カラ??つにまとめて作?されます?
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     */
075    public 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    }