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         * 選択されている値は、複数指定が可能です。CSV形式データとして渡された場合は、
087         * 個別に分解して、ラベル化します。
088         *
089         * @og.rev 6.4.4.0 (2016/03/11) CHBOX2は、コードリソースも使用できるように変更。
090         * @og.rev 7.0.1.0 (2018/10/15) XHTML → HTML5 対応(空要素の、"/>" 止めを、">" に変更します)。
091         *
092         * @param   name         ラジオの name
093         * @param   selectValue  選択されている値
094         * @param   useLabel     ラベル表示の有無 [true:有/false:無]
095         *
096         * @return  オプションタグ
097         * @og.rtnNotNull
098         */
099        @Override
100        public String getOption( final String name,final String selectValue,final boolean useLabel ) {
101                // 選択値を、カンマで分解(split)して、Listを作成し、HashSetに渡している。存在チェック用
102                final Set<String> valSet = selectValue == null
103                                                                                ? new HashSet<>()
104                                                                                : new HashSet<>( Arrays.asList( selectValue.split( "," ) ) );
105
106                final String inputTag = "<input type=\"checkbox\" name=\"" + name + "\" value=\"" ;
107                final StringBuilder buf = new StringBuilder( BUFFER_LARGE );
108                final int size = codeData.getSize();
109                for( int i=0; i<size; i++ ) {
110                        final String value = codeData.getCodeKey(i);
111                        if( useLabel ) { buf.append( "<label>" ); }
112                        buf.append( inputTag ).append( value ).append( '"' );           // 6.0.2.5 (2014/10/31) char を append する。
113                        if( valSet.contains( value ) ) {
114                                buf.append( " checked=\"checked\"" );
115                        }
116//                      buf.append( "/>" );
117                        buf.append( '>' );                              // 7.0.1.0 (2018/10/15)
118                        if( useLabel ) { buf.append( codeData.getShortLabel(i) ).append( ' ' ).append( "</label>" ); }
119                }
120
121                return buf.toString();
122        }
123
124        /**
125         * 選択肢(value)に対するラベルを返します。
126         * 選択肢(value)が、存在しなかった場合は、選択肢そのものを返します。
127         * このメソッドでは、短縮ラベルを返すかどうかを指定するフラグを指定します。
128         * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じです。
129         *
130         * @og.rev 6.4.4.0 (2016/03/11) CHBOX2は、コードリソースも使用できるように変更。
131         *
132         * @param       selectValue     選択肢の値
133         * @param       isSLbl  短縮ラベルを使用する [true:使用する/false:しない]
134         *
135         * @return  選択肢のラベル
136         * @see     #getValueLabel( String )
137         */
138        @Override
139        public String getValueLabel( final String selectValue,final boolean isSLbl ) {
140                // 選択値を、カンマで分解(split)して、Listを作成し、HashSetに渡している。存在チェック用
141                final Set<String> valSet = selectValue == null
142                                                                                ? new HashSet<>()
143                                                                                : new HashSet<>( Arrays.asList( selectValue.split( "," ) ) );
144
145                final StringBuilder buf = new StringBuilder( BUFFER_LARGE );
146                final int size = codeData.getSize();
147                for( int i=0; i<size; i++ ) {
148                        final String value = codeData.getCodeKey(i);
149                if( valSet.contains( value ) ) {
150                                buf.append( '■' );
151                        }
152                        else {
153                                buf.append( '□' );
154                        }
155
156                        if( isSLbl ) { buf.append( codeData.getShortLabel(i) ).append( ' ' ); }
157                }
158                return buf.toString();
159        }
160}