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 * INDBMENU エディターは、カラムの表示パラメーターのSQL文を実行結果より、
035 * 作成したプルダウンメニューと、テキストフィールドによる入力の両方をサポートする、
036 * 編集に使用するクラスです。
037 *
038 * JavaScript によりテキストフィールドとメニュー(コンボボックス)を重ね合わせて
039 * 表示しておき、メニューで選択した値を、テキストフィールドに設定しています。
040 * このエディタを使用するには、jsp/common/inputMenu.js を予め使用できるように
041 * 設定しておく必要があります。
042 *
043 * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
044 * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
045 * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
046 * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
047 * 変数は、""(ゼロ文字列)として、扱われます。
048 *
049 * このエディタはeventColumnに対応していません。
050 *
051 *  カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
052 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
053 *
054 * @og.rev 3.5.6.2 (2004/07/05) 新規作成
055 * @og.rev 6.2.2.0 (2015/03/27) SelectionCellEditor I/Fを追加
056 * @og.group データ編集
057 *
058 * @version  4.0
059 * @author       Kazuhiko Hasegawa
060 * @since    JDK5.0,
061 */
062public class Editor_INDBMENU extends AbstractEditor implements SelectionCellEditor {
063        /** このプログラムのVERSION文字列を設定します。   {@value} */
064        private static final String VERSION = "6.4.5.3 (2016/05/13)" ;
065
066        private static final String SEL1 = "<script type=\"text/javascript\">makeInputMenu('" ;
067        private static final String SEL2 = "');</script>" ;
068
069        private final String query ;
070        private final String dbid ;
071        private final String lang ;                             // 4.0.0 (2006/11/15)
072        private final boolean addNoValue ;              // 3.5.5.7 (2004/05/10)
073        private final boolean seqFlag ;                 // 3.6.0.6 (2004/10/22)
074        private final String useSLabel ;                // 6.2.0.0 (2015/02/27) SLABEL 対応
075        private final String addKeyLabel ;              // 6.2.0.0 (2015/02/27) キー:ラベル形式
076
077        private final TagBuffer selTagBuffer = new TagBuffer() ;
078
079        /**
080         * デフォルトコンストラクター。
081         * このコンストラクターで、基本オブジェクトを作成します。
082         *
083         * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します
084         * @og.rev 6.2.0.0 (2015/02/27) SLABEL 対応
085         * @og.rev 6.2.0.0 (2015/02/27) キー:ラベル形式で表示するかどうかを、指定できるようにします。
086         * @og.rev 6.4.4.2 (2016/04/01) nameのfinal化
087         */
088        public Editor_INDBMENU() {
089                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
090                // 4.3.4.4 (2009/01/01)
091                query           = null;
092                dbid            = null;
093                lang            = null;                 // 4.0.0 (2006/11/15)
094                addNoValue      = false;                // 3.5.5.7 (2004/05/10)
095                seqFlag         = false;                // 3.6.0.6 (2004/10/22)
096        //      name            = null;                 // 4.3.4.0 (2008/12/01)
097                useSLabel       = "auto";               // 6.2.0.0 (2015/02/27) SLABEL 対応
098                addKeyLabel = null;                     // 6.2.0.0 (2015/02/27) キー:ラベル形式
099        }
100
101        /**
102         * コンストラクター。
103         *
104         * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します
105         * @og.rev 4.0.0.0 (2006/11/24) TextField分の属性設定
106         * @og.rev 5.6.3.0 (2013/04/01) プルダウンのonChangeの設定場所を変更
107         * @og.rev 6.2.0.0 (2015/02/27) SLABEL 対応
108         * @og.rev 6.2.0.0 (2015/02/27) キー:ラベル形式で表示するかどうかを、指定できるようにします。
109         * @og.rev 6.4.4.2 (2016/04/01) nameのfinal化
110         *
111         * @param       clm     DBColumnオブジェクト
112         */
113        private Editor_INDBMENU( final DBColumn clm ) {
114                super( clm );
115                tagBuffer.add( XHTMLTag.inputAttri( attributes ) );
116
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.0 (2006/11/15)
121                seqFlag         = false;                                        // 3.6.0.6 (2004/10/22)
122        //      name            = clm.getName();                        // 4.3.4.0 (2008/12/01) , 6.4.4.2 (2016/04/01) nameのfinal化
123                useSLabel       = clm.getUseSLabel() ;          // 6.2.0.0 (2015/02/27) SLABEL 対応
124                addKeyLabel = clm.getAddKeyLabel();             // 6.2.0.0 (2015/02/27) キー:ラベル形式
125
126                if( query == null || query.isEmpty() ) {
127                        final String errMsg = "INDBMENU Editor では、編集パラメータは必須です。"
128                                        + " name=[" + name + "]" + CR ;
129                        throw new HybsSystemException( errMsg );
130                }
131
132                final String disabled = clm.isWritable() ? null : "disabled" ;
133
134                // 6.1.1.0 (2015/01/17) Attributesの連結記述
135                final Attributes selAttri = new Attributes()
136                        .set( "disabled"        ,disabled )
137                        .set( clm.getEditorAttributes() )                               // #addAttributes( Attributes ) の代替え
138                        .set( "onChange" ,"selChanged(this);" );                // INDBMENU 特有のJavaScript 5.6.3.0 (2013/03/01) 場所変更
139
140                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
141                selTagBuffer.add( XHTMLTag.selectAttri( selAttri ) )
142                                        .add( selAttri.get( "optionAttributes" ) );             // 6.0.4.0 (2014/11/28)
143
144        }
145
146        /**
147         * 各オブジェクトから自分のインスタンスを返します。
148         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
149         * まかされます。
150         *
151         * @param       clm     DBColumnオブジェクト
152         *
153         * @return      CellEditorオブジェクト
154         * @og.rtnNotNull
155         */
156        public CellEditor newInstance( final DBColumn clm ) {
157                return new Editor_INDBMENU( clm );
158        }
159
160        /**
161         * データの編集用文字列を返します。
162         *
163         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
164         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
165         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
166         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
167         * 変数は、""(ゼロ文字列)として、扱われます。
168         *
169         * @og.rev 3.8.5.3 (2006/06/30) 位置を絶対位置指定(position:absolute;)
170         * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない
171         * @og.rev 6.2.0.0 (2015/02/27) SLABEL 対応
172         * @og.rev 6.4.5.3 (2016/05/13) value は、コロン区切りの先頭だけ分離する。
173         *
174         * @param       value 入力値
175         *
176         * @return      データの編集用文字列
177         * @og.rtnNotNull
178         */
179        @Override
180        public String getValue( final String value ) {
181                final String newValue = StringFormat.getValue( value );                         // 6.4.5.3 (2016/05/13) コロン区切りの先頭だけ
182
183                // input タグの作成
184                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
185                final String intag = new TagBuffer( "input" )
186                                                .add( "name"    , name )
187                                                .add( "id"              , name , isNull( attributes.get( "id" ) ) )     // 4.3.7.2 (2009/06/15)
188                                                .add( "value"   , newValue )                                                            // 6.4.5.3 (2016/05/13)
189                                                .add( "size"    , size1 )
190                                                .add( tagBuffer.makeTag() )
191                                                .makeTag();
192
193                final boolean useSlbl = "true".equalsIgnoreCase( useSLabel );           // 6.2.0.0 (2015/02/27)
194
195                // select タグの作成
196                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
197                final String seltag = getOption(
198                                        new TagBuffer( "select" )
199                                                .add( "id"              , name + ".sel" )                       // INDBMENU 特有のJavaScript用のキー
200                                                .add( "style"   , "position:absolute;" )        // 3.8.5.3 (2006/06/30) 位置を絶対位置指定
201                                                .add( selTagBuffer.makeTag() )
202                                        , value
203                                        , useSlbl                                                       // 6.2.0.0 (2015/02/27) SLABEL 対応
204                        ).makeTag() ;
205
206                return intag + CR + seltag + CR + SEL1 + name + SEL2;
207
208        }
209
210        /**
211         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
212         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し,
213         * リクエスト情報を1つ毎のフィールドで処理できます。
214         *
215         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
216         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
217         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
218         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
219         * 変数は、""(ゼロ文字列)として、扱われます。
220         *
221         * @og.rev 3.8.5.1 (2006/04/28) makeInputMenu 呼び出し時の引数記述ミスを修正
222         * @og.rev 3.8.5.3 (2006/06/30) 位置を絶対位置指定(position:absolute;)
223         * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない
224         * @og.rev 6.2.0.0 (2015/02/27) SLABEL 対応
225         * @og.rev 6.4.5.3 (2016/05/13) value は、コロン区切りの先頭だけ分離する。
226         *
227         * @param       row   行番号
228         * @param       value 入力値
229         *
230         * @return      データ表示/編集用の文字列
231         * @og.rtnNotNull
232         */
233        @Override
234        public String getValue( final int row,final String value ) {
235                final String name2 = name + HybsSystem.JOINT_STRING + row ;
236                final String newValue = StringFormat.getValue( value );                         // 6.4.5.3 (2016/05/13) コロン区切りの先頭だけ
237
238                // input タグの作成
239                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
240                final String intag = new TagBuffer( "input" )
241                                                .add( "name"    , name2 )
242                                                .add( "id"              , name2 , isNull( attributes.get( "id" ) ) )    // 4.3.7.2 (2009/06/15)
243                                                .add( "value"   , newValue )                    // 6.4.5.3 (2016/05/13)
244                                                .add( "size"    , size2 )
245                                                .add( tagBuffer.makeTag() )
246                                                .makeTag( row,newValue );                               // 6.4.5.3 (2016/05/13) [V] に割り当てる値。
247
248                final boolean useSlbl = "auto".equalsIgnoreCase( useSLabel ) || "true".equalsIgnoreCase( useSLabel );           // 6.2.0.0 (2015/02/27)
249
250                // select タグの作成
251                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
252                final String seltag = getOption(
253                                        new TagBuffer( "select" )
254                                                .add( "id"              , name2 + ".sel" )                      // INDBMENU 特有のJavaScript用のキー
255                                                .add( "style"   , "position:absolute;" )        // 3.8.5.3 (2006/06/30) 位置を絶対位置指定
256                                                .add( selTagBuffer.makeTag() )
257                                        , value
258                                        , useSlbl                                                       // 6.2.0.0 (2015/02/27) SLABEL 対応
259                        ).makeTag( row,newValue ) ;                                     // 6.4.5.3 (2016/05/13) [V] に割り当てる値。
260
261                return intag + CR + seltag + CR + SEL1 + name2 + SEL2;
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 6.2.0.0 (2015/02/27) SLABEL 対応
281         * @og.rev 6.2.0.0 (2015/02/27) キー:ラベル形式で表示するかどうかを、指定できるようにします。
282         *
283         * @param       buf  反映させるTagBufferオブジェクト
284         * @param       value 選択されている値
285         * @param   useSlbl ラベル(短)をベースとしたオプション表示を行うかどうか。
286         *
287         * @return      オプションタグ
288         * @og.rtnNotNull
289         */
290        private TagBuffer getOption( final TagBuffer buf,final String value,final boolean useSlbl ) {
291
292                // StringFormat format = new StringFormat( query,value );
293                final StringFormat format = new StringFormat( query, value, name );             // 4.3.4.0 (2008/12/01)
294                final String newQuery = format.format();
295                final String newValue = format.getValue();
296
297                // 6.2.0.0 (2015/02/27) キー:ラベル形式
298                final Selection selection = SelectionFactory.newDBSelection( newQuery, dbid, lang, addKeyLabel );
299
300                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
301                return buf.addBody( Selection.NO_VALUE_OPTION , addNoValue )                            // 5.5.1.0 (2012/04/03)
302                                  .addBody( selection.getOption( newValue, seqFlag, useSlbl ) );        // 6.2.0.0 (2015/02/27) SLABEL 対応
303
304        }
305}