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 = "6.4.5.2 (2016/05/06)" ;
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                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
098                lang            = null;
099                isSave          = false;
100                codeQuery       = null;
101                addNoValue      = false;                // 5.4.2.3 (2011/12/22)
102        }
103
104        /**
105         * デフォルトコンストラクター。
106         *
107         * @og.rev 5.4.2.3 (2011/12/22) addNoValue 属性を追加します。
108         * @og.rev 6.4.4.2 (2016/04/01) nameのfinal化
109         *
110         * @param       clm     DBColumnオブジェクト
111         */
112        private Editor_ENTCLM( final DBColumn clm ) {
113        //      super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
114                super( clm );   // 6.4.4.2 (2016/04/01) nameのfinal化
115        //      name = clm.getName();           // ここでいう名前は、オリジナルなので、動的に作成するときには使用しない。
116                lang = clm.getLang();
117                addNoValue = clm.isAddNoValue() ;               // 5.4.2.3 (2011/12/22)
118
119                final String orgParam = clm.getEditorParam();
120                // 6.4.1.1 (2016/01/16) PMD refactoring. Avoid if (x != y) ..; else ..;
121                if( orgParam == null ) {
122                        isSave    = false;
123                        codeQuery = null;
124                }
125                else {
126                        final String upParam = orgParam.toUpperCase(Locale.JAPAN);
127                        // 編集パラメータに、SAVE=TRUE というキーワードがあるかどうかのチェック
128                        isSave = upParam.indexOf( "SAVE=TRUE" ) >= 0 ;
129
130                        // QUERY= のキーワードがあれば、コードリソースの検索用SQLとなる。(パラメータの残りすべて)
131                        final int adrs = upParam.indexOf( "QUERY=" );
132                        codeQuery = adrs >= 0 ? orgParam.substring( adrs+6,orgParam.length() ) : null;
133                }
134
135        }
136
137        /**
138         * 各オブジェクトから自分のインスタンスを返します。
139         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
140         * まかされます。
141         *
142         * @param       clm     DBColumnオブジェクト
143         *
144         * @return      CellEditorオブジェクト
145         * @og.rtnNotNull
146         */
147        public CellEditor newInstance( final DBColumn clm ) {
148                return new Editor_ENTCLM( clm );
149        }
150
151        /**
152         * データの編集用文字列を返します。
153         *
154         * 通常は、値(Value)に、カラム名のみをセットしますが、コロン(:)で、
155         * 区切ってパラメータを渡せます。
156         *
157         * カラム名:値:must:Length:Label:Editor:DBType:EditParam の順番です。
158         *
159         * コロンの数だけ分離しますが、数は少なくても良いが並び順は、必須です。
160         *
161         * @og.rev 5.4.2.3 (2011/12/22) addNoValue 属性を追加します。
162         * @og.rev 5.4.3.4 (2012/01/12) official フラグをセットします。パラメータにラベル追加
163         * @og.rev 6.4.5.2 (2016/05/06) rendAttri,editAttri は、not null にします。
164         *
165         * @param       value 入力値
166         *
167         * @return      データの表示用文字列
168         * @og.rtnNotNull
169         */
170        @Override
171        public String getValue( final String value ) {
172                // 先頭文字が コロン(:)の場合は、カラム名が省略されているので、エラー
173                if( value == null || value.isEmpty() || value.charAt(0) == ':'  ) {             // or判定なので、StringUtil.startsChar は使えません。
174                        final String errMsg = "指定のカラムの値が設定されていません。"
175                                                + CR
176                                                + "  name=[" + name + "]"
177                                                + "  value=[" + value + "]";
178                        throw new HybsSystemException( errMsg );
179                }
180
181                final ResourceManager resource = ResourceFactory.newInstance( lang ) ;
182                final String[] vals = value.split( ":" , 8 );                   // 8分割します。
183
184                final String key    = vals[0];                                                                  // 配列0は、カラム名
185                final String val    = vals.length >= 2 ? vals[1] : null;                // 配列1は、値
186                final String must   = vals.length >= 3 ? vals[2] : null;                // 配列2は、must
187                final String len    = vals.length >= 4 ? vals[3] : null;                // 配列3は、Length
188                final String lbl    = vals.length >= 5 ? vals[4] : null;                // 配列4は、Label
189                final String edit   = vals.length >= 6 ? vals[5] : null;                // 配列5は、Editor
190                final String dbtype = vals.length >= 7 ? vals[6] : null;                // 配列6は、DBType
191                final String edPrm  = vals.length >= 8 ? vals[7] : null;                // 配列7は、EditParam
192
193                final boolean isMust = "1".equalsIgnoreCase( must ) || "true".equalsIgnoreCase( must ) ;        // mustが設定されているかどうか
194                // キーに対応するDBColumnがなければ、null が返される。
195                DBColumn dbColumn = resource.getDBColumn( key );
196
197                // DBColumnConfig で値のセット
198                // 6.4.1.1 (2016/01/16) PMD refactoring. Avoid if (x != y) ..; else ..;
199                final DBColumnConfig config = (dbColumn == null) ? new DBColumnConfig( key ) : dbColumn.getConfig();
200
201                // 5.4.3.4 (2012/01/12) official フラグをセットします。
202                config.setOfficial( true );
203
204                // 5.4.2.3 (2011/12/22) addNoValue 属性を追加
205                config.setAddNoValue( addNoValue );
206
207                String mstChStr = "";
208                if( isMust ) {                  // 配列2は、must
209                        final Attributes editAttri = config.getEditorAttributes();
210                        editAttri.add( "class","must" );
211                        config.setEditorAttributes( editAttri );
212
213                        // must 指定の場合に、チェック用のhidden を作成します。
214                        mstChStr = XHTMLTag.hidden( HybsSystem.MUST_KEY + "must", key );
215                }
216                if( len != null && !len.isEmpty() ) {                   // 配列3は、Length
217                        config.setMaxlength( len );
218                }
219                // 5.4.3.4 (2012/01/12) パラメータにラベル追加
220                if( lbl != null && !lbl.isEmpty() ) {                   // 配列4は、Label
221                        final LabelData labelData = resource.getLabelData( lbl ) ;
222                        config.setLabelData( labelData );
223                }
224                if( edit != null && !edit.isEmpty() ) {                 // 配列5は、Editor
225                        config.setEditor( edit );
226                }
227                if( dbtype != null && !dbtype.isEmpty() ) {             // 配列6は、DBType
228                        config.setDbType( dbtype );
229                }
230                if( edPrm != null && !edPrm.isEmpty() ) {               // 配列7は、EditParam
231                        config.setEditorParam( edPrm );
232                }
233
234                // 動的なコードリソースの作成
235                if( codeQuery != null && isSave ) {
236                        config.setCodeData( resource.getCodeData( key,codeQuery ) );
237                }
238
239                dbColumn = new DBColumn( config );
240                if( isSave ) {  // isSave が true で、セーブする。
241                        resource.setDBColumn( key,dbColumn );
242                }
243
244                // val と must は、キャッシュされた DBColumn と別に、毎回異なるケースを想定します。
245                return dbColumn.getEditorValue( val ) + mstChStr;
246        }
247
248        /**
249         * このクラスでは、Entry形式の編集用の文字列を作成します。
250         * よって、行番号を付加しません。
251         *
252         * @param       row   行番号
253         * @param       value 入力値
254         *
255         * @return      データ表示/編集用の文字列
256         * @og.rtnNotNull
257         */
258        @Override
259        public String getValue( final int row,final String value ) {
260                return getValue( value );
261        }
262}