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