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.resource.UserInfo;
019    import org.opengion.hayabusa.db.DBTableModel;
020    import org.opengion.hayabusa.db.Query;
021    import org.opengion.hayabusa.db.QueryFactory;
022    import org.opengion.fukurou.db.Transaction;
023    import org.opengion.fukurou.db.TransactionReal;
024    
025    import static org.opengion.fukurou.util.StringUtil.nval ;
026    
027    import java.util.Locale ;
028    import java.io.ObjectOutputStream;
029    import java.io.ObjectInputStream;
030    import java.io.IOException;
031    
032    /**
033     * ユーザーæƒ??を管ç�?�—てã�?‚‹タグですã?
034     *
035     * 設定したå?は、{@USER.XXXX} 形式で 取り出すことができますã?
036     * またã?command 属æ?で 直接画面に値を書きå?すことも可能ですã?
037     *
038     * 以下ã?値は UserInfo オブジェクトã?é ?›®から取得しますã?
039     * ・USER.JNAME     ユーザー日本語名称
040     * ・USER.ID        ユーザー?©?¤
041     * ・USER.IDNO      USER.ID が5Byte以上ã?時ã?み先é??‘文字を除ã�?�Ÿユーザー?©?¤
042     * ・USER.INFO      ユーザーæƒ??(ユーザーID?š日本語名称)
043     * ・USER.LANG      è¨?ª?
044     * ・USER.ROLES     ロール
045     * ・USER.IPADDRESS IPアドレス
046     * ・USER.LOGINTIME ログイン時刻
047     *
048     * 以下ã?値はあらかじめã?動的に作æ?されますã?
049     * ・USER.YMD       ?˜byte の今日のシスãƒ?ƒ 日ä»?
050     * ・USER.YMDH    ?‘4byte の今日のシスãƒ?ƒ 日æ™?
051     *
052     * それ以外ã?、外部より設定された値ですã?
053     *
054     * ※ こã?タグは、Transaction タグの対象ですã?
055     *
056     * @og.formSample
057     * ●形式ï¼?lt;og:userInfo command="[…]" key="[…]" value="[…]" />
058     * ●body?šあã‚?
059     *
060     * ●Tag定義??
061     *   <og:userInfo
062     *       command            【TAG】コマンãƒ?SET,GET,NVLEGT,REMOVE,SQL)をセãƒ?ƒˆしまã�?初期値:SET)
063     *       key                【TAG】UserInfo に登録するキーをセãƒ?ƒˆしまã�?予ç´?ª?JNAME,ID,PASSWD,IDNO,INFO,LANG,ROLE,IPADDRESS,LOGINTIME)
064     *       value              【TAG】UserInfo に登録する値をセãƒ?ƒˆしまã�?
065     *       dbid               【TAGã€?通常は使ã�?�¾せん)Queryオブジェクトを作æ?する時ã?DB接続IDを指定しまã�?
066     *       save               【TAG】UserInfo に登録した値を永続化するかをæŒ?®šしまã�?
067     *       caseKey            【TAG】このタグ自体を利用するかどã�?�‹の条件キーを指定しまã�?初期値:null)
068     *       caseVal            【TAG】このタグ自体を利用するかどã�?�‹の条件値を指定しまã�?初期値:null)
069     *       debug              【TAG】デバッグæƒ??をå?力するかどã�?�‹[true/false]を指定しまã�?初期値:false)
070     *   >   ... Body ...
071     *   </og:userInfo>
072     *
073     * ●使用ä¾?
074     *      <og:userInfo
075     *         command       = "コマンãƒ?SET,GET,NVLEGT,REMOVE,SQL)をセãƒ?ƒˆしまã�?初期値:SET)ã€?quot;
076     *         key           = "UserInfo に登録するキーをセãƒ?ƒˆしまã�?予ç´?ª?JNAME,ID,PASSWD,IDNO,INFO,LANG,ROLE,IPADDRESS,LOGINTIME)ã€?quot;
077     *         value         = "UserInfo に登録する値をセãƒ?ƒˆしますã?"
078     *         dbid          = "(通常は使ã�?�¾せん)Queryオブジェクトを作æ?する時ã?DB接続IDを指定しますã?"
079     *      >
080     *
081     * @og.group そã?他部å“?
082     *
083     * @version  4.0
084     * @author   Kazuhiko Hasegawa
085     * @since    JDK5.0,
086     */
087    public class UserInfoTag extends CommonTagSupport {
088            //* こã?プログラãƒ??VERSIONæ–?­—å?を設定しますã?       {@value} */
089            private static final String VERSION = "5.3.8.0 (2011/08/01)" ;
090    
091            private static final long serialVersionUID = 538020110801L ;
092    
093            /** command 引数に渡す事ã?出来ã‚?コマンãƒ? セãƒ?ƒˆ {@value} */
094            public static final String CMD_SET   = "SET" ;
095            /** command 引数に渡す事ã?出来ã‚?コマンãƒ? ゲãƒ?ƒˆ {@value} */
096            public static final String CMD_GET   = "GET" ;
097            /** command 引数に渡す事ã?出来ã‚?コマンãƒ? ゲãƒ?ƒˆ {@value} */
098            public static final String CMD_REMOVE   = "REMOVE" ;
099            /** command 引数に渡す事ã?出来ã‚?コマンãƒ? ゲãƒ?ƒˆ {@value} */
100            public static final String CMD_SQL   = "SQL" ;
101            // 3.5.5.3 (2004/04/09) 値ã�?null の場合ã?ã€?"(ゼロæ–?­—å?)を返すNVLGETを採用
102            /** command 引数に渡す事ã?出来ã‚?コマンãƒ? NVLゲãƒ?ƒˆ {@value} */
103            public static final String CMD_NVLGET   = "NVLGET" ;
104    
105            // 3.5.5.3 (2004/04/09) 値ã�?null の場合ã?ã€?"(ゼロæ–?­—å?)を返すNVLGETを採用
106            /** command 引数に渡す事ã?出来ã‚?コマンãƒ?リスãƒ? */
107            private static final String[] COMMAND_LIST = new String[] {
108                    CMD_SET , CMD_GET , CMD_NVLGET , CMD_REMOVE , CMD_SQL };
109    
110            // 3.5.6.0 (2004/06/18) すべてã‚?protected から private に変更しますã?
111            private String                  command         = CMD_SET;
112            private String                  key                     = null;
113            private String                  value           = null;
114    //      private boolean                 isNullSet       = true;                 // NULL のときにセãƒ?ƒˆし直すかどã�?�‹ã€?
115            private transient DBTableModel  table           = null;
116            // 4.0.0.0 (2007/10/10) dbid の初期値をã?"DEFAULT" から null に変更
117    //      private String                  dbid            = "DEFAULT";
118            private String                  dbid            = null;
119            private String                  sql                     = null;
120            // 4.3.4.0 (2008/12/01) save属æ?追åŠ?
121            private boolean                 save            = false;
122    
123            /**
124             * Taglibの開始タグが見つかったときに処ç�?�™ã‚?doStartTag() ã‚?オーバã?ライドしますã?
125             *
126             * @og.rev 5.2.2.0 (2010/11/01) caseKey 、caseVal 属æ?対å¿?
127             *
128             * @return      後続å?ç�??æŒ?¤º
129             */
130            @Override
131            public int doStartTag() {
132                    // 5.2.2.0 (2010/11/01) caseKey 、caseVal 属æ?対å¿?
133    //              if( CMD_SQL.equals( command ) ) {
134                    if( useTag() && CMD_SQL.equals( command ) ) {
135                            return( EVAL_BODY_BUFFERED );           // Body を評価する
136                    }
137                    else {
138                            return( SKIP_BODY ); // Body を評価しなã�?
139                    }
140            }
141    
142            /**
143             * Taglibのタグ本体を処ç�?�™ã‚?doAfterBody() ã‚?オーバã?ライドしますã?
144             *
145             * @og.rev 3.1.1.0 (2003/03/28) ボディのå†?®¹を取得するå?ç�?‚’、CommonTagSupport で行うã€?
146             * @og.rev 3.6.0.8 (2004/11/19) エラー発生時に確実にリリースされるよã�?�« try finally 追åŠ?
147             * @og.rev 3.8.6.3 (2006/11/30) SQL æ–??前後ã?スペã?スを取り除きますã?
148             * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得ã?為,ApplicationInfoオブジェクトを設å®?
149             * @og.rev 4.0.0.0 (2005/01/31) lang â‡?ResourceManager へ変更
150             * @og.rev 5.1.9.0 (2010/08/01) TransactionTag 対応ã?上位に TransactionTag があれã?、そこからConnection をもらうã€?
151             * @og.rev 5.3.7.0 (2011/07/01) TransactionReal の引数変更
152             * @og.rev 5.3.8.0 (2011/08/01) Transaction発生ç®?‰€でclose()
153             *
154             * @return      後続å?ç�??æŒ?¤º(SKIP_BODY)
155             */
156            @Override
157            public int doAfterBody() {
158                    sql = getBodyString().trim();
159    
160                    Query query = QueryFactory.newInstance();               // 4.0.0 (2005/01/31)
161                    Transaction tran = null;
162                    try {
163                            // 5.1.9.0 (2010/08/01) TransactionTag 対å¿?
164    //                      final Transaction tran ;
165                            TransactionTag tranTag = (TransactionTag)findAncestorWithClass( this,TransactionTag.class );
166                            if( tranTag == null ) {
167    //                              tran = new TransactionReal( dbid,getApplicationInfo() );
168                                    tran = new TransactionReal( getApplicationInfo() );             // 5.3.7.0 (2011/07/01) 引数変更
169                            }
170                            else {
171                                    tran = tranTag.getTransaction();
172                            }
173                            query.setTransaction( dbid,tran );      // 5.1.9.0 (2010/08/01) TransactionTag 対å¿?
174    
175    //                      query.setConnectionID( dbid );
176                            query.setResourceManager( getResource() );      // 4.0.0 (2005/01/31)
177    
178                            query.setStatement( sql );
179    //                      query.setApplicationInfo( getApplicationInfo() );       // 3.8.7.0 (2006/12/15)
180                            query.execute();
181    
182                            table = query.getDBTableModel();
183                    }
184                    finally {
185    //                      if( query != null ) { query.close(); }
186                            QueryFactory.close( query );
187                            if( tran != null ) { tran.close(); }            // 5.3.8.0 (2011/08/01) Transaction発生ç®?‰€でclose()
188                    }
189                    return(SKIP_BODY);
190            }
191    
192            /**
193             * Taglibの終äº?‚¿グが見つかったときに処ç�?�™ã‚?doEndTag() ã‚?オーバã?ライドしますã?
194             *
195             * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応ã?release2() ã‚?doEndTag()で呼ぶã€?
196             * @og.rev 3.5.5.3 (2004/04/09) 値ã�?null の場合ã?ã€?"(ゼロæ–?­—å?)を返すフラグを採用
197             * @og.rev 5.2.2.0 (2010/11/01) caseKey 、caseVal 属æ?対å¿?
198             *
199             * @return      後続å?ç�??æŒ?¤º
200             */
201            @Override
202            public int doEndTag() {
203                    debugPrint();           // 4.0.0 (2005/02/28)
204                    // 5.2.2.0 (2010/11/01) caseKey 、caseVal 属æ?対å¿?
205    //              if( check( command, COMMAND_LIST ) ) {
206                    if( useTag() && check( command, COMMAND_LIST ) ) {
207                            commandExec( command );
208    
209                            if( CMD_GET.equals( command ) ) {
210                                    jspPrint( value );
211                            }
212                            else if( CMD_NVLGET.equals( command ) ) {       // 3.5.5.3 (2004/04/09)
213                                    jspPrint( nval(value,"") );
214                            }
215                    }
216    
217                    return(EVAL_PAGE);
218            }
219    
220            /**
221             * タグリブオブジェクトをリリースしますã?
222             * キャãƒ?‚·ュされて再利用されるã?で、フィールドã?初期設定を行いますã?
223             *
224             * @og.rev 2.0.0.4 (2002/09/27) カスタãƒ?‚¿グの release() メソãƒ?ƒ‰をã?追åŠ?
225             * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応ã?release2() ã‚?doEndTag()で呼ぶã€?
226             * @og.rev 4.0.0.0 (2007/10/10) dbid の初期値をã?"DEFAULT" から null に変更
227             * @og.rev 4.3.4.0 (2008/12/01) save属æ?対å¿?
228             *
229             */
230            @Override
231            protected void release2() {
232                    super.release2();
233                    command         = CMD_SET;
234                    key                     = null;
235                    value           = null;
236    //              isNullSet       = true;                 // NULL のときにセãƒ?ƒˆし直すかどã�?�‹ã€?
237                    table           = null;
238    //              dbid            = "DEFAULT";
239                    dbid            = null;
240                    sql                     = null;
241                    save            = false;
242            }
243    
244            /**
245             * コマンドを実行しますã?
246             *
247             * コマンドã?,HTMLから(get/post)æŒ?®šされますã?で,setCommand()メソãƒ?ƒ‰で
248             * 登録しますã?
249             * コマンドを登録すると同時に,実行も行なã�?�¾すã?
250             *
251             * @og.rev 3.5.5.3 (2004/04/09) 値ã�?null の場合ã?ã€?"(ゼロæ–?­—å?)を返すフラグを採用
252             *
253             * @param   command コマンãƒ?public static final 宣è¨?�•れてã�?‚‹æ–?­—å?)
254             * @see         <a href="{&#064;docRoot}/constant-values.html#org.opengion.hayabusa.taglib.UserInfoTag.CMD_SET">コマンド定数</a>
255             */
256            protected void commandExec( final String command ) {
257                    if( CMD_SQL.equals( command ) ) { setSQLAttribute() ; }
258                    else if( CMD_SET.equals(    command ) ) { setAttribute() ;    }
259                    else if( CMD_GET.equals(    command ) ) { getAttribute() ;    }
260                    else if( CMD_NVLGET.equals( command ) ) { getAttribute() ;    } // 3.5.5.3 (2004/04/09)
261                    else if( CMD_REMOVE.equals( command ) ) { removeAttribute() ; }
262            }
263    
264            /**
265             * UserInfoのæ–?­—å?を登録しますã?
266             *
267             * @og.rev 4.3.4.0 (2008/12/01) GE20(ユーザー定数)へ登録するかã?フラグを追åŠ?
268             *
269             */
270            private void setAttribute() {
271    //              if( isNullSet ) {
272    //                      setUserInfo( key, value );
273                            setUserInfo( key, value, save );
274    //              }
275            }
276    
277            /**
278             * UserInfoのæ–?­—å?を作æ?しますã?
279             *
280             */
281            private void getAttribute() {
282                    value = getUserInfo( key );
283            }
284    
285            /**
286             * UserInfoのæ–?­—å?を削除しますã?
287             *
288             * @og.rev 5.3.6.0 (2011/06/01) GE20(ユーザー定数)から削除するかã?フラグを追åŠ?
289             */
290            private void removeAttribute() {
291                    UserInfo userInfo = getUser();
292    //              userInfo.removeAttribute( key );
293                    userInfo.removeAttribute( key, save );
294            }
295    
296            /**
297             * UserInfoのæ–?­—å?を指定ã?SQLæ–?‚ˆり作æ?しますã?
298             *
299             * @og.rev 4.3.4.0 (2008/12/01) GE20(ユーザー定数)へ登録するかã?フラグを追åŠ?
300             *
301             */
302            private void setSQLAttribute() {
303                    if( table == null || table.getRowCount() == 0 ) { return ; }
304    
305                    int row    = 0;
306                    int clmCnt = table.getColumnCount();
307                    for( int clm=0; clm<clmCnt; clm++ ) {
308                            String clmkey = table.getColumnName( clm );
309                            String clmval = table.getValue( row,clm );
310    //                      setUserInfo( clmkey,clmval );
311                            setUserInfo( clmkey, clmval, save );
312                    }
313            }
314    
315            /**
316             * 【TAG】コマンãƒ?SET,GET,NVLEGT,REMOVE,SQL)をセãƒ?ƒˆしまã�?初期値:SET)ã€?
317             *
318             * @og.tag
319             * コマンドã?,HTMLから(get/post)æŒ?®šされますã?で,CMD_xxx で設定される
320             * フィールド定数値のã�?�šれかをã?æŒ?®šできますã?
321             * 何も設定されなã�??またã?、null の場合ã?ã€?SET" がå?期å?にセãƒ?ƒˆされますã?
322             * SQL の場合ã?検索結果の戻りå?がè¤?•°存在する場合ã?、最初ã??‘件目のみ使用しますã?
323             *
324             * @param       cmd コマンãƒ?public static final 宣è¨?�•れてã�?‚‹æ–?­—å?)
325             * @see         <a href="{&#064;docRoot}/constant-values.html#org.opengion.hayabusa.taglib.UserInfoTag.CMD_SET">コマンド定数</a>
326             */
327            public void setCommand( final String cmd ) {
328                    String cmd2 = getRequestParameter( cmd );
329                    if( cmd2 != null && cmd2.length() > 0 ) { command = cmd2.toUpperCase(Locale.JAPAN); }
330            }
331    
332            /**
333             * 【TAG】UserInfo に登録するキーをセãƒ?ƒˆしまã�?予ç´?ª?JNAME,ID,PASSWD,IDNO,INFO,LANG,ROLE,IPADDRESS,LOGINTIME)ã€?
334             *
335             * @og.tag UserInfo に登録するキーをセãƒ?ƒˆしますã?
336             *
337             * @param       inkey UserInfo に登録するキー
338             */
339            public void setKey( final String inkey ) {
340                    key = getRequestParameter( inkey ).toUpperCase(Locale.JAPAN);
341                    if( key.startsWith( "USER." ) ) {
342                            key = key.substring( 5 );
343                    }
344            }
345    
346            /**
347             * 【TAG】UserInfo に登録する値をセãƒ?ƒˆしますã?
348             *
349             * @og.tag UserInfo に登録する値をセãƒ?ƒˆしますã?
350             *
351             * @param       val     UserInfoに登録する値
352             */
353            public void setValue( final String val ) {
354                    value = getRequestParameter( val );
355            }
356    
357            /**
358             * 【TAG】UserInfo に登録した値を永続化するかをæŒ?®šしますã?
359             *
360             * @og.tag
361             * UserInfo に登録した値を永続化するかをæŒ?®šしますã?
362             * trueが指定された場合ã?UserInfoに設定された値は、GE20(ユーザー定数)に保存されã?
363             * UserInfoがå?作æ?されるタイミングで自動的に復å…?�•れますã?
364             * ここで、登録されたå?は、そのユーザーの全てのロールに対して有効となりますã?
365             * 初期値は、false(永続化しなã�?ですã?
366             *
367             * @param       sv      登録値を永続化するã�?
368             */
369            public void setSave( final String sv ) {
370                    save = nval( getRequestParameter( sv ),save );
371            }
372    
373    //      /**
374    //       * 【å»?­¢】Query を実行して、value に値をセãƒ?ƒˆするかどã�?�‹を指定しますã?
375    //       *
376    //       * @og.tag
377    //       * 初期値は、実行しなã�?("false")
378    //       *
379    //       * @og.rev 3.1.0.1 (2003/03/26) query 属æ?を非推奨属æ?としますã?å†?ƒ¨では使用しませんã€?
380    //       *
381    //       * @param   flag Query を実行すã‚?("true")?�実行しなã�?それ以å¤?
382    //       * @deprecated
383    //       */
384    //      @Deprecated public void setQuery( final String flag ) {
385    //              // ここでは処ç�?‚’行いませんã€?
386    //      }
387    
388    //      /**
389    //       * 【å»?­¢】value ã�?NULL の時に、設定するかどã�?�‹[true/false]を指定しまã�?初期値:true)ã€?
390    //       *
391    //       * @og.tag
392    //       * true の場合ã?, null のときでもセãƒ?ƒˆしますã?
393    //       * false の場合ã?, null のときã?、既存ã?値を置き換えませんã€?
394    //       * 初期値は、null のときでもセãƒ?ƒˆするですã? ("true")
395    //       *
396    //       * @param   flag NULL の時に、設定すã‚?("true")?�設定しなã�?それ以å¤?
397    //       * @deprecated
398    //       */
399    //      @Deprecated public void setNullSet( final String flag ) {
400    //              isNullSet = nval( getRequestParameter( flag ),isNullSet );
401    //      }
402    
403            /**
404             * 【TAGã€?通常は使ã�?�¾せん)Queryオブジェクトを作æ?する時ã?DB接続IDを指定しますã?
405             *
406             * @og.tag Queryオブジェクトを作æ?する時ã?DB接続IDを指定しますã?
407             *
408             * @param       id ãƒ??タベã?ス接続ID
409             */
410            public void setDbid( final String id ) {
411                    dbid = nval( getRequestParameter( id ),dbid );
412            }
413    
414            /**
415             * シリアライズ用のカスタãƒ?‚·リアライズ書き込みメソãƒ?ƒ‰
416             *
417             * @og.rev 4.0.0.0 (2006/09/31) 新規追åŠ?
418             * @serialData
419             *
420             * @param       strm    ObjectOutputStreamオブジェクãƒ?
421             */
422            private void writeObject( final ObjectOutputStream strm ) throws IOException {
423                    strm.defaultWriteObject();
424            }
425    
426            /**
427             * シリアライズ用のカスタãƒ?‚·リアライズ読み込みメソãƒ?ƒ‰
428             *
429             * ここでは、transient 宣è¨?�•れたå†?ƒ¨変数のå†??初期化がå¿?¦�なフィールドã?み設定しますã?
430             *
431             * @og.rev 4.0.0.0 (2006/09/31) 新規追åŠ?
432             * @serialData
433             *
434             * @param       strm    ObjectInputStreamオブジェクãƒ?
435             * @see #release2()
436             */
437            private void readObject( final ObjectInputStream strm ) throws IOException , ClassNotFoundException {
438                    strm.defaultReadObject();
439            }
440    
441            /**
442             * こã?オブジェクトã?æ–?­—å?表現を返しますã?
443             * 基本çš?�«ãƒ?ƒ�ãƒ?‚°目çš?�«使用しますã?
444             *
445             * @return こã?クラスのæ–?­—å?表現
446             */
447            @Override
448            public String toString() {
449                    return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
450                                    .println( "VERSION"                     ,VERSION        )
451                                    .println( "command"                     ,command        )
452                                    .println( "key"                         ,key            )
453                                    .println( "value"                       ,value          )
454    //                              .println( "isNullSet"           ,isNullSet      )
455                                    .println( "dbid"                        ,dbid           )
456                                    .println( "sql"                         ,sql            )
457                                    .println( "save"                        ,save           )
458                                    .println( "COMMAND_LIST"        ,COMMAND_LIST   )
459                                    .println( "Other..."    ,getAttributes().getAttribute() )
460                                    .fixForm().toString() ;
461            }
462    }