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.2 (2016/04/01)
022import org.opengion.fukurou.db.DBUtil;
023import org.opengion.fukurou.db.Transaction;
024import org.opengion.fukurou.util.ErrorMessage;
025import org.opengion.fukurou.util.FileUtil;
026import org.opengion.fukurou.util.FixLengthData;
027import org.opengion.fukurou.util.StringUtil;
028
029import org.opengion.hayabusa.common.HybsSystem;
030import org.opengion.hayabusa.common.HybsSystemException;
031import org.opengion.hayabusa.db.AbstractTableFilter;
032import org.opengion.hayabusa.db.DBTableModel;
033
034/**
035 * TableFilter_DBSRC_OUT は、TableFilter インターフェースを継承した、DBTableModel 処理用の
036 * 実装クラスです。
037 *
038 * ここでは、オブジェクト一覧(GF82)の検索結果より、オブジェクト明細テーブル(GF83)から
039 * 必要な情報を取得し、各種オブジェクトソースを抜き出します。
040 * 出力ファイルは、オブジェクト名+".sql" という命名規則で作成します。
041 *
042 * ここで、PACKAGE と、PACKAGE BODY が同じオブジェクト名の場合、同じファイルに追加(append=true)されます。
043 * 本来は、処理フォルダを先に削除しておかないと、上書きされてしまいます。
044 * ここでは、フォルダ削除ではなく、できるだけローカル処理するように、PACKAGE の場合だけ、
045 * 先に、ファイルを削除する処理を実行します。
046 *
047 * また、オブジェクトタイプによって、出力フォルダを変える場合は、個々に指定してください。
048 * 以下のコメントは参考ですので、詳細は、jsp 側の抜出プログラムの仕様をご確認ください。
049 *
050 *   view                       04_VIEW
051 *   function           05_SRC
052 *   package            05_SRC
053 *   package body       05_SRC
054 *   procedure          05_SRC
055 *   trigger            06_TRG
056 *
057 * オブジェクト一覧(GF82)の検索では、(SYSTEM_ID,TBLSYU,OBJ_TYPE,OBJ_NAME,NAME_JA)
058 * の項目を取得する必要があります。
059 *
060 * パラメータは、tableFilterタグの keys, vals にそれぞれ記述するか、BODY 部にCSS形式で記述します。
061 * 【パラメータ】
062 *  {
063 *       DIR : {@BASE_DIR}/sql/install/05_SRC ;   出力ファイルの基準フォルダ(必須)
064 *       XML : false ;                                 XML出力を行うかどうか[true/false]を指定します(初期値:false)。
065 *  }
066 *
067 * @og.formSample
068 * ●形式:
069 *      select SYSTEM_ID,TBLSYU,OBJ_TYPE,OBJ_NAME,NAME_JA from GF82
070 *      ① <og:tableFilter classId="DBSRC_OUT" keys="DIR" vals="{@BASE_DIR}/sql/install/05_SRC" />
071 *
072 *      ② <og:tableFilter classId="DBSRC_OUT" >
073 *               {
074 *                   DIR : {@BASE_DIR}/sql/install/05_SRC ;
075 *                   XML : false ;
076 *               }
077 *         </og:tableFilter>
078 *
079 * @og.rev 5.6.7.0 (2013/07/27) 新規作成
080 *
081 * @version  0.9.0  2000/10/17
082 * @author   Kazuhiko Hasegawa
083 * @since    JDK1.1,
084 */
085public class TableFilter_DBSRC_OUT extends AbstractTableFilter {
086        /** このプログラムのVERSION文字列を設定します。   {@value} */
087        private static final String VERSION = "6.5.0.1 (2016/10/21)" ;
088
089        private static final String[] KEYS = { "SYSTEM_ID","TBLSYU","OBJ_TYPE","OBJ_NAME","NAME_JA" };
090
091        private static final int SYSTEM_ID              = 0;
092        private static final int TBLSYU                 = 1;
093        private static final int OBJ_TYPE               = 2;
094        private static final int OBJ_NAME               = 3;
095 //     private static final int NAME_JA                = 4;
096
097        private static final String ENCODE = "UTF-8" ;
098
099        // オブジェクト明細テーブル(GF83) の検索SQL
100        private static final String GF83_SEL = "select NOLINE,SRC_TEXT"
101                                                                                        + " from GF83"
102                                                                                        + " where SYSTEM_ID=? and TBLSYU=? and OBJ_TYPE=? and OBJ_NAME=?"
103                                                                                        + " and   FGJ='1'"
104                                                                                        + " order by NOLINE" ;
105
106        // 5.6.6.0 (2013/07/05) ヘッダー部作成用
107        private static final String CMNT  = "************************************************************************" ;
108
109        private static final int X = FixLengthData.X ;          // type 定数
110        private static final int K = FixLengthData.K ;          // type 定数
111
112        /** 5.6.7.0 (2013/07/27) 各種定数  */
113        // 6.0.2.3 (2014/10/10) AbstractTableFilter へ移動
114
115        /** XML形式かどうか */
116
117        /**
118         * デフォルトコンストラクター
119         *
120         * @og.rev 6.4.1.1 (2016/01/16) keysMap を、サブクラスから設定させるように変更。
121         */
122        public TableFilter_DBSRC_OUT() {
123                super();
124                initSet( "DIR"  , "出力ファイルの基準フォルダ(必須)"                                           );
125                initSet( "XML"  , "XML出力を行うかどうか[true/false]を指定(初期値:false)"      );
126        }
127
128        /**
129         * DBTableModel処理を実行します。
130         *
131         * @og.rev 5.8.2.2 (2014/12/19) XML時エスケープと、BODYの場合の追記はタグ制御難しいのでファイルを分ける
132         * @og.rev 6.3.7.0 (2015/09/04) AutoCloseableを使用したtry-with-resources構築に対応。
133         * @og.rev 6.5.0.1 (2016/10/21) ErrorMessage をまとめるのと、直接 Throwable を渡します。
134         *
135         * @return 処理結果のDBTableModel
136         */
137        public DBTableModel execute() {
138                isXml = StringUtil.nval( getValue( "XML" ), false );
139                // 6.0.2.3 (2014/10/10) ※ このクラスでは、EXEC_END_TAG は、キャッシュしません。
140
141                final File dir = new File( getValue( "DIR" ) );
142                if( ! dir.exists() && !dir.mkdirs() ) {
143                        final String errMsg = "所定のフォルダが作成できませんでした。[" + dir + "]" ;
144                        throw new HybsSystemException( errMsg );
145                }
146
147                String[]        data    = null;
148                final Transaction tran = getTransaction();
149
150                final int[] clmNo = getTableColumnNo( KEYS );
151                // 6.4.1.1 (2016/01/16) PMD refactoring. Avoid declaring a variable if it is unreferenced before a possible exit point.
152                final DBTableModel table = getDBTableModel();
153                final int rowCnt = table.getRowCount();
154                for( int row=0; row<rowCnt; row++ ) {
155                        String objType  = null;
156                        String objName  = null;
157                        String fileName = null;         // 5.8.2.2. (2014/12/19)
158                        try {
159                                data = table.getValues( row );
160                                objType                 = data[clmNo[OBJ_TYPE]];
161                                objName                 = data[clmNo[OBJ_NAME]];
162                                fileName                = objName;      // 5.8.2.2. (2014/12/19)
163
164                                // 5.8.2.2. (2014/12/19) BODYの場合の追記はタグ制御難しいのでファイルを分ける
165                                if( "PACKAGE BODY".equalsIgnoreCase( objType ) ){ fileName = fileName + "_BODY"; }
166
167                                // パッケージの場合は、既存のファイルを削除します。(PACKAGE BODY がappendされるため)
168                                final File objFile = new File( dir,fileName + ( isXml ? ".xml" : ".sql" ) );    // 5.8.2.2. (2014/12/19)
169                                // 6.0.0.1 (2014/04/25) These nested if statements could be combined
170                                if( "PACKAGE".equalsIgnoreCase( objType ) && objFile.exists() && !objFile.delete() ) {
171                                        // このExceptionは、catchでErrorMessageにセットされます。
172                                        final String errMsg = "所定のファイルが削除できませんでした。[" + objFile + "]" ;
173                                        throw new HybsSystemException( errMsg );
174                                }
175
176                                // 出力ファイル名は、オブジェクト名と同じ
177                                // PACKAGE と、PACKAGE BODY が同じオブジェクト名の場合、同じファイルに追加(append=true)されます。
178                                // 5.8.2.2. (2014/12/19) 出力ファイルは原則オブジェクト名と同じだが、PachageBodyのみ後ろに_BODYを付けている
179                                // 6.3.7.0 (2015/09/04) AutoCloseableを使用したtry-with-resources構築に対応。
180                                try( PrintWriter writer = FileUtil.getPrintWriter( objFile,ENCODE,false ) ) {
181                                        if( isXml ) { writer.println( XML_START_TAG ); }
182                                        writer.println( makeHeadLine( clmNo,data ) );
183
184                                        // オブジェクト明細テーブル(GF83) の検索
185                                        final String[] vals = new String[] { data[clmNo[SYSTEM_ID]],data[clmNo[TBLSYU]],objType,objName };
186                                        final String[][] gf83 = DBUtil.dbExecute( GF83_SEL,vals,tran );
187                                        if( gf83.length == 0 ) {
188                                                System.out.println( "OBJ_TYPE=[" + objType + "], OBJ_NAME=[" + objName + "] is Not Found!" );
189                                                continue;
190                                        }
191
192                                        // ソースの出力
193                                        for( int j=0; j<gf83.length; j++ ) {
194                                                // 5.8.8.2 (2014/12/19) XML時は内容エスケープする
195                                                if( isXml ) { writer.println( StringUtil.htmlFilter( gf83[j][1] ) ); }
196                                                else {            writer.println( gf83[j][1] ); }
197                                        }
198
199                                        writer.println( makeEndLine() );
200                                        if( isXml ) { writer.println( XML_END_TAG ); }
201                                }
202                        }
203                        catch( final RuntimeException ex ) {
204                                // 6.5.0.1 (2016/10/21) ErrorMessage をまとめるのと、直接 Throwable を渡します。
205                                makeErrorMessage( "TableFilter_DBSRC_OUT Error",ErrorMessage.NG )
206                                        .addMessage( row+1,ErrorMessage.NG,"DBSRC_OUT"
207                                                , "OBJ_TYPE=[" + objType + "], OBJ_NAME=[" + objName + "]"
208                                                , StringUtil.array2csv( data )
209                                        )
210                                        .addMessage( ex );
211                        }
212                }
213
214                return table;
215        }
216
217        /**
218         * ヘッダーとして使用する文字列を作成します。
219         *
220         * @og.rev 5.7.2.0 (2014/01/10) 構文の見直し
221         * @og.rev 5.8.8.2 (2014/12/19) View以外の場合に不正Create文となるので修正
222         * @og.rev 6.2.2.1 (2015/03/31) View以外の場合に不正Create文となる箇所の文法修正
223         * @og.rev 6.4.4.2 (2016/04/01) StringBuilderの代わりに、OgBuilderを使用する。
224         *
225         * @param       clmNo   カラム番号配列
226         * @param       data    1行分のデータ配列
227         *
228         * @return      ヘッダーとして使用する文字列
229         * @og.rtnNotNull
230         */
231        protected String makeHeadLine( final int[] clmNo,final String[] data ) {
232                // 5.7.2.0 (2014/01/10) objType,objName の再利用と、VIEWの場合は、AS を追加します。
233                final String objType  = data[clmNo[OBJ_TYPE]];
234                final String objName  = data[clmNo[OBJ_NAME]];
235
236                final String LINE1 = "SYSTEM_ID : " + data[clmNo[SYSTEM_ID]] ;
237                final String LINE2 = objName + " ( " + objType + " )" ;                                         // 5.7.2.0 (2014/01/10)
238                final String LINE3 = "Created : " + HybsSystem.getDate() ;
239
240                final int[] addLen = new int[] { 0,0,0 };       // 各データ間のスペース
241                final int[] type   = new int[] { X,K,X };       // 各データの種別 X:半角 S:空白前埋め K:全角混在
242                final FixLengthData fixData = new FixLengthData( addLen,type );
243
244                final String[][] outData = new String[][] {
245                        { "/**",        CMNT ,  "**/" },
246                        { "/* ",        LINE1,  " */" },
247                        { "/* ",        LINE2,  " */" },
248                        { "/* ",        LINE3,  " */" },
249                        { "/**",        CMNT ,  "**/" },
250                };
251
252                // 5.6.6.0 (2013/07/05) 簡易メソッドを利用
253                fixData.addAllListData( outData );
254
255                // 5.8.8.2 (2014/12/19) View以外の場合に不正Create文となるので修正
256                // 6.2.2.1 (2015/03/31) View以外の場合に不正Create文となる箇所の文法修正
257
258                // 6.4.4.2 (2016/04/01)
259                final OgBuilder buf = new OgBuilder();
260                fixData.getAllFixData( buf.getBuilder() );                              // OgBuilder の内部 Builder に、fixData のデータを書き込む。
261                return buf.appendIfCR( isXml , EXEC_START_TAG )
262                                        .append(   "CREATE " )
263                                        .appendIf( "VIEW".equalsIgnoreCase( objType )
264                                                                , objType , " " , objName , " AS " )
265                                        .toString();
266        }
267
268        /**
269         * 最後の行に相当する文字列を作成します。
270         *
271         * @og.rev 6.0.2.3 (2014/10/10) 処理を簡素化します。
272         * @og.rev 6.2.2.1 (2015/03/31) PostgreSQL対応。最後の記号を、"/" から、";" に変更します。
273         *
274         * @return      最後の行に相当する文字列
275         * @og.rtnNotNull
276         */
277        private String makeEndLine() {
278                return isXml ? CR + EXEC_END_TAG : ";" ;
279        }
280}