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.hayabusa.resource.ResourceFactory;
022import org.opengion.hayabusa.resource.ResourceManager;
023
024import org.opengion.fukurou.util.ErrorMessage;
025import org.opengion.fukurou.util.StringUtil;
026
027import java.util.Map;
028
029/**
030 * TableFilter_LABEL は、TableFilter インターフェースを継承した、DBTableModel 処理用の
031 * 実装クラスです。
032 *
033 * ここでは、キーのCLM,LBL と、LANG より、ラベルリソースに変換した値を設定します。
034 *
035 * パラメータは、tableFilterタグの keys, vals にそれぞれ記述するか、BODY 部にCSS形式で記述します。
036 * 【パラメータ】
037 *  {
038 *       SYSTEM_ID  : ラベルリソースの作成システムIDを指定します。無指定時は、ログイン時のリソースになります。
039 *       LANG       : ラベルリソースの言語を指定します。無指定時は、日本語になります。
040 *       CLM        : ラベルリソースのキーとなる値が設定されているカラム名を指定します。
041 *       LBL        : CLMで指定されたカラム名から値を取り出し、ラベルリソースに変換した結果をセットします。
042 *  }
043 *
044 * <del>CLM と LBL に同じカラムを指定すると、このフィルターを通過するたびに、変換後のラベルが
045 * 再びキーとして変換しようと試みることになります。不測の事態に陥る可能性があるため、
046 * その様な設定は出来なくなっています。 5.7.8.0 (2014/07/04) 解除</del>
047 *
048 * 5.7.8.0 (2014/07/04) CLM と LBL に同じカラムを指定する事ができます。
049 *
050 * また、CLM または LBL で指定したカラムが DBTableModel に存在しない場合は、処理そのものを
051 * 無視します。その場合は、警告も出力されませんので、ご注意ください。
052 *
053 * @og.formSample
054 * ●形式:
055 *      @ &lt;og:tableFilter classId="LABEL" keys="CLM,LBL" vals="COLUMN,LABEL_CLM" /&gt;
056 *
057 *      A &lt;og:tableFilter classId="LABEL" &gt;
058 *               {
059 *                      SYSTEM_ID  : GF ;
060 *                      LANG       : ja ;
061 *                      CLM        : COLUMN ;
062 *                      LBL        : LABEL_CLM ;
063 *               }
064 *         &lt;/og:tableFilter&gt;
065 *
066 * @og.rev 5.6.6.0 (2013/07/05) keys の整合性チェックを追加
067 *
068 * @version  0.9.0  2000/10/17
069 * @author   Kazuhiko Hasegawa
070 * @since    JDK1.1,
071 */
072public class TableFilter_LABEL extends AbstractTableFilter {
073        //* このプログラムのVERSION文字列を設定します。   {@value} */
074        private static final String VERSION = "5.7.8.0 (2014/07/04)" ;
075
076        /**
077         * keys の整合性チェックを行うための初期設定を行います。
078         *
079         * @og.rev 5.6.6.1 (2013/07/12) keys の整合性チェック対応
080         *
081         * @param       keysMap keys の整合性チェックを行うための Map
082         */
083        @Override
084        protected void init( final Map<String,String> keysMap ) {
085                keysMap.put( "SYSTEM_ID"        , "システムIDを指定します(初期値:ログイン時のリソース)"                );
086                keysMap.put( "LANG"                     , "言語を指定(初期値:日本語)"                                                              );
087                keysMap.put( "CLM"                      , "キーとなる値が設定されているカラム名を指定"                               );
088                keysMap.put( "LBL"                      , "ラベルリソースに変換した結果をセットするカラム名を指定" );
089        }
090
091        /**
092         * DBTableModel処理を実行します。
093         *
094         * @og.rev 5.5.2.2 (2012/05/09) SYSTEM_ID を受け取るように変更
095         * @og.rev 5.5.2.6 (2012/05/25) protected変数を、private化したため、getterメソッドで取得するように変更
096         * @og.rev 5.7.8.0 (2014/07/04) CLM と LBL に同じカラムを指定できるように変更
097         *
098         * @return 処理結果のDBTableModel
099         */
100        public DBTableModel execute() {
101                DBTableModel table = getDBTableModel();         // 5.5.2.6 (2012/05/25) インターフェースにgetterメソッド追加
102
103                String systemId = getValue( "SYSTEM_ID" );              // 5.5.2.2 (2012/05/09)
104                String lang             = getValue( "LANG" );
105                ResourceManager resource = ResourceFactory.newInstance( systemId,lang,false );          // 5.5.2.2 (2012/05/09)
106
107                String clm = getValue( "CLM" );
108                String lbl = getValue( "LBL" );
109
110                int clmNo  = table.getColumnNo( clm,false );    // 存在しない場合は、-1 を返す。
111                int lblNo  = table.getColumnNo( lbl,false );
112
113                // 5.7.8.0 (2014/07/04) CLM と LBL に同じカラムを指定できるように変更
114//              if( clmNo == lblNo && clmNo >= 0 ) {
115//                      String errMsg = "CLM と LBL に同じカラムを指定することは出来ません。" ;
116//                      ErrorMessage errMessage = makeErrorMessage( "TableFilter_LABEL Error",ErrorMessage.NG );
117//                      errMessage.addMessage( 0,ErrorMessage.NG,errMsg );
118//                      errMessage.addMessage( 0,ErrorMessage.NG,"CLM=[" + clm + "],LBL=[" + lbl + "]" );
119//              }
120//              else if( clmNo >= 0 && lblNo >= 0 ) {
121                if( clmNo >= 0 && lblNo >= 0 ) {
122                        String[] data  = null;
123                        int rowCnt = table.getRowCount();
124                        for( int row=0; row<rowCnt; row++ ) {
125                                String clmVal = null;
126                                try {
127                                        data   = table.getValues( row );
128                                        clmVal = data[clmNo];           // 変換する元のカラム名
129                                        data[lblNo] = resource.getLabel( clmVal ) ;             // 副作用を及ぼしています。注意
130                                }
131                                catch( RuntimeException ex ) {
132                                        ErrorMessage errMessage = makeErrorMessage( "TableFilter_LABEL Error",ErrorMessage.NG );
133                                        errMessage.addMessage( row+1,ErrorMessage.NG,ex.getMessage() );
134                                        errMessage.addMessage( row+1,ErrorMessage.NG,StringUtil.array2csv( data ) );
135                                        errMessage.addMessage( row+1,ErrorMessage.NG,"CLM=[" + clm + "],VAL=[" + clmVal + "]" );
136                                }
137                        }
138                }
139
140                return table;
141        }
142}