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.fukurou.util.Attributes;
020    
021    import java.util.List;
022    import java.util.ArrayList;
023    
024    /**
025     * and ã‚¿ã‚°ã§æŒ?®šã•ã‚ŒãŸæ¡ä»¶ã‚’å?ã«ã€WHEREå¥ã‚’作æ?ã—ã¾ã™ã?
026     *
027     * å?±žæ?ã¯ã€{@XXXX} 変数ãŒä½¿ç”¨ã§ãã¾ã™ã?
028     * ã“れã¯ã€ServletRequest ã‹ã‚‰ã€XXXX をキーã«å€¤ã‚’å–りå?ã?ã“ã?変数ã«
029     * 割り当ã¦ã¾ã™ã?ã¤ã¾ã‚Šã?ã“ã?XXXXをキーã«ãƒªã‚¯ã‚¨ã‚¹ãƒˆã™ã‚Œã?ã€?
030     * ã“ã?変数ã«å€¤ã‚’ã‚»ãƒ?ƒˆã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã?
031     *
032     * @og.formSample
033     * â—å½¢å¼ï¼?lt;og:where startKey="…" > <og:and startKey="…" ... /> </og:where>
034     * â—body?šã‚ã‚?EVAL_BODY_INCLUDE:BODYをインクルードã—ã€{@XXXX} ã¯è§£æžã—ã¾ã›ã‚“)
035     *
036     * â—Tag定義??
037     *   <og:where
038     *       startKey           ã€TAG】SQLæ¡ä»¶å¥ã®æœ??ã®æ¼”ç®—å­ã‚’æŒ?®šã—ã¾ã?åˆæœŸå€¤:where)
039     *       debug              ã€TAG】デãƒãƒƒã‚°æƒ??ã‚’å?力ã™ã‚‹ã‹ã©ã?‹[true/false]を指定ã—ã¾ã?åˆæœŸå€¤:false)
040     *   >   ... Body ...
041     *   </og:where>
042     *
043     * â—使用ä¾?
044     *     <og:query command="NEW">
045     *             select PN,YOBI,NMEN,HINM from XX01 where PN = '{@PN}' order by PN
046     *     </og:query>
047     *
048     *          ・検索æ¡ä»¶ãŒå?力ã•ã‚ŒãŸæ™?{@PN}ãŒNOT NULLã®ã¨ã?
049     *            作æ?ã•れるSQLæ–?çselect PN,YOBI,NMEN,HINM from XX01 where PN = 'AAA' order by PN
050     *          ・検索æ¡ä»¶ãŒå?力ã•れãªã‹ã£ãŸæ™‚({@PN}ãŒNULLã®ã¨ã?
051     *            作æ?ã•れるSQLæ–?çselect PN,YOBI,NMEN,HINM from XX01 where PN = '' order by PN
052     *
053     *     <og:query command="NEW">
054     *             select PN,YOBI,NMEN,HINM from XX01
055     *         <og:where>
056     *             <og:and value="PN = '{@PN}%'" />
057     *             <og:and value="YOBI like '{@YOBI}%'" />
058     *         </og:where>
059     *             order by PN
060     *     </og:query>
061     *
062     *          ・検索æ¡ä»¶ãŒå?力ã•ã‚ŒãŸæ™?{@PN}ãŒNOT NULLã®ã¨ã?
063     *            作æ?ã•れるSQLæ–?çselect PN,YOBI,NMEN,HINM from XX01 PN = 'AAA%' and YOBI like 'BBB%' order by PN
064     *          ・検索æ¡ä»¶ãŒå?力ã•れãªã‹ã£ãŸæ™‚({@PN}ãŒNULLã®ã¨ã? WHEREå¥ãŒãªããªã‚‹ã?
065     *            作æ?ã•れるSQLæ–?çselect PN,YOBI,NMEN,HINM from XX01 order by PN
066     *
067     *        注æ„?WhereTagを使ã£ãŸå?åˆã?下ã?よã†ã«ã¯ãªã‚Šã¾ã›ã‚“ã€?
068     *            select PN,YOBI,NMEN,HINM from XX01 PN = '' and YOBI like '%' order by PN
069     *
070     *     <og:query command="NEW">
071     *             select PN,YOBI,NMEN,HINM from XX01 where PN="11111"
072     *         <og:where startKey="and">
073     *             <og:and value="YOBI like '{@PN}%'" />
074     *         </og:where>
075     *             order by PN
076     *     </og:query>
077     *
078     *          ・検索æ¡ä»¶ãŒå?力ã•ã‚ŒãŸæ™?{@YOBI}ãŒNOT NULLã®ã¨ã?
079     *            作æ?ã•れるSQLæ–?çselect PN,YOBI,NMEN,HINM from XX01 PN = '11111' and YOBI like 'BBB%' order by PN
080     *          ・検索æ¡ä»¶ãŒå?力ã•れãªã‹ã£ãŸæ™‚({@YOBI}ãŒNULLã®ã¨ã? WHEREå¥ãŒãªããªã‚‹ã?
081     *            作æ?ã•れるSQLæ–?çselect PN,YOBI,NMEN,HINM from XX01 PN = '11111' order by PN
082     *
083     * @og.group ç”»é¢éƒ¨å“?
084     *
085     * @version  4.0
086     * @author       Kazuhiko Hasegawa
087     * @since    JDK5.0,
088     */
089    public class SqlWhereTag extends CommonTagSupport {
090            //* ã“ã?プログラãƒ??VERSIONæ–?­—å?を設定ã—ã¾ã™ã?       {@value} */
091            private static final String VERSION = "4.0.0.0 (2005/01/31)" ;
092    
093            private static final long serialVersionUID = 400020050131L ;
094    
095            private transient List<Attributes>        values          = null;
096    
097            private String  startKey        = "where";
098            private String  whereWord       = "";
099    
100            /**
101             * Taglibã®é–‹å§‹ã‚¿ã‚°ãŒè¦‹ã¤ã‹ã£ãŸã¨ãã«å‡¦ç?™ã‚?doStartTag() ã‚?オーãƒã?ライドã—ã¾ã™ã?
102             *
103             * @return      後続å?ç??æŒ?¤º( EVAL_BODY_INCLUDE )
104             */
105            @Override
106            public int doStartTag() {
107                    return( EVAL_BODY_INCLUDE );    // Body インクルー� extends TagSupport �
108            }
109    
110            /**
111             * Taglibã®çµ‚äº?‚¿ã‚°ãŒè¦‹ã¤ã‹ã£ãŸã¨ãã«å‡¦ç?™ã‚?doEndTag() ã‚?オーãƒã?ライドã—ã¾ã™ã?
112             *
113             * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応ã?release2() ã‚?doEndTag()ã§å‘¼ã¶ã€?
114             *
115             * @return      後続å?ç??æŒ?¤º
116             */
117            @Override
118            public int doEndTag() {
119                    debugPrint();           // 4.0.0 (2005/02/28)
120                    jspPrint( makeSqlWhere() );
121    
122                    return(EVAL_PAGE);
123            }
124    
125            /**
126             * タグリブオブジェクトをリリースã—ã¾ã™ã?
127             * キャãƒ?‚·ãƒ¥ã•れã¦å†åˆ©ç”¨ã•れるã?ã§ã€ãƒ•ィールドã?åˆæœŸè¨­å®šã‚’行ã„ã¾ã™ã?
128             *
129             * @og.rev 2.0.0.4 (2002/09/27) カスタãƒ?‚¿ã‚°ã® release() メソãƒ?ƒ‰ã‚’ã?追åŠ?
130             * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応ã?release2() ã‚?doEndTag()ã§å‘¼ã¶ã€?
131             *
132             */
133            @Override
134            protected void release2() {
135                    super.release2();
136                    values          = null;
137                    startKey        = "where";
138                    whereWord       = "";
139            }
140    
141            /**
142             * å†?ƒ¨ã‚¿ã‚°ã® SqlAndTag / SqlAndTag よりã€å?ã€??カラãƒ??å€¤ã‚’æ›¸ãæ›ãˆã‚‹
143             * 為ã®ã‚¢ãƒˆãƒªãƒ“ュートをå—ã‘å–ã‚‹ã€?
144             *
145             * è¤?•°ã®å€¤ã‚’å—ã‘å–ã£ã¦ã€å¾Œã»ã©ã€ã™ã¹ã¦ã®ã‚«ãƒ©ãƒ?«å¯¾ã—ã¦å‡¦ç?‚’行ã„ã¾ã™ã?
146             *
147             * @og.rev 3.1.0.0 (2003/03/20) Vector を使用ã—ã¦ã?‚‹ç®?‰€ã§ã€?žåŒæœŸã§ã‚‚æ§‹ã‚ãªã?®?‰€ã‚’ã?ArrayList ã«ç½®æ›ãˆã€?
148             * @og.rev 3.1.2.0 (2003/04/07) taglib パッケージå†?ƒ¨ã§ä½¿ç”¨ã—ã¦ã?‚‹ç®?‰€ã‚?protected 化ã™ã‚‹ã?
149             *
150             * @param   attri       属æ?リスãƒ?
151             */
152            protected void setAttributes( final Attributes attri ) {
153                    if( values == null ) { values = new ArrayList<Attributes>(); }
154                    values.add( attri );
155            }
156    
157            /**
158             * ã€TAG】SQLæ¡ä»¶å¥ã®æœ??ã®æ¼”ç®—å­ã‚’æŒ?®šã—ã¾ã?åˆæœŸå€¤:where)ã€?
159             *
160             * @og.tag SQLæ¡ä»¶å¥ã®æœ??ã®æ¼”ç®—å­ã‚’æŒ?®šã—ã¾ã™ã?
161             *
162             * @param       skey æ¡ä»¶å¥ã®æœ??ã®æ¼”ç®—å­?
163             */
164            public void setStartKey( final String skey ) {
165                    String key = getRequestParameter( skey );
166                    if( key != null && key.length() > 0 ) { startKey = key; }
167            }
168    
169            /**
170             * SqlAndTag / SqlOrTag ã§æŒ?®šã•ã‚ŒãŸæ¡ä»¶ã‚’å?ã«ã€WHEREå¥ã‚’作æ?ã—ã¾ã™ã?
171             *
172             * @return      WHEREå¥ã®æ–?­—å?
173             */
174            private String makeSqlWhere() {
175                    StringBuilder rtn = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
176                    if( values != null ) {
177                            for( int i=0; i<values.size(); i++ ) {
178                                    Attributes attri = values.get( i );
179                                    String keyWord = attri.get( "keyWord" );
180                                    String value   = attri.get( "value" );
181                                    if( rtn.length() == 0 ) { keyWord = this.startKey; }
182                                    rtn.append( " " );
183                                    rtn.append( keyWord );
184                                    rtn.append( " " );
185                                    rtn.append( value );
186                            }
187                    }
188                    rtn.append( " " );
189                    whereWord = rtn.toString();
190                    return whereWord;
191            }
192    
193            /**
194             * ã‚¿ã‚°ã®åç§°ã‚’ã?è¿”ã—ã¾ã™ã?
195             * 自åˆ??身ã®ã‚¯ãƒ©ã‚¹åよりã?自動的ã«å–り出ã›ãªã?Ÿã‚ã?ã“ã?メソãƒ?ƒ‰ã‚’オーãƒã?ライドã—ã¾ã™ã?
196             *
197             * @og.rev 4.0.0.0 (2005/01/31) æ–°è¦è¿½åŠ?
198             *
199             * @return  ã‚¿ã‚°ã®åç§°
200             */
201            @Override
202            protected String getTagName() {
203                    return "where" ;
204            }
205    
206            /**
207             * ã“ã?オブジェクトã?æ–?­—å?表ç¾ã‚’è¿”ã—ã¾ã™ã?
208             * 基本çš?«ãƒ?ƒãƒ?‚°ç›®çš?«ä½¿ç”¨ã—ã¾ã™ã?
209             *
210             * @return ã“ã?ã‚¯ãƒ©ã‚¹ã®æ–?­—å?表ç¾
211             */
212            @Override
213            public String toString() {
214                    return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
215                                    .println( "VERSION"                     ,VERSION        )
216                                    .println( "startKey"            ,startKey       )
217                                    .println( "whereWord"           ,whereWord      )
218                                    .println( "Other..."    ,getAttributes().getAttribute() )
219                                    .fixForm().toString() ;
220            }
221    }