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.hayabusa.common.HybsSystemException;
019import org.opengion.hayabusa.resource.CodeData;
020import static org.opengion.fukurou.system.HybsConst.CR ;                                // 6.1.0.0 (2014/12/26)
021import static org.opengion.fukurou.system.HybsConst.BUFFER_LARGE;               // 6.1.0.0 (2014/12/26) refactoring
022
023/**
024 * データのコード情報を取り扱うクラスです。
025 *
026 * コードのキーとラベルの情報から、HTMLのメニューやリストを作成するための オプション
027 * タグを作成したり、与えられたキーをもとに、チェック済みのオプションタグを作成したり
028 * します。
029 *
030 * @og.rev 6.2.2.4 (2015/04/24) 新規作成
031 * @og.group 選択データ制御
032 *
033 * @version  4.0
034 * @author   Kazuhiko Hasegawa
035 * @since    JDK5.0,
036 */
037public class Selection_BITBOX extends Selection_NULL {
038        private final CodeData codeData ;
039
040        /**
041         * コンストラクター
042         *
043         * @og.rev 6.2.2.4 (2015/04/24) 新規追加
044         *
045         * @param       cdData  コードデータオブジェクト
046         */
047        public Selection_BITBOX( final CodeData cdData ) {
048                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
049                if( cdData == null ) {
050                        final String errMsg = "コードリソースが定義されていません。" + CR ;
051                        throw new HybsSystemException( errMsg );
052                }
053
054                codeData = cdData ;
055        }
056
057        /**
058         * 初期値が選択済みの 選択肢(オプション)を返します。
059         * このオプションは、引数の値を初期値とするオプションタグを返します。
060         * ※ このクラスでは実装されていません。
061         *
062         * @og.rev 6.2.2.4 (2015/04/24) 新規追加
063         *
064         * @param       selectValue     選択されている値
065         * @param       seqFlag シーケンスアクセス機能の指定
066         * @param       useShortLabel   短ラベルの指定
067         *
068         * @return  オプションタグ
069         */
070        @Override
071        public String getOption( final String selectValue,final boolean seqFlag, final boolean useShortLabel ) {
072                final String errMsg = "このクラスでは実装されていません。";
073                throw new UnsupportedOperationException( errMsg );
074        }
075
076        /**
077         * 初期値が選択済みの 選択肢(オプション)を返します。
078         * このオプションは、引数の値を初期値とするオプションタグを返します。
079         *
080         * @og.rev 6.2.2.4 (2015/04/24) 新規追加
081         * @og.rev 6.4.4.0 (2016/03/11) ラベルとラジオボタンの間に、全角スペースをひとつ入れます。
082         * @og.rev 7.0.1.0 (2018/10/15) XHTML → HTML5 対応(空要素の、"/>" 止めを、">" に変更します)。
083         * @og.rev 7.2.6.1 (2020/07/17) codeGroupが使えるように、isUseを判定する。
084         *
085         * @param   name         ラジオの name
086         * @param   selectValue  選択されている値
087         * @param   useLabel     ラベル表示の有無 [true:有/false:無]
088         *
089         * @return  オプションタグ
090         * @og.rtnNotNull
091         */
092        @Override
093        public String getOption( final String name,final String selectValue,final boolean useLabel ) {
094                String value = null;
095                try {
096                        final String inputTag = "<input type=\"checkbox\" name=\"" + name + "\" value=\"" ;
097                        final StringBuilder buf = new StringBuilder( BUFFER_LARGE );
098                        final int size = codeData.getSize();
099                        final int selVal = selectValue == null || selectValue.isEmpty() ? 0 : Integer.parseInt( selectValue );  // 数値化して、論理和の準備をする。
100                        for( int i=0; i<size; i++ ) {
101                                if( ! codeData.isUse(i) ) { continue; }         // 7.2.6.1 (2020/07/17)
102
103                                value = codeData.getCodeKey(i);
104                                final int val = value == null || value.isEmpty() ? 0 : Integer.parseInt( value );       // 数値化して、論理和の準備をする。
105                                if( useLabel ) { buf.append( "<label>" ); }
106                                buf.append( inputTag ).append( value ).append( '"' );   // 6.0.2.5 (2014/10/31) char を append する。
107                                if( ( val & selVal ) != 0 ) {                                                   // 論理積
108                                        buf.append( " checked=\"checked\"" );
109                                }
110//                              buf.append( "/>" );
111                                buf.append( '>' );                              // 7.0.1.0 (2018/10/15)
112                                if( useLabel ) { buf.append( codeData.getShortLabel(i) ).append( ' ' ).append( "</label>" ); }
113                        }
114                        return buf.toString();
115                }
116                catch( final NumberFormatException ex ) {               // 文字列が解析可能な数値を含まない場合
117                        final String errMsg = "BITBOXで、数値変換できませんでした。 " + CR
118                                                                + " name=[" + name + ", selectValue=[" + selectValue + "] , value=[" + value + "]" + CR
119                                                                + ex.getMessage() ;
120                        return errMsg;
121                }
122        }
123
124        /**
125         * 選択肢(value)に対するラベルを返します。
126         * 選択肢(value)が、存在しなかった場合は、選択肢そのものを返します。
127         * このメソッドでは、短縮ラベルを返すかどうかを指定するフラグを指定します。
128         * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じです。
129         *
130         * @og.rev 6.2.2.4 (2015/04/24) 新規追加
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                String value = null;
141                try {
142                        final StringBuilder buf = new StringBuilder( BUFFER_LARGE );
143                        final int size = codeData.getSize();
144                        // 6.4.2.1 (2016/02/05) PMD refactoring. Useless parentheses.
145                        final int selVal = selectValue == null || selectValue.isEmpty() ? 0 : Integer.parseInt( selectValue );          // 数値化して、論理積の準備をする。
146                        for( int i=0; i<size; i++ ) {
147                                value = codeData.getCodeKey(i);
148                                // 6.4.2.1 (2016/02/05) PMD refactoring. Useless parentheses.
149                                final int val = value == null || value.isEmpty() ? 0 : Integer.parseInt( value );       // 数値化して、論理積の準備をする。
150                                if( ( val & selVal ) == 0 ) {                                                   // 論理積
151                                        buf.append( '□' );
152                                }
153                                else {
154                                        buf.append( '■' );
155                                }
156
157                                if( isSLbl ) { buf.append( codeData.getShortLabel(i) ).append( ' ' ); }
158                        }
159                        return buf.toString();
160                }
161                catch( final NumberFormatException ex ) {               // 文字列が解析可能な数値を含まない場合
162                        final String errMsg = "BITBOXで、数値変換できませんでした。 " + CR
163                                                                + " selectValue=[" + selectValue + "] , value=[" + value + "]" + CR
164                                                                + ex.getMessage() ;
165                        return errMsg;
166                }
167        }
168
169}