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
024import java.text.Normalizer;
025
026/**
027 * TableFilter_BIKOSET は、TableFilter インターフェースを継承した、DBTableModel 処理用の
028 * 実装クラスです。
029 *
030 * ここでは、NAME_JA と、BIKO をそれぞれ最適な値に再設定します。
031 * 具体的には、NAME_JA は、最初のスペースか、括弧"(" より前方のみを切り出します。
032 * BIKO は、スペース、括弧"(",")" 、カンマ で分割して、コロン(全角なら半角に変換)の
033 * ある文字列のみを、スペース区切りに区切りなおします。
034 * これらの変換を行うことで、BIKO2CODE を使用して、コードリソースの自動作成が可能になります。
035 *
036 * パラメータは、tableFilterタグの keys, vals にそれぞれ記述するか、BODY 部にCSS形式で記述します。
037 * 【パラメータ】
038 *  {
039 *       NAME_JA        : NAME_JA ;   カラムリソースのラベルとなる値が設定されているカラム名を指定します。
040 *       BIKO           : BIKO    ;   コードリソース作成に使用する備考欄が設定されているカラム名を指定します。
041 *  }
042 *
043 * @og.formSample
044 * ●形式:
045 *      ① <og:tableFilter classId="BIKOSET" keys="NAME_JA,BIKO"
046 *                                              vals="CLM_NAME,COMMENTS" />
047 *
048 *      ② <og:tableFilter classId="BIKOSET" >
049 *               {
050 *                       NAME_JA   : CLM_NAME ;
051 *                       BIKO      : COMMENTS ;
052 *               }
053 *         </og:tableFilter>
054 *
055 * @og.rev 6.4.3.3 (2016/03/04) コードリソース作成時の効率化アップのため。
056 *
057 * @version  0.9.0  2000/10/17
058 * @author   Kazuhiko Hasegawa
059 * @since    JDK1.8,
060 */
061public class TableFilter_BIKOSET extends AbstractTableFilter {
062        /** このプログラムのVERSION文字列を設定します。   {@value} */
063        private static final String VERSION = "6.5.0.1 (2016/10/21)" ;
064
065        /**
066         * デフォルトコンストラクター
067         *
068         * @og.rev 6.4.3.3 (2016/03/04) 新規追加。
069         */
070        public TableFilter_BIKOSET() {
071                super();
072                initSet( "NAME_JA"      , "カラムリソースのラベルとなる値が設定されているカラム名を指定します。"          );
073                initSet( "BIKO"         , "コードリソース作成に使用する備考欄が設定されているカラム名を指定します。"        );
074        }
075
076        /**
077         * DBTableModel処理を実行します。
078         *
079         * @og.rev 6.4.3.3 (2016/03/04) 新規追加。
080         * @og.rev 6.5.0.1 (2016/10/21) ErrorMessage をまとめるのと、直接 Throwable を渡します。
081         *
082         * @return 処理結果のDBTableModel
083         */
084        public DBTableModel execute() {
085                final DBTableModel table = getDBTableModel();
086                if( table == null ) { return table; }
087
088                final int nameNo = table.getColumnNo( StringUtil.nval( getValue( "NAME_JA" ), "NAME_JA" ), false );             // 存在しない場合は、-1 を返す。
089                final int bikoNo = table.getColumnNo( StringUtil.nval( getValue( "BIKO"    ), "BIKO"    ), false );
090
091                if( nameNo >= 0 && bikoNo >= 0 ) {
092                        String[] data  = null;
093                        final int rowCnt = table.getRowCount();
094                        for( int row=0; row<rowCnt; row++ ) {
095                                String bkName = null ;
096                                try {
097                                        data = table.getValues( row );
098
099                                        bkName = data[nameNo];                                  // Exception 発生に表示する値
100
101                                        // 副作用を及ぼします。
102                                        data[nameNo]    = makeName( bkName );
103                                        data[bikoNo]    = makeBiko( data[bikoNo],data[nameNo] );
104                                }
105                                catch( final RuntimeException ex ) {
106                                        // 6.5.0.1 (2016/10/21) ErrorMessage をまとめるのと、直接 Throwable を渡します。
107                                        makeErrorMessage( "TableFilter_BIKOSET Error",ErrorMessage.NG )
108                                                .addMessage( row+1,ErrorMessage.NG,"BIKOSET"
109                                                        , "NAME_JA=[" + bkName + "]"
110                                                        , StringUtil.array2csv( data )
111                                                )
112                                                .addMessage( ex );
113                                }
114                        }
115                }
116
117                return table;
118        }
119
120        /**
121         * ラベルとなる文字列をピックアップして、返します。
122         * 最初のスペースか、括弧"(" より前方のみを切り出します。
123         *
124         * @param       name    カラム名称となる元の値
125         *
126         * @return      切り出したカラム名称
127         */
128        private String makeName( final String name ) {
129                return name == null ? "" : Normalizer.normalize( name, Normalizer.Form.NFKC ).split( "[\\( ]" )[0];
130        }
131
132        /**
133         * 備考となる文字列をピックアップして、返します。
134         * スペース、括弧"(",")" 、カンマ で分割して、コロン(全角なら半角に変換)の
135         * ある文字列のみを、スペース区切りに区切りなおします。
136         *
137         * 備考欄は、カラム名称と同じ場合は、""(ゼロ文字列)を返します。
138         * 先頭が、カラム名称と同じ場合は、先頭部分(同じ文字列部分)は削除します。
139         *
140         * @param       biko    カラム名称となる元の値
141         * @param       name    カラム名称となる元の値
142         *
143         * @return      切り出したカラム名称
144         */
145        private String makeBiko( final String biko,final String name ) {
146                // 全角の"(" , ")"," ",":","," は、半角に変換されます。
147                String rtn = biko == null ? "" : Normalizer.normalize( biko, Normalizer.Form.NFKC );    // 半角英数記号に変換する。
148
149                // 先頭文字列が、name と一致する場合は、一致部分(重複部分)を削除します。
150                if( rtn.startsWith( name ) ) {
151                        rtn = rtn.substring( name.length() );
152                }
153
154                // コロンを含む場合は、コードリソースとして分解対象になる。
155                if( rtn.contains( ":" ) ) {
156                        rtn = String.join( " " , rtn.split( "[\\(\\),、 ]" ) );
157                }
158
159                return rtn.trim();
160        }
161}