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.resource.GUIInfo;
021    import static org.opengion.fukurou.util.StringUtil.nval ;
022    import org.opengion.fukurou.util.StringUtil ;
023    
024    import javax.servlet.ServletRequest ;
025    import javax.servlet.http.HttpSession ;
026    
027    import java.util.Map;
028    import java.util.HashMap;
029    
030    /**
031     * 戻るリンクで戻るå?合に使用する、検索時ã? request æƒ??をキャãƒ?‚·ュするタグでã�?通常はquery.jsp にçµ?¾¼み)ã€?
032     *
033     * requestタグをキャãƒ?‚·ュすることによりã€?再検索時や、各画面遷移時ã?é ?›®の持ち回りを行いますã?
034     * command = "NEW" で、キャãƒ?‚·ュしã?"RENEW" で、取りå?しますã?
035     * 暫定的にこã?タグは、å?通JSPファイルに設定し、HTMLそã?もã?のキャãƒ?‚·ュ制御ã‚?
036     * 行うように設定してã�?�¾すã?
037     *
038     * @og.formSample
039     * ●形式ï¼?lt;og:requestCache cacheKey="[?¥?¥?¥]" />
040     * ●body?šなã�?
041     *
042     * ●Tag定義??
043     *   <og:requestCache
044     *       cacheKey           【TAG】キャãƒ?‚·ュするサブキーを指定しまã�?初期値:"")
045     *       action             【TAG】アクション(SET,DELETE)をセãƒ?ƒˆしまã�?
046     *       keys               【TAG】リンク先に渡すキーを指定しまã�?
047     *       vals               【TAG】keys属æ?に対応するå?をCSV形式でè¤?•°æŒ?®šしまã�?
048     *       debug              【TAG】デバッグæƒ??をå?力するかどã�?�‹[true/false]を指定しまã�?初期値:false)
049     *   />
050     *
051     * ●使用ä¾?
052     *       <og:requestCache
053     *                  cacheKey="{@GUI.KEY}"     キャãƒ?‚·ュするサブキーを指定しますã?
054     *       />
055     *
056     * @og.group 画面制御
057     *
058     * @version  4.0
059     * @author   Kazuhiko Hasegawa
060     * @since    JDK5.0,
061     */
062    public class RequestCacheTag extends CommonTagSupport {
063            //* こã?プログラãƒ??VERSIONæ–?­—å?を設定しますã?       {@value} */
064            private static final String VERSION = "4.0.1.0 (2007/12/17)" ;
065    
066            private static final long serialVersionUID = 401020071217L ;
067    
068            private static final String CACHE_KEY = HybsSystem.REQ_CACHE_KEY;
069    
070            /** command 引数に渡す事ã?出来ã‚?アクションコマンãƒ?  ニュー {@value} */
071            public static final String CMD_NEW   = "NEW" ;
072            /** command 引数に渡す事ã?出来ã‚?アクションコマンãƒ?  レニュー {@value} */
073            public static final String CMD_RENEW = "RENEW" ;
074            /** command 引数に渡す事ã?出来ã‚?アクションコマンãƒ?  イニッãƒ?{@value} */
075            public static final String CMD_INIT = "INIT" ;
076            /** command 引数に渡す事ã?出来ã‚?アクションコマンãƒ?  リセãƒ?ƒˆ {@value} */
077            public static final String CMD_RESET = "RESET" ;        // 3.5.5.0 (2004/03/12) 追åŠ?
078    
079            // 3.8.8.0 (2006/12/22)
080            /** action 引数に渡す事ã?出来ã‚?アクション  設å®?{@value} */
081            public static final String ACT_SET = "SET" ;
082            /** action 引数に渡す事ã?出来ã‚?アクション  削除 {@value} */
083            public static final String ACT_DELETE = "DELETE" ;
084    
085            /** action 引数に渡す事ã?出来ã‚?アクション リスãƒ? */
086            private static final String[] ACTION_LIST = new String[] { ACT_SET , ACT_DELETE };
087    
088            private String  cacheKey        = "";
089            private String  action          = null;
090            private String[] keys           = null;
091            private String[] vals           = null;
092    
093            /**
094             * Taglibの終äº?‚¿グが見つかったときに処ç�?�™ã‚?doEndTag() ã‚?オーバã?ライドしますã?
095             *
096             * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応ã?release2() ã‚?doEndTag()で呼ぶã€?
097             * @og.rev 3.1.1.2 (2003/04/04) 継承å…?‚’、CommonTagSupport から TagSupport に変更するã€?
098             * @og.rev 3.1.3.0 (2003/04/10) Cache-Control ヘッãƒ??のセãƒ?ƒˆを削除しますã?
099             * @og.rev 3.1.6.0 (2003/04/24) キャãƒ?‚·ュすべきå?をã?キー毎にæŒ?®šできるように、cacheKey 属æ?を追åŠ??
100             * @og.rev 3.1.7.0 (2003/05/02) command=INIT またã?、null のときに、キャãƒ?‚·ュを削除するように変更するã€?
101             * @og.rev 3.1.7.0 (2003/05/02) command=INIT またã?、null のときに、キャãƒ?‚·ュを削除するように変更するã€?
102             * @og.rev 3.1.8.0 (2003/05/16) BACK_GAMENID のキャãƒ?‚·ュの設定å?を変更するã€?
103             * @og.rev 3.5.1.0 (2003/10/03) GAMENID æƒ??を取得し、backGamenIdSetメソãƒ?ƒ‰に渡すよã�?�«変更ã€?
104             * @og.rev 3.7.0.3 (2005/03/01) BACK_ROW æƒ??を取得し、backGamenIdSetメソãƒ?ƒ‰に渡すよã�?�«変更ã€?
105             * @og.rev 4.0.1.0 (2007/12/17) BackAddress対å¿?
106             *
107             * @return      後続å?ç�??æŒ?¤º
108             */
109            @Override
110            public int doEndTag() {
111                    debugPrint();           // 4.0.0 (2005/02/28)
112                    // request をキャãƒ?‚·ュするタグなので、直接取り込ã‚??
113                    ServletRequest request = pageContext.getRequest();
114                    String command = request.getParameter( "command" );
115                    String backGamenId = request.getParameter( "BACK_GAMENID" );
116                    String gamenId     = request.getParameter( "GAMENID" );                 // 3.5.1.0 (2003/10/03)
117                    String backRow     = request.getParameter( "BACK_ROW" );                // 3.7.0.3 (2005/03/01)
118                    String backAdrs    = request.getParameter( "BACK_ADDRESS" );    // 4.0.1.0 (2007/12/17)
119    
120                    commandExec( command,request );
121    //              backGamenIdSet( command,backGamenId,gamenId,backRow );  // 3.7.0.3 (2005/03/01)
122                    backGamenIdSet( command,backGamenId,gamenId,backRow,backAdrs ); // 4.0.1.0 (2007/12/17)
123    
124                    return(EVAL_PAGE);              // ペã?ジの残りを評価するã€?
125            }
126    
127            /**
128             * タグリブオブジェクトをリリースしますã?
129             *
130             * キャãƒ?‚·ュされて再利用されるã?で、フィールドã?初期設定を行いますã?
131             *
132             * @og.rev 3.5.5.3 (2004/04/09) Tomcat4.1 対応ã?release2() ã‚?doEndTag()で呼ぶã€?
133             * @og.rev 3.8.8.0 (2006/12/22) action,keys,vals 追åŠ?
134             *
135             */
136            @Override
137            protected void release2() {
138                    super.release2();
139                    cacheKey        = "";
140                    action          = null;
141                    keys            = null;
142                    vals            = null;
143            }
144    
145            /**
146             * アクションを実行しますã?
147             * アクションは,æŒ?®šã?アクションコマンドに対応するå?ç�?‚’入力データに
148             * 対して行いますã?
149             *
150             * @og.rev 3.1.1.2 (2003/04/04) 継承å…?‚’、CommonTagSupport から TagSupport に変更するã€?
151             * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応ã?HybsRequestWrapper å»?­¢。直接 Mapでキャãƒ?‚·ュするã€?
152             * @og.rev 3.1.6.0 (2003/04/24) キャãƒ?‚·ュすべきå?をã?キー毎にæŒ?®šできるように、cacheKey 属æ?を追åŠ??
153             * @og.rev 3.1.7.0 (2003/05/02) RENEW のときに、キャãƒ?‚·ュを削除しなã�?‚ˆã�?�«変更するã€?
154             * @og.rev 3.1.7.0 (2003/05/02) command=INIT またã?、null のときに、キャãƒ?‚·ュを削除するように変更するã€?
155             * @og.rev 3.5.5.0 (2004/03/12) command=RESET時にもã?キャãƒ?‚·ュを取りå?すよã�?�«変更するã€?
156             * @og.rev 3.8.6.3 (2006/11/30) debug 処ç�?‚’追åŠ?
157             * @og.rev 3.8.8.0 (2006/12/22) actionCacheData 処ç�?‚’追åŠ?
158             *
159             * @param       command アクションコマンãƒ?public static final 宣è¨?�•れてã�?‚‹æ–?­—å?)
160             * @param       request リクエストオブジェクãƒ?
161             */
162            @SuppressWarnings(value={"unchecked"})
163            private void commandExec( final String command,final ServletRequest request ) {
164                    String key = CACHE_KEY + cacheKey ;
165                    HttpSession session = pageContext.getSession();
166                    String msg = null;      // 3.8.6.3 (2006/11/30)
167    
168                    if( CMD_NEW.equals( command ) ) {
169                            Map<String,String[]> map = new HashMap<String,String[]>( request.getParameterMap() );
170                            map = actionCacheData( map );
171                            session.setAttribute( key,map );
172                            msg = "command=[NEW] CACHE=[Set]" ;
173                    }
174                    else if( cacheKey.length() > 0 &&
175                                            ( CMD_RENEW.equals( command ) || CMD_RESET.equals( command ) ) ) {
176                            Map<String,String[]> map = (Map<String,String[]>)session.getAttribute( key );
177                            if( map != null ) {
178                                    map = actionCacheData( map );
179                                    session.setAttribute( CACHE_KEY,map );          // 共有キャãƒ?‚·ュにセãƒ?ƒˆ
180                                    msg = "command=[" + command + "] CACHE=[Load]" ;
181                            }
182                    }
183                    else if( CMD_INIT.equals( command ) || command == null || command.length() == 0 ) {
184                            session.removeAttribute( key );
185                            msg = "command=[" + command + "] CACHE=[remove]" ;
186                    }
187    
188                    // 3.8.6.3 (2006/11/30)
189                    if( isDebug() ) {
190                            jspPrint( msg + "<br />" );
191                    }
192            }
193    
194            /**
195             * キャãƒ?‚·ュãƒ??タに対して、追記ã?削除を行いますã?
196             *
197             * @og.rev 3.8.8.0 (2006/12/22) 新規追åŠ?
198             *
199             * @param       map     キャãƒ?‚·ュãƒ??タ
200             *
201             * @return      追記ã?削除のキャãƒ?‚·ュãƒ??タのマッãƒ?入力ã?Mapと同ä¸?
202             */
203            private Map<String,String[]> actionCacheData( final Map<String,String[]> map ) {
204                    if( action != null && map != null && keys != null && vals != null ) {
205                            if( ACT_SET.equalsIgnoreCase( action ) ) {
206                                    for( int i=0; i<keys.length; i++ ) {
207                                            String[] val = new String[] { vals[i] } ;
208                                            map.put( keys[i],val );
209                                    }
210                            }
211                            else if( ACT_DELETE.equalsIgnoreCase( action ) ) {
212                                    for( int i=0; i<keys.length; i++ ) {
213                                            map.remove( keys[i] );
214                                    }
215                            }
216                    }
217    
218                    return map ;
219            }
220    
221            /**
222             * BACK_GAMENID のキャãƒ?‚·ュの設定å?を変更しますã?
223             *
224             * @og.rev 3.1.8.0 (2003/05/16) BACK_GAMENID のキャãƒ?‚·ュの設定å?を変更するã€?
225             * @og.rev 3.5.1.0 (2003/10/03) BACK_GAMENID のリクエストがNULLのときã?処ç�?バグ)訂正ã€?
226             * @og.rev 3.7.0.3 (2005/03/01) BACK_ROW æƒ??を取得し、backGamenIdSetメソãƒ?ƒ‰に渡すよã�?�«変更ã€?
227             * @og.rev 3.8.6.3 (2006/11/30) debug 処ç�?‚’追åŠ?
228             * @og.rev 4.0.1.0 (2007/12/17) BACK_ADDRESSを追åŠ?
229             *
230             * @param       command アクションコマンãƒ?public static final 宣è¨?�•れてã�?‚‹æ–?­—å?)
231             * @param       backGamenId     リクエストから取得したã?BACK_GAMENID
232             * @param       gamenId 画面ID
233             * @param   backRow Stringリクエストから取得したã?BACK_ROW æƒ??
234             * @param   backAdrs Stringリクエストから取得したã?BACK_ADDRESS æƒ??
235             */
236            private void backGamenIdSet( final String command,final String backGamenId,final String gamenId,final String backRow,final String backAdrs) {           // 3.5.1.0 (2003/10/03)
237                    HttpSession session = pageContext.getSession();
238    
239                    // こã?画面自身の?©?¤
240                    GUIInfo guiInfo = (GUIInfo)session.getAttribute( HybsSystem.GUIINFO_KEY );
241                    String guiId = guiInfo.getAttribute( "KEY" );
242    
243                    String guikey = HybsSystem.BACK_GAMENID_KEY + guiId ;
244                    String rowkey = HybsSystem.BACK_ROW_KEY + guiId ;               // 3.7.0.3 (2005/03/01)
245                    String msg = null;      // 3.8.6.3 (2006/11/30)
246                    String adrskey = HybsSystem.BACK_ADDRESS_KEY + guiId;   // 4.0.1.0 (2007/12/17)
247                    if( CMD_NEW.equals( command ) && backGamenId != null && ! backGamenId.equals( guiId ) ) {
248                            session.setAttribute( guikey,backGamenId );
249                            session.setAttribute( rowkey,backRow );                 // 3.7.0.3 (2005/03/01)
250                            session.setAttribute( adrskey,backAdrs );               // 4.0.1.0 (2007/12/17)
251                            msg = "command=[" + command + "] backGamenId=[Set]" ;
252                    }
253                    else if( CMD_INIT.equals( command ) || command == null || command.length() == 0 ) {
254                            session.removeAttribute( guikey );
255                            session.removeAttribute( rowkey );              // 3.7.0.3 (2005/03/01)
256                            msg = "command=[" + command + "] backGamenId=[command Remove]" ;
257                            session.removeAttribute( adrskey );             // 4.0.1.0 (2007/12/17)
258                    }
259                    // 以ä¸?追åŠ?3.5.1.0 (2003/10/03)
260                    // 変更 4.0.1.0 (2007/12/17)
261                    else if( ( gamenId == null || gamenId.length() == 0 ) &&
262                                     ( backGamenId == null || backGamenId.length() == 0 ) &&
263                                     ( backAdrs == null || backAdrs.length() == 0 )) {
264                                            session.removeAttribute(guikey );
265                                            session.removeAttribute( rowkey );              // 3.7.0.3 (2005/03/01)
266                                            msg = "command=[" + command + "] backGamenId=[null Remove] " ;
267                                            session.removeAttribute( adrskey );             // 4.0.1.0 (2007/12/17)
268                    }
269    
270                    // 3.8.6.3 (2006/11/30)
271                    if( isDebug() ) {
272                            jspPrint( msg + "<br />" );
273                    }
274            }
275    
276            /**
277             * 【TAG】キャãƒ?‚·ュするサブキーを指定しまã�?初期値:"")ã€?
278             *
279             * @og.tag
280             * キャãƒ?‚·ュすべきå?をã?キー毎にæŒ?®šできるようにしますã?
281             * 例えば、これに、画面IDを追åŠ?�—ておけば、画面ごとに、è?åˆ??リクエスト情報ã‚?
282             * キャãƒ?‚·ュしておき、è?åˆ??画面が呼ばれたらã?再度使用することができる様になりますã?
283             * NEW でキャãƒ?‚·ュ登録を行い、RENEW でã€??常のキャãƒ?‚·ュキーに値を取りå?しますã?
284             *
285             * @og.rev 3.1.6.0 (2003/04/24) キャãƒ?‚·ュすべきå?をã?キー毎にæŒ?®šできるように、cacheKey 属æ?を追åŠ??
286             *
287             * @param       ck キャãƒ?‚·ュするサブキー
288             */
289            public void setCacheKey( final String ck ) {
290                    cacheKey = nval( getRequestParameter( ck ),cacheKey );
291            }
292    
293            /**
294             * 【TAG】アクション(SET,DELETE)をセãƒ?ƒˆしますã?
295             *
296             * @og.tag
297             * アクションは,HTMLから(get/post)æŒ?®šされますã?で,ACT_xxx で設定される
298             * フィールド定数値のã�?�¥れかをã?æŒ?®šできますã?
299             * 無æŒ?®šã?場合ã?、なにもしませんã€?
300             *
301             * <table border="1" frame="box" rules="all" >
302         *      <th><td>action  </td><td>名称</td><td>機è?                                                  </td></th>
303         *      <tr><td>SET     </td><td>登録</td><td>æŒ?®šã? keys のキーに vals のキャãƒ?‚·ュをセãƒ?ƒˆしますã?</td></tr>
304         *      <tr><td>DELETE  </td><td>削除</td><td>æŒ?®šã? keys のキャãƒ?‚·ュを削除しますã?                </td></tr>
305             * </table>
306             *
307             * @param       act アクション(public static final 宣è¨?�•れてã�?‚‹æ–?­—å?)
308             * @see         <a href="{&#064;docRoot}/constant-values.html#org.opengion.hayabusa.taglib.RequestCacheTag.ACT_DELETE">アクション定数</a>
309             */
310            public void setAction( final String act ) {
311                    action = nval( getRequestParameter( act ),action );
312    
313                    if( action != null && !check( action, ACTION_LIST ) ) {
314                            String errMsg = "æŒ?®šã?アクションは実行できません。アクションエラー"
315                                                            + HybsSystem.CR
316                                                            + "action=[" + action + "] "
317                                                            + HybsSystem.CR
318                                                            + StringUtil.array2csv( ACTION_LIST ) ;
319                            throw new HybsSystemException( errMsg );
320                    }
321            }
322    
323            /**
324             * 【TAG】リンク先に渡すキーを指定しますã?
325             *
326             * @og.tag
327             * 戻る時に、検索時ã?キャãƒ?‚·ュにæŒ?®šした引数以外にæŒ?®šしたり、別の値に置き換えたã‚?
328             * する場合ã?キーを設定できますã?カンマ区åˆ?‚Šでè¤?•°æŒ?®šできますã?
329             * vals 属æ?には、キーに対応するå?をã?設定してくださいã€?
330             * åˆ?§£方法ã?、CSV変数をå?にåˆ?§£してから、getRequestParameter で値を取得しますã?
331             * こうしなã�?�¨ãƒ??タ自身にカンマを持ってã�?‚‹場合にåˆ?§£をミスる為ですã?
332             *
333             * @og.rev 3.8.8.0 (2006/12/22) 新規追åŠ?
334             *
335             * @param       key リンク先に渡すキー
336             */
337            public void setKeys( final String key ) {
338                    keys = getCSVParameter( key );
339            }
340    
341            /**
342             * 【TAG】keys属æ?に対応するå?をCSV形式でè¤?•°æŒ?®šしますã?
343             *
344             * @og.tag
345             * キーに設定したå?をã?カンマ区åˆ?‚Šæ–?­—でè¤?•°して出来ますã?
346             * æŒ?®šé?序ã?、キーと同じにしておいて下さã�??
347             * åˆ?§£方法ã?、CSV変数をå?にåˆ?§£してから、getRequestParameter で値を取得しますã?
348             * こうしなã�?�¨ãƒ??タ自身にカンマを持ってã�?‚‹場合にåˆ?§£をミスる為ですã?
349             *
350             * @og.rev 3.8.8.0 (2006/12/22) 新規追åŠ?
351             *
352             * @param       val keys属æ?に対応するå?
353             */
354            public void setVals( final String val ) {
355                    vals = getCSVParameter( val );
356            }
357    
358            /**
359             * こã?オブジェクトã?æ–?­—å?表現を返しますã?
360             * 基本çš?�«ãƒ?ƒ�ãƒ?‚°目çš?�«使用しますã?
361             *
362             * @return こã?クラスのæ–?­—å?表現
363             */
364            @Override
365            public String toString() {
366                    return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
367                                    .println( "VERSION"             ,VERSION        )
368                                    .println( "cacheKey"    ,cacheKey       )
369                                    .println( "Other..."    ,getAttributes().getAttribute() )
370                                    .fixForm().toString() ;
371            }
372    }