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.hayabusa.common.HybsSystem;
019import org.opengion.hayabusa.common.HybsSystemException;
020import org.opengion.hayabusa.db.AbstractEditor;
021import org.opengion.hayabusa.db.CellEditor;
022import org.opengion.hayabusa.db.DBColumn;
023import org.opengion.hayabusa.db.SelectionCellEditor;                                    // 6.2.2.0 (2015/03/27)
024import org.opengion.hayabusa.db.Selection;
025import org.opengion.hayabusa.db.SelectionFactory;
026import org.opengion.fukurou.util.StringFormat;
027import org.opengion.fukurou.util.XHTMLTag;
028import org.opengion.fukurou.util.Attributes;
029import org.opengion.fukurou.util.TagBuffer;
030
031import static org.opengion.fukurou.util.StringUtil.isNull;                              // 6.1.1.0 (2015/01/17)
032
033/**
034 * カラムの編集パラメーターのSQL文の実行結果より、プルダウンメニューを作成して
035 * 編集する場合に使用するエディタークラスです。
036 *
037 * 編集パラメータには、プルダウンメニューを作成するための、SQL文を記述します。
038 * このSQL文は、select KEY,LABEL from xx ・・・ という構文で、KEY部分とLABEL部分が
039 * 選択されます。
040 * 第一カラムはキー、第二カラムはラベルでこの2つは必須です。第三カラムは短縮ラベル、
041 * 第四カラムはグループ(optgroup)、第五カラムは色付け等に使うクラスです。
042 * 第五カラムの指定方法には、3通りあります。
043 * ここでは、
044 *   ① "=" を含む場合は、そのままセット。(style='AAAA' など)
045 *   ② disabled 単体の場合は、disabled="disabled" をセット
046 *   ③ それ以外は、class= の後に、引数をセット
047 * します。
048 * 短縮ラベルが設定されている場合、一覧でこのエディタが適用されると短縮ラベル表示を
049 * した上でマウスオーバー時はツールチップで通常のラベルを表示します。
050 *
051 * 各カラムの値(value値)に、AAA:BBB:CCC:DDD という値を設定できます。これは、
052 * $1,$2,$3,$4 に割り当てなおして、QUERYを実行します。また、$1 は、本来の値として、
053 * メニューの初期値設定等に使用します。上記の例では、AAA が値で、それ以降は、
054 * 引数になります。
055 * 又、$Cには自分自身のカラム名を割り当てます。
056 * この機能を使用すれば、動的メニューを行ごとに条件を変えて作成することが
057 * 可能になります。
058 * 例:select KEY,LABEL from xx where KUBUN='$2' and CDK='$3'
059 * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
060 * 変数は、""(ゼロ文字列)として、扱われます。
061 *
062 * 編集パラメータに"SEQ"と記述することで正方向にしか選べないシークメニューを実現できます。
063 * これにより、シーケンスにステータスを順に挙げていくような、プルダウンメニュー
064 * を作成することが出来ます。(逆に戻れないメニュー)
065 *
066 * カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
067 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
068 *
069 * 8.0.0.1 (2021/10/08)
070 * 検索機能(絞り機能)付きのプルダウンメニューを作成する為には、文字パラメータに
071 * "class=sel2sel" と入力してください。
072 *
073 * @og.rev 3.2.3.0 (2003/06/06) 新規作成
074 * @og.rev 3.4.0.1 (2003/09/03) DB検索をリアルタイムに変更。
075 * @og.rev 4.3.6.0 (2009/04/01) eventColumn対応
076 * @og.rev 5.4.3.6 (2012/01/19) コメント変更
077 * @og.rev 6.2.2.0 (2015/03/27) SelectionCellEditor I/Fを追加
078 * @og.group データ編集
079 *
080 * @version  4.0
081 * @author       Kazuhiko Hasegawa
082 * @since    JDK5.0,
083 */
084public class Editor_DBMENU extends AbstractEditor implements SelectionCellEditor {
085        /** このプログラムのVERSION文字列を設定します。   {@value} */
086        private static final String VERSION = "8.0.0.1 (2021/10/08)" ;
087
088        private static final String STR_CLS = "class=" ;                                                        // 8.0.0.1 (2021/10/08)
089        private static final int LEN_CLS = STR_CLS.length() ;                                           // 8.0.0.1 (2021/10/08)
090
091        private final String query ;
092        private final String dbid ;
093        private final String lang ;                             // 4.0.0 (2006/11/15)
094        private final boolean addNoValue ;              // 3.5.5.7 (2004/05/10)
095        private final boolean seqFlag ;                 // 3.6.0.6 (2004/10/22)
096        private final String useSLabel ;                // 5.5.1.0 (2012/04/03)
097        private final String addKeyLabel ;              // 6.2.0.0 (2015/02/27) キー:ラベル形式
098
099        /**
100         * デフォルトコンストラクター。
101         * このコンストラクターで、基本オブジェクトを作成します。
102         *
103         * @og.rev 3.4.0.1 (2003/09/03) 初期値でQUERY文をキープする。
104         * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します
105         * @og.rev 5.5.1.0 (2012/04/03) Slabel対応
106         * @og.rev 6.2.0.0 (2015/02/27) キー:ラベル形式で表示するかどうかを、指定できるようにします。
107         */
108        public Editor_DBMENU() {
109                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
110                // 4.3.4.4 (2009/01/01)
111                query           = null;
112                dbid            = null;
113                lang            = null;                 // 4.0.0 (2006/11/15)
114                addNoValue      = false;                // 3.5.5.7 (2004/05/10)
115                seqFlag         = false;                // 3.6.0.6 (2004/10/22)
116                useSLabel       = "auto";               // 5.5.1.0 (2012/04/03)
117                addKeyLabel     = null;                 // 6.2.0.0 (2015/02/27) キー:ラベル形式
118        }
119
120        /**
121         * DBColumnオブジェクトを指定したprivateコンストラクター。
122         *
123         * @og.rev 3.3.1.1 (2003/07/03) name , attributes 属性を final にする。
124         * @og.rev 3.4.0.1 (2003/09/03) 継承の親元の変更に伴う実装の移動。
125         * @og.rev 3.5.5.7 (2004/05/10) addNoValue 属性を追加します。
126         * @og.rev 3.5.5.9 (2004/06/07) editorParam 属性が null の場合は、エラーとします。
127         * @og.rev 3.5.6.0 (2004/06/18) XHTMLTag の 内部配列 SELECT_KEY を隠蔽します。
128         * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します
129         * @og.rev 5.1.7.0 (2010/06/01) 動的プルダウン実装見直し
130         * @og.rev 5.5.1.0 (2012/04/03) Slabel対応
131         * @og.rev 6.0.4.0 (2014/11/28) optionAttributes は、コンストラクタで設定します。
132         * @og.rev 6.0.4.0 (2014/11/28) useMultiSelect は、selection ではなく、colomn から取得する。
133         * @og.rev 6.2.0.0 (2015/02/27) キー:ラベル形式で表示するかどうかを、指定できるようにします。
134         * @og.rev 6.4.4.2 (2016/04/01) nameのfinal化
135         * @og.rev 6.9.5.0 (2018/04/23) useMultiSelect 廃止
136         * @og.rev 7.0.5.1 (2019/09/27) optionAttributes が二重に設定されていたため、削除
137         * @og.rev 8.0.0.1 (2021/10/08) dbType パラメータのclass属性対応
138         *
139         * @param       clm     DBColumnオブジェクト
140         */
141        private Editor_DBMENU( final DBColumn clm ) {
142        //      super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
143                super( clm );   // 6.4.4.2 (2016/04/01) nameのfinal化
144        //      name            = clm.getName();
145                addNoValue      = clm.isAddNoValue() ;          // 3.5.5.7 (2004/05/10)
146                query           = clm.getEditorParam();
147                dbid            = clm.getDbid();
148                lang            = clm.getLang();                        // 4.0.0.0 (2006/11/15)
149                seqFlag         = false;                                        // 3.6.0.6 (2004/10/22)
150                useSLabel       = clm.getUseSLabel() ;          // 5.5.1.0 (2012/04/03)
151                addKeyLabel     = clm.getAddKeyLabel();         // 6.2.0.0 (2015/02/27) キー:ラベル形式
152
153                // 3.5.5.9 (2004/06/07)
154                if( query == null || query.isEmpty() ) {
155                        final String errMsg = "DBMENU Editor では、編集パラメータは必須です。"
156                                        + " name=[" + name + "]" + CR ;
157                        throw new HybsSystemException( errMsg );
158                }
159
160                final String disabled = clm.isWritable() ? null : "disabled" ;
161
162                // 6.1.1.0 (2015/01/17) Attributesの連結記述
163                attributes = new Attributes()
164                                        .set( "disabled"        ,disabled )
165                                        .set( clm.getEditorAttributes() );                              // #addAttributes( Attributes ) の代替え
166
167                // class設定 8.0.0.1 (2021/10/08)
168                final String dbTypeParam = clm.getDbTypeParam();
169                if( dbTypeParam != null ) {
170                        final int st = dbTypeParam.indexOf( STR_CLS );
171                        if( st >= 0 ) {
172                                String newTypePram = dbTypeParam;
173                                final int ed = newTypePram.indexOf( ';' , st );
174                                // 文字パラメータの文字列から class属性の文字列を取得する
175                                if( ed >= 0 ) {
176                                        // [0-9]|class=sel2sel;[A-z] の場合、sel2sel を取得
177                                        newTypePram = newTypePram.substring( st + LEN_CLS , ed );
178                                }
179                                else {
180                                        // [0-9A-z]class=sel2sel の場合、sel2sel を取得
181                                        newTypePram = newTypePram.substring( st + LEN_CLS );
182                                }
183                                // attributes に class 付与
184                                attributes.add( "class" , newTypePram );
185                        }
186                }
187
188//              // 6.9.5.0 (2018/04/23) useMultiSelect 廃止
189//              // 6.1.1.0 (2015/01/17) TagBufferの連結記述
190//              tagBuffer.add( XHTMLTag.selectAttri( attributes ) )
191//                                      .add( attributes.get( "optionAttributes" ) )                                            // 6.0.4.0 (2014/11/28)
192//                                      .add( "onkeydown" , "setKeySelect(this);" , clm.useMultiSelect() );     // 6.0.4.0 (2014/11/28)
193
194                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
195                // 7.0.5.1 (2019/09/27) optionAttributes が二重に設定されていたため、削除
196                tagBuffer.add( XHTMLTag.selectAttri( attributes ) );
197//                                      .add( attributes.get( "optionAttributes" ) );                                           // 6.0.4.0 (2014/11/28)
198        }
199
200        /**
201         * 各オブジェクトから自分のインスタンスを返します。
202         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
203         * まかされます。
204         *
205         * @param       clm     DBColumnオブジェクト
206         *
207         * @return      CellEditorオブジェクト
208         * @og.rtnNotNull
209         */
210        public CellEditor newInstance( final DBColumn clm ) {
211                return new Editor_DBMENU( clm );
212        }
213
214        /**
215         * データの編集用文字列を返します。
216         *
217         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
218         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
219         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
220         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
221         * 変数は、""(ゼロ文字列)として、扱われます。
222         * 又、$Cには自分自身のカラム名を割り当てます。
223         *
224         * @og.rev 3.4.0.1 (2003/09/03) リアルタイムで値を作成する様に変更。
225         * @og.rev 3.5.5.5 (2004/04/23) 新規に Attributes オブジェクトを作成する方式を止めます。
226         * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない
227         * @og.rev 5.1.3.0 (2010/02/01) 一覧表示のみで、ツールチップ表示を行う。
228         * @og.rev 5.1.7.0 (2010/06/01) 動的プルダウン実装見直し
229         * @og.rev 5.5.1.0 (2012/04/03) Slabel対応
230         *
231         * @param       value 入力値
232         *
233         * @return      データの編集用文字列
234         * @og.rtnNotNull
235         */
236        @Override
237        public String getValue( final String value ) {
238                final boolean useSlbl = "true".equalsIgnoreCase( useSLabel );           // 5.5.1.0 (2012/04/03)
239
240                // select タグの作成
241                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
242                return getOption(
243                                new TagBuffer( "select" )
244                                        .add( "name"    , name )                                                                        // 4.3.6.0 (2009/04/01)
245                                        .add( "id"              , name , isNull( attributes.get( "id" ) ) )     // 4.3.7.2 (2009/06/15)
246                                        .add( tagBuffer.makeTag() )
247                                , value
248                                , useSlbl
249                        ).makeTag() ;
250        }
251
252        /**
253         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
254         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し,
255         * リクエスト情報を1つ毎のフィールドで処理できます。
256         *
257         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
258         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
259         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
260         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
261         * 変数は、""(ゼロ文字列)として、扱われます。
262         * 又、$Cには自分自身のカラム名を割り当てます。
263         *
264         * @og.rev 2.0.0.3 (2002/09/26) optionAttributes 属性に "$i" を使うとその行数に置き換る機能を追加。
265         * @og.rev 3.4.0.1 (2003/09/03) リアルタイムで値を作成する様に変更。
266         * @og.rev 3.5.5.0 (2004/03/12) 名前と行番号の区切り記号("__")を、HybsSystem.JOINT_STRING  に変更。
267         * @og.rev 3.5.5.5 (2004/04/23) 新規に Attributes オブジェクトを作成する方式を止めます。
268         * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない
269         * @og.rev 5.1.3.0 (2010/02/01) 一覧表示のみで、ツールチップ表示を行う。
270         * @og.rev 5.1.7.0 (2010/06/01) 動的プルダウン実装見直し
271         * @og.rev 5.5.1.0 (2012/04/03) Slabel対応
272         * @og.rev 6.4.5.3 (2016/05/13) value は、コロン区切りの先頭だけ分離する。
273         *
274         * @param       row   行番号
275         * @param       value 入力値
276         *
277         * @return      データ表示/編集用の文字列
278         * @og.rtnNotNull
279         */
280        @Override
281        public String getValue( final int row,final String value ) {
282                final boolean useSlbl = "auto".equalsIgnoreCase( useSLabel ) || "true".equalsIgnoreCase( useSLabel );           // 5.5.1.0 (2012/04/03)
283                final String newName = name + HybsSystem.JOINT_STRING + row; // 4.3.6.0 (2009/04/01)
284                final String newValue = StringFormat.getValue( value );                         // 6.4.5.3 (2016/05/13) コロン区切りの先頭だけ
285
286                // select タグの作成
287                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
288                return getOption(
289                                new TagBuffer( "select" )
290                                        .add( "name"    , newName )                                                                             // 4.3.6.0 (2009/04/01)
291                                        .add( "id"              , newName , isNull( attributes.get( "id" ) ) )  // 4.3.7.2 (2009/06/15)
292                                        .add( tagBuffer.makeTag() )
293                                , value
294                                , useSlbl
295                        ).makeTag( row,newValue ) ;             // 6.4.5.3 (2016/05/13)  ※ 元は、makeTag() でしたが、バグ? の可能性がある。
296        }
297
298        /**
299         * 初期値が選択済みの 選択肢(オプション)をTagBuffer に反映します。
300         * このオプションは、引数の値を初期値とするオプションタグ作成し、TagBuffer
301         * に値を設定して返します。
302         *
303         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
304         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
305         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
306         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
307         * 変数は、""(ゼロ文字列)として、扱われます。
308         * 又、$Cには自分自身のカラム名を割り当てます。
309         *
310         * @og.rev 3.5.5.7 (2004/05/10) getOption( String value )の廃止を受けて、新規作成
311         * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します
312         * @og.rev 4.0.0.0 (2006/11/15) SelectionFactory に lang 属性を追加します。
313         * @og.rev 4.3.4.0 (2008/12/01) $Cのカラム名置換えを追加
314         * @og.rev 5.1.3.0 (2010/02/01) 一覧表示のみで、ツールチップ表示を行う。
315         * @og.rev 6.0.4.0 (2014/11/28) useMultiSelect は、selection ではなく、colomn から取得する。
316         * @og.rev 6.2.0.0 (2015/02/27) キー:ラベル形式で表示するかどうかを、指定できるようにします。
317         *
318         * @param       buf    タグ文字列のバッファー
319         * @param       value  選択されている値
320         * @param   useSlbl ラベル(短)をベースとしたオプション表示を行うかどうか。
321         *
322         * @return      オプションタグ
323         * @og.rtnNotNull
324         */
325        private TagBuffer getOption( final TagBuffer buf,final String value,final boolean useSlbl ) {
326
327                // StringFormat format = new StringFormat( query,value);
328                final StringFormat format = new StringFormat( query, value, name ); // 4.3.4.0 (2008/12/01)
329                final String newQuery = format.format();
330                final String newValue = format.getValue();
331
332                // 6.2.0.0 (2015/02/27) キー:ラベル形式
333                final Selection selection = SelectionFactory.newDBSelection( newQuery, dbid, lang, addKeyLabel );
334                // 6.0.4.0 (2014/11/28) useMultiSelect は、selection ではなく、colomn から取得する。
335
336                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
337                return buf.addBody( Selection.NO_VALUE_OPTION , addNoValue )                    // 5.5.1.0 (2012/04/03)
338                                  .addBody( selection.getOption( newValue, seqFlag, useSlbl ) );
339        }
340}