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_MATCHES は、TableFilter インターフェースを継承した、DBTableModel 処理用の
026 * 実装クラスです。
027 *
028 * ここでは、指定のカラムに対して、正規表現でマッチングを行い、true のデータだけに絞ります。
029 * つまり、マッチしなければ、そのデータを削除します。
030 * 新しくテーブルを作成するのではなく、既存のテーブルのデータを物理削除しますので、ご注意ください。
031 *
032 * このフィルターでは、KEYSに指定するのは、カラム名で、自由に指定できます。
033 * カラムが存在しない場合は、無視(削除しない)します。
034 * エラーチェックは行いません。
035 *
036 * パラメータは、tableFilterタグの keys, vals にそれぞれ記述するか、BODY 部にCSS形式で記述します。
037 *
038 * @og.formSample
039 * ●形式:
040 *      ① <og:tableFilter classId="MATCHES" keys="CLM1,CLM2..." vals="正規表現1,正規表現2..." />
041 *
042 *      ② <og:tableFilter classId="MATCHES" >
043 *               {
044 *                   CLM1   : 正規表現1   ;
045 *                   CLM2   : 正規表現2   ;
046 *                   ・・・    : ・・・   ;
047 *               }
048 *         </og:tableFilter>
049 *
050 * @og.rev 6.7.9.1 (2017/05/19) 新規追加
051 *
052 * @version  6.7  2017/05/19
053 * @author   Kazuhiko Hasegawa
054 * @since    JDK1.8,
055 */
056public class TableFilter_MATCHES extends AbstractTableFilter {
057        /** このプログラムのVERSION文字列を設定します。   {@value} */
058        private static final String VERSION = "6.7.9.1 (2017/05/19)" ;
059
060        /**
061         * デフォルトコンストラクター
062         *
063         * @og.rev 6.7.9.1 (2017/05/19) 新規追加
064         */
065        public TableFilter_MATCHES() {
066                super();
067                // initSet(String,String) を指定しない場合は、KEYSに制限を行わない。
068        }
069
070        /**
071         * DBTableModel処理を実行します。
072         *
073         * @og.rev 6.7.9.1 (2017/05/19) 新規追加
074         *
075         * @return 処理結果のDBTableModel
076         */
077        public DBTableModel execute() {
078                final DBTableModel table = getDBTableModel();
079
080                final String[] keys = getKeys();                        // 判定対象のカラム名の配列
081                final int      len  = keys.length;
082
083                final String[] regex = new String[len];         // 判定に使う正規表現(valsの値)
084                final int[]    clmNo = new int[len];            // カラムの番号
085
086                for( int i=0; i<len; i++ ) {
087                        clmNo[i] = table.getColumnNo( keys[i],false );  // カラムが存在しなければ、-1
088                        regex[i] = getValue( keys[i] );
089                }
090
091                final int rowCnt = table.getRowCount();
092
093                String[] data = null;   // エラー時に表示するため。
094                // KBCLMに変更がなければ、レコードを削除します。よって、逆順にチェックします。
095                for( int row=rowCnt-1; row>=0; row-- ) {
096                        try {
097                                data = table.getValues( row );
098
099                                for( int j=0; j<len; j++ ) {
100                                        if( clmNo[j] >= 0 && ! data[clmNo[j]].matches( regex[j] ) ) {
101                                                table.removeValue( row );
102                                                break;                                                  // 一つでもマッチしなければ、削除して抜ける。
103                                        }
104                                }
105                        }
106                        catch( final RuntimeException ex ) {
107                                // 6.5.0.1 (2016/10/21) ErrorMessage をまとめるのと、直接 Throwable を渡します。
108                                makeErrorMessage( "TableFilter_MATCHES Error",ErrorMessage.NG )
109                                        .addMessage( row+1,ErrorMessage.NG,"MATCHES"
110                                                , "keys=[" + StringUtil.array2csv( keys ) + "]"
111                                                , "vals=[" + StringUtil.array2csv( regex ) + "]"
112                                                , StringUtil.array2csv( data )
113                                        )
114                                        .addMessage( ex );
115                        }
116                }
117
118                return table;
119        }
120}