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.develop;
017    
018    import java.util.regex.Matcher;
019    import java.util.regex.Pattern;
020    import java.util.Locale;
021    
022    /**
023     * JSPã®ç”Ÿæ?・ãƒ??ã‚¿å–り込ã¿å‡¦ç?§å¿?¦ãªåˆ—挙型をã¾ã¨ã‚ãŸã‚¯ãƒ©ã‚¹ã€?
024     *
025     * 主ã«ã‚­ãƒ¼ãƒ¯ãƒ¼ãƒ‰ç®¡ç?¨ãƒ—ログラãƒ?¸­ã®switchæ–??削減を目çš?¨ã—ã¦ä½œæ?ã€?
026     *
027     *
028     * @author Takeshi.Takada
029     *
030     */
031    public class JspEnumeration {
032            /**
033             * GROUP BYå¥ã‚’å¿?¦ã¨ã™ã‚‹é–¢æ•°ã‚’å?挙ã—ã¾ã™ã?
034             *
035             * containsã€searchã¨è¨?£ãŸç‹¬è‡ªãƒ¡ã‚½ãƒ?ƒ‰ã‚‚実è£?—ã¦ã?¾ã™ã?
036             *
037             */
038    //      protected static enum GROUPING_FUNCTIONS { MAX , MIN , SUM , COUNT , max , min , sum , count ;
039            public static enum GROUPING_FUNCTIONS { MAX , MIN , SUM , COUNT  ;
040    
041                    /**
042                     * 与ãˆã‚‰ã‚ŒãŸæ–‡å­—ãŒè‡ªèº«ã«åˆ—挙ã•れãŸå?ã®ä½•れã‹ã¨ä¸??ã™ã‚‹ã‹æ¤œè¨¼ã™ã‚‹ã€?
043                     * ä¸??ã™ã‚‹å ´åˆã«çœŸã‚’è¿”ã™ã€?
044                     * ä¸??ã—ãªã??åˆã«å½ã‚’è¿”ã™ã€?
045                     *
046                     * @param arg   引数
047                     * @return      検証ã®çµæžœ
048                     */
049                    public static boolean contains( final String arg ) {
050                            for( GROUPING_FUNCTIONS fnc : values() ){
051    //                              if ( arg.equals( fnc.toString() ) ){
052                                    if ( arg.equalsIgnoreCase( fnc.toString() ) ){
053                                            return true;
054                                    }
055                            }
056                            return false;
057                    }
058    
059                    /**
060                     * 自身ã«åˆ—挙ã•れã¦ã?‚‹å€¤ãŒä¸Žãˆã‚‰ã‚ŒãŸæ–‡å­—å?ã«å«ã¾ã‚Œã¦ã?‚‹ã‹æ¤œè¨¼ã™ã‚‹ã€?
061                     * å«ã¾ã‚Œã¦ã?‚‹å ´åˆã?真を返ã™ã€?
062                     * å«ã¾ã‚Œã¦ã?ªã??åˆã?å½ã‚’è¿”ã™ã€?
063                     *
064                     * @param arg   引数
065                     * @return      検証ã®çµæžœ
066                     */
067                    public static boolean search( final String arg ) {
068                            String argU = arg.toUpperCase(Locale.JAPAN);
069    
070                            for( GROUPING_FUNCTIONS fnc : values() ){
071    //                              if( arg.indexOf( fnc.toString() ) > -1 ) {
072                                    if( argU.indexOf( fnc.toString() ) > -1 ) {
073                                            return true;
074                                    }
075                            }
076                            return false;
077                    }
078            }
079    
080            /**
081             * ãƒ??タ上ã?ãŸã ã®æ–?­—å?ã¨ã—ã¦æ‰±ã?–¢æ•°ã‚’å?挙ã—ã¾ã™ã?
082             * (注?šç¾åœ¨ã€å?挙中ã®é–¢æ•°ã¯Oracleã®å†?®¹ã§ã™ã?)
083             *
084             */
085            public static enum TREATS_STRING_FUNCTIONS {
086                    CASE ,
087                    CEIL , ROUND , FLOOR , TRUNC , MOD , CHR , CONCAT , SUBSTR , INITCAP ,
088                    SUBSTRB , LOWER , TRIM , LPAD   , LTRIM , UPPER , REPLACE , USER , RPAD ,
089                    ASCII , LENGTH , LENGTHB , INSTR , POSITION , INSTRB  , ADD_MONTHS , DAYOFMONTH ,
090                    MONTHNAME , TIMESTAMPADD , CURDATE , DAYOFWEEK , MONTHS_BETWEEN  , TIMESTAMPDIFF ,
091                    CURRENT_DATE  , DAYOFYEAR , NEXT_DAY  , CURRENT_TIME  , HOUR , NOW , WEEK , CURRENT_TIMESTAMP  ,
092                    LAST_DAY  , YEAR , CURTIME , MINUTE , SECOND , DAYNAME , MONTH , SYSDATE  , CAST , AVG   ,
093                    CONVERT , DATABASE  , TO_CHAR  , DECODE , TO_NUMBER  , EXTRACT , TO_DATE  , GREATEST , STDDEV ,
094                    INTERVAL , VARIANCE , LEAST , LOCATE , NVL  ;
095    
096                    /**
097                     * 関数ã®å†?®¹ã«ç¬¬äºŒå¼•æ•°ã®å†?®¹ã‚’付加ã™ã‚‹å‡¦ç?‚’実è£?—ã¾ã?
098                     * 第二引数ã®å†?®¹
099                     * 0:カラãƒ?¸ä»˜ä¸Žã™ã‚‹ãƒ†ãƒ¼ãƒ–ルå?ãƒ??ブル別å?
100                     *
101                     * @param column String
102                     * @param args String[]
103                     * @return      関数を更新ã—ãŸçµæžœ
104                     */
105                    public String update( final String column , final String[] args ) {
106                            return column;
107                    }
108    
109            }
110    
111            /**
112             * 演算å­ã‚’列挙ã™ã‚‹ã€?
113             *
114             * â—使用ä¾?
115             * WHERE_OPERATORS op = WHERE_OPERATORS.valueOf("eq");
116             * System.out.println(op.apply("GF92.CLM","{@CLM}",false));
117             *
118             * â—上記å?ç?µæžœ
119             * GF92.CLM     =       '{@CLM}'
120             *
121             */
122            public static enum WHERE_OPERATORS {
123                    eq() {
124                            public String apply(final String left , final String right, final boolean is_num) {
125                                    if ( is_num ){
126                                            return leftVal(left) + "=\t " + right ;
127                                    }else {
128                                            return leftVal(left) + "=\t '" + right + "'";
129                                    }
130                            }
131                            public String[] symbol(){
132                                    return new String[] {"="};
133                            }
134                    }  ,
135                    lk1() {
136                            public String apply(final String left , final String right, final boolean is_num) {
137                                    return leftVal(left) + "like '" + right + "%'";
138                            }
139                            public String[] symbol(){
140                                    return new String[] {"like","","%"};
141                            }
142                    }  ,
143                    lk2() {
144                            public String apply(final String left , final String right, final boolean is_num) {
145                                    return leftVal(left) + "like '%" + right + "'";
146                            }
147                            public String[] symbol(){
148                                    return new String[] {"like","%",""};
149                            }
150                    }  ,
151                    lk3() {
152                            public String apply(final String left , final String right, final boolean is_num) {
153                                    return leftVal(left) + "like '%" + right + "%'";
154                            }
155                            public String[] symbol(){
156                                    return new String[] {"like","%","%"};
157                            }
158                    }  ,
159                    gt() {
160                            public String apply(final String left , final String right, final boolean is_num) {
161                                    if ( is_num ) {
162                                            return leftVal(left) + ">\t " + right + "";
163                                    }else{
164                                            return leftVal(left) + ">\t '" + right + "'";
165                                    }
166                            }
167                            public String[] symbol(){
168                                    return new String[] {">"};
169                            }
170                    }  ,
171                    ge() {
172                            public String apply(final String left , final String right, final boolean is_num) {
173                                    if ( is_num ) {
174                                            return leftVal(left) + ">=\t " + right + "";
175                                    }else{
176                                            return leftVal(left) + ">=\t '" + right + "'";
177                                    }
178                            }
179                            public String[] symbol(){
180                                    return new String[] {">="};
181                            }
182                    }  ,
183                    lt()  {
184                            public String apply(final String left , final String right, final boolean is_num) {
185                                    if ( is_num ) {
186                                            return leftVal(left) + "<\t " + right + "";
187                                    }else {
188                                            return leftVal(left) + "<\t '" + right + "'";
189                                    }
190                            }
191                            public String[] symbol(){
192                                    return new String[] {"<"};
193                            }
194                    }  ,
195                    le()  {
196                            public String apply(final String left , final String right, final boolean is_num) {
197                                    if ( is_num ){
198                                            return leftVal(left) + "<=\t " + right + "";
199                                    }else{
200                                            return leftVal(left) + "<=\t '" + right + "'";
201                                    }
202                            }
203                            public String[] symbol(){
204                                    return new String[] {"<="};
205                            }
206                    }  ,
207                    not() {
208                            public String apply(final String left , final String right, final boolean is_num) {
209                                    if ( is_num ) {
210                                            return leftVal(left) + "!=\t " + right + "";
211                                    } else {
212                                            return leftVal(left) + "!=\t '" + right + "'";
213                                    }
214                            }
215                            public String[] symbol(){
216                                    return new String[] {"!="};
217                            }
218                    } ,
219                    bw()  {
220                            public String apply(final String left , final String right, final boolean is_num) {
221                                    if ( is_num ) {
222                                            return leftVal(left) + "between " + betweenFormat( right , "_FROM" ) + " and " + betweenFormat( right , "_TO" ) + "";
223                                    }else {
224                                            return leftVal(left) + "between '" + betweenFormat( right , "_FROM" ) + "' and '" + betweenFormat( right , "_TO" ) + "'";
225                                    }
226                            }
227                            public String[] symbol(){
228                                    return new String[] {"between"};
229                            }
230                    } ,
231                    in() {
232                            public String apply(final String left , final String right, final boolean is_num) {
233                                    return leftVal(left) + "in\t (" +inFormat( right , is_num ) + ")";
234                            }
235                            public String[] symbol(){
236                                    return new String[] {"in"};
237                            }
238                    } ,
239                    ;
240    
241    //              static final String[] TABS = new String[] { "\t\t\t" , "\t\t" , "\t" };
242                    static final String[] TABS = new String[] { "\t\t\t\t" , "\t\t\t" , "\t\t" , "\t" };            // 5.6.4.4 (2013/05/31)
243    
244                    /**
245                     * 演算å­ã?記å·ã‚’略語ã«å¤‰æ›ã™ã‚‹ã€?
246                     *
247                     * @og.rev 5.6.4.4 (2013/05/31) タブã«ã‚ˆã‚‹ä½ç½®åˆã‚ã›ã?計算方法修正ã€?
248                     *
249                     * @param left  引数
250                     * @return      演算å­ã?記å·ã®ç•¥èª?
251                     */
252                    static String leftVal( final String left ) {
253    //                      return left + TABS[Math.max( left.length()/4, TABS.length-1 )] ;                // ?”タブを想定ã?
254    
255                            int adrs = ((left.length()-1)/4 > 3 ) ? 3 : (left.length()-1)/4 ;
256                            return left + TABS[adrs] ;                                                                                              // ?”タブを想定ã?
257                    }
258    
259                    /**
260                     * 与ãˆã‚‰ã‚ŒãŸå·¦è¾ºã¨å³è¾ºã‚’å?ã«æ¼”ç®—å­ä»˜ãã®æ–?­—å?を作æ?ã™ã‚‹ã€?
261                     * 第?“引数ã®booleanã¯ã€trueã®æ™‚ã«å€¤ãŒæ•°å€¤ã§ã‚ã‚‹ã“ã¨ã‚’æ„味ã™ã‚‹ã€?
262                     *
263                     * @param left String
264                     * @param right String
265                     * @param is_num boolean
266                     * @return      演算å­ã‚’åŠ?ˆãŸçµæžœ
267                     */
268                    abstract public String apply(final String left , final String right , final boolean is_num);
269    
270                    /**
271                     * 演算å­ã‚’è¿”å´ã™ã‚‹ã€?
272                     *
273                     * @return String[] 演算�
274                     */
275                    abstract public String[] symbol();
276    
277                    /**
278                     * INå¥ã®å€¤ã‚’çµ?¿ç«‹ã¦ãªãŠã—ã¾ã™ã?
279                     *
280                     * @param str String
281                     * @param is_number boolean
282                     * @return      INå¥ã®ãƒ•ォーマッãƒ?
283                     */
284                    static String inFormat(final String str , final boolean is_number){
285                            StringBuilder formated = new StringBuilder("");
286                            String[] ins = str.split( "," );
287                            for (String in :ins ){
288                                    if (formated.length() > 0 ){
289                                            formated.append( "," );
290                                    }
291                                    if ( is_number ) {
292                                            formated.append( in );
293                                    }else{
294                                            formated.append( "'" ).append( in ).append( "'" );
295                                    }
296                            }
297    
298                            return formated.toString();
299    
300                    }
301    
302                    /**
303                     * BETWEENã‚’çµ?¿ç«‹ã¦ãªãŠã™ã€?
304                     *
305                     * @param str String
306                     * @param suffix String
307                     * @return      BETWEENã®ãƒ•ォーマッãƒ?
308                     */
309                    static String betweenFormat(final String str , final String suffix){
310                            StringBuilder sb = new StringBuilder(str);
311                            if ( str.indexOf("{@") == 0 ){
312                                    sb.insert( sb.length() - 1 , suffix );
313                            }else{
314                                    sb.append( suffix );
315                            }
316                            return sb.toString();
317                    }
318    
319                    static Pattern LK1_PTN = Pattern.compile("like\\s+\\'\\{@(\\w*?)\\}%\\'");
320                    static Pattern LK2_PTN = Pattern.compile("like\\s+\\'%\\{@(\\w*?)\\}\\'");
321                    static Pattern LK3_PTN = Pattern.compile("like\\s+\\'%\\{@(\\w*?)\\}%\\'");
322    
323                    /**
324                     * 演算å­ã?記å·ã‚’略語ã«å¤‰æ›ã™ã‚‹ã€?
325                     *
326                     * @param arg   引数
327                     * @return      演算å­ã?記å·ã®ç•¥èª?
328                     */
329                    static String convert(final String arg){
330                            for( WHERE_OPERATORS fnc : values() ){
331                                    if ( fnc.symbol().length == 1 && arg.trim().indexOf( fnc.symbol()[0] ) == 0 ){
332                                            return fnc.toString();
333                                    }
334                                    if( fnc.symbol().length == 3){
335    //                                      Matcher matcher = Pattern.compile("like\\s+\\'\\{@(\\w*?)\\}%\\'").matcher( arg );
336                                            Matcher matcher = LK1_PTN.matcher( arg );
337                                            if (matcher.find()){
338                                                    return lk1.toString();
339                                            }
340    //                                      matcher = Pattern.compile("like\\s+\\'%\\{@(\\w*?)\\}\\'").matcher( arg );
341                                            matcher = LK2_PTN.matcher( arg );
342                                            if (matcher.find()){
343                                                    return lk2.toString();
344                                            }
345    //                                      matcher = Pattern.compile("like\\s+\\'%\\{@(\\w*?)\\}%\\'").matcher( arg );
346                                            matcher = LK3_PTN.matcher( arg );
347                                            if (matcher.find()){
348                                                    return lk3.toString();
349                                            }
350                                    }
351                            }
352                            return "";
353                    }
354            }
355    
356    }