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 = "6.4.5.3 (2016/05/13)" ;
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         * コンストラクター。
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         *
098         * @param       clm     DBColumnオブジェクト
099         */
100        private Editor_DBRADIO( final DBColumn clm ) {
101        //      super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
102                super( clm );   // 6.4.4.2 (2016/04/01) nameのfinal化
103        //      name  = clm.getName();
104                dbid = clm.getDbid();
105                lang = clm.getLang();
106
107                query = clm.getEditorParam();
108                if( query == null || query.isEmpty() ) {
109                        final String errMsg = "DBRADIO Editor では、編集パラメータは必須です。"
110                                        + " name=[" + name + "]" + CR ;
111                        throw new HybsSystemException( errMsg );
112                }
113
114                writable = clm.isWritable();
115
116                // 6.1.1.0 (2015/01/17) Attributesの連結記述
117                attributes = new Attributes()
118                                        .set( clm.getEditorAttributes() )                               // #addAttributes( Attributes ) の代替え
119                                        .add( "class","RADIO" );
120
121                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
122                tagBuffer.add( XHTMLTag.inputAttri( attributes ) )
123                                 .add( attributes.get( "optionAttributes" ) );          // 6.0.4.0 (2014/11/28)
124
125        }
126
127        /**
128         * 各オブジェクトから自分のインスタンスを返します。
129         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
130         * まかされます。
131         *
132         * @param       clm     DBColumnオブジェクト
133         *
134         * @return      CellEditorオブジェクト
135         * @og.rtnNotNull
136         */
137        public CellEditor newInstance( final DBColumn clm ) {
138                return new Editor_DBRADIO( clm );
139        }
140
141        /**
142         * データの編集用文字列を返します。
143         *
144         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
145         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
146         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
147         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
148         * 変数は、""(ゼロ文字列)として、扱われます。
149         * 又、$Cには自分自身のカラム名を割り当てます。
150         *
151         * @og.rev 4.3.4.0 (2008/12/01) $Cのカラム名置換えを追加
152         * @og.rev 6.2.2.4 (2015/04/24) getRadio廃止。getOption として、引数違いとして用意する。
153         *
154         * @param       value 入力値
155         *
156         * @return      データの編集用文字列
157         * @og.rtnNotNull
158         */
159        @Override
160        public String getValue( final String value ) {
161                // StringFormat format = new StringFormat( query,value);
162                final StringFormat format = new StringFormat( query, value, name ); // 4.3.4.0 (2008/12/01)
163                final String newQuery = format.format();
164                final String newValue = format.getValue();
165                final Selection selection = SelectionFactory.newDBRadioSelection( newQuery,dbid,lang );
166
167                final String radio ;
168                if( writable ) {
169                        radio = selection.getOption( name,newValue,true );              // 6.2.2.4 (2015/04/24)
170                }
171                else {
172                        radio = selection.getValueLabel( newValue );
173                }
174
175                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
176                return new TagBuffer( "pre" )
177                                        .add( tagBuffer.makeTag() )
178                                        .addBody( radio )
179                                        .makeTag();
180
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        }
228}