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