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.plugin.column;
017
018import org.opengion.fukurou.util.ErrorMessage;
019import org.opengion.fukurou.util.StringUtil;
020import org.opengion.hayabusa.db.AbstractDBType;
021import org.opengion.hayabusa.db.DBTypeCheckUtil;
022
023/**
024 * 半角数字の NUMBER を扱う為の、カラム属性を定義します。
025 *
026 * '0' ~ '9' ,'-' ,'.',',' でのみ構成されている文字型カラム属性を定義します。
027 * S9 と異なり、カンマ','が含まれていても OK とし、データからも取り除きません。
028 *
029 * タイプチェックとして、以下の条件を判定します。
030 * ・0~9、マイナス(-)、小数点(.)およびカンマ(,)を許可
031 * ・文字列の長さチェック
032 * ・小数点の位置チェック
033 * ・符号の位置チェック
034 * ・文字パラメータの 正規表現チェック
035 *
036 * @og.group データ属性
037 *
038 * @version  4.0
039 * @author       Kazuhiko Hasegawa
040 * @since    JDK5.0,
041 */
042public class DBType_X9 extends AbstractDBType {
043        /** このプログラムのVERSION文字列を設定します。   {@value} */
044        private static final String VERSION = "6.4.2.0 (2016/01/29)" ;
045
046        /**
047         * デフォルトコンストラクター
048         *
049         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
050         */
051        public DBType_X9() { super(); }         // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
052
053        /**
054         * 半角0文字の固定長でFILL埋めされた文字列を返します。
055         * なお、エラーチェックは行われません。
056         * 実行前に、必ず valueCheck( String value ,int len ) が行われる必要があります。
057         *
058         * @og.rev 3.5.4.5 (2004/01/23) エンコード指定に変更します。
059         *
060         * @param       value    FILL埋めする文字列
061         * @param       sizeX    整数部分の文字列の長さ
062         * @param       sizeY    小数部分の文字列の長さ
063         * @param       encode   エンコード指定
064         *
065         * @return      FILL埋めした新しい文字列
066         */
067        @Override
068        public String valueFill( final String value ,final int sizeX ,final int sizeY,final String encode ) {
069                final int len = (sizeY == 0) ? sizeX : sizeX + sizeY + 1;
070
071                // 注意 マイナス記号の処理がまだです。
072                return StringUtil.intFill( value,len );
073        }
074
075        /**
076         * String引数の文字列を+1した文字列を返します。
077         * これは、文字型数字タイプの場合は、マイナス、カンマ、ピリオドを避けて、
078         * +1 します。(桁上がりもあり)
079         * データの値が、マイナスの場合は、結果的にマイナスしていきます。
080         * カンマ編集(12,345,678 など)がある場合に、桁上がりしても、カンマは追加されません。
081         * マイナス時の桁上がりには、対応しています。
082         * 引数が null の場合と、ゼロ文字列("")の場合は、物理的初期設定値(String getDefault())
083         * の値を返します。
084         *
085         * @param       value   String引数の文字列
086         *
087         * @return      String引数の文字列を+1した文字列
088         */
089        @Override
090        public String valueAdd( final String value ) {
091                if( value == null || value.isEmpty() ) { return getDefault(); }
092                char[] chs = value.toCharArray() ;
093                boolean over = false;
094                for( int i=chs.length-1; i>=0; i-- ) {
095                        if( chs[i] == '-' || chs[i] == '.' || chs[i] == ',' ) { continue; }
096                        if( chs[i] == '9' ) { chs[i] = '0'; over = true; }
097                        else { chs[i]++; over = false; break; }
098                }
099                if( over ) {
100                        char[] chs2 = new char[ chs.length + 1 ];
101                        System.arraycopy( chs,0,chs2,1,chs.length );
102                        if( chs[0] == '-' ) {
103                                chs2[0] = '-';
104                                chs2[1] = '1';
105                        }
106                        else {
107                                chs2[0] = '1';
108                        }
109                        chs = chs2;
110                }
111                return new String( chs );
112        }
113
114        /**
115         * エディターで編集されたデータを登録する場合に、データそのものを
116         * 変換して、実登録データを作成します。
117         * 例えば、大文字のみのフィールドなら、大文字化します。
118         * 実登録データの作成は、DBType オブジェクトを利用しますので、
119         * これと Editor とがアンマッチの場合は、うまくデータ変換
120         * されない可能性がありますので、注意願います。
121         *
122         * @og.rev 3.3.3.0 (2003/07/09) 前後のスペースを取り除いておく。
123         * @og.rev 3.3.3.1 (2003/07/18) 後ろスペースを取り除く。(StringUtil#rTrim)
124         *
125         * @param       value   (一般に編集データとして登録されたデータ)
126         *
127         * @return      修正後の文字列(一般にデータベースに登録するデータ)
128         */
129        @Override
130        public String valueSet( final String value ) {
131                return StringUtil.rTrim( value ) ;
132        }
133
134        /**
135         * データが登録可能かどうかをチェックします。
136         * データがエラーの場合は、そのエラー内容を返します。
137         * 許可する文字は、0-9 の数字、マイナス(-) 、小数点(.)です。
138         * 小数点の位置チェックは行いません。
139         *
140         * @og.rev 2.3.1.4 (2003/02/18) 属性チェックを強化した。
141         * @og.rev 3.6.0.0 (2004/09/22) チェック項目は、数字を許可する文字列とする。
142         * @og.rev 3.6.0.0 (2004/09/22) dbType パラメータ(文字パラメータ)を引数に追加
143         * @og.rev 5.2.2.0 (2010/11/01) 厳密にチェック(isStrict=true)するフラグを追加
144         *
145         * @param   key         キー
146         * @param   value       値
147         * @param   sizeX       整数部分の文字列の長さ
148         * @param   sizeY       小数部分の文字列の長さ
149         * @param   typeParam   dbType パラメータ(文字パラメータ)
150         * @param   isStrict    厳密にチェックするかどうか[true:する/false:標準的]
151         *
152         * @return  エラー内容
153         * @og.rtnNotNull
154         */
155        @Override
156        public ErrorMessage valueCheck( final String key ,final String value ,
157                                                                        final int sizeX ,final int sizeY ,final String typeParam ,final boolean isStrict) {
158
159                final ErrorMessage msg = new ErrorMessage();
160                if( value == null || value.isEmpty() ) { return msg; }
161
162                // チェック用のvalue を用意します。カンマ(,)を取り除いておく為です。
163                final String tmpValue = StringUtil.deleteChar(value,',' );
164                String check;
165
166                // 3.6.0.0 (2004/09/22) チェック内容変更
167                check = DBTypeCheckUtil.decimalFormatCheck( tmpValue );
168                if( check != null ) {
169                        // 指定の文字以外の文字が使われています。
170                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,check );
171                }
172
173                check = DBTypeCheckUtil.decimalCodeCheck( tmpValue );
174                if( check != null ) {
175                        // 符号の位置が不正です。
176                        msg.addMessage( 0, ErrorMessage.NG, "ERR0023", key, check );
177                }
178
179                // 3.6.0.0 (2004/09/22) 追加
180                check = DBTypeCheckUtil.decimalPointCheck( tmpValue );
181                if( check != null ) {
182                        // 小数点の位置が不正です。
183                        msg.addMessage( 0, ErrorMessage.NG, "ERR0024", key, check );
184                }
185
186                // 3.6.0.0 (2004/09/22) dbType パラメータ(文字パラメータ)を使用したマッチチェック
187                check = DBTypeCheckUtil.matcheCheck( tmpValue,typeParam );
188                if( check != null ) {
189                        // 指定の文字以外の文字が使われています。
190                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,check );
191                }
192
193                return msg;
194        }
195}