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.model.NativeType;
019import org.opengion.fukurou.util.ErrorMessage;
020import org.opengion.fukurou.util.StringUtil;
021import org.opengion.hayabusa.db.AbstractDBType;
022import org.opengion.hayabusa.db.DBTypeCheckUtil;
023
024/**
025 * 半角数字の NUMBER を扱う為の、カラム属性を定義します。
026 *
027 * '0' ~ '9' ,'-' でのみ構成されている数字型カラム属性を定義します。
028 * さらに、カンマ','が含まれていても OK とします。ただし、データからは取り除きます。
029 *
030 * タイプチェックとして、以下の条件を判定します。
031 * ・0~9およびマイナス(-)を許可
032 * ・整数部の長さチェック
033 * ・符号の位置チェック
034 * ・文字パラメータの 正規表現チェック
035 *
036 * @og.group データ属性
037 *
038 * @version  4.0
039 * @author       Kazuhiko Hasegawa
040 * @since    JDK5.0,
041 */
042public class DBType_S9 extends AbstractDBType {
043        /** このプログラムのVERSION文字列を設定します。   {@value} */
044        private static final String VERSION = "5.6.0.3 (2012/01/24)" ;
045
046        private static final String DEF_VALUE = "0" ;           // データのデフォルト値
047
048        /**
049         * デフォルトコンストラクター
050         *
051         * @og.rev 4.0.0.0 (2005/01/31) type 廃止
052         */
053        public DBType_S9() {
054                super( DEF_VALUE );
055        }
056
057        /**
058         * NATIVEの型の識別コードを返します。
059         *
060         * @og.rev 3.5.4.7 (2004/02/06) 新規作成
061         * @og.rev 4.1.1.2 (2008/02/28) Enum型(fukurou.model.NativeType)に変更
062         *
063         * @return  NATIVEの型の識別コード(DBType で規定)
064         * @og.rtnNotNull
065         * @see org.opengion.fukurou.model.NativeType
066         */
067        @Override
068        public NativeType getNativeType() {
069                return NativeType.INT;
070        }
071
072        /**
073         * 半角0文字の固定長でFILL埋めされた文字列を返します。
074         * なお、エラーチェックは行われません。
075         * 実行前に、必ず valueCheck( String value ,int len ) が行われる必要があります。
076         *
077         * @og.rev 3.5.4.5 (2004/01/23) エンコード指定に変更します。
078         *
079         * @param       value    FILL埋めする文字列
080         * @param       sizeX    整数部分の文字列の長さ
081         * @param       sizeY    小数部分の文字列の長さ
082         * @param   encode   固定長で変換する文字エンコード
083         *
084         * @return      FILL埋めした新しい文字列
085         */
086        @Override
087        public String valueFill( final String value ,final int sizeX ,final int sizeY,final String encode ) {
088                final int len = (sizeY == 0) ? sizeX : sizeX + sizeY + 1;
089
090                // 注意 マイナス記号の処理がまだです。
091                return StringUtil.intFill( value,len );
092        }
093
094        /**
095         * String引数の文字列を+1した文字列を返します。
096         * これは、英字の場合(A,B,C など)は、B,C,D のように,最終桁の文字コードを
097         * +1 します。
098         * 文字列が数字タイプの場合は, 数字に変換して、+1 します。(桁上がりもあり)
099         * 混在タイプの場合は,最後の桁だけを確認して +1します。
100         * 引数が null の場合と、ゼロ文字列("")の場合は,物理的初期設定値(String getDefault())
101         * の値を返します。
102         *
103         * @og.rev 4.0.0.0 (2005/01/31) Integer ⇒ Long に変更
104         *
105         * @param       value   String引数の文字列
106         *
107         * @return      String引数の文字列を+1した文字列
108         */
109        @Override
110        public String valueAdd( final String value ) {
111                // 6.4.1.1 (2016/01/16) PMD refactoring. A method should have only one exit point, and that should be the last statement in the method
112                return value == null || value.isEmpty() ? getDefault() : String.valueOf( Long.parseLong( value ) + 1L );
113        }
114
115        /**
116         * String引数の文字列に、第2引数に指定の文字列(数字、日付等)を加算して返します。
117         *
118         * ここでは、数字文字列に、別の数字文字列を加算します。
119         * 内部処理に、long を使用しています。
120         *
121         * @og.rev 5.6.0.3 (2012/01/24) ADD に、引数の値を加算する機能を追加します。
122         *
123         * @param   value  String引数
124         * @param   add    加算する数字文字列
125         *
126         * @return  引数の文字列に数字を加算します。
127         */
128        @Override
129        public String valueAdd( final String value,final String add ) {
130                if( value == null || value.isEmpty() ) { return getDefault(); }
131
132                long addSu = 1L;
133                if( add != null && !add.isEmpty() ) {
134                        addSu = Long.parseLong( add );
135                }
136
137                final long val = Long.parseLong( value ) + addSu ;
138
139                return String.valueOf( val );
140        }
141
142        /**
143         * エディターで編集されたデータを登録する場合に、データそのものを
144         * 変換して、実登録データを作成します。
145         * 例えば,大文字のみのフィールドなら、大文字化します。
146         * 実登録データの作成は、DBType オブジェクトを利用しますので,
147         * これと Editor とがアンマッチの場合は、うまくデータ変換
148         * されない可能性がありますので、注意願います。
149         *
150         * @og.rev 3.3.3.0 (2003/07/09) 前後のスペースを取り除いておく。
151         * @og.rev 3.3.3.1 (2003/07/18) 後ろスペースを取り除く。(StringUtil#rTrim)
152         * @og.rev 3.8.5.3 (2006/08/07) 先頭ゼロサプレス処理を行います。
153         *
154         * @param       value   (一般に編集データとして登録されたデータ)
155         *
156         * @return      修正後の文字列(一般にデータベースに登録するデータ)
157         * @og.rtnNotNull
158         */
159        @Override
160        public String valueSet( final String value ) {
161                // 6.4.1.1 (2016/01/16) PMD refactoring. A method should have only one exit point, and that should be the last statement in the method
162                return value == null || value.isEmpty() ? "" : StringUtil.lTrim0( StringUtil.deleteChar( value,',' ) );
163        }
164
165        /**
166         * データが登録可能かどうかをチェックします。
167         * データがエラーの場合は、そのエラー内容を返します。
168         *
169         * @og.rev 2.3.1.4 (2003/02/18) 属性チェックを強化した。
170         * @og.rev 3.6.0.0 (2004/09/22) dbType パラメータ(文字パラメータ)を引数に追加
171         * @og.rev 5.2.2.0 (2010/11/01) 厳密にチェック(isStrict=true)するフラグを追加
172         *
173         * @param   key         キー
174         * @param   value       値
175         * @param   sizeX       整数部分の文字列の長さ
176         * @param   sizeY       小数部分の文字列の長さ
177         * @param   typeParam   dbType パラメータ(文字パラメータ)
178         * @param   isStrict    厳密にチェックするかどうか[true:する/false:標準的]
179         *
180         * @return  エラー内容
181         * @og.rtnNotNull
182         */
183        @Override
184        public ErrorMessage valueCheck( final String key ,final String value ,
185                                                                        final int sizeX ,final int sizeY ,final String typeParam ,final boolean isStrict) {
186
187                final ErrorMessage msg = new ErrorMessage();
188                if( value == null || value.isEmpty() ) { return msg; }
189
190                String check;
191
192                check = DBTypeCheckUtil.numberFormatCheck( value );
193                if( check != null ) {
194                        // 指定の文字以外の文字が使われています。
195                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,check );
196                }
197
198                check = DBTypeCheckUtil.sizeXCheck( value ,sizeX ,sizeY );
199                if( check != null ) {
200                        // 文字列の長さが指定の長さよりも長いです。
201                        msg.addMessage( 0, ErrorMessage.NG, "ERR0006", key, value, check, String.valueOf( sizeX ) );
202                }
203
204                check = DBTypeCheckUtil.decimalCodeCheck( value );
205                if( check != null ) {
206                        // 符号の位置が不正です。
207                        msg.addMessage( 0, ErrorMessage.NG, "ERR0023", key, check );
208                }
209
210                // 3.6.0.0 (2004/09/22) dbType パラメータ(文字パラメータ)を使用したマッチチェック
211                check = DBTypeCheckUtil.matcheCheck( value,typeParam );
212                if( check != null ) {
213                        // 指定の文字以外の文字が使われています。
214                        msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,check );
215                }
216
217                return msg;
218        }
219}