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    }