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 */
016package org.opengion.hayabusa.taglib;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.fukurou.util.Attributes;
020
021import java.util.List;
022import java.util.ArrayList;
023
024/**
025 * and タグで指定された条件を元に、WHERE句を作成します。
026 *
027 * 各属性は、{@XXXX} 変数が使用できます。
028 * これは、ServletRequest から、XXXX をキーに値を取り出し,この変数に
029 * 割り当てます。つまり、このXXXXをキーにリクエストすれば、
030 * この変数に値をセットすることができます。
031 *
032 * @og.formSample
033 * ●形式:<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 */
089public 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}