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_CLM は、TableFilter インターフェースを継承した、DBTableModel 処?の
034     * 実?ラスです?
035     *
036     * ここでは、テーブルカラ??の検索結果より、ORACLE の??ブルカラ?コメントを作?します?
037     * 構文は、?COMMENT ON COLUMN ??ブル?カラ? IS 'コメン?」です?
038     * こ?コメントを取り出す?合??
039     *「SELECT COLUMN_NAME, COMMENTS FROM USER_COL_COMMENTS WHERE TABLE_NAME = '??ブル??
040     * とします?
041     * 
042     * SQLの??ル類には、このコメントを使用して、テーブルカラ??日本語名を表示させたりします?
043     * 
044     * 検索では?TABLE_NAME,CLM,NAME_JA) の?を取得する?があります?
045     *
046     * パラメータは、tableFilterタグの keys, vals にそれぞれ記述するか?BODY 部にCSS形式で記述します?
047     * 出力ファイル名???常、テーブル で?つ、カラ??つにまとめて作?されます?
048     * 【パラメータ?
049     *  {
050     *       DIR  : {@BASE_DIR}/sql/install/08_CMNT ;    出力ファイルの基準フォル???)
051     *       FILE : false ;                                   出力ファイル?初期値:CMNT_CLM[.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_CLM" keys="DIR" vals='"{@BASE_DIR}/sql/install/08_CMNT"' />
060     *
061     *      ② <og:tableFilter classId="CMNT_CLM" >
062     *               {
063     *                   DIR   : {@BASE_DIR}/sql/install/08_CMNT ;
064     *                   FILE  : CMNT_CLM ;
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_CLM 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_CLM[.sql|.xml])"                             );
090                    keysMap.put( "XML"      , "XML出力を行うかど?[true/false]を指?初期値:false)"      );
091            }
092    
093            private static final String[] DBKEY = {"TABLE_NAME","CLM","NAME_JA"};
094    
095            /** ??タのアクセス用の配?番号 {@value} */
096            protected static final int TABLE_NAME           = 0;
097            /** ??タのアクセス用の配?番号 {@value} */
098            protected static final int CLM                          = 1;
099            /** ??タのアクセス用の配?番号 {@value} */
100            protected static final int NAME_JA                      = 2;
101    
102     //     private static final String ENCODE = "Windows-31J" ;
103            private static final String ENCODE = "UTF-8" ;
104    
105            private static final int X = FixLengthData.X ;          // type 定数
106            private static final int K = FixLengthData.K ;          // type 定数
107    
108            /** ?定数  */
109            protected static final String XML_START_TAG     = "<?xml version='1.0' encoding='UTF-8'?>" + CR + "<ROWSET>";
110            protected static final String XML_END_TAG       = "</ROWSET>";
111            protected static final String EXEC_START_TAG= "<EXEC_SQL>";
112            protected static final String EXEC_END_TAG      = "</EXEC_SQL>";
113    
114            /** XML形式かど? */
115            protected boolean               isXml                           = false;
116    
117            /** ファイル?拡張子な? */
118            private String          fileName                                = "CMNT_CLM";
119    
120            /**
121             * DBTableModel処?実行します?
122             *
123             * @return      実行結果の??ブルモ?
124             */
125            public DBTableModel execute() {
126                    DBTableModel table = getDBTableModel();
127    
128                    isXml   = StringUtil.nval( getValue( "XML"   ), isXml   );
129    
130                    int[] clmNo = getTableColumnNo( DBKEY );
131                    int rowCnt = table.getRowCount();
132    
133                    File dir = new File( getValue( "DIR" ) );
134    
135                    if( ! dir.exists() && ! dir.mkdirs() ) {
136                            String errMsg = "??フォル?作?できませんでした?" + dir + "]" ;
137                            // 4.3.4.4 (2009/01/01)
138                            throw new HybsSystemException( errMsg );
139                    }
140    
141                    fileName =  StringUtil.nval( getValue( "FILE" ), fileName );
142    
143                    // COMMENT ON COLUMN ??ブル?カラ? IS 'コメン?
144                    int[] addLen = new int[] { 0,0,0,0 };   // ?ータ間?スペ?ス
145                    int[] type   = new int[] { X,X,X,K };   // ?ータの種別 X:半?S:空白前埋?K:全角混在
146                    FixLengthData fixData = new FixLengthData( addLen,type );
147    
148                    String[] data  = null;
149                    int row = 0;
150                    try {
151                            PrintWriter writer = FileUtil.getPrintWriter( new File( dir,fileName + ( isXml ? ".xml" : ".sql" ) ),ENCODE );
152    
153                            if( isXml ) { writer.println( XML_START_TAG ); }
154    
155                            // 全??タを読み込んで、最大長の計算を行う?
156                            for( row=0; row<rowCnt; row++ ) {
157                                    data      = table.getValues( row );
158                                    String tblClm  = data[clmNo[TABLE_NAME]] + "." + data[clmNo[CLM]];
159                                    String name_ja = "'" + data[clmNo[NAME_JA]] + "'";
160    
161                                    String[] outData = new String[] { "COMMENT ON COLUMN " , tblClm , " IS " , name_ja } ;
162                                    fixData.addListData( outData );
163                            }
164    
165                            // ??タの出?
166                            for( row=0; row<rowCnt; row++ ) {
167                                    if( isXml ) { writer.print( EXEC_START_TAG ); }
168                                    writer.print( fixData.getFixData( row ) );
169                                    if( isXml ) { writer.println( EXEC_END_TAG ); }
170                                    else            { writer.println( ";" ); }
171                            }
172    
173                            if( isXml ) { writer.println( XML_END_TAG ); }
174                            writer.close();
175                    }
176                    catch( RuntimeException ex ) {
177                            ErrorMessage errMessage = makeErrorMessage( "TableFilter_CMNT_CLM Error",ErrorMessage.NG );
178                            data = table.getValues( row );
179                            errMessage.addMessage( row+1,ErrorMessage.NG,"CMNT_CLM",StringUtil.array2csv( data ) );
180                            // BAT から呼び出す?合があるため、標準エラー出力にも情報を?しておきます?
181                            System.out.println( errMessage );
182                            System.out.println( ex );
183                    }
184    
185                    return table;
186            }
187    }