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                query   = null;
093                dbid    = null;
094                lang    = null;                 // 4.0.0 (2006/11/15)
095                addNoValue = false;             // 3.5.5.7 (2004/05/10)
096                seqFlag    = false;             // 3.6.0.6 (2004/10/22)
097                useSLabel = "auto";             // 5.5.1.0 (2012/04/03)
098        }
099
100        /**
101         * コンストラクター。
102         *
103         * @og.rev 3.3.1.1 (2003/07/03) name , attributes 属性を final にする。
104         * @og.rev 3.4.0.1 (2003/09/03) 継承の親元の変更に伴う実装の移動。
105         * @og.rev 3.5.5.7 (2004/05/10) addNoValue 属性を追加します。
106         * @og.rev 3.5.5.9 (2004/06/07) editorParam 属性が null の場合は、エラーとします。
107         * @og.rev 3.5.6.0 (2004/06/18) XHTMLTag の 内部配列 SELECT_KEY を隠蔽します。
108         * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します
109         * @og.rev 5.1.7.0 (2010/06/01) 動的プルダウン実装見直し
110         * @og.rev 5.5.1.0 (2012/04/03) Slabel対応
111         *
112         * @param       clm     DBColumnオブジェクト
113         */
114        private Editor_DBMENU( final DBColumn clm ) {
115        //      super( clm );
116                name            = clm.getName();
117                addNoValue      = clm.isAddNoValue() ;          // 3.5.5.7 (2004/05/10)
118                query           = clm.getEditorParam();
119                dbid            = clm.getDbid();
120                lang            = clm.getLang();                        // 4.0.0 (2006/11/15)
121                seqFlag         = false;                // 3.6.0.6 (2004/10/22)
122                useSLabel = clm.getUseSLabel() ;        // 5.5.1.0 (2012/04/03)
123
124                // 3.5.5.9 (2004/06/07)
125                if( query == null || query.length() == 0 ) {
126                        String errMsg = "DBMENU Editor では、編集パラメータは必須です。"
127                                        + " name=[" + name + "]" + HybsSystem.CR ;
128                        throw new HybsSystemException( errMsg );
129                }
130
131                String  disabled = clm.isWritable() ? null : "disabled" ;
132
133                attributes = new Attributes();
134                attributes.set( "disabled"      ,disabled );
135
136                attributes.addAttributes( clm.getEditorAttributes() );
137                optAttr = attributes.get( "optionAttributes" );
138                tagBuffer.add( XHTMLTag.selectAttri( attributes ) );
139        }
140
141        /**
142         * 各オブジェクトから自分のインスタンスを返します。
143         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
144         * まかされます。
145         *
146         * @param       clm     DBColumnオブジェクト
147         *
148         * @return      CellEditorオブジェクト
149         */
150        public CellEditor newInstance( final DBColumn clm ) {
151                return new Editor_DBMENU( clm );
152        }
153
154        /**
155         * データの編集用文字列を返します。
156         *
157         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
158         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
159         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
160         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
161         * 変数は、""(ゼロ文字列)として、扱われます。
162         * 又、$Cには自分自身のカラム名を割り当てます。
163         *
164         * @og.rev 3.4.0.1 (2003/09/03) リアルタイムで値を作成する様に変更。
165         * @og.rev 3.5.5.5 (2004/04/23) 新規に Attributes オブジェクトを作成する方式を止めます。
166         * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない
167         * @og.rev 5.1.3.0 (2010/02/01) 一覧表示のみで、ツールチップ表示を行う。
168         * @og.rev 5.1.7.0 (2010/06/01) 動的プルダウン実装見直し
169         * @og.rev 5.5.1.0 (2012/04/03) Slabel対応
170         *
171         * @param       value 入力値
172         *
173         * @return      データの編集用文字列
174         */
175        @Override
176        public String getValue( final String value ) {
177                final boolean uslbl = "true".equalsIgnoreCase( useSLabel );             // 5.5.1.0 (2012/04/03)
178
179                TagBuffer tag = new TagBuffer( "select" );
180                tag.add( "name"    , name );
181                if( attributes.get( "id" ) == null || attributes.get( "id" ).length() == 0 ) { // 4.3.7.2 (2009/06/15)
182                        tag.add( "id"      , name ); // 4.3.6.0 (2009/04/01)
183                }
184                tag.add( tagBuffer.makeTag() );
185                tag.add( optAttr );             // 3.5.5.8 (2004/05/20)
186
187                // 5.1.3.0 (2010/02/01)
188                tag = getOption( tag,value,uslbl ); // 5.5.1.0 (2012/04/03)
189
190                return tag.makeTag();
191        }
192
193        /**
194         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
195         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し,
196         * リクエスト情報を1つ毎のフィールドで処理できます。
197         *
198         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
199         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
200         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
201         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
202         * 変数は、""(ゼロ文字列)として、扱われます。
203         * 又、$Cには自分自身のカラム名を割り当てます。
204         *
205         * @og.rev 2.0.0.3 (2002/09/26) optionAttributes 属性に "$i" を使うとその行数に置き換る機能を追加。
206         * @og.rev 3.4.0.1 (2003/09/03) リアルタイムで値を作成する様に変更。
207         * @og.rev 3.5.5.0 (2004/03/12) 名前と行番号の区切り記号("__")を、HybsSystem.JOINT_STRING  に変更。
208         * @og.rev 3.5.5.5 (2004/04/23) 新規に Attributes オブジェクトを作成する方式を止めます。
209         * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない
210         * @og.rev 5.1.3.0 (2010/02/01) 一覧表示のみで、ツールチップ表示を行う。
211         * @og.rev 5.1.7.0 (2010/06/01) 動的プルダウン実装見直し
212         * @og.rev 5.5.1.0 (2012/04/03) Slabel対応
213         *
214         * @param       row   行番号
215         * @param       value 入力値
216         *
217         * @return      データ表示/編集用の文字列
218         */
219        @Override
220        public String getValue( final int row,final String value ) {
221                final boolean uslbl = "auto".equalsIgnoreCase( useSLabel ) || "true".equalsIgnoreCase( useSLabel );             // 5.5.1.0 (2012/04/03)
222
223                TagBuffer tag = new TagBuffer( "select" );
224                String newName = name + HybsSystem.JOINT_STRING + row; // 4.3.6.0 (2009/04/01)
225                //tag.add( "name"    , name + HybsSystem.JOINT_STRING + row );
226                tag.add( "name", newName ); // 4.3.6.0 (2009/04/01)
227                if( attributes.get( "id" ) == null || attributes.get( "id" ).length() == 0 ) { // 4.3.7.2 (2009/06/15)
228                        tag.add( "id"    , newName ); // 4.3.6.0 (2009/04/01)
229                }
230                tag.add( tagBuffer.makeTag() );
231                tag.add( optAttr );             // 3.5.5.8 (2004/05/20)
232
233                // 5.1.3.0 (2010/02/01)
234                tag = getOption( tag,value,uslbl );
235
236                return tag.makeTag();
237        }
238
239        /**
240         * 初期値が選択済みの 選択肢(オプション)をTagBuffer に反映します。
241         * このオプションは、引数の値を初期値とするオプションタグ作成し、TagBuffer
242         * に値を設定して返します。
243         *
244         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
245         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
246         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
247         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
248         * 変数は、""(ゼロ文字列)として、扱われます。
249         * 又、$Cには自分自身のカラム名を割り当てます。
250         *
251         * @og.rev 3.5.5.7 (2004/05/10) getOption( String value )の廃止を受けて、新規作成
252         * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します
253         * @og.rev 4.0.0.0 (2006/11/15) SelectionFactory に lang 属性を追加します。
254         * @og.rev 4.3.4.0 (2008/12/01) $Cのカラム名置換えを追加
255         * @og.rev 5.1.3.0 (2010/02/01) 一覧表示のみで、ツールチップ表示を行う。
256         *
257         * @param       buf    タグ文字列のバッファー
258         * @param       value  選択されている値
259         * @param   useShortLabel ラベル(短)をベースとしたオプション表示を行うかどうか。
260         *
261         * @return      オプションタグ
262         */
263        private TagBuffer getOption( final TagBuffer buf,final String value,final boolean useShortLabel ) {
264
265                // StringFormat format = new StringFormat( query,value);
266                StringFormat format = new StringFormat( query, value, name ); // 4.3.4.0 (2008/12/01)
267                String newQuery = format.format();
268                String newValue = format.getValue();
269
270                Selection selection = SelectionFactory.newDBSelection( newQuery, dbid, lang );
271                boolean useMultiSelect = selection.useMultiSelect();
272                if( useMultiSelect ) {
273                        buf.add( "onkeydown", "setKeySelect(this);" );
274                }
275
276                if( addNoValue ) {
277                        // 5.1.3.0 (2010/02/01)
278                        buf.setBody( Selection.NO_VALUE_OPTION + selection.getOption( newValue, seqFlag, useShortLabel ) );
279                }
280                else {
281                        // 5.1.3.0 (2010/02/01)
282                        buf.setBody( selection.getOption( newValue, seqFlag, useShortLabel ) );
283                }
284
285                return buf;
286        }
287}