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.hayabusa.db;
017
018import org.opengion.fukurou.system.LogWriter;
019import org.opengion.hayabusa.resource.ResourceFactory;                          // 6.2.6.0 (2015/06/19)
020import org.opengion.hayabusa.resource.ResourceManager;                          // 6.2.6.0 (2015/06/19)
021import org.opengion.fukurou.util.StringUtil;
022import static org.opengion.fukurou.system.HybsConst.CR ;                                // 6.1.0.0 (2014/12/26)
023import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE;      // 6.1.0.0 (2014/12/26) refactoring
024
025/**
026 * データのコード情報を取り扱うクラスです。
027 *
028 * 文字列の 「キー:ラベル キー:ラベル」の情報から、HTMLのメニューやリストを作成するための 
029 * オプションタグを作成したり、与えられたキーをもとに、チェック済みのオプションタグを
030 * 作成したりします。
031 * ラベル にスペースを含ませる場合は、ダブルクォーテーションで囲ってください。
032 *
033 * @og.rev 5.6.6.0 (2013/07/05) 新規追加
034 * @og.rev 5.7.7.1 (2014/06/13) Selection_NULL を 基本実装とします。
035 * @og.group 選択データ制御
036 *
037 * @version  4.0
038 * @author   Kazuhiko Hasegawa
039 * @since    JDK5.0,
040 */
041public class Selection_KEYVAL extends Selection_NULL {
042        private final String    ORG_KEYVAL ;
043
044        private final String    CACHE ;
045
046        /**
047         * コンストラクター
048         *
049         * 既存の、CodeData には存在しない、新しいコードリソースを作成する為の、文字列を指定します。
050         * 文字列は、「キー:ラベル キー:ラベル」形式で、スペースで分解後、":" でキーとラベルに分離します。
051         * スペース分解後の文字列に、":" が含まれていない場合は、キーをラベルとして扱います。
052         * また、ラベル部分は、ラベルリソースを使用して、変換を行います。
053         * 内部的には、CodeData を作成しません。DBColumnオブジェクト内で、直接、Selection_KEYVAL を生成します。
054         * codeName 、codeList、codeGroup などが指定された場合は、そちらが優先されます。
055         * 「キー:ラベル キー:ラベル」で、ラベル にスペースを含ませる場合は、ダブルクォーテーションで囲ってください。
056         * 「"キー:ラベル" "キー:ラベル"」という感じです。
057         *
058         * @og.rev 5.6.7.1 (2013/08/09) 「キー:ラベル キー:ラベル」分解に、クオート処理を加味
059         * @og.rev 6.2.6.0 (2015/06/19) type別Selectionの場合、ラベルリソースを使用する為、言語を引数で渡す。
060         *
061         * @param       strCode コードデータパラメータ文字列
062         * @param       lang  言語
063         */
064        public Selection_KEYVAL( final String strCode,final String lang ) {
065                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
066                ORG_KEYVAL = strCode ;
067
068                // 6.2.6.0 (2015/06/19)
069                final ResourceManager resource = ResourceFactory.newInstance( lang );
070
071                // 6.2.6.0 (2015/06/19) 仕様変更。コロン が無ければ、キーからラベルを求める。
072                // 6.4.1.1 (2016/01/16) PMD refactoring. Avoid if (x != y) ..; else ..;
073                if( strCode == null || strCode.isEmpty() ) {
074                        CACHE = "";
075                }
076                else {
077                        final String[] keyvals = StringUtil.csv2Array( strCode, ' ' );  // 5.6.7.1 (2013/08/09) クオート処理を加味
078                        final int size = keyvals.length;
079
080                        final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
081                        for( int i=0; i<size; i++ ) {
082                                final String keyval = keyvals[i] ;
083                                if( keyval == null || keyval.length() <= 0 ) { continue; }
084                                final int idx = keyval.indexOf( ':' );
085                                // 6.2.6.0 (2015/06/19) 仕様変更。コロン が無ければ、キーからラベルを求める。
086
087                                final String key = idx < 0 ? keyval : keyval.substring( 0,idx ).trim();
088                                final String val = resource.getLabel( 
089                                                                                idx < 0 ? keyval : keyval.substring( idx+1 ).trim()
090                                                                        );
091
092                                // 6.0.2.5 (2014/10/31) char を append する。
093                                buf.append( "<option value=\"" ).append( key )
094                                        .append( "\">" ).append( val ).append( "</option>" );
095                        }
096
097                        CACHE = buf.toString();
098                }
099        }
100
101        /**
102         * 初期値が選択済みの 選択肢(オプション)を返します。
103         * このオプションは、引数の値を初期値とするオプションタグを返します。
104         * このクラスでは、useShortLabel は、無視されます。(常に、false です)
105         *
106         * @param   selectValue  選択されている値
107         * @param   seqFlag  シーケンスアクセス機能 [true:ON/false:OFF]
108         * @param   useShortLabel ラベル(短)をベースとしたオプション表示を行うかどうか(常にfalse)。
109         *
110         * @return  オプションタグ
111         * @og.rtnNotNull
112         */
113        @Override
114        public String getOption( final String selectValue,final boolean seqFlag, final boolean useShortLabel ) {
115                // マッチするアドレスを探す。キーの前後のダブルクオートを加味して検索
116                final String selVal = "\"" + selectValue + "\"" ;
117
118                final int indx = CACHE.indexOf( selVal );
119
120                if( indx < 0 ) {
121                        // 4.0.0 (2005/01/31)
122                        if( selectValue != null && selectValue.length() > 0 ) {
123                                final String errMsg = "コードに存在しない値が指定されました。"
124                                                        + " value=[" + selectValue + "]"
125                                                        + CR + ORG_KEYVAL ;
126                                LogWriter.log( errMsg );
127                        }
128                        return CACHE;
129                }
130                else {
131                        final int addIndx = indx + selVal.length() ;    // selected の挿入位置
132
133                        final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
134                        // 3.6.0.6 (2004/10/22) シーケンスアクセス機能を指定する seqFlag を導入
135                        if( seqFlag ) {
136                                buf.append( "<option value=\"" ).append( selectValue ).append( '"' );           // 6.0.2.5 (2014/10/31) char を append する。
137                        }
138                        else {
139                                buf.append( CACHE.substring( 0,addIndx ) );
140                        }
141                        buf.append( " selected=\"selected\"" )
142                                .append( CACHE.substring( addIndx ) );
143                        return buf.toString() ;
144                }
145        }
146
147        /**
148         * 選択肢(value)に対するラベルを返します。
149         * 選択肢(value)が、存在しなかった場合は、選択肢そのものを返します。
150         * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じです。
151         *
152         * ※ このクラスでは、短縮ラベルは使用されません。
153         *
154         * @param       selectValue     選択肢の値
155         * @param       isSLbl  短縮ラベルを [true:使用する/false:しない](常に false)
156         *
157         * @return  選択肢のラベル
158         * @see     #getValueLabel( String )
159         */
160        @Override
161        public String getValueLabel( final String selectValue,final boolean isSLbl ) {
162                // マッチするアドレスを探す。キーの前後のダブルクオートを加味して検索
163                final String selVal = "\"" + selectValue + "\"" ;
164
165                final int indx = CACHE.indexOf( selVal );
166
167                if( indx < 0 ) {
168                        // マッチしなければ、選択肢そのものを返す。
169                        return selectValue;
170                }
171                else {
172                        // マッチすれば、キー以下のBODY部の文字列を切り出して返す。
173                        final int stIdx = indx + selVal.length() + 1 ;                  // +1 は、">" の位置
174                        final int edIdx = CACHE.indexOf( '<',stIdx );                           // 終了アドレス
175
176                        return CACHE.substring( stIdx,edIdx );
177                }
178        }
179
180}