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