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         *
084         * @param   name         ラジオの name
085         * @param   selectValue  選択されている値
086         * @param   useLabel     ラベル表示の有無 [true:有/false:無]
087         *
088         * @return  オプションタグ
089         * @og.rtnNotNull
090         */
091        @Override
092        public String getOption( final String name,final String selectValue,final boolean useLabel ) {
093                String value = null;
094                try {
095                        final String inputTag = "<input type=\"checkbox\" name=\"" + name + "\" value=\"" ;
096                        final StringBuilder buf = new StringBuilder( BUFFER_LARGE );
097                        final int size = codeData.getSize();
098                        final int selVal = selectValue == null || selectValue.isEmpty() ? 0 : Integer.parseInt( selectValue );  // 数値化して、論理和の準備をする。
099                        for( int i=0; i<size; i++ ) {
100                                value = codeData.getCodeKey(i);
101                                final int val = value == null || value.isEmpty() ? 0 : Integer.parseInt( value );       // 数値化して、論理和の準備をする。
102                                if( useLabel ) { buf.append( "<label>" ); }
103                                buf.append( inputTag ).append( value ).append( '"' );   // 6.0.2.5 (2014/10/31) char を append する。
104                                if( ( val & selVal ) != 0 ) {                                                   // 論理積
105                                        buf.append( " checked=\"checked\"" );
106                                }
107//                              buf.append( "/>" );
108                                buf.append( '>' );                              // 7.0.1.0 (2018/10/15)
109                                if( useLabel ) { buf.append( codeData.getShortLabel(i) ).append( ' ' ).append( "</label>" ); }
110                        }
111                        return buf.toString();
112                }
113                catch( final NumberFormatException ex ) {               // 文字列が解析可能な数値を含まない場合
114                        final String errMsg = "BITBOXで、数値変換できませんでした。 " + CR
115                                                                + " name=[" + name + ", selectValue=[" + selectValue + "] , value=[" + value + "]" + CR
116                                                                + ex.getMessage() ;
117                        return errMsg;
118                }
119        }
120
121        /**
122         * 選択肢(value)に対するラベルを返します。
123         * 選択肢(value)が、存在しなかった場合は、選択肢そのものを返します。
124         * このメソッドでは、短縮ラベルを返すかどうかを指定するフラグを指定します。
125         * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じです。
126         *
127         * @og.rev 6.2.2.4 (2015/04/24) 新規追加
128         *
129         * @param       selectValue     選択肢の値
130         * @param       isSLbl  短縮ラベルを [true:使用する/false:しない] (未使用)
131         *
132         * @return  選択肢のラベル
133         * @see     #getValueLabel( String )
134         */
135        @Override
136        public String getValueLabel( final String selectValue,final boolean isSLbl ) {
137                String value = null;
138                try {
139                        final StringBuilder buf = new StringBuilder( BUFFER_LARGE );
140                        final int size = codeData.getSize();
141                        // 6.4.2.1 (2016/02/05) PMD refactoring. Useless parentheses.
142                        final int selVal = selectValue == null || selectValue.isEmpty() ? 0 : Integer.parseInt( selectValue );          // 数値化して、論理積の準備をする。
143                        for( int i=0; i<size; i++ ) {
144                                value = codeData.getCodeKey(i);
145                                // 6.4.2.1 (2016/02/05) PMD refactoring. Useless parentheses.
146                                final int val = value == null || value.isEmpty() ? 0 : Integer.parseInt( value );       // 数値化して、論理積の準備をする。
147                                if( ( val & selVal ) == 0 ) {                                                   // 論理積
148                                        buf.append( '□' );
149                                }
150                                else {
151                                        buf.append( '■' );
152                                }
153
154                                if( isSLbl ) { buf.append( codeData.getShortLabel(i) ).append( ' ' ); }
155                        }
156                        return buf.toString();
157                }
158                catch( final NumberFormatException ex ) {               // 文字列が解析可能な数値を含まない場合
159                        final String errMsg = "BITBOXで、数値変換できませんでした。 " + CR
160                                                                + " selectValue=[" + selectValue + "] , value=[" + value + "]" + CR
161                                                                + ex.getMessage() ;
162                        return errMsg;
163                }
164        }
165
166}