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.HybsSystemException;
019
020import org.opengion.hayabusa.db.AbstractRenderer;
021import org.opengion.hayabusa.db.CellRenderer;
022import org.opengion.hayabusa.db.DBColumn;
023import org.opengion.hayabusa.db.Selection;
024import org.opengion.hayabusa.db.SelectionFactory;
025import org.opengion.fukurou.util.StringFormat;
026
027/**
028 * DBMENU レンデラーは、表示パラメータで指定された SQL文を実行し、
029 * プルダウンメニューで表示する場合に使用するクラスです。
030 *
031 * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
032 * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
033 * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
034 * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
035 * 変数は、""(ゼロ文字列)として、扱われます。
036 * 又、$Cには自分自身のカラム名を割り当てます。
037 *
038 *  カラムの表示に必要な属性は、DBColumn オブジェクト より取り出します。
039 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
040 *
041 * @og.rev 3.2.3.0 (2003/06/06) 新規作成
042 * @og.rev 3.4.0.1 (2003/09/03) DB検索をリアルタイムに変更。
043 * @og.rev 3.5.4.2 (2003/12/15) 継承元クラスを、Editor_DBMENU から AbstractRenderer に変更。
044 * @og.group データ表示
045 *
046 * @version  4.0
047 * @author   Kazuhiko Hasegawa
048 * @since    JDK5.0,
049 */
050public class Renderer_DBMENU extends AbstractRenderer {
051        /** このプログラムのVERSION文字列を設定します。   {@value} */
052        private static final String VERSION = "6.2.0.0 (2015/02/27)" ;
053
054        private final String query ;
055        private final String dbid ;
056        private final String lang ;                             // 4.0.0 (2006/11/15)
057        private final String name  ;
058        private final String useSLabel ;                // 5.5.1.0 (2012/04/03)
059        private final String noDisplayVal ;             // 5.6.2.3 (2013/03/22)
060        private final String addKeyLabel ;              // 6.2.0.0 (2015/02/27) キー:ラベル形式
061
062        /**
063         * デフォルトコンストラクター。
064         * このコンストラクターで、基本オブジェクトを作成します。
065         *
066         * @og.rev 3.4.0.2 (2003/09/05) クラス内部見直し。query の final 化
067         * @og.rev 3.5.4.2 (2003/12/15) name , query 変数を、初期設定しておきます。
068         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数初期化
069         * @og.rev 6.2.0.0 (2015/02/27) キー:ラベル形式で表示するかどうかを、指定できるようにします。
070         */
071        public Renderer_DBMENU() {
072                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
073                query   = null;                 // 3.5.4.2 (2003/12/15)
074                dbid    = null;
075                lang    = null;                 // 4.0.0 (2006/11/15)
076                name    = null;                 // 3.5.4.2 (2003/12/15)
077                useSLabel        = "auto";      // 5.5.1.0 (2012/04/03)
078                noDisplayVal = null;    // 5.5.1.0 (2012/04/03)
079                addKeyLabel  = null;    // 6.2.0.0 (2015/02/27) キー:ラベル形式
080        }
081
082        /**
083         * DBColumnオブジェクトを指定したprivateコンストラクター。
084         *
085         * @og.rev 3.3.1.1 (2003/07/03) CodeSelection の設定において、バグ修正。
086         * @og.rev 3.4.0.1 (2003/09/03) 継承の親元の変更に伴う実装の移動。
087         * @og.rev 3.5.4.2 (2003/12/15) 継承元クラスを、Editor_DBMENU から AbstractRenderer に変更。
088         * @og.rev 3.5.5.9 (2004/06/07) getRendererParam 属性が null の場合は、エラーとします。
089         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
090         * @og.rev 6.2.0.0 (2015/02/27) キー:ラベル形式で表示するかどうかを、指定できるようにします。
091         *
092         * @param       clm     DBColumnオブジェクト
093         */
094        private Renderer_DBMENU( final DBColumn clm ) {
095                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
096                name    = clm.getName();
097                query   = clm.getRendererParam();
098                dbid    = clm.getDbid();
099                lang    = clm.getLang();                                // 4.0.0 (2006/11/15)
100                useSLabel        = clm.getUseSLabel() ;         // 5.5.1.0 (2012/04/03)
101                noDisplayVal = clm.getNoDisplayVal();   // 5.6.2.3 (2013/03/22)
102                addKeyLabel  = clm.getAddKeyLabel();    // 6.2.0.0 (2015/02/27) キー:ラベル形式
103
104                // 3.5.5.9 (2004/06/07)
105                if( query == null || query.isEmpty() ) {
106                        final String errMsg = "DBMENU Renderer では、表示パラメータは必須です。"
107                                        + " name=[" + name + "]" + CR ;
108                        throw new HybsSystemException( errMsg );
109                }
110        }
111
112        /**
113         * 各オブジェクトから自分のインスタンスを返します。
114         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
115         * まかされます。
116         *
117         * @param       clm     DBColumnオブジェクト
118         *
119         * @return      CellRendererオブジェクト
120         * @og.rtnNotNull
121         */
122        public CellRenderer newInstance( final DBColumn clm ) {
123                return new Renderer_DBMENU( clm );
124        }
125
126        /**
127         * データの表示用文字列を返します。
128         *
129         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
130         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
131         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
132         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
133         * 変数は、""(ゼロ文字列)として、扱われます。
134         *
135         * @og.rev 3.4.0.1 (2003/09/03) DB検索をリアルタイムに変更。
136         * @og.rev 3.4.0.2 (2003/09/05) AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てます。
137         * @og.rev 3.5.4.2 (2003/12/15) makeCodeSelection メソッドを CodeSelectionクラスに変更。
138         * @og.rev 3.5.5.7 (2004/05/10) SelectionFactory を使用して、オブジェクト作成
139         * @og.rev 4.0.0.0 (2006/11/15) SelectionFactory に lang 属性を追加します。
140         * @og.rev 4.3.4.0 (2008/12/01) $Cのカラム名置換えを追加
141         * @og.rev 5.5.1.0 (2012/04/03) Slabel対応。"auto"は、false になります。
142         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
143         * @og.rev 6.0.4.0 (2014/11/28) ロジックの共通化
144         *
145         * @param   value 入力値
146         *
147         * @return  データの表示用文字列
148         */
149        @Override
150        public String getValue( final String value ) {
151                final boolean useSlbl = "true".equalsIgnoreCase( useSLabel );           // 5.5.1.0 (2012/04/03)
152                return getSelectionValue( value,useSlbl );
153        }
154
155        /**
156         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
157         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し、
158         * リクエスト情報を1つ毎のフィールドで処理できます。
159         *
160         * @og.rev 4.0.0.0 (2005/11/30) 一覧表示では、短縮ラベルを使用します。
161         * @og.rev 4.0.0.0 (2006/11/15) SelectionFactory に lang 属性を追加します。
162         * @og.rev 4.3.4.0 (2008/12/01) $Cのカラム名置換えを追加
163         * @og.rev 5.5.1.0 (2012/04/03) Slabel対応。"auto"は、true になります。
164         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
165         * @og.rev 6.0.4.0 (2014/11/28) ロジックの共通化
166         *
167         * @param   row   行番号
168         * @param   value 入力値
169         *
170         * @return  データ表示/編集用の文字列
171         */
172        @Override
173        public String getValue( final int row,final String value ) {
174                final boolean useSlbl = "auto".equalsIgnoreCase( useSLabel ) || "true".equalsIgnoreCase( useSLabel );           // 5.5.1.0 (2012/04/03)
175                return getSelectionValue( value,useSlbl );
176        }
177
178        /**
179         * データの表示用文字列を返します。
180         *
181         * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、
182         * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に
183         * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。
184         * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない
185         * 変数は、""(ゼロ文字列)として、扱われます。
186         *
187         * @og.rev 6.0.4.0 (2014/11/28) ロジックの共通化
188         * @og.rev 6.2.0.0 (2015/02/27) キー:ラベル形式で表示するかどうかを、指定できるようにします。
189         *
190         * @param   value       入力値 表示文字列
191         * @param       useSlbl SLabelを使うかどうか(true:使う/false:使わない)
192         *
193         * @return  データ表示用の文字列
194         */
195        private String getSelectionValue( final String value,final boolean useSlbl ) {
196                // 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
197                if( noDisplayVal != null && noDisplayVal.equalsIgnoreCase( value ) ) { return "" ; }
198
199                // StringFormat format = new StringFormat( query,value);
200                final StringFormat format = new StringFormat( query, value, name );     // 4.3.4.0 (2008/12/01)
201                final String newQuery = format.format();
202                final String newValue = format.getValue();
203
204                // 6.2.0.0 (2015/02/27) キー:ラベル形式
205                final Selection selection = SelectionFactory.newDBSelection( newQuery,dbid,lang, addKeyLabel );
206
207                return selection.getValueLabel( newValue,useSlbl );
208        }
209}