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.column;
017
018import org.opengion.fukurou.util.Attributes;
019import org.opengion.fukurou.util.XHTMLTag;
020
021import org.opengion.hayabusa.common.HybsSystem;
022import org.opengion.hayabusa.common.HybsSystemException;
023
024import org.opengion.hayabusa.db.AbstractEditor;
025import org.opengion.hayabusa.db.CellEditor;
026import org.opengion.hayabusa.db.DBColumn;
027import org.opengion.hayabusa.db.DBColumnConfig;
028import org.opengion.hayabusa.resource.ResourceFactory;
029import org.opengion.hayabusa.resource.ResourceManager;
030import org.opengion.hayabusa.resource.LabelData;
031
032import java.util.Locale ;
033
034/**
035 * 動的カラムのEntryカラムを編集する場合に使用するエディタークラスです。
036 *
037 * Editor_ENTCLM は、Editor_EntryColumn の略で、Editor_COLUMN.java を
038 * 強化した形で作成します。
039 * これは、引数の値をキーに、DBColumn を動的に作成する機能になります。
040 *
041 * Editor_COLUMN との違いは、こちらは、行ではなく、Entry形式のカラムを
042 * 作成するところです。つまり、行番号は、関係ありません。
043 * 通常は、カラム名__行番号 をキーとするテキストフィールドなどを
044 * 出力しますが、Editor_ENTCLM は、名前そのものをキーとする
045 * テキストフィールドなどを出力します。
046 * あと、カラム引数(:で区切られた値)が使えます。
047 * 通常は、値(Value)に、カラム名のみをセットしますが、コロン(:)で、
048 * 区切ってパラメータを渡せます。
049 *
050 * カラム名:値:must:Length:Label:Editor:DBType:EditParam の順番です。
051 *
052 * コロンの数だけ分離しますが、数は少なくても良いが並び順は、必須です。
053 *
054 * 通常、このままでは、リソースに存在することが前提ですが、
055 * 編集パラメータに、SAVE=TRUE というキーワードをセットすると、
056 * 個々に作成した値を元に、ResourceManager に、動的に作成した
057 * LabelData を追加する機能を持たせます。
058 * この、LabelData は、通常のLabelDataLoaderのプールではなく、
059 * ResourceManagerで、個別に管理されるため、特殊な方法を使わないと
060 * 値を取り出すことはできません。
061 * このキャッシュされたラベルを用いることで、columnCheckのエラーメッセージ
062 * のラベルを動的に書き換えたラベルで表示することができます。
063 *
064 * さらに、編集パラメータに、QUERY=・・・・ というキーワードを
065 * セットすると、そのSQL分を実行して、コードリソースを作成します。
066 * こちらは、SAVE=TRUE の場合のみ実行され、コードリソースのキャッシュに
067 * セットされます。
068 *
069 * このエディタはeventColumnに対応していません。
070 *
071 * カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
072 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
073 * @og.group データ編集
074 *
075 * @og.rev 5.4.2.2 (2011/12/14) 新規追加。
076 *
077 * @version  4.0
078 * @author       Kazuhiko Hasegawa
079 * @since    JDK5.0,
080 */
081public class Editor_ENTCLM extends AbstractEditor {
082        //* このプログラムのVERSION文字列を設定します。   {@value} */
083        private static final String VERSION = "5.4.3.4 (2012/01/12)" ;
084
085        private final String    lang ;
086        private final boolean   isSave ;
087        private final String    codeQuery ;
088        private final boolean   addNoValue ;            // 5.4.2.3 (2011/12/22)
089
090        /**
091         * デフォルトコンストラクター。
092         * このコンストラクターで、基本オブジェクトを作成します。
093         *
094         * @og.rev 5.4.2.3 (2011/12/22) addNoValue 属性を追加します。
095         */
096        public Editor_ENTCLM() {
097                lang            = null;
098                isSave          = false;
099                codeQuery       = null;
100                addNoValue      = false;                // 5.4.2.3 (2011/12/22)
101        }
102
103        /**
104         * デフォルトコンストラクター。
105         *
106         * @og.rev 5.4.2.3 (2011/12/22) addNoValue 属性を追加します。
107         *
108         * @param       clm     DBColumnオブジェクト
109         */
110        private Editor_ENTCLM( final DBColumn clm ) {
111        //      super( clm );
112                name = clm.getName();           // ここでいう名前は、オリジナルなので、動的に作成するときには使用しない。
113                lang = clm.getLang();
114                addNoValue = clm.isAddNoValue() ;               // 5.4.2.3 (2011/12/22)
115
116                String orgParam = clm.getEditorParam();
117                if( orgParam != null ) {
118                        String upParam = orgParam.toUpperCase(Locale.JAPAN);
119                        // 編集パラメータに、SAVE=TRUE というキーワードがあるかどうかのチェック
120                        isSave = upParam.indexOf( "SAVE=TRUE" ) >= 0 ;
121
122                        // QUERY= のキーワードがあれば、コードリソースの検索用SQLとなる。(パラメータの残りすべて)
123                        int adrs = upParam.indexOf( "QUERY=" );
124                        codeQuery = ( adrs >= 0 ) ? orgParam.substring( adrs+6,orgParam.length() ) : null;
125                }
126                else {
127                        isSave    = false;
128                        codeQuery = null;
129                }
130        }
131
132        /**
133         * 各オブジェクトから自分のインスタンスを返します。
134         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
135         * まかされます。
136         *
137         * @param       clm     DBColumnオブジェクト
138         *
139         * @return      CellEditorオブジェクト
140         */
141        public CellEditor newInstance( final DBColumn clm ) {
142                return new Editor_ENTCLM( clm );
143        }
144
145        /**
146         * データの編集用文字列を返します。
147         *
148         * 通常は、値(Value)に、カラム名のみをセットしますが、コロン(:)で、
149         * 区切ってパラメータを渡せます。
150         *
151         * カラム名:値:must:Length:Label:Editor:DBType:EditParam の順番です。
152         *
153         * コロンの数だけ分離しますが、数は少なくても良いが並び順は、必須です。
154         *
155         * @og.rev 5.4.2.3 (2011/12/22) addNoValue 属性を追加します。
156         * @og.rev 5.4.3.4 (2012/01/12) official フラグをセットします。パラメータにラベル追加
157         *
158         * @param       value 入力値
159         *
160         * @return      データの表示用文字列
161         */
162        @Override
163        public String getValue( final String value ) {
164                // 先頭文字が コロン(:)の場合は、カラム名が省略されているので、エラー
165                if( value == null || value.isEmpty() || value.charAt(0) == ':'  ) {
166                        String errMsg = "指定のカラムの値が設定されていません。"
167                                                + HybsSystem.CR
168                                                + "  name=[" + name + "]"
169                                                + "  value=[" + value + "]";
170                        throw new HybsSystemException( errMsg );
171                }
172
173                ResourceManager resource = ResourceFactory.newInstance( lang ) ;
174                String[] vals = value.split( ":" , 8 );         // 8分割します。
175
176                String key    = vals[0];                                                                        // 配列0は、カラム名
177                String val    = (vals.length >= 2) ? vals[1] : null;            // 配列1は、値
178                String must   = (vals.length >= 3) ? vals[2] : null;            // 配列2は、must
179                String len    = (vals.length >= 4) ? vals[3] : null;            // 配列3は、Length
180                String lbl    = (vals.length >= 5) ? vals[4] : null;            // 配列4は、Label
181                String edit   = (vals.length >= 6) ? vals[5] : null;            // 配列5は、Editor
182                String dbtype = (vals.length >= 7) ? vals[6] : null;            // 配列6は、DBType
183                String edPrm  = (vals.length >= 8) ? vals[7] : null;            // 配列7は、EditParam
184
185                boolean isMust = "1".equalsIgnoreCase( must ) || "true".equalsIgnoreCase( must ) ;      // mustが設定されているかどうか
186                // キーに対応するDBColumnがなければ、null が返される。
187                DBColumn dbColumn = resource.getDBColumn( key );
188
189                // DBColumnConfig で値のセット
190                DBColumnConfig config = (dbColumn != null) ? dbColumn.getConfig() : new DBColumnConfig( key );
191
192                // 5.4.3.4 (2012/01/12) official フラグをセットします。
193                config.setOfficial( true );
194
195                // 5.4.2.3 (2011/12/22) addNoValue 属性を追加
196                config.setAddNoValue( addNoValue );
197
198                String mstChStr = "";
199                if( isMust ) {                  // 配列2は、must
200                        Attributes editAttri = config.getEditorAttributes();
201                        if( editAttri == null ) { editAttri = new Attributes(); }
202                        editAttri.add( "class","must" );
203                        config.setEditorAttributes( editAttri );
204
205                        // must 指定の場合に、チェック用のhidden を作成します。
206                        mstChStr = XHTMLTag.hidden( HybsSystem.MUST_KEY + "must", key );
207                }
208                if( len != null && !len.isEmpty() ) {                   // 配列3は、Length
209                        config.setMaxlength( len );
210                }
211                // 5.4.3.4 (2012/01/12) パラメータにラベル追加
212                if( lbl != null && !lbl.isEmpty() ) {                   // 配列4は、Label
213                        LabelData labelData = resource.getLabelData( lbl ) ;
214                        config.setLabelData( labelData );
215                }
216                if( edit != null && !edit.isEmpty() ) {                 // 配列5は、Editor
217                        config.setEditor( edit );
218                }
219                if( dbtype != null && !dbtype.isEmpty() ) {             // 配列6は、DBType
220                        config.setDbType( dbtype );
221                }
222                if( edPrm != null && !edPrm.isEmpty() ) {               // 配列7は、EditParam
223                        config.setEditorParam( edPrm );
224                }
225
226                // 動的なコードリソースの作成
227                if( codeQuery != null && isSave ) {
228                        config.setCodeData( resource.getCodeData( key,codeQuery ) );
229                }
230
231                dbColumn = new DBColumn( config );
232                if( isSave ) {  // isSave が true で、セーブする。
233                        resource.setDBColumn( key,dbColumn );
234                }
235
236                // val と must は、キャッシュされた DBColumn と別に、毎回異なるケースを想定します。
237                return dbColumn.getEditorValue( val ) + mstChStr;
238        }
239
240        /**
241         * このクラスでは、Entry形式の編集用の文字列を作成します。
242         * よって、行番号を付加しません。
243         *
244         * @param       row   行番号
245         * @param       value 入力値
246         *
247         * @return      データ表示/編集用の文字列
248         */
249        @Override
250        public String getValue( final int row,final String value ) {
251                return getValue( value );
252        }
253}