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 /** 117 * String引数の文字列に、第2引数に指定の文字列(数字、日付等)を加算して返します。 118 * 119 * ここでは、数字文字列に、別の数字文字列を加算します。 120 * 内部処理に、long を使用しています。 121 * 122 * @og.rev 5.6.0.3 (2012/01/24) ADD に、引数の値を加算する機能を追加します。 123 * 124 * @param value String引数 125 * @param add 加算する数字文字列 126 * 127 * @return 引数の文字列に数字を加算します。 128 */ 129 @Override 130 public String valueAdd( final String value,final String add ) { 131 if( value == null || value.isEmpty() ) { return getDefault(); } 132 133 long addSu = 1L; 134 if( add != null && !add.isEmpty() ) { 135 addSu = Long.parseLong( add ); 136 } 137 138 final long val = Long.parseLong( value ) + addSu ; 139 140 return String.valueOf( val ); 141 } 142 143 /** 144 * エディターで編集されたデータを登録する場合に、データそのものを 145 * 変換して、実登録データを作成します。 146 * 例えば,大文字のみのフィールドなら、大文字化します。 147 * 実登録データの作成は、DBType オブジェクトを利用しますので, 148 * これと Editor とがアンマッチの場合は、うまくデータ変換 149 * されない可能性がありますので、注意願います。 150 * 151 * @og.rev 3.3.3.0 (2003/07/09) 前後のスペースを取り除いておく。 152 * @og.rev 3.3.3.1 (2003/07/18) 後ろスペースを取り除く。(StringUtil#rTrim) 153 * @og.rev 3.8.5.3 (2006/08/07) 先頭ゼロサプレス処理を行います。 154 * 155 * @param value (一般に編集データとして登録されたデータ) 156 * 157 * @return 修正後の文字列(一般にデータベースに登録するデータ) 158 * @og.rtnNotNull 159 */ 160 @Override 161 public String valueSet( final String value ) { 162 // 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 163 return value == null || value.isEmpty() ? "" : StringUtil.lTrim0( StringUtil.deleteChar( value,',' ) ); 164 165 } 166 167 /** 168 * データが登録可能かどうかをチェックします。 169 * データがエラーの場合は、そのエラー内容を返します。 170 * 171 * @og.rev 2.3.1.4 (2003/02/18) 属性チェックを強化した。 172 * @og.rev 3.6.0.0 (2004/09/22) dbType パラメータ(文字パラメータ)を引数に追加 173 * @og.rev 5.2.2.0 (2010/11/01) 厳密にチェック(isStrict=true)するフラグを追加 174 * 175 * @param key キー 176 * @param value 値 177 * @param sizeX 整数部分の文字列の長さ 178 * @param sizeY 小数部分の文字列の長さ 179 * @param typeParam dbType パラメータ(文字パラメータ) 180 * @param isStrict 厳密にチェックするかどうか[true:する/false:標準的] 181 * 182 * @return エラー内容 183 * @og.rtnNotNull 184 */ 185 @Override 186 public ErrorMessage valueCheck( final String key ,final String value , 187 final int sizeX ,final int sizeY ,final String typeParam ,final boolean isStrict) { 188 189 final ErrorMessage msg = new ErrorMessage(); 190 if( value == null || value.isEmpty() ) { return msg; } 191 192 String check; 193 194 check = DBTypeCheckUtil.numberFormatCheck( value ); 195 if( check != null ) { 196 // 指定の文字以外の文字が使われています。 197 msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,check ); 198 } 199 200 check = DBTypeCheckUtil.sizeXCheck( value ,sizeX ,sizeY ); 201 if( check != null ) { 202 // 文字列の長さが指定の長さよりも長いです。 203 msg.addMessage( 0, ErrorMessage.NG, "ERR0006", key, value, check, String.valueOf( sizeX ) ); 204 } 205 206 check = DBTypeCheckUtil.decimalCodeCheck( value ); 207 if( check != null ) { 208 // 符号の位置が不正です。 209 msg.addMessage( 0, ErrorMessage.NG, "ERR0023", key, check ); 210 } 211 212 // 3.6.0.0 (2004/09/22) dbType パラメータ(文字パラメータ)を使用したマッチチェック 213 check = DBTypeCheckUtil.matcheCheck( value,typeParam ); 214 if( check != null ) { 215 // 指定の文字以外の文字が使われています。 216 msg.addMessage( 0,ErrorMessage.NG,"ERR0009", key,check ); 217 } 218 219 return msg; 220 } 221}