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.hayabusa.taglib;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.hayabusa.common.HybsSystemException;
020import org.opengion.fukurou.util.ErrorMessage;
021import org.opengion.fukurou.util.XHTMLTag;
022import org.opengion.fukurou.util.Attributes;
023
024import static org.opengion.fukurou.util.StringUtil.nval ;
025
026import java.util.Locale ;
027import java.io.ObjectOutputStream;
028import java.io.ObjectInputStream;
029import java.io.IOException;
030
031/**
032 * エラーメッセージを 表形式で表示するタグです。
033 *
034 * Query 関係の実行時にエラー/ワーニングが発生すると、HybsSystem.ERR_MSG_KEY をキーに
035 * ErrorMessage オブジェクト をセッションに登録します。
036 * この情報を元に、表題(TITLE)か、内容(BODY)を表示します。
037 * 基本的には,表題表示時には,リンクを張り、共通エラー表示画面をオープン
038 * 出来る様になっています。
039 *
040 * @og.formSample
041 * ●形式:
042 *     <og:errorMessage command="{@command}" clear="{@clear}" />
043 * ●body:なし
044 *
045 * ●Tag定義:
046 *   <og:errorMessage
047 *       command            【TAG】コマンド(NEW,RENEW,RESET,REVIEW)をセットします
048 *       clear              【TAG】メッセージを初期化するかどうか[true/false]を指定します(初期値:false)
049 *       viewType           【TAG】表示形式『表題(TITLE)か、内容(BODY)』を指定します(初期値:TITLE)
050 *       displayMsg         【TAG】plsqlUpdate の結果を画面上に表示するメッセージIDを指定します(初期値:MSG0059『登録しました』)
051 *       warningMsg         【TAG】登録処理実行後のワーニング結果を画面上に表示するメッセージIDを指定します(初期値:ERR0020)
052 *       debug              【TAG】デバッグ情報を出力するかどうか[true/false]を指定します(初期値:false)
053 *   />
054 *
055 * ●使用例
056 *    result.jsp 等のSQL登録実行後の戻り画面に、上記タグを配置すれば、
057 *    エラーメッセージが存在すれば,リンクとなって現れ、無ければ,なにも
058 *    現れません。
059 *    リンクのとび先は自動的に設定されます。
060 *    なお、clear="true" または、command="NEW" の場合に、エラーメッセージは、
061 *    クリアされます。
062 *
063 *    [entry.jsp]
064 *        <% String forwardPage="result.jsp"; %>
065 *        <jsp:forward page="<%= response.encodeRedirectURL( forwardPage ) %>" >
066 *            <jsp:param name="command" value="REVIEW" />
067 *            <jsp:param name="clear"   value="false"  />
068 *        </jsp:forward>
069 *
070 *    [result.jsp]
071 *        <og:errorMessage command="{@command}" clear="{@clear}" />
072 *
073 * @og.group エラー処理
074 *
075 * @version  4.0
076 * @author       Kazuhiko Hasegawa
077 * @since    JDK5.0,
078 */
079public class ErrorMessageTag extends CommonTagSupport {
080        //* このプログラムのVERSION文字列を設定します。   {@value} */
081        private static final String VERSION = "5.2.1.0 (2010/10/01)" ;
082
083        private static final long serialVersionUID = 521020101001L ;
084
085        /** command 引数に渡す事の出来る コマンド  新規 {@value} */
086        public static final String CMD_NEW       = "NEW" ;
087        /** command 引数に渡す事の出来る コマンド  再検索 {@value} */
088        public static final String CMD_RENEW = "RENEW" ;
089        /** command 引数に渡す事の出来る コマンド  取消 {@value} */
090        public static final String CMD_RESET = "RESET" ;
091        /** command 引数に渡す事の出来る コマンド  再表示 {@value} */
092        public static final String CMD_REVIEW   = "REVIEW" ;
093        /** command 引数に渡す事の出来る コマンド リスト  */
094//      private static final String[] COMMAND_LIST = new String[] { CMD_NEW , CMD_RENEW , CMD_RESET , CMD_REVIEW };
095        private static final String errMsgId    = HybsSystem.ERR_MSG_KEY;
096        private final String errMsgFile = HybsSystem.sys( "ERR_MSG_FILENAME" );
097        private final int       maxRowCount = HybsSystem.sysInt( "DB_MAX_ROW_COUNT" ) ;
098
099        private transient ErrorMessage  errMessage      = null;
100        private String                  command         = null;
101        private boolean                 msgClear        = false;
102        private String                  viewType        = "TITLE";              // TITLE/BODY
103
104        // 2.0.1.0 (2002/10/10) デフォルト表示しないから、MSG0059=登録しました。に変更します。
105        private String          displayMsg       = "MSG0059";   // 初期値は『登録しました。』
106        private String          warningMsg       = "ERR0020";   // データ登録時にワーニングが発生しました。
107
108        /**
109         * Taglibの開始タグが見つかったときに処理する doStartTag() を オーバーライドします。
110         *
111         * @og.rev 3.5.4.0 (2003/11/25) エラーオブジェクトのクリアに、RENEW or null も追加します。
112         *
113         * @return      後続処理の指示(SKIP_BODY)
114         */
115        @Override
116        public int doStartTag() {
117                // クリアが指示されるか、コマンドが NEW or RESET or RENEW or null の場合は、エラーをクリアする。
118                if( msgClear || CMD_NEW.equals( command ) || CMD_RESET.equals( command ) ) {
119                // 3.5.4.9 (2004/02/25) RENEW の時は、エラーをクリアしない。
120                        removeSessionAttribute( errMsgId );
121                        msgClear = true;
122                }
123                else {
124                        errMessage = (ErrorMessage)getSessionAttribute( errMsgId );
125                        if( errMessage == null ) { msgClear = true; }
126                }
127
128                return(SKIP_BODY);              // Body を評価しない
129        }
130
131        /**
132         * Taglibの終了タグが見つかったときに処理する doEndTag() を オーバーライドします。
133         *
134         * @og.rev 2.1.0.3 (2002/11/08) command = NEW のときも、『登録しました。』メッセージが表示されるバグを修正
135         * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応。release2() を doEndTag()で呼ぶ。
136         * @og.rev 3.5.5.2 (2004/04/02) TaglibUtil.makeHTMLErrorTable メソッドを利用
137         * @og.rev 4.0.0.0 (2007/10/18) メッセージリソース統合( getResource().getMessage ⇒ getResource().getLabel )
138         * @og.rev 4.1.3.0 (2008/09/04) メッセージをspanで囲う(画面遷移なしモード対応)
139         * @og.rev 5.2.1.0 (2010/10/01) 戻るリンク時に不要な改行が出力される件に対応
140         *
141         * @return      後続処理の指示
142         */
143        @Override
144        public int doEndTag() {
145                debugPrint();           // 4.0.0 (2005/02/28)
146
147                String msg = null;
148                if( errMessage == null ) {
149                        if( CMD_REVIEW.equals( command ) || CMD_RENEW.equals( command ) ) {
150                                // 5.2.1.0 (2010/10/01) 戻るリンク時に不要な改行が出力される件に対応
151//                              msg = getResource().getMessage( displayMsg ) + HybsSystem.BR;
152                                msg = getResource().getLabel( displayMsg );
153                                if( msg != null && msg.length() > 0 ) { msg += HybsSystem.BR; }
154                        }
155                }
156                else {
157                        if( "TITLE".equalsIgnoreCase( viewType ) ) {
158                                msg = makeTitle();
159                        }
160                        else if( "BODY".equalsIgnoreCase( viewType ) ) {
161                                msg = TaglibUtil.makeHTMLErrorTable( errMessage,getResource() );        // 3.5.5.2 (2004/04/02)
162                        }
163                        else {
164                                String errMsg = "viewType属性に TITLE/BODY 以外の項目が指定されています。"
165                                                        + "[" + viewType + "]" ;
166                                throw new HybsSystemException( errMsg );
167                        }
168                }
169
170                jspPrint( "<span class=\"errmsg\">" ); // 4.1.3.0 (2008/09/04)
171                if( msg != null && msg.length() > 0 ) { jspPrint( msg ); }
172                jspPrint( "</span>" );
173
174                return(EVAL_PAGE);
175        }
176
177        /**
178         * タグリブオブジェクトをリリースします。
179         * キャッシュされて再利用されるので、フィールドの初期設定を行います。
180         *
181         * @og.rev 2.0.0.4 (2002/09/27) カスタムタグの release() メソッドを、追加
182         * @og.rev 2.0.1.0 (2002/10/10) デフォルト表示しないから、MSG0059=登録しました。に変更します。
183         * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応。release2() を doEndTag()で呼ぶ。
184         *
185         */
186        @Override
187        protected void release2() {
188                super.release2();
189                command         = null;
190                errMessage      = null;
191                msgClear        = false;
192                viewType        = "TITLE";              // TITLE/BODY
193                displayMsg      = "MSG0059";    // 初期値は『登録しました。』
194                warningMsg      = "ERR0020";    // データ登録時にワーニングが発生しました。
195        }
196
197        /**
198         * エラーメッセージをタグ情報の文字列に変換して返します。
199         *
200         * ここでは、正常なメッセージも異常なメッセージも作成します。
201         *
202         * @og.rev 3.6.0.1 (2004/09/29) ワーニング、エラー時のスタイルシートを適用
203         * @og.rev 3.6.0.7 (2004/11/06) target 属性を _new から _blank に変更
204         * @og.rev 4.0.0.0 (2007/10/18) メッセージリソース統合( getResource().getMessage ⇒ getResource().getLabel )
205         * @og.rev 5.1.7.0 (2010/06/01) エラー・ワーニングメッセージの後に改行を入れる(displayMsgと仕様を合わせる)
206         *
207         * @return      エラーメッセージのタグ情報文字列
208         */
209        private String makeTitle() {
210                String href = getContextPath() + "/" + errMsgFile ;
211
212                Attributes attri = new Attributes();
213                attri.set( "href"       , href   );
214                attri.set( "target" , "_blank" );       // 3.6.0.7 (2004/11/06)
215
216                final String title ;
217                if( warningMsg != null ) {
218                        title = "<span class=\"msg_warning\">"
219//                                       + getResource().getMessage( warningMsg )
220                                         + getResource().getLabel( warningMsg )
221                                         // 5.1.7.0 (2010/06/01) ワーニングメッセージの後に改行を入れる
222                                         + "</span>" + HybsSystem.BR;
223                }
224                else {
225                        title = "<span class=\"msg_error\">"
226                                        + errMessage.getTitle()
227                                         // 5.1.7.0 (2010/06/01) エラーメッセージの後に改行を入れる
228                                         + "</span>" + HybsSystem.BR;
229                }
230
231                attri.set( "body" , title );
232
233                String key = "pageSize";
234                String val = String.valueOf( maxRowCount );
235                String urlEnc = XHTMLTag.urlEncode( key,val );
236
237                return XHTMLTag.link( attri,urlEnc ) ;
238        }
239
240        /**
241         * 【TAG】コマンド(NEW,RENEW,RESET,REVIEW)をセットします。
242         *
243         * @og.tag
244         * コマンドは,HTMLから(get/post)指定されますので,CMD_xxx で設定される
245         * フィールド定数値のいづれかを、指定できます。
246         *
247         * @param       cmd コマンド(public static final 宣言されている文字列)
248         * @see         <a href="../../../../constant-values.html#org.opengion.hayabusa.taglib.ErrorMessageTag.CMD_NEW">コマンド定数</a>
249         */
250        public void setCommand( final String cmd ) {
251                String cmd2 = getRequestParameter( cmd );
252                if( cmd2 != null && cmd2.length() > 0 ) { command = cmd2.toUpperCase(Locale.JAPAN); }
253        }
254
255        /**
256         * 【TAG】メッセージを初期化するかどうか[true/false]を指定します(初期値:false)。
257         *
258         * @og.tag
259         * メッセージは、一般には,エラーメッセージかワーニングです。
260         * 最終処理でメッセージが無ければ,標準でクリアします。
261         * また、command が NEW の場合も、メッセージは自動でクリアされます。
262         * 初期値は、クリアしない (true 以外)です。
263         *
264         * @param       flag  [true:クリアする/それ以外:しない]
265         */
266        public void setClear( final String flag ) {
267                msgClear = nval( getRequestParameter( flag ),msgClear );
268        }
269
270        /**
271         * 【TAG】表示形式『表題(TITLE)か、内容(BODY)』を指定します(初期値:TITLE)。
272         *
273         * @og.tag
274         * 一般には,表題(TITLE) を表示しておきます。
275         * 表題表示時には,リンクを張り、共通エラー表示画面をオープン
276         * 出来る様になっています。
277         *
278         * @param       flag 表示形式 表題(TITLE)か、内容(BODY)
279         */
280        public void setViewType( final String flag ) {
281                viewType = nval( getRequestParameter( flag ),viewType );
282        }
283
284        /**
285         * 【TAG】plsqlUpdate の結果を画面上に表示するメッセージIDを指定します(初期値:MSG0059『登録しました』)。
286         *
287         * @og.tag
288         * 指定したメッセージをリソースから取得して表示します。
289         * 表示させたくない場合は, displayMsg = "MSG0065" をセットしてください。
290         * 初期値は、MSG0059『登録しました。』を表示します。
291         *
292         * @og.rev 2.0.1.0 (2002/10/10) デフォルト表示しないから、MSG0059=登録しました。に変更します。
293         * @og.rev 3.2.0.0 (2003/05/22) 引数に何もセットされないときに、デフォルトの文字を表示するように変更。
294         *
295         * @param       id ディスプレイに表示させるメッセージ ID
296         */
297        public void setDisplayMsg( final String id ) {
298                displayMsg = nval( getRequestParameter( id ),displayMsg );
299        //      String ids = getRequestParameter( id );
300        //      if( ids != null ) { displayMsg = ids; }
301        }
302
303        /**
304         * 【TAG】登録処理実行後のワーニング結果を画面上に表示するメッセージIDを指定します(初期値:ERR0020)。
305         *
306         * @og.tag
307         * 指定したメッセージをリソースから取得して表示します。
308         * 表示させたくない場合は, warningMsg = "" をセットしてください。
309         * 初期値は、ERR0020『データ登録時にワーニングが発生しました。』を表示します。
310         *
311         * @og.rev 2.0.1.0 (2002/10/10) デフォルト表示しないから、ERR0020=データ登録時にワーニングが発生しました。に変更します。
312         *
313         * @param       id ディスプレイに表示させるメッセージ ID
314         */
315        public void setWarningMsg( final String id ) {
316//              warningMsg = nval( getRequestParameter( id ),warningMsg );
317                String ids = getRequestParameter( id );
318                if( ids != null ) { warningMsg = ids; }
319        }
320
321        /**
322         * シリアライズ用のカスタムシリアライズ書き込みメソッド
323         *
324         * @og.rev 4.0.0.0 (2006/09/31) 新規追加
325         * @serialData 一部のオブジェクトは、シリアライズされません。
326         *
327         * @param       strm    ObjectOutputStreamオブジェクト
328         * @throws IOException  入出力エラーが発生した場合
329         */
330        private void writeObject( final ObjectOutputStream strm ) throws IOException {
331                strm.defaultWriteObject();
332        }
333
334        /**
335         * シリアライズ用のカスタムシリアライズ読み込みメソッド
336         *
337         * ここでは、transient 宣言された内部変数の内、初期化が必要なフィールドのみ設定します。
338         *
339         * @og.rev 4.0.0.0 (2006/09/31) 新規追加
340         * @serialData 一部のオブジェクトは、シリアライズされません。
341         *
342         * @param       strm    ObjectInputStreamオブジェクト
343         * @see #release2()
344         * @throws IOException  シリアライズに関する入出力エラーが発生した場合
345         * @throws ClassNotFoundException       クラスを見つけることができなかった場合
346         */
347        private void readObject( final ObjectInputStream strm ) throws IOException , ClassNotFoundException {
348                strm.defaultReadObject();
349        }
350
351        /**
352         * デバッグ時の文字列を返します。
353         *
354         * @return      このオブジェクトのデバッグ表現文字列
355         */
356        @Override
357        public String toString() {
358                return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
359                                .println( "VERSION"             ,VERSION        )
360                                .println( "errMsgId"    ,errMsgId       )
361                                .println( "errMsgFile"  ,errMsgFile     )
362                                .println( "maxRowCount" ,maxRowCount)
363                                .println( "command"             ,command        )
364                                .println( "msgClear"    ,msgClear       )
365                                .println( "viewType"    ,viewType       )
366                                .println( "displayMsg"  ,displayMsg     )
367                                .println( "warningMsg"  ,warningMsg     )
368                                .println( "Other..."    ,getAttributes().getAttribute() )
369                                .fixForm().toString() ;
370        }
371}