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.hayabusa.taglib;
017
018 import org.opengion.hayabusa.common.HybsSystem;
019 import org.opengion.hayabusa.common.HybsSystemException;
020 import org.opengion.hayabusa.db.DBTableModel;
021 import org.opengion.hayabusa.develop.JspCreateFactory;
022 import org.opengion.hayabusa.develop.JspConvertEntity;
023 import org.opengion.fukurou.util.ErrorMessage;
024 import org.opengion.fukurou.util.StringUtil;
025 import static org.opengion.fukurou.util.StringUtil.nval ;
026 import org.opengion.fukurou.xml.JspSaxParser;
027
028 import java.util.Map;
029 import java.util.HashMap;
030 import java.util.List;
031 import java.util.ArrayList;
032 import java.util.Locale ;
033
034 import java.io.File;
035 import java.io.ObjectOutputStream;
036 import java.io.ObjectInputStream;
037 import java.io.IOException;
038
039 /**
040 * JspCreateTag は、画面定義æƒ??より、JSP画面をè?動作æ?するãƒ??ルですã?
041 *
042 * 画面作æ?するにあたりã??“ç®?‰€の参ç?å…?�Œ存在しますã?
043 *
044 * ?‘.画面属æ?の設å®???雛形 â†?仮画面 ・・・ hpgid でæŒ?®šã?画面ID
045 * ?’.仮画面修正 ??仮画面 â†?仮画面 ・・・ jsp/customUserDef/画面ID
046 * ?“.本環å¢?¿®正 ??本画面 â†?本画面 ・・・ jsp/画面ID
047 *
048 * それぞれ、作æ?先ã?画面IDフォルãƒ?�Œ存在する場合ã?、取込å…?‚’作æ?先に設定しますã?
049 * つまりã?ä¸?º¦作æ?すると、それ以降ã?、作æ?された画面を基準に処ç�?‚’行いますã?
050 *
051 * @og.formSample
052 * ●形式ï¼?lt;og:jspCreate outdir="…" pgid="…" />
053 * ●body?šなã�?
054 *
055 * ●Tag定義??
056 * <og:jspCreate
057 * pgid ○ã?TAG】ã?ログラãƒ?Dを指定しますã?(å¿??)
058 * outdir 【TAG】作æ?先ã?ãƒ?‚£レクトリを指定しまã�?
059 * hpgid 【TAG】雛形のプログラãƒ?Dを指定しまã�?
060 * useUserDef 【TAG】仮環å¢?‚’使用するかどã�?�‹[true/false]を指定しまã�?初期値:true:使用する)
061 * tagNames 【TAG】å?ç�?‚’行うタグを部åˆ?Œ‡定しまã�?初期値:å†?ƒ¨登録タグすべて)
062 * tableId 【TAGã€?通常は使ã�?�¾せん)DBTableModel が登録されてã�?‚‹メモリのキーを指定しまã�?
063 * scope 【TAG】キャãƒ?‚·ュする場合ã?スコープ[request/page/session/applicaton]を指定しまã�?初期値:session)
064 * debug 【TAG】デバッグæƒ??をå?力するかどã�?�‹[true/false]を指定しまã�?初期値:false)
065 * />
066 *
067 * ●使用ä¾?
068 * ・先に、query タグでDBTableModel が作æ?済み
069 * <og:jspCreate
070 * outdir = "{@SYS.REAL_PATH}jsp/" ?šå?力å?フォルãƒ?
071 * pgid = "{@PGID}" ?š作æ?画面ID
072 * hpgid = "{@HPGID}" ?š雛形画面ID
073 * useUserDef = "true/false" ?š仮環å¢??使用可否(初期値:true 使用する)
074 * />
075 *
076 * @og.group 開発補助
077 * @og.rev 5.1.9.0 (2010/08/01) 新規作æ?
078 *
079 * @version 0.9.0 2000/10/17
080 * @author Kazuhiko Hasegawa
081 * @since JDK1.6,
082 */
083 public class JspCreateTag extends CommonTagSupport {
084 //* こã?プログラãƒ??VERSIONæ–?—å?を設定しますã? {@value} */
085 private static final String VERSION = "5.1.9.0 (2010/08/01)" ;
086
087 private static final long serialVersionUID = 5190 ; // 5.1.9.0 (2010/08/01)
088
089 // private static final String ENCODE = "UTF-8" ;
090
091 // private static String JSP_DIR = "C:/opengion/uap/webapps/gf/jsp/";
092 private static String JSP_DIR = HybsSystem.sys( "REAL_PATH" ) + "jsp/" ;
093
094 private static final String CUST_DEF = "customUserDef/" ;
095
096 private transient DBTableModel table = null;
097
098 private String tableId = HybsSystem.TBL_MDL_KEY;
099 private String outdir = JSP_DIR; // 出力å?フォルãƒ?
100 private String pgid = null; // 作æ?画面ID
101 private String hpgid = null; // 雛形画面ID
102 private String[] tagNames = null; // 5.6.8.0 (2013/09/06) 処ç�?‚’行うタグを部åˆ?Œ‡å®?
103 private boolean useUserDef = true; // 仮環å¢??使用可否(初期値:true 使用する)
104
105 // private static final String[] JCF_LIST = new String[] { "COMMENT" , "COLUMN" , "ORDER_BY" , "QUERY" , "VIEW" , "TABLE_UPDATE" };
106 private static final String[] JCF_LIST = new String[] { "COMMENT" , "HIDEMENU" , "COLUMN" , "ORDER_BY" , "QUERY" , "VIEW" , "TABLE_UPDATE" }; // 5.6.4.4 (2013/05/31)
107
108 /**
109 * Taglibの開始タグが見つかったときに処ç�?�™ã‚?doStartTag() ã‚?オーバã?ライドしますã?
110 *
111 * @return 後続å?ç�??æŒ?¤º(SKIP_BODY)
112 */
113 @Override
114 public int doStartTag() {
115 table = (DBTableModel)getObject( tableId );
116
117 return(SKIP_BODY); // Body を評価しなã�?
118 // return ( EVAL_BODY_BUFFERED ); // Body を評価する
119 }
120
121 /**
122 * Taglibのタグ本体を処ç�?�™ã‚?doAfterBody() ã‚?オーバã?ライドしますã?
123 *
124 * @return int
125 */
126 // @Override
127 // public int doAfterBody() {
128 // body = nval( getBodyString(),body );
129 //
130 // return(SKIP_BODY);
131 // }
132
133 /**
134 * Taglibの終äº?‚¿グが見つかったときに処ç�?�™ã‚?doEndTag() ã‚?オーバã?ライドしますã?
135 *
136 * @return 後続å?ç�??æŒ?¤º
137 */
138 @Override
139 public int doEndTag() {
140 // ãƒ?ƒ�ãƒ?‚°時には、オブジェクトå?部æƒ??を表示するã€?
141 if( isDebug() ) {
142 debugPrint();
143 }
144
145 boolean okFlag = execute();
146
147 if( okFlag ) { // 正常
148 return(EVAL_PAGE);
149 }
150 else {
151 return(SKIP_PAGE);
152 }
153 }
154
155 /**
156 * タグリブオブジェクトをリリースしますã?
157 * キャãƒ?‚·ュされて再利用されるã?で、フィールドã?初期設定を行いますã?
158 *
159 * @og.rev 5.6.8.0 (2013/09/06) tagNames 追åŠ?
160 */
161 @Override
162 protected void release2() {
163 super.release2();
164 table = null;
165 tableId = HybsSystem.TBL_MDL_KEY;
166 outdir = JSP_DIR;
167 pgid = null;
168 hpgid = null;
169 useUserDef = true;
170 tagNames = null; // 5.6.8.0 (2013/09/06) 処ç�?‚’行うタグを部åˆ?Œ‡å®?
171 }
172
173 /**
174 * DBTableModel処ç�?‚’実行しますã?
175 *
176 * @og.rev 5.2.1.0 (2010/10/01) 実行クラスのクラス名を変更しますã? _OG_ を削除
177 * @og.rev 5.5.2.6 (2012/05/25) findbugs対応ã?JspConvertEntity.DBKEY をã?JspConvertEntity.getDBKEY() に変更ã€?
178 * @og.rev 5.6.8.0 (2013/09/06) 処ç�?¯¾象タグの部åˆ?Œ‡定対å¿?(tagNames 追åŠ?
179 *
180 * @return 処ç�??実行結果
181 */
182 public boolean execute() {
183 boolean okFlag = false;
184
185 // 出力å?(セーブå?)のフォルãƒ?
186 File saveDir = (useUserDef) ? new File( outdir + CUST_DEF , pgid ) : new File( outdir , pgid ) ;
187
188 // 雛形(参ç?å…?のフォルãƒ?
189 final File refDir ;
190 if( saveDir.exists() ) { // 出力å?フォルãƒ?�Œ存在した場å�?
191 refDir = saveDir ; // 出力å?フォルãƒ?�Œã€?››形フォルãƒ?�«なるã?
192 }
193 else { // 出力å?フォルãƒ?�Œ存在しなã�??å�?
194 refDir = new File( outdir , hpgid ); // 雛形画面IDをそのまま使用しますã?
195 if( ! saveDir.mkdirs() ) {
196 String errMsg = "出力å?のフォルãƒ?�Œ作æ?できませんでしたã€?" + saveDir + "]" ;
197 throw new HybsSystemException( errMsg );
198 }
199 }
200
201 if( ! refDir.exists() ) { // 雛形(参ç?å…?フォルãƒ?�Œ存在しなã�??合ã?エラー
202 String errMsg = "雛形(参ç?å…?フォルãƒ?�Œ存在しませんã€?" + refDir + "]" ;
203 throw new HybsSystemException( errMsg );
204 }
205
206 // PrintWriter writer = null;
207
208 // int[] clmNo = getTableColumnNo( JspConvertEntity.DBKEY );
209 int[] clmNo = getTableColumnNo( JspConvertEntity.getDBKEY() ); // 5.5.2.6 (2012/05/25) findbugs対å¿?
210 int rowCnt = table.getRowCount();
211
212 int row = 0;
213 boolean unCmntSet = true;
214 String[] data = null;
215 try {
216 //JSPを書く為にå¿?¦�なæƒ??を設å®?
217 Map<String,List<JspConvertEntity>> tables = new HashMap<String,List<JspConvertEntity>>();
218
219 for( row=0; row<rowCnt; row++ ) {
220 data = table.getValues( row );
221 JspConvertEntity entry = JspConvertEntity.newInstance( data, clmNo );
222
223 if( entry != null ) {
224 List<JspConvertEntity> array = tables.get( entry.getType() );
225 if( array == null ) { array = new ArrayList<JspConvertEntity>(); }
226 array.add( entry );
227 tables.put( entry.getType(),array );
228 if( unCmntSet ) {
229 tables.put( "COMMENT",array );
230 unCmntSet = false;
231 }
232 }
233 }
234
235 // 参ç?先フォルãƒ?�‹らã?出力å?フォルãƒ?�«、XMLコピã?処ç�?‚’行いますã?
236 JspSaxParser spar = new JspSaxParser();
237
238 // 5.2.1.0 (2010/10/01) 実行クラスのクラス名を変更しますã? _OG_ を削除
239 // spar.addFilter( JspCreateFactory.newInstance( "COMMENT" ,tables) );
240 // spar.addFilter( JspCreateFactory.newInstance( "COLUMN" ,tables) );
241 // spar.addFilter( JspCreateFactory.newInstance( "SELECT" ,tables) );
242 // spar.addFilter( JspCreateFactory.newInstance( "QUERY" ,tables) );
243 // spar.addFilter( JspCreateFactory.newInstance( "VIEW" ,tables) );
244
245 // 5.6.8.0 (2013/09/06) 処ç�?¯¾象タグの部åˆ?Œ‡定対å¿?(tagNames 追åŠ?
246 if( tagNames == null ) { tagNames = JCF_LIST; }
247 // for( String jcf : JCF_LIST ) {
248 for( String jcf : tagNames ) {
249 spar.addFilter( JspCreateFactory.newInstance( jcf , tables ) );
250 }
251
252 spar.copyDirectry( refDir,saveDir );
253
254 // //query.jsp作æ?
255 // writer = FileUtil.getPrintWriter( new File( saveDir,"query.jsp" ),ENCODE );
256 // writer.print(writeQueryJSP( refDir , tables ));
257 // writer.println();
258 // writer.flush();
259 //
260 // //result.jsp作æ?
261 // writer = FileUtil.getPrintWriter( new File( saveDir,"result.jsp" ),ENCODE );
262 // writer.print(writeResultJSP( refDir , tables ));
263 // writer.println();
264 // writer.flush();
265
266 okFlag = true;
267 }
268 catch( RuntimeException ex ) {
269 ex.printStackTrace();
270 ErrorMessage errMessage = new ErrorMessage( "JspCreateTag Error" );
271 errMessage.addMessage( row+1,ErrorMessage.NG,"JSPOUT",ex.getMessage() );
272 errMessage.addMessage( row+1,ErrorMessage.NG,"JSPOUT",StringUtil.array2csv( data ) );
273 errMessage.addMessage( row+1,ErrorMessage.NG,"JSPOUT","PRGID=[" + pgid + "]" );
274 // BAT から呼び出すå?合があるため、標準エラー出力にも情報をå?しておきますã?
275 System.out.println( errMessage );
276 }
277 // finally {
278 // Closer.ioClose( writer );
279 // }
280
281 return okFlag;
282 }
283
284 /**
285 * query.jspファイルのå†?®¹を生成しますã?
286 *
287 * @param refDir
288 * @param tables
289 *
290 * @return 生æ?したå†?®¹
291 */
292 // private String writeQueryJSP( File refDir , HashMap<String , ArrayList<JspConvertEntity>> tables ) {
293 // String jsp = "";
294 // try{
295 // //利用する生æ?クラスを設å®?
296 // HashMap< String , JspCreate > generates = new HashMap< String , JspCreate >();
297 // generates.put( "og:comment" , JspCreateFactory.newInstance( "OG_COMMENT",tables) );
298 // generates.put( "table" , JspCreateFactory.newInstance( "OG_COLUMN",tables) );
299 // generates.put( "og:select" , JspCreateFactory.newInstance( "OG_SELECT",tables) );
300 //
301 // //JSPの作æ?処ç�?‚’実施ã€?
302 // JspHandler handler = new JspHandler();
303 // jsp = handler.generate( new File( refDir , "query.jsp" ), ENCODE , generates);
304 //
305 // }catch(Throwable ex){
306 // ex.printStackTrace();
307 // throw new RuntimeException(ex);
308 // }
309 // return jsp;
310 // }
311
312 /**
313 * result.jspファイルのå†?®¹を生成しますã?
314 *
315 * @param refDir
316 * @param tables
317 *
318 * @return 生æ?したå†?®¹
319 */
320 // private String writeResultJSP(File refDir , HashMap<String , ArrayList<JspConvertEntity>> tables ) {
321 // String jsp = "";
322 // try{
323 // //利用する生æ?クラスを設å®?
324 // HashMap< String , JspCreate > generates = new HashMap< String , JspCreate >();
325 // generates.put( "og:comment" , JspCreateFactory.newInstance( "OG_COMMENT",tables) );
326 // generates.put( "og:query" , JspCreateFactory.newInstance( "OG_QUERY",tables ) );
327 // generates.put( "og:view" , JspCreateFactory.newInstance( "OG_VIEW",tables) );
328 //
329 // //JSPの作æ?処ç�?‚’実施ã€?
330 // JspHandler handler = new JspHandler();
331 // jsp = handler.generate( new File( refDir , "result.jsp" ) , ENCODE , generates);
332 //
333 // }catch(Throwable ex){
334 // ex.printStackTrace();
335 // throw new RuntimeException(ex);
336 // }
337 // return jsp;
338 // }
339
340 /**
341 * 【TAGã€?通常は使ã�?�¾せん)DBTableModel が登録されてã�?‚‹メモリのキーを指定しますã?
342 *
343 * @og.tag
344 * 初期値は、HybsSystem.TBL_MDL_KEY ですã?
345 * こã?設定å?から、DBTableModel を取りå?しますã?リクエストかセãƒ?‚·ョンかã?ã€?
346 * scope 属æ?でæŒ?®šしますã?
347 *
348 * @param id メモリから取り出す時の ID
349 */
350 public void setTableId( final String id ) {
351 tableId = nval( getRequestParameter( id ),tableId );
352 }
353
354 /**
355 * 【TAG】作æ?先ã?ãƒ?‚£レクトリを指定しますã?
356 *
357 * @og.tag
358 * 作æ?先ã?ãƒ?‚£レクトリを指定しますã?
359 * 初期値は、実際に実行してã�?‚‹アプリケーションの REAL_PATH + jsp フォルãƒ?»¥下ですã?
360 * 作æ?先ã?フォルãƒ?‚‚、useUserDef の設定によって異なりますã?
361 *
362 * @param dir 出力å?のãƒ?‚£レクトリ
363 */
364 public void setOutdir( final String dir ) {
365 outdir = nval( getRequestParameter( dir ),outdir );
366 }
367
368 /**
369 * 【TAG】ã?ログラãƒ?Dを指定しますã?
370 *
371 * @og.tag
372 * 作æ?先ã?プログラãƒ?Dを指定しますã?
373 * ただしã?作æ?先ã?実際のフォルãƒ??、useUserDef の設定によって異なりますã?
374 *
375 * @param id プログラãƒ?D
376 */
377 public void setPgid( final String id ) {
378 pgid = nval( getRequestParameter( id ),pgid );
379 }
380
381 /**
382 * 【TAG】雛形のプログラãƒ?Dを指定しますã?
383 *
384 * @og.tag
385 * 雛形のプログラãƒ?Dをパースして、実際のプログラãƒ?‚’作æ?しますã?
386 * ただしã?作æ?先ã?実際のフォルãƒ??、useUserDef の設定によって異なりますã?
387 * またã?パã?スするのは、作æ?先ã?画面IDのフォルãƒ?�Œ存在しなã�??合ã?みですã?
388 * すでに、存在してã�?‚‹場合ã?、å?の画面IDのフォルãƒ?‚’読み取って、パースã‚?
389 * 行いますã?基本çš?�«、作æ?先ã?ソースを手で修正した場合でもã?パã?スと
390 * 無関係なç®?‰€の修正はそã?まま反映のこされますã?
391 *
392 * @param id 雛形のプログラãƒ?D
393 */
394 public void setHpgid( final String id ) {
395 hpgid = nval( getRequestParameter( id ),hpgid );
396 }
397
398 /**
399 * 【TAG】仮環å¢?‚’使用するかどã�?�‹[true/false]を指定しまã�?初期値:true:使用する)ã€?
400 *
401 * @og.tag
402 * true:使用する を設定するとã€?customUserDef" フォルãƒ??下に、画面IDの
403 * フォルãƒ?‚’作æ?しますã?
404 * false:使用しなã�?を設定すると、実際の リアルパス(REAL_PATH/jsp)の下にã€?
405 * 画面IDのフォルãƒ?‚’作æ?しますã?こちらã?、実際の画面と同様に、画面リソース等を
406 * 作æ?してアクセスすることになりますã?
407 *
408 * @param flag 仮環å¢?‚’使用するかどã�?�‹ [true:使用する/false:使用しない]
409 */
410 public void setUseUserDef( final String flag ) {
411 useUserDef = nval( getRequestParameter( flag ),useUserDef );
412 }
413
414 /**
415 * 【TAG】å?ç�?‚’行うタグを部åˆ?Œ‡定しまã�?初期値:å†?ƒ¨登録タグすべて)ã€?
416 *
417 * @og.tag
418 * 処ç�?‚’行うタグは、å?部çš?�«、COMMENT,HIDEMENU,COLUMN,ORDER_BY,QUERY,VIEW,TABLE_UPDATE ã�?�‘
419 * 予ç´?�•れており、å?期å?は、すべてのタグをå?ç�?�—ますã?
420 * ここでは、そのä¸?ƒ¨のみ実行できるように、カンマ区åˆ?‚Šæ–?—でæŒ?®šできるようにしますã?
421 * 実行不可のタグ名をæŒ?®šすると、エラーになりますã?
422 *
423 * @og.rev 5.6.8.0 (2013/09/06) 新規追åŠ?
424 *
425 * @param tags 処ç�?‚’行うタグを部åˆ?Œ‡定するã?
426 */
427 public void setTagNames( final String tags ) {
428 String tagNms = nval( getRequestParameter( tags ),null );
429
430 if( tagNms != null ) {
431 tagNames = tagNms.split( "," );
432
433 for( int i=0; i<tagNames.length; i++ ) {
434 String tag = tagNames[i].trim().toUpperCase(Locale.JAPAN);
435 if( ! check( tag, JCF_LIST ) ) {
436 String errMsg = "æŒ?®šã? tagNames は、下記ã?ç¯?›²でæŒ?®šしてくださいã€?
437 + "tagNames=" + tagNms + " (NG=" + tag + ") : "
438 + JCF_LIST.toString();
439 throw new HybsSystemException( errMsg );
440 }
441 tagNames[i] = tag ; // 大æ–?—に変換した値を戻すã?
442 }
443 }
444 }
445
446 /**
447 * カラãƒ?��配å?(String[])より、対応するカラãƒ?o配å?(int[])を作æ?しますã?
448 *
449 * @param nameArray カラãƒ?��配å?
450 *
451 * @return カラãƒ?o配å?
452 */
453 private int[] getTableColumnNo( final String[] nameArray ) {
454 int[] clmNo = new int[ nameArray.length ];
455 for( int i=0; i<clmNo.length; i++ ) {
456 clmNo[i] = table.getColumnNo( nameArray[i] );
457 }
458 return clmNo;
459 }
460
461 /**
462 * シリアライズ用のカスタãƒ?‚·リアライズ書き込みメソãƒ?ƒ‰
463 *
464 * @param strm ObjectOutputStreamオブジェクãƒ?
465 * @serialData
466 */
467 private void writeObject( final ObjectOutputStream strm ) throws IOException {
468 strm.defaultWriteObject();
469 }
470
471 /**
472 * シリアライズ用のカスタãƒ?‚·リアライズ読み込みメソãƒ?ƒ‰
473 *
474 * ここでは、transient 宣è¨?�•れたå†?ƒ¨変数のå†??初期化がå¿?¦�なフィールドã?み設定しますã?
475 *
476 * @param strm ObjectInputStreamオブジェクãƒ?
477 * @see #release2()
478 * @serialData
479 */
480 private void readObject( final ObjectInputStream strm ) throws IOException , ClassNotFoundException {
481 strm.defaultReadObject();
482 }
483
484 /**
485 * こã?オブジェクトã?æ–?—å?表現を返しますã?
486 * 基本çš?�«ãƒ?ƒ�ãƒ?‚°目çš?�«使用しますã?
487 *
488 * @return こã?クラスのæ–?—å?表現
489 */
490 @Override
491 public String toString() {
492 return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
493 .println( "VERSION" ,VERSION )
494 .println( "tableId" ,tableId )
495 .println( "outdir" ,outdir )
496 .println( "pgid" ,pgid )
497 .println( "hpgid" ,hpgid )
498 .println( "useUserDef" ,useUserDef )
499 .fixForm().toString() ;
500 }
501 }