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