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                query   = null;
084                dbid    = null;
085                lang    = null;
086                writable  = false;
087        }
088
089        /**
090         * コンストラクター。
091         *
092         * @param       clm     DBColumnオブジェクト
093         */
094        private Editor_DBRADIO( final DBColumn clm ) {
095                name  = clm.getName();
096                dbid = clm.getDbid();
097                lang = clm.getLang();
098
099                query = clm.getEditorParam();
100                if( query == null || query.length() == 0 ) {
101                        String errMsg = "DBRADIO Editor では、編集パラメータは必須です。"
102                                        + " name=[" + name + "]" + HybsSystem.CR ;
103                        throw new HybsSystemException( errMsg );
104                }
105
106                writable = clm.isWritable();
107
108                attributes = new Attributes();
109                attributes.addAttributes( clm.getEditorAttributes() );
110
111                attributes.add( "class","RADIO" );
112                tagBuffer.add( XHTMLTag.inputAttri( attributes ) );
113
114                optAttr = attributes.get( "optionAttributes" );
115        }
116
117        /**
118         * 各オブジェクトから自分のインスタンスを返します。
119         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
120         * まかされます。
121         *
122         * @param       clm     DBColumnオブジェクト
123         *
124         * @return      CellEditorオブジェクト
125         */
126        public CellEditor newInstance( final DBColumn clm ) {
127                return new Editor_DBRADIO( clm );
128        }
129
130        /**
131         * データの編集用文字列を返します。
132         *
133         * @og.rev 4.3.4.0 (2008/12/01) $Cのカラム名置換えを追加
134         *
135         * @param       value 入力値
136         *
137         * @return      データの編集用文字列
138         */
139        @Override
140        public String getValue( final String value ) {
141                // StringFormat format = new StringFormat( query,value);
142                StringFormat format = new StringFormat( query, value, name ); // 4.3.4.0 (2008/12/01)
143                String newQuery = format.format();
144                String newValue = format.getValue();
145                Selection selection = SelectionFactory.newDBRadioSelection( newQuery,dbid,lang );
146
147                final String radio ;
148                if( writable ) {
149                        radio = selection.getRadio( name,newValue,true );
150                }
151                else {
152                        radio = selection.getValueLabel( newValue );
153                }
154
155                TagBuffer tag = new TagBuffer( "pre" );
156                tag.add( tagBuffer.makeTag() );
157                tag.add( optAttr );
158                tag.setBody( radio );
159
160                return tag.makeTag();
161        }
162
163        /**
164         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
165         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し,
166         * リクエスト情報を1つ毎のフィールドで処理できます。
167         *
168         * @og.rev 4.3.4.0 (2008/12/01) $Cのカラム名置換えを追加
169         *
170         * @param       row   行番号
171         * @param       value 入力値
172         *
173         * @return      データ表示/編集用の文字列
174         */
175        @Override
176        public String getValue( final int row,final String value ) {
177                // StringFormat format = new StringFormat( query,value);
178                StringFormat format = new StringFormat( query, value, name ); // 4.3.4.0 (2008/12/01)
179                String newQuery = format.format();
180                String newValue = format.getValue();
181                Selection selection = SelectionFactory.newDBRadioSelection( newQuery,dbid,lang );
182
183                final String radio ;
184                if( writable ) {
185                        radio = selection.getRadio( name + HybsSystem.JOINT_STRING + row,newValue,true );
186                }
187                else {
188                        radio = selection.getRadioLabel( newValue );
189                }
190
191                TagBuffer tag = new TagBuffer( "pre" );
192                tag.add( tagBuffer.makeTag() );
193                tag.setBody( radio );
194                tag.add( optAttr );
195
196                return tag.makeTag( row,value );
197        }
198}