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