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 java.util.Set;
019import java.util.HashSet;
020import java.util.Arrays;
021
022import org.opengion.hayabusa.common.HybsSystemException;
023import org.opengion.hayabusa.resource.CodeData;
024import static org.opengion.fukurou.system.HybsConst.CR ;                                // 6.1.0.0 (2014/12/26)
025import static org.opengion.fukurou.system.HybsConst.BUFFER_LARGE;               // 6.1.0.0 (2014/12/26) refactoring
026
027/**
028 * データのコード情報を取り扱うクラスです。
029 *
030 * コードのキーとラベルの情報から、HTMLのチェックボックスを作成するための オプション
031 * タグを作成したり、与えられたキーをもとに、チェック済みのオプションタグを作成したり
032 * します。
033 *
034 * ※ このクラスは、CHBOX 用ではなく、CHBOX2 用です。
035 *
036 * @og.group 選択データ制御
037 * @og.rev 6.4.4.0 (2016/03/11) 新規追加
038 *
039 * @version  6.4
040 * @author   Kazuhiko Hasegawa
041 * @since    JDK8.0,
042 */
043public class Selection_CHBOX extends Selection_NULL {
044        private final CodeData codeData ;
045
046        /**
047         * コンストラクター
048         *
049         * @og.rev 6.4.4.0 (2016/03/11) 新規追加
050         *
051         * @param       cdData  コードデータオブジェクト
052         *
053         */
054        public Selection_CHBOX( final CodeData cdData ) {
055                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
056                if( cdData == null ) {
057                        final String errMsg = "コードリソースが定義されていません。" + CR ;
058                        throw new HybsSystemException( errMsg );
059                }
060
061                codeData = cdData ;
062        }
063
064        /**
065         * 初期値が選択済みの 選択肢(オプション)を返します。
066         * このオプションは、引数の値を初期値とするオプションタグを返します。
067         * ※ このクラスでは実装されていません。
068         *
069         * @og.rev 6.4.4.0 (2016/03/11) CHBOX2は、コードリソースも使用できるように変更。
070         *
071         * @param       selectValue     選択されている値
072         * @param       seqFlag シーケンスアクセス機能の指定
073         * @param       useShortLabel   短ラベルの指定
074         *
075         * @return  オプションタグ
076         */
077        @Override
078        public String getOption( final String selectValue,final boolean seqFlag, final boolean useShortLabel ) {
079                final String errMsg = "このクラスでは実装されていません。";
080                throw new UnsupportedOperationException( errMsg );
081        }
082
083        /**
084         * 初期値が選択済みの 選択肢(オプション)を返します。
085         * このオプションは、引数の値を初期値とするオプションタグを返します。
086         * 選択されている値は、複数指定が可能です。カンマ区切りデータとして渡された場合は、
087         * 個別に分解して、ラベル化します。
088         *
089         * @og.rev 6.4.4.0 (2016/03/11) CHBOX2は、コードリソースも使用できるように変更。
090         *
091         * @param   name         ラジオの name
092         * @param   selectValue  選択されている値
093         * @param   useLabel     ラベル表示の有無 [true:有/false:無]
094         *
095         * @return  オプションタグ
096         * @og.rtnNotNull
097         */
098        @Override
099        public String getOption( final String name,final String selectValue,final boolean useLabel ) {
100                // 選択値を、カンマで分解(split)して、Listを作成し、HashSetに渡している。存在チェック用
101                final Set<String> valSet = selectValue == null
102                                                                                ? new HashSet<>()
103                                                                                : new HashSet<>( Arrays.asList( selectValue.split( "," ) ) );
104
105                final String inputTag = "<input type=\"checkbox\" name=\"" + name + "\" value=\"" ;
106                final StringBuilder buf = new StringBuilder( BUFFER_LARGE );
107                final int size = codeData.getSize();
108                for( int i=0; i<size; i++ ) {
109                        final String value = codeData.getCodeKey(i);
110                        if( useLabel ) { buf.append( "<label>" ); }
111                        buf.append( inputTag ).append( value ).append( '"' );           // 6.0.2.5 (2014/10/31) char を append する。
112                        if( valSet.contains( value ) ) {
113                                buf.append( " checked=\"checked\"" );
114                        }
115                        buf.append( "/>" );
116                        if( useLabel ) { buf.append( codeData.getShortLabel(i) ).append( ' ' ).append( "</label>" ); }
117                }
118
119                return buf.toString();
120
121        }
122
123        /**
124         * 選択肢(value)に対するラベルを返します。
125         * 選択肢(value)が、存在しなかった場合は、選択肢そのものを返します。
126         * このメソッドでは、短縮ラベルを返すかどうかを指定するフラグを指定します。
127         * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じです。
128         *
129         * @og.rev 6.4.4.0 (2016/03/11) CHBOX2は、コードリソースも使用できるように変更。
130         *
131         * @param       selectValue     選択肢の値
132         * @param       isSLbl  短縮ラベルを使用する [true:使用する/false:しない]
133         *
134         * @return  選択肢のラベル
135         * @see     #getValueLabel( String )
136         */
137        @Override
138        public String getValueLabel( final String selectValue,final boolean isSLbl ) {
139                // 選択値を、カンマで分解(split)して、Listを作成し、HashSetに渡している。存在チェック用
140                final Set<String> valSet = selectValue == null
141                                                                                ? new HashSet<>()
142                                                                                : new HashSet<>( Arrays.asList( selectValue.split( "," ) ) );
143
144                final StringBuilder buf = new StringBuilder( BUFFER_LARGE );
145                final int size = codeData.getSize();
146                for( int i=0; i<size; i++ ) {
147                        final String value = codeData.getCodeKey(i);
148                if( valSet.contains( value ) ) {
149                                buf.append( '■' );
150                        }
151                        else {
152                                buf.append( '□' );
153                        }
154
155                        if( isSLbl ) { buf.append( codeData.getShortLabel(i) ).append( ' ' ); }
156                }
157                return buf.toString();
158
159        }
160}