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.fukurou.util.Attributes;
019import org.opengion.fukurou.util.TagBuffer;
020import org.opengion.fukurou.util.XHTMLTag;
021import org.opengion.hayabusa.common.HybsSystem;
022import org.opengion.hayabusa.common.HybsSystemException;
023import org.opengion.hayabusa.db.AbstractEditor;
024import org.opengion.hayabusa.db.CellEditor;
025import org.opengion.hayabusa.db.DBColumn;
026import org.opengion.hayabusa.db.SelectionCellEditor;                                    // 6.2.2.0 (2015/03/27)
027import org.opengion.hayabusa.db.Selection;
028import org.opengion.hayabusa.db.SelectionFactory;
029import org.opengion.fukurou.util.StringFormat;
030
031/**
032 * DBRADIO エディターは、カラムの編集パラメーターのSQL文の実行結果より、動的にラジオボタンを
033 * 作成して編集する場合に使用するエディタークラスです。
034 *
035 * 編集パラメータには、ラジオボタンを作成するための、SQL文を記述します。
036 * このSQL文は、select KEY,LABEL from xx ・・・ という構文で、KEY部分とLABEL部分が
037 * 選択されます。各カラムの意味は次のようになります。
038 *  第1カラム(必須) : ラジオボタンのキー(値)
039 *  第2カラム       : ラベル(指定されない場合は、ラベルリソースの短縮ラベルを使用します)
040 *  第3カラム       : クラス そのオプションに色づけなどを行う為の指定します。
041 *                     NULL(または、ゼロ文字列)の場合は、適用されません。
042 *  第4カラム       : この値は'false'又は'0'である場合にそのラジオボタンを選択不可にします。
043 *                     NULL(または、ゼロ文字列)の場合は、選択可能になります。
044 *
045 * 各カラムの値(value値)に、AAA:BBB:CCC:DDD という値を設定できます。これは、
046 * $1,$2,$3,$4 に割り当てなおして、QUERYを実行します。また、$1 は、本来の値として、
047 * メニューの初期値設定等に使用します。上記の例では、AAA が値で、それ以降は、
048 * 引数になります。
049 * 又、$Cには自分自身のカラム名を割り当てます。
050 * この機能を使用すれば、動的メニューを行ごとに条件を変えて作成することが
051 * 可能になります。
052 * 例:select KEY,LABEL from xx where KUBUN='$2' and CDK='$3'
053 * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
054 * 変数は、""(ゼロ文字列)として、扱われます。
055 *
056 * このエディタはeventColumnに対応していません。
057 *
058 *  カラムの表示に必要な属性は、DBColumn オブジェクト より取り出します。
059 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
060 *
061 * @og.rev 4.3.3.6 (2008/11/15) 新規作成
062 * @og.rev 6.2.2.0 (2015/03/27) SelectionCellEditor I/Fを追加
063 * @og.group データ編集
064 *
065 * @version  4.0
066 * @author       Hiroki Nakamura
067 * @since    JDK5.0,
068 */
069public class Editor_DBRADIO extends AbstractEditor implements SelectionCellEditor {
070        /** このプログラムのVERSION文字列を設定します。   {@value} */
071        private static final String VERSION = "7.0.5.1 (2019/09/27)" ;
072
073        private final String query ;
074        private final String dbid ;
075        private final String lang;
076        private final boolean writable ;
077
078        /**
079         * デフォルトコンストラクター。
080         * このコンストラクターで、基本オブジェクトを作成します。
081         *
082         */
083        public Editor_DBRADIO() {
084                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
085                // 4.3.4.4 (2009/01/01)
086                query   = null;
087                dbid    = null;
088                lang    = null;
089                writable  = false;
090        }
091
092        /**
093         * DBColumnオブジェクトを指定したprivateコンストラクター。
094         *
095         * @og.rev 6.0.4.0 (2014/11/28) optionAttributes は、コンストラクタで設定します。
096         * @og.rev 6.4.4.2 (2016/04/01) nameのfinal化
097         * @og.rev 7.0.5.1 (2019/09/27) optionAttributes が二重に設定されていたため、削除
098         *
099         * @param       clm     DBColumnオブジェクト
100         */
101        private Editor_DBRADIO( final DBColumn clm ) {
102        //      super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
103                super( clm );   // 6.4.4.2 (2016/04/01) nameのfinal化
104        //      name  = clm.getName();
105                dbid = clm.getDbid();
106                lang = clm.getLang();
107
108                query = clm.getEditorParam();
109                if( query == null || query.isEmpty() ) {
110                        final String errMsg = "DBRADIO Editor では、編集パラメータは必須です。"
111                                        + " name=[" + name + "]" + CR ;
112                        throw new HybsSystemException( errMsg );
113                }
114
115                writable = clm.isWritable();
116
117                // 6.1.1.0 (2015/01/17) Attributesの連結記述
118                attributes = new Attributes()
119                                        .set( clm.getEditorAttributes() )                               // #addAttributes( Attributes ) の代替え
120                                        .add( "class","RADIO" );
121
122                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
123                // 7.0.5.1 (2019/09/27) optionAttributes が二重に設定されていたため、削除
124                tagBuffer.add( XHTMLTag.inputAttri( attributes ) );
125//                               .add( attributes.get( "optionAttributes" ) );          // 6.0.4.0 (2014/11/28)
126        }
127
128        /**
129         * 各オブジェクトから自分のインスタンスを返します。
130         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
131         * まかされます。
132         *
133         * @param       clm     DBColumnオブジェクト
134         *
135         * @return      CellEditorオブジェクト
136         * @og.rtnNotNull
137         */
138        public CellEditor newInstance( final DBColumn clm ) {
139                return new Editor_DBRADIO( clm );
140        }
141
142        /**
143         * データの編集用文字列を返します。
144         *
145         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
146         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
147         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
148         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
149         * 変数は、""(ゼロ文字列)として、扱われます。
150         * 又、$Cには自分自身のカラム名を割り当てます。
151         *
152         * @og.rev 4.3.4.0 (2008/12/01) $Cのカラム名置換えを追加
153         * @og.rev 6.2.2.4 (2015/04/24) getRadio廃止。getOption として、引数違いとして用意する。
154         *
155         * @param       value 入力値
156         *
157         * @return      データの編集用文字列
158         * @og.rtnNotNull
159         */
160        @Override
161        public String getValue( final String value ) {
162                // StringFormat format = new StringFormat( query,value);
163                final StringFormat format = new StringFormat( query, value, name ); // 4.3.4.0 (2008/12/01)
164                final String newQuery = format.format();
165                final String newValue = format.getValue();
166                final Selection selection = SelectionFactory.newDBRadioSelection( newQuery,dbid,lang );
167
168                final String radio ;
169                if( writable ) {
170                        radio = selection.getOption( name,newValue,true );              // 6.2.2.4 (2015/04/24)
171                }
172                else {
173                        radio = selection.getValueLabel( newValue );
174                }
175
176                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
177                return new TagBuffer( "pre" )
178                                        .add( tagBuffer.makeTag() )
179                                        .addBody( radio )
180                                        .makeTag();
181        }
182
183        /**
184         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
185         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し、
186         * リクエスト情報を1つ毎のフィールドで処理できます。
187         *
188         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
189         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
190         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
191         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
192         * 変数は、""(ゼロ文字列)として、扱われます。
193         * 又、$Cには自分自身のカラム名を割り当てます。
194         *
195         * @og.rev 4.3.4.0 (2008/12/01) $Cのカラム名置換えを追加
196         * @og.rev 6.2.2.4 (2015/04/24) getRadio廃止。getOption として、引数違いとして用意する。
197         * @og.rev 6.4.5.3 (2016/05/13) value は、コロン区切りの先頭だけ分離する。
198         *
199         * @param       row   行番号
200         * @param       value 入力値
201         *
202         * @return      データ表示/編集用の文字列
203         * @og.rtnNotNull
204         */
205        @Override
206        public String getValue( final int row,final String value ) {
207                // StringFormat format = new StringFormat( query,value);
208                final StringFormat format = new StringFormat( query, value, name ); // 4.3.4.0 (2008/12/01)
209                final String newQuery = format.format();
210                final String newValue = format.getValue();
211                final Selection selection = SelectionFactory.newDBRadioSelection( newQuery,dbid,lang );
212
213                final String radio ;
214                if( writable ) {
215                        radio = selection.getOption( name + HybsSystem.JOINT_STRING + row,newValue,true );
216                }
217                else {
218                        radio = selection.getValueLabel( newValue );            // 6.2.2.4 (2015/04/24)
219                }
220
221                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
222                return new TagBuffer( "pre" )
223                                        .add( tagBuffer.makeTag() )
224                                        .addBody( radio )
225                                        .makeTag( row,newValue );                                       // 6.4.5.3 (2016/05/13)
226        }
227}