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    }