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 = "5.6.8.1 (2013/09/13)" ;
065    
066            private static final long serialVersionUID = 568120130913L ;
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             * @og.rev 5.6.8.1 (2013/09/13) UserInfo に、request ??を渡してキャ?ュ対象??をセ?します?
159             *
160             * @param       command アクションコマン?public static final 宣?れて???)
161             * @param       request リクエストオブジェク?
162             */
163            @SuppressWarnings(value={"unchecked"})
164            private void commandExec( final String command,final ServletRequest request ) {
165                    String key = CACHE_KEY + cacheKey ;
166                    HttpSession session = pageContext.getSession();
167                    String msg = null;      // 3.8.6.3 (2006/11/30)
168    
169                    if( CMD_NEW.equals( command ) ) {
170                            Map<String,String[]> map = new HashMap<String,String[]>( request.getParameterMap() );
171                            map = actionCacheData( map );
172                            session.setAttribute( key,map );
173    
174                            // 5.6.8.1 (2013/09/13) UserInfo に、request ??を渡してキャ?ュ対象??をセ?します?
175                            getUser().setLastRequestMap( map );
176                            msg = "command=[NEW] CACHE=[Set]" ;
177                    }
178                    else if( cacheKey.length() > 0 &&
179                                            ( CMD_RENEW.equals( command ) || CMD_RESET.equals( command ) ) ) {
180                            Map<String,String[]> map = (Map<String,String[]>)session.getAttribute( key );
181                            if( map != null ) {
182                                    map = actionCacheData( map );
183                                    session.setAttribute( CACHE_KEY,map );          // 共有キャ?ュにセ?
184                                    msg = "command=[" + command + "] CACHE=[Load]" ;
185                            }
186                    }
187                    else if( CMD_INIT.equals( command ) || command == null || command.length() == 0 ) {
188                            session.removeAttribute( key );
189                            msg = "command=[" + command + "] CACHE=[remove]" ;
190                    }
191    
192                    // 3.8.6.3 (2006/11/30)
193                    if( isDebug() ) {
194                            jspPrint( msg + "<br />" );
195                    }
196            }
197    
198            /**
199             * キャ?ュ??タに対して、追記?削除を行います?
200             *
201             * @og.rev 3.8.8.0 (2006/12/22) 新規追?
202             *
203             * @param       map     キャ?ュ??タ
204             *
205             * @return      追記?削除のキャ?ュ??タのマッ?入力?Mapと同?
206             */
207            private Map<String,String[]> actionCacheData( final Map<String,String[]> map ) {
208                    if( action != null && map != null && keys != null && vals != null ) {
209                            if( ACT_SET.equalsIgnoreCase( action ) ) {
210                                    for( int i=0; i<keys.length; i++ ) {
211                                            String[] val = new String[] { vals[i] } ;
212                                            map.put( keys[i],val );
213                                    }
214                            }
215                            else if( ACT_DELETE.equalsIgnoreCase( action ) ) {
216                                    for( int i=0; i<keys.length; i++ ) {
217                                            map.remove( keys[i] );
218                                    }
219                            }
220                    }
221    
222                    return map ;
223            }
224    
225            /**
226             * BACK_GAMENID のキャ?ュの設定?を変更します?
227             *
228             * @og.rev 3.1.8.0 (2003/05/16) BACK_GAMENID のキャ?ュの設定?を変更する?
229             * @og.rev 3.5.1.0 (2003/10/03) BACK_GAMENID のリクエストがNULLのとき?処?バグ)訂正?
230             * @og.rev 3.7.0.3 (2005/03/01) BACK_ROW ??を取得し、backGamenIdSetメソ?に渡すよ?変更?
231             * @og.rev 3.8.6.3 (2006/11/30) debug 処?追?
232             * @og.rev 4.0.1.0 (2007/12/17) BACK_ADDRESSを追?
233             *
234             * @param       command アクションコマン?public static final 宣?れて???)
235             * @param       backGamenId     リクエストから取得した?BACK_GAMENID
236             * @param       gamenId 画面ID
237             * @param   backRow Stringリクエストから取得した?BACK_ROW ??
238             * @param   backAdrs Stringリクエストから取得した?BACK_ADDRESS ??
239             */
240            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)
241                    HttpSession session = pageContext.getSession();
242    
243                    // こ?画面自身の??
244                    GUIInfo guiInfo = (GUIInfo)session.getAttribute( HybsSystem.GUIINFO_KEY );
245                    String guiId = guiInfo.getAttribute( "KEY" );
246    
247                    String guikey = HybsSystem.BACK_GAMENID_KEY + guiId ;
248                    String rowkey = HybsSystem.BACK_ROW_KEY + guiId ;               // 3.7.0.3 (2005/03/01)
249                    String msg = null;      // 3.8.6.3 (2006/11/30)
250                    String adrskey = HybsSystem.BACK_ADDRESS_KEY + guiId;   // 4.0.1.0 (2007/12/17)
251                    if( CMD_NEW.equals( command ) && backGamenId != null && ! backGamenId.equals( guiId ) ) {
252                            session.setAttribute( guikey,backGamenId );
253                            session.setAttribute( rowkey,backRow );                 // 3.7.0.3 (2005/03/01)
254                            session.setAttribute( adrskey,backAdrs );               // 4.0.1.0 (2007/12/17)
255                            msg = "command=[" + command + "] backGamenId=[Set]" ;
256                    }
257                    else if( CMD_INIT.equals( command ) || command == null || command.length() == 0 ) {
258                            session.removeAttribute( guikey );
259                            session.removeAttribute( rowkey );              // 3.7.0.3 (2005/03/01)
260                            msg = "command=[" + command + "] backGamenId=[command Remove]" ;
261                            session.removeAttribute( adrskey );             // 4.0.1.0 (2007/12/17)
262                    }
263                    // 以?追?3.5.1.0 (2003/10/03)
264                    // 変更 4.0.1.0 (2007/12/17)
265                    else if( ( gamenId == null || gamenId.length() == 0 ) &&
266                                     ( backGamenId == null || backGamenId.length() == 0 ) &&
267                                     ( backAdrs == null || backAdrs.length() == 0 )) {
268                                            session.removeAttribute(guikey );
269                                            session.removeAttribute( rowkey );              // 3.7.0.3 (2005/03/01)
270                                            msg = "command=[" + command + "] backGamenId=[null Remove] " ;
271                                            session.removeAttribute( adrskey );             // 4.0.1.0 (2007/12/17)
272                    }
273    
274                    // 3.8.6.3 (2006/11/30)
275                    if( isDebug() ) {
276                            jspPrint( msg + "<br />" );
277                    }
278            }
279    
280            /**
281             * 【TAG】キャ?ュするサブキーを指定しま?初期値:"")?
282             *
283             * @og.tag
284             * キャ?ュすべき?を?キー毎に?できるようにします?
285             * 例えば、これに、画面IDを追?ておけば、画面ごとに、???リクエスト情報?
286             * キャ?ュしておき、???画面が呼ばれたら?再度使用することができる様になります?
287             * NEW でキャ?ュ登録を行い、RENEW で??常のキャ?ュキーに値を取り?します?
288             *
289             * @og.rev 3.1.6.0 (2003/04/24) キャ?ュすべき?を?キー毎に?できるように、cacheKey 属?を追??
290             *
291             * @param       ck キャ?ュするサブキー
292             */
293            public void setCacheKey( final String ck ) {
294                    cacheKey = nval( getRequestParameter( ck ),cacheKey );
295            }
296    
297            /**
298             * 【TAG】アクション(SET,DELETE)をセ?します?
299             *
300             * @og.tag
301             * アクションは,HTMLから(get/post)?されます?で,ACT_xxx で設定される
302             * フィールド定数値の?れかを??できます?
303             * 無??場合?、なにもしません?
304             *
305             * <table border="1" frame="box" rules="all" >
306         *      <th><td>action  </td><td>名称</td><td>機?                                                  </td></th>
307         *      <tr><td>SET     </td><td>登録</td><td>?? keys のキーに vals のキャ?ュをセ?します?</td></tr>
308         *      <tr><td>DELETE  </td><td>削除</td><td>?? keys のキャ?ュを削除します?                </td></tr>
309             * </table>
310             *
311             * @param       act アクション(public static final 宣?れて???)
312             * @see         <a href="{&#064;docRoot}/constant-values.html#org.opengion.hayabusa.taglib.RequestCacheTag.ACT_DELETE">アクション定数</a>
313             */
314            public void setAction( final String act ) {
315                    action = nval( getRequestParameter( act ),action );
316    
317                    if( action != null && !check( action, ACTION_LIST ) ) {
318                            String errMsg = "??アクションは実行できません。アクションエラー"
319                                                            + HybsSystem.CR
320                                                            + "action=[" + action + "] "
321                                                            + HybsSystem.CR
322                                                            + StringUtil.array2csv( ACTION_LIST ) ;
323                            throw new HybsSystemException( errMsg );
324                    }
325            }
326    
327            /**
328             * 【TAG】リンク先に渡すキーを指定します?
329             *
330             * @og.tag
331             * 戻る時に、検索時?キャ?ュに?した引数以外に?したり、別の値に置き換えた?
332             * する場合?キーを設定できます?カンマ区?で??できます?
333             * vals 属?には、キーに対応する?を?設定してください?
334             * ?方法?、CSV変数を?に?してから、getRequestParameter で値を取得します?
335             * こうしな???タ自身にカンマを持って?場合に?をミスる為です?
336             *
337             * @og.rev 3.8.8.0 (2006/12/22) 新規追?
338             *
339             * @param       key リンク先に渡すキー
340             */
341            public void setKeys( final String key ) {
342                    keys = getCSVParameter( key );
343            }
344    
345            /**
346             * 【TAG】keys属?に対応する?をCSV形式で??します?
347             *
348             * @og.tag
349             * キーに設定した?を?カンマ区??で?して出来ます?
350             * ??序?、キーと同じにしておいて下さ??
351             * ?方法?、CSV変数を?に?してから、getRequestParameter で値を取得します?
352             * こうしな???タ自身にカンマを持って?場合に?をミスる為です?
353             *
354             * @og.rev 3.8.8.0 (2006/12/22) 新規追?
355             *
356             * @param       val keys属?に対応する?
357             */
358            public void setVals( final String val ) {
359                    vals = getCSVParameter( val );
360            }
361    
362            /**
363             * こ?オブジェクト???表現を返します?
364             * 基本???目?使用します?
365             *
366             * @return こ?クラスの??表現
367             */
368            @Override
369            public String toString() {
370                    return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
371                                    .println( "VERSION"             ,VERSION        )
372                                    .println( "cacheKey"    ,cacheKey       )
373                                    .println( "Other..."    ,getAttributes().getAttribute() )
374                                    .fixForm().toString() ;
375            }
376    }