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