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.plugin.table;
017
018import org.opengion.hayabusa.db.AbstractTableFilter;
019import org.opengion.hayabusa.db.DBTableModel;
020
021import org.opengion.fukurou.util.ErrorMessage;
022import org.opengion.fukurou.util.StringUtil;
023
024/**
025 * TableFilter_CLM_LIKE は、TableFilter インターフェースを継承した、DBTableModel 処理用の
026 * 実装クラスです。
027 *
028 * ここでは、DBのLIKE に似た動きで、データを絞ります。
029 *
030 * vals で指定した値の"%" 記号を、前、後ろ、前後 なし 、で判断します。
031 * DBの like と異なるのは、文字列の間の "%" と、1文字あいまいの"_" は判定しません。
032 *
033 * 新しくテーブルを作成するのではなく、既存のテーブルのデータを物理削除しますので、ご注意ください。
034 *
035 * パラメータは、tableFilterタグの keys, vals にそれぞれ記述するか、BODY 部にCSS形式で記述します。
036 *
037 * @og.formSample
038 * ●形式:
039 *      ① <og:tableFilter classId="CLM_LIKE" keys="CLM1,CLM2..." vals="判定値1,判定値2..." />
040 *
041 *      ② <og:tableFilter classId="CLM_LIKE" >
042 *               {
043 *                   CLM1   : 判定値1   ;
044 *                   CLM2   : 判定値2   ;
045 *                   ・・・    : ・・・   ;
046 *               }
047 *         </og:tableFilter>
048 *
049 * @og.rev 8.2.1.1 (2022/07/19) 新規追加
050 *
051 * @version  8.2 (2022/07/19)
052 * @author   Kazuhiko Hasegawa
053 * @since    JDK1.8,
054 */
055public class TableFilter_CLM_LIKE extends AbstractTableFilter {
056        /** このプログラムのVERSION文字列を設定します。   {@value} */
057        private static final String VERSION = "8.2.1.1 (2022/07/19)" ;
058
059        /**
060         * デフォルトコンストラクター
061         *
062         * @og.rev 8.2.1.1 (2022/07/19) 新規追加
063         */
064        public TableFilter_CLM_LIKE() {
065                super();
066        }
067
068        /**
069         * DBTableModel処理を実行します。
070         *
071         * @og.rev 8.2.1.1 (2022/07/19) 新規追加
072         *
073         * @return 処理結果のDBTableModel
074         */
075        public DBTableModel execute() {
076                final DBTableModel table = getDBTableModel();
077
078                final String[] keys = getKeys();                        // 判定対象のカラム名の配列
079                final int      len  = keys.length;
080                final int[]    clmNo = new int[len];            // カラムの番号
081
082                final String[] vals = new String[len];          // 判定値
083                final int[]    like = new int[len];                     // 判定方法(-1:判定しない、0:そのまま、1:先頭一致、2:後方一致、3:前後曖昧
084
085                for( int i=0; i<len; i++ ) {
086                        clmNo[i] = table.getColumnNo( keys[i],false );  // カラムが存在しなければ、-1
087                        final String val =  StringUtil.nval( getValue( keys[i] ) , null );      // なければ、null
088                        if( clmNo[i] < 0 || val == null )       { like[i] = -1; }       // -1:判定しない
089                        else {
090                                final char chS = val.charAt(0);                                         // 先頭文字
091                                final char chE = val.charAt(val.length()-1);            // 終端文字
092                                if( '%' == chS && '%' == chE )  { like[i] = 3; vals[i] = val.substring(1,val.length()-1); }     // 3:前後曖昧
093                                else if( '%' == chS )                   { like[i] = 2; vals[i] = val.substring(1); }                            // 2:後方一致
094                                else if( '%' == chE )                   { like[i] = 1; vals[i] = val.substring(0,val.length()-1); }     // 1:先頭一致
095                                else                                                    { like[i] = 0; vals[i] = val; }                                                         // 0:そのまま
096                        }
097                }
098
099                final int rowCnt = table.getRowCount();
100
101                // 不一致の場合は、レコードを削除します。よって、逆順にチェックします。
102                for( int row=rowCnt-1; row>=0; row-- ) {
103                        final String[] data = table.getValues( row );
104
105                        boolean okFlag = true ;
106                        for( int j=0; j<len; j++ ) {
107                                switch( like[j] ) {
108                                        case 0 : okFlag = okFlag && data[clmNo[j]].equals( vals[j] ); break;            // 0:そのまま
109                                        case 1 : okFlag = okFlag && data[clmNo[j]].startsWith( vals[j] ); break;        // 1:先頭一致
110                                        case 2 : okFlag = okFlag && data[clmNo[j]].endsWith( vals[j] ); break;          // 2:後方一致
111                                        case 3 : okFlag = okFlag && data[clmNo[j]].contains( vals[j] ); break;          // 3:前後曖昧
112                                        default : break;                                // 判定しない
113                                }
114
115                                if( !okFlag ) {
116                                        table.removeValue( row );
117                                        break;                                                  // 不一致があれば、抜ける
118                                }
119                        }
120                }
121
122                return table;
123        }
124}