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.system.OgBuilder ;                          // 6.4.4.1 (2016/03/18)
022import org.opengion.fukurou.util.ErrorMessage;
023import org.opengion.fukurou.util.FileUtil;
024import org.opengion.fukurou.util.FixLengthData;
025import org.opengion.fukurou.util.StringUtil;
026import org.opengion.hayabusa.common.HybsSystem;
027import org.opengion.hayabusa.common.HybsSystemException;
028import org.opengion.hayabusa.db.AbstractTableFilter;
029import org.opengion.hayabusa.db.DBTableModel;
030import org.opengion.hayabusa.resource.ResourceFactory;
031import org.opengion.hayabusa.resource.ResourceManager;
032
033/**
034 * TableFilter_DBARG_OUT は、TableFilter インターフェースを継承した、DBTableModel 処理用の
035 * 実装クラスです。
036 *
037 * ここでは、テーブル一覧の検索結果より、GF81 のARG変数チェックテーブルから
038 * 必要な情報を取得し、ARG変数作成スクリプトを作成します。
039 * 出力ファイルは、オブジェクト名+".sql" という命名規則で作成します。
040 * 検索では、(SYSTEM_ID,TBLSYU,OBJ_NAME,SEQNO,CLM,CLM_NAME,CLS_NAME,USE_LENGTH,MAX_LENGTH,TABLE_NAME)
041 * の項目を取得する必要があります。
042 *
043 * パラメータは、tableFilterタグの keys, vals にそれぞれ記述するか、BODY 部にCSS形式で記述します。
044 * 【パラメータ】
045 *  {
046 *       DIR : {@BASE_DIR}/sql/install/07_ARG ;   出力ファイルの基準フォルダ(必須)
047 *       XML : false ;                                 XML出力を行うかどうか[true/false]を指定します(初期値:false)。
048 *  }
049 *
050 * @og.formSample
051 * ●形式:
052 *      select SYSTEM_ID,TBLSYU,OBJ_NAME,SEQNO,CLM,CLM_NAME,CLS_NAME,USE_LENGTH,MAX_LENGTH,TABLE_NAME from GF81
053 *      ① <og:tableFilter classId="DBARG_OUT" keys="DIR" vals="{@BASE_DIR}/sql/install/07_ARG" />
054 *
055 *      ② <og:tableFilter classId="DBARG_OUT" >
056 *               {
057 *                   DIR : {@BASE_DIR}/sql/install/07_ARG ;
058 *                   XML : false ;
059 *               }
060 *         </og:tableFilter>
061 *
062 * @og.rev 5.6.6.0 (2013/07/05) keys の整合性チェックを追加
063 *
064 * @version  0.9.0  2000/10/17
065 * @author   Kazuhiko Hasegawa
066 * @since    JDK1.1,
067 */
068public class TableFilter_DBARG_OUT extends AbstractTableFilter {
069        /** このプログラムのVERSION文字列を設定します。   {@value} */
070        private static final String VERSION = "6.5.0.1 (2016/10/21)" ;
071
072        // 5.6.6.0 (2013/07/05) 検索項目を増やしておきます。
073        private static final String[] KEYS = { "SYSTEM_ID","TBLSYU","OBJ_NAME","SEQNO","CLM","CLM_NAME","CLS_NAME","USE_LENGTH","MAX_LENGTH","TABLE_NAME" };
074
075        private static final int SYSTEM_ID              = 0;
076 //     private static final int TBLSYU                 = 1;
077        private static final int OBJ_NAME               = 2;
078        private static final int SEQNO                  = 3;
079        private static final int CLM                    = 4;
080        private static final int CLM_NAME               = 5;
081        private static final int CLS_NAME               = 6;
082        private static final int USE_LENGTH             = 7;
083 //     private static final int MAX_LENGTH             = 8;
084 //     private static final int TABLE_NAME             = 9;
085
086 //     private static final String ENCODE = "Windows-31J" ;
087        private static final String ENCODE = "UTF-8" ;                  // 5.6.7.0 (2013/07/27) sqlも UTF-8 で出力
088
089        // 5.6.6.0 (2013/07/05) ヘッダー部作成用
090        private static final String CMNT  = "************************************************************************" ;
091
092        private static final int X = FixLengthData.X ;          // type 定数
093        private static final int S = FixLengthData.S ;          // type 定数
094        private static final int K = FixLengthData.K ;          // type 定数
095        private static final int T = FixLengthData.T ;          // addLen 定数
096        private static final int T3= FixLengthData.T3 ;         // addLen 定数
097
098        /** 5.6.7.0 (2013/07/27) 各種定数  */
099        // 6.0.2.3 (2014/10/10) AbstractTableFilter へ移動
100
101        /**
102         * デフォルトコンストラクター
103         *
104         * @og.rev 6.4.1.1 (2016/01/16) keysMap を、サブクラスから設定させるように変更。
105         */
106        public TableFilter_DBARG_OUT() {
107                super();
108                initSet( "DIR"  , "出力ファイルの基準フォルダ(必須)"                                           );
109                initSet( "XML"  , "XML出力を行うかどうか[true/false]を指定(初期値:false)"      );              // 5.6.7.0 (2013/07/27) xml対応
110        }
111
112        /**
113         * DBTableModel処理を実行します。
114         *
115         * @og.rev 4.0.0.0 (2007/11/28) メソッドの戻り値をチェックします。
116         * @og.rev 5.5.2.6 (2012/05/25) protected変数を、private化したため、getterメソッドで取得するように変更
117         * @og.rev 5.6.7.0 (2013/07/27) xml 出力対応
118         * @og.rev 6.0.2.3 (2014/10/10) EXEC_END_TAG 自体にCRを追加。キャッシュします。
119         * @og.rev 6.3.7.0 (2015/09/04) AutoCloseableを使用したtry-with-resources構築に対応。
120         * @og.rev 6.3.9.0 (2015/11/06) resourceをローカル変数にして引数で渡すようにする。
121         * @og.rev 6.5.0.1 (2016/10/21) ErrorMessage をまとめるのと、直接 Throwable を渡します。
122         *
123         * @return 処理結果のDBTableModel
124         */
125        public DBTableModel execute() {
126                isXml = StringUtil.nval( getValue( "XML" ), false );    // 5.6.7.0 (2013/07/27) xml 出力対応
127                // 6.0.2.3 (2014/10/10) EXEC_END_TAG 自体にCRを追加。キャッシュします。
128                execEndTag = isXml ? CR + EXEC_END_TAG : ";" ;
129
130                final File dir = new File( getValue( "DIR" ) );
131                if( ! dir.exists() && !dir.mkdirs() ) {
132                        final String errMsg = "所定のフォルダが作成できませんでした。[" + dir + "]" ;
133                        // 4.3.4.4 (2009/01/01)
134                        throw new HybsSystemException( errMsg );
135                }
136
137                // カンマ,カラム,クラス,(,桁数,),記号(--),表示順,名称
138                final int[] addLen = new int[] { 0,T,0,0,0,T3,0,1,0 };  // 各データ間のスペース
139                final int[] type   = new int[] { X,X,X,X,S,X, X,S,K };  // 各データの種別 X:半角 S:空白前埋め K:全角混在
140                final FixLengthData fixData = new FixLengthData( addLen,type );
141
142                String[] data  = null;
143                String oldObjName  = null;
144
145                final DBTableModel table = getDBTableModel();           // 5.5.2.6 (2012/05/25) インターフェースにgetterメソッド追加
146                final int[] clmNo  = getTableColumnNo( KEYS );
147                final int   rowCnt = table.getRowCount();
148
149                // 6.3.9.0 (2015/11/06) resourceをローカル変数にして引数で渡すようにする。
150                ResourceManager resource = null;
151
152                for( int row=0; row<rowCnt; row++ ) {
153                        String objName = null;
154                        try {
155                                data = table.getValues( row );
156                                objName = data[clmNo[OBJ_NAME]];
157
158                                // 5.6.6.0 (2013/07/05) 初めに一度だけ作成しておきます。
159                                if( resource == null ) {
160                                        final String systemId = data[clmNo[SYSTEM_ID]];
161                                        resource = ResourceFactory.newInstance( systemId,"ja",false );
162                                }
163
164                                final boolean blk = ! objName.equals( oldObjName ) ;
165                                if( row > 0 && blk ) {
166                                        // 5.6.7.0 (2013/07/27) xml 出力対応
167                                        // 6.3.7.0 (2015/09/04) AutoCloseableを使用したtry-with-resources構築に対応。
168                                        try( final PrintWriter writer = FileUtil.getPrintWriter( new File( dir,oldObjName + ( isXml ? ".xml" : ".sql" ) ),ENCODE ) ) {
169                                                if( isXml ) { writer.println( XML_START_TAG ); }                // 5.6.7.0 (2013/07/27) xml 出力対応
170                                                writer.print( makeHeadLine( oldObjName,resource ) );    // 6.3.9.0 (2015/11/06)
171                                                writer.print( fixData.getAllFixData() );
172                                                writer.println( makeEndLine( oldObjName ) );
173                                                if( isXml ) { writer.println( XML_END_TAG ); }                  // 5.6.7.0 (2013/07/27) xml 出力対応
174                                                fixData.clear();
175                                        }
176                                }
177
178                                final String[] outData = makeLineList( clmNo,data,blk );
179                                fixData.addListData( outData );
180
181                                oldObjName = objName ;
182                        }
183                        catch( final RuntimeException ex ) {
184                                // 6.5.0.1 (2016/10/21) ErrorMessage をまとめるのと、直接 Throwable を渡します。
185                                makeErrorMessage( "TableFilter_DBARG_OUT Error",ErrorMessage.NG )
186                                        .addMessage( row+1,ErrorMessage.NG,"DBARG_OUT"
187                                                , "OBJ_NAME=[" + objName + "]"
188                                                , StringUtil.array2csv( data )
189                                        )
190                                        .addMessage( ex );
191                        }
192                }
193
194                // 常に、一回り遅れてデータ出力している為、最後のデータを出力しておく必要がある。
195                // 5.6.7.0 (2013/07/27) xml 出力対応
196                // 6.3.7.0 (2015/09/04) AutoCloseableを使用したtry-with-resources構築に対応。
197                try( final PrintWriter writer = FileUtil.getPrintWriter( new File( dir,oldObjName + ( isXml ? ".xml" : ".sql" ) ),ENCODE ) ) {
198                        if( isXml ) { writer.println( XML_START_TAG ); }                // 5.6.7.0 (2013/07/27) xml 出力対応
199                        writer.print( makeHeadLine( oldObjName,resource ) );    // 6.3.9.0 (2015/11/06)
200                        writer.print( fixData.getAllFixData() );
201                        writer.println( makeEndLine( oldObjName ) );
202                        if( isXml ) { writer.println( XML_END_TAG ); }                  // 5.6.7.0 (2013/07/27) xml 出力対応
203                        fixData.clear();
204                }
205
206                return table;
207        }
208
209        /**
210         * ヘッダーとして使用する文字列を作成します。
211         *
212         * @og.rev 5.6.6.0 (2013/07/05) ヘッダーとして各種情報をセットします。
213         * @og.rev 5.6.7.0 (2013/07/27) xml 出力対応
214         * @og.rev 6.0.2.3 (2014/10/10) EXEC_END_TAG 自体にCRを追加
215         * @og.rev 6.3.9.0 (2015/11/06) resourceをローカル変数にして引数で渡すようにする。
216         * @og.rev 6.4.4.2 (2016/04/01) StringBuilderの代わりに、OgBuilderを使用する。
217         *
218         * @param       objName         オブジェクト名
219         * @param       resource        リソースオブジェクト
220         *
221         * @return      ヘッダーとして使用する文字列
222         * @og.rtnNotNull
223         */
224        private String makeHeadLine( final String objName , final ResourceManager resource ) {
225                // objName から、ARG を取り除いた文字列が、画面IDになるはず。リソースから検索
226                final String gamenId = objName.substring( 0,objName.length()-3 );               // 3文字は、"ARG" 分
227                final String gamenNm = resource.getLabel( gamenId ) ;
228
229                final String LINE1 = objName ;
230                final String LINE2 = gamenId + " ( " + gamenNm + " )" ;
231                final String LINE3 = "Created : " + HybsSystem.getDate() ;
232
233                final int[] addLen = new int[] { 0,0,0 };       // 各データ間のスペース
234                final int[] type   = new int[] { X,K,X };       // 各データの種別 X:半角 S:空白前埋め K:全角混在
235                final FixLengthData fixData = new FixLengthData( addLen,type );
236
237                final String[][] outData = new String[][] {
238                        { "/**",        CMNT ,  "**/" },
239                        { "/* ",        LINE1,  " */" },
240                        { "/* ",        LINE2,  " */" },
241                        { "/* ",        LINE3,  " */" },
242                        { "/**",        CMNT ,  "**/" },
243                };
244
245                // 5.6.6.0 (2013/07/05) 簡易メソッドを利用
246                fixData.addAllListData( outData );
247
248                // 6.4.4.2 (2016/04/01)
249                final OgBuilder buf = new OgBuilder();
250                fixData.getAllFixData( buf.getBuilder() );                              // OgBuilder の内部 Builder に、fixData のデータを書き込む。
251                return buf.appendIfCR( isXml , EXEC_START_TAG )
252                                        .appendCR( "DROP TYPE " , objName , "_ARRAY" , execEndTag )
253                                        .appendIfCR( isXml , EXEC_START_TAG )
254                                        .appendCR( "CREATE OR REPLACE TYPE " , objName , " AS OBJECT" )
255                                        .appendCR( " (" )
256                                        .toString();
257        }
258
259        /**
260         * 各行に相当する文字列の配列を作成します。
261         * カンマ,カラム,クラス,(,桁数,),記号(--),表示順,名称
262         *
263         * @og.rev 5.5.1.9 (2012/04/18) useLen.length=0対応
264         * @og.rev 5.6.6.0 (2013/07/05) カラムの表示順も出力します。
265         *
266         * @param       clmNo   カラム番号配列
267         * @param       data    1行分の入力データ配列
268         * @param       first   最初の行かどうか[true:最初/false:それ以降]
269         *
270         * @return      1行分に相当する結果配列(カンマ,カラム,クラス(桁数),コメント記号,表示順,名称)
271         * @og.rtnNotNull
272         */
273        private String[] makeLineList( final int[] clmNo,final String[] data,final boolean first ) {
274                // カンマ,カラム,クラス(桁数),記号(--),表示順,名称
275                // 6.3.9.1 (2015/11/27) Found 'DD'-anomaly for variable(PMD)
276                final String[] outData = new String[9];                         // 5.6.6.0 (2013/07/05) カラムの表示順追加
277
278                outData[0] = first ? "   " : " , " ;                    // 0:カンマ
279                outData[1] = data[clmNo[CLM]] ;                                         // 1:カラム
280
281                final String clsName = data[clmNo[CLS_NAME]];
282                if( clsName.startsWith( "CLOB" ) || clsName.startsWith( "DATE" ) ) {
283                        data[clmNo[USE_LENGTH]] = null;
284                }
285                final String useLen = data[clmNo[USE_LENGTH]];
286                if( useLen != null && ! useLen.equals( "0" ) && useLen.length() > 0 ) { // 5.5.1.9 (2012/04/19)
287                        outData[2] = clsName ;                                                  // 2:クラス
288                        outData[3] = " ( " ;                                                    // 3:(
289                        outData[4] = useLen ;                                                   // 4:桁数
290                        outData[5] = " )" ;                                                             // 5:)
291                }
292                else {
293                        outData[2] = clsName ;                                                  // NUMBER型桁数オープン対応。ARGで使えるか不明。
294                }
295
296                final String seqno  = data[clmNo[SEQNO]] ;                              // 表示順
297                final String nameJA = data[clmNo[CLM_NAME]] ;                           // 名称
298                if( nameJA != null ) {
299                        outData[6] = "-- " ;                                                    // 3:コメント記号
300                        outData[7] = seqno ;                                                    // 4:表示順
301                        outData[8] = nameJA ;                                                   // 5:名称
302                }
303
304                return outData ;
305        }
306
307        /**
308         * 最後の行に相当する文字列を作成します。
309         *
310         * @og.rev 5.6.7.0 (2013/07/27) xml 出力対応
311         * @og.rev 6.0.2.3 (2014/10/10) EXEC_END_TAG の置き換え+"/" の対応
312         * @og.rev 6.4.4.1 (2016/03/18) StringBuilderの代わりに、OgBuilderを使用する。
313         *
314         * @param       objName オブジェクト名
315         *
316         * @return      最後の行に相当する文字列
317         * @og.rtnNotNull
318         */
319        private String makeEndLine( final String objName ) {
320                // 6.0.2.3 (2014/10/10) EXEC_END_TAG の置き換え+"/" の判定が必要。
321                final String e_e_tag = execEndTag + (isXml ? CR : CR + "/" + CR) ;
322
323                return new OgBuilder()
324                                .appendCR( "  )" , e_e_tag )
325                                .appendIfCR( isXml , EXEC_START_TAG )
326                                .append( "CREATE OR REPLACE TYPE "              , objName
327                                                , "_ARRAY AS VARRAY(10000) OF " , objName )             // 6.0.2.3 (2014/10/10) 1000 → 10000
328                                .append( e_e_tag )                                                                                              // 6.0.2.3 (2014/10/10)
329                                .toString();
330        }
331}