1 64 65 package com.jcorporate.expresso.core.dbobj; 66 67 import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData; 68 import com.jcorporate.expresso.core.db.DBException; 69 import org.apache.log4j.Logger; 70 import org.apache.oro.text.regex.MalformedPatternException; 71 import org.apache.oro.text.regex.Pattern; 72 import org.apache.oro.text.regex.PatternCompiler; 73 import org.apache.oro.text.regex.Perl5Compiler; 74 75 import java.io.IOException ; 76 import java.io.ObjectInputStream ; 77 import java.io.Serializable ; 78 import java.util.HashMap ; 79 import java.util.HashSet ; 80 import java.util.Hashtable ; 81 import java.util.Iterator ; 82 import java.util.Set ; 83 84 90 public class DBField 91 implements Serializable , DataFieldMetaData { 92 93 94 98 transient public static final String AUTOINC_TYPE = "auto-inc"; 99 100 transient public static final String ARRAY_TYPE = "array"; 101 transient public static final String BIGINT_TYPE = "bigint"; 102 transient public static final String BINARY_TYPE = "binary"; 103 transient public static final String BIT_TYPE = "bit"; 104 107 transient public static final String BOOLEAN_TYPE = "boolean"; 108 transient public static final String BLOB_TYPE = "blob"; 109 transient public static final String CHAR_TYPE = "char"; 110 transient public static final String CLOB_TYPE = "clob"; 111 112 transient public static final String DATE_TYPE = "date"; 113 116 transient public static final String DATETIME_TYPE = "datetime"; 117 transient public static final String DOUBLE_TYPE = "double"; 118 transient public static final String DECIMAL_TYPE = "decimal"; 119 120 transient public static final String FLOAT_TYPE = "float"; 121 transient public static final String INTEGER_TYPE = "int"; 122 125 transient public static final String INT_TYPE = "integer"; 126 transient public static final String JAVA_OBJECT = "java_object"; 127 128 131 transient public static final String LONG_TYPE = "long"; 132 transient public static final String LONGVARCHAR_TYPE = "longvarchar"; 133 transient public static final String LONGVARBINARY = "longvarbinary"; 134 137 transient public static final String NULL_TYPE = "null"; 138 transient public static final String NUMERIC_TYPE = "numeric"; 139 transient public static final String OTHER_TYPE = "other"; 140 141 transient public static final String REAL_TYPE = "real"; 142 transient public static final String REF_TYPE = "ref"; 143 transient public static final String SMALLINT_TYPE = "smallint"; 144 transient public static final String STRUCT_TYPE = "struct"; 145 146 transient public static final String TIME_TYPE = "time"; 147 transient public static final String TIMESTAMP_TYPE = "timestamp"; 148 151 transient public static final String TEXT_TYPE = "text"; 152 transient public static final String TINYINT_TYPE = "tinyint"; 153 transient public static final String VARBINARY_TYPE = "varbinary"; 154 transient public static final String VARCHAR_TYPE = "varchar"; 155 156 159 public static final String BOOLEAN_MASK = "^([01]|true|TRUE|false|FALSE|y|Y|n|N)$"; 160 161 164 public static final String DATE_MASK = "^[\\d\\s-:/.+]+$"; 165 166 169 public static final String INT_MASK = "^[+-]?[0-9]+"; 170 171 174 public static final String FLOAT_MASK = "^([+-]?)(?=\\d|\\.\\d)\\d*(\\.\\d*)?([Ee]([+-]?\\d+))?$"; 175 176 180 transient private static PatternCompiler patternCompiler = 181 new Perl5Compiler(); 182 183 186 public static final String ATTRIBUTE_ERROR = "error"; 187 188 192 public static final String ATTRIBUTE_ERROR_MESSAGE = "error-message"; 193 194 195 protected String fieldName; 196 197 200 protected String expressoFieldTypeString; 201 202 204 207 protected boolean allowNull; 208 209 212 protected String description; 213 214 217 protected int fieldSize; 218 219 223 protected boolean isVirtual = false; 224 225 226 protected Pattern mask = null; 227 228 231 protected Hashtable attributes = null; 232 233 236 237 240 protected boolean isMultiValued = false; 241 242 245 protected boolean isSecret = false; 246 247 250 protected boolean isReadOnly = false; 251 252 255 private boolean isBoolean; 256 257 258 261 private boolean isNumeric; 262 265 private boolean isDate; 266 267 273 private boolean isDateTime; 274 280 private boolean isTime; 281 282 288 private boolean isDateOnly; 289 290 291 294 private String defaultValue; 295 296 303 private boolean isLongBinary; 304 305 private boolean isLongCharacter; 306 307 310 private boolean isText; 311 312 315 protected String lookupObject; 316 317 321 protected String lookupField; 322 323 327 protected String lookupDefinition; 328 329 332 protected boolean isKey = false; 333 334 337 protected boolean isLongObject = false; 338 339 342 protected boolean isCharacterLongObject = false; 343 344 347 protected String filterMethod = "standardFilter"; 348 349 352 private transient static Logger logCat = Logger.getLogger(DBField.class); 353 354 357 private boolean isAutoInc = false; 358 359 363 protected boolean encrypted = false; 364 365 370 protected boolean hashed = false; 371 372 375 protected int precision; 376 377 protected boolean isFloatingPointType = false; 378 379 382 private Class mFilterClass; 383 384 388 transient private static Set quotedDataTypes = new HashSet (5); 389 transient private static Set numericDataTypes = new HashSet (10); 390 transient private static Set dateTypes = new HashSet (4); 391 392 395 transient public static Set allDataTypes = new HashSet (40); 396 397 398 static { 399 quotedDataTypes.add(CHAR_TYPE); 400 quotedDataTypes.add(VARCHAR_TYPE); 401 quotedDataTypes.add(TEXT_TYPE); 402 quotedDataTypes.add(LONGVARCHAR_TYPE); 403 404 numericDataTypes.add(NUMERIC_TYPE); 405 numericDataTypes.add(INT_TYPE); 406 numericDataTypes.add(INTEGER_TYPE); 407 numericDataTypes.add(DOUBLE_TYPE); 408 numericDataTypes.add(FLOAT_TYPE); 409 numericDataTypes.add(REAL_TYPE); 410 numericDataTypes.add(DECIMAL_TYPE); 411 numericDataTypes.add(AUTOINC_TYPE); 412 numericDataTypes.add(BIGINT_TYPE); 413 numericDataTypes.add(LONG_TYPE); 414 numericDataTypes.add(SMALLINT_TYPE); 415 numericDataTypes.add(TINYINT_TYPE); 416 417 418 dateTypes.add(DATE_TYPE); 419 dateTypes.add(DATETIME_TYPE); 420 dateTypes.add(TIME_TYPE); 421 dateTypes.add(TIMESTAMP_TYPE); 422 423 allDataTypes.add(DBField.ARRAY_TYPE); 424 allDataTypes.add(DBField.BIGINT_TYPE); 425 allDataTypes.add(DBField.LONG_TYPE); 426 allDataTypes.add(DBField.BINARY_TYPE); 427 allDataTypes.add(DBField.BIT_TYPE); 428 allDataTypes.add(DBField.BOOLEAN_TYPE); 429 allDataTypes.add(DBField.BLOB_TYPE); 430 allDataTypes.add(DBField.CHAR_TYPE); 431 allDataTypes.add(DBField.CLOB_TYPE); 432 allDataTypes.add(DBField.DATE_TYPE); 433 allDataTypes.add(DBField.DECIMAL_TYPE); 434 allDataTypes.add(DBField.DOUBLE_TYPE); 435 allDataTypes.add(DBField.FLOAT_TYPE); 436 allDataTypes.add(DBField.INTEGER_TYPE); 437 allDataTypes.add(DBField.INT_TYPE); 438 allDataTypes.add(DBField.JAVA_OBJECT); 439 allDataTypes.add(DBField.LONGVARBINARY); 440 allDataTypes.add(DBField.LONGVARCHAR_TYPE); 441 allDataTypes.add(DBField.NUMERIC_TYPE); 442 allDataTypes.add(DBField.OTHER_TYPE); 443 allDataTypes.add(DBField.REAL_TYPE); 444 allDataTypes.add(DBField.REF_TYPE); 445 allDataTypes.add(DBField.SMALLINT_TYPE); 446 allDataTypes.add(DBField.STRUCT_TYPE); 447 allDataTypes.add(DBField.TIME_TYPE); 448 allDataTypes.add(DBField.TIMESTAMP_TYPE); 449 allDataTypes.add(DBField.DATETIME_TYPE); 450 allDataTypes.add(DBField.TINYINT_TYPE); 451 allDataTypes.add(DBField.VARBINARY_TYPE); 452 allDataTypes.add(DBField.VARCHAR_TYPE); 453 454 allDataTypes.add(DBField.TEXT_TYPE); 456 457 allDataTypes.add(DBField.AUTOINC_TYPE); 459 460 } 461 462 463 474 public DBField(String myName, String myType, int mySize, int newPrecision, 475 boolean myAllowNull, String myDescrip) 476 throws DBException { 477 if (myName.length() > 18) { 478 logCat.warn("Field name '" + myName + 479 "' is over 18 characters - may not be portable"); 480 } 481 482 fieldName = myName; 483 expressoFieldTypeString = myType; 484 allowNull = myAllowNull; 485 description = myDescrip; 486 fieldSize = mySize; 487 precision = newPrecision; 488 489 isNumeric = numericDataTypes.contains(myType); 490 isBoolean = myType.equalsIgnoreCase(BOOLEAN_TYPE); 491 isDate = dateTypes.contains(myType); 492 isDateOnly = myType.equalsIgnoreCase(DATE_TYPE); 493 isTime = myType.equalsIgnoreCase(TIME_TYPE); 494 isDateTime = myType.equalsIgnoreCase(TIMESTAMP_TYPE) || 495 myType.equalsIgnoreCase(DATETIME_TYPE); 496 isText = quotedDataTypes.contains(myType); 497 isAutoInc = myType.equalsIgnoreCase(AUTOINC_TYPE); 498 isFloatingPointType = myType.equalsIgnoreCase(FLOAT_TYPE); 499 500 isCharacterLongObject = (myType.equalsIgnoreCase(TEXT_TYPE) || 501 myType.equalsIgnoreCase(LONGVARCHAR_TYPE) || 502 myType.equalsIgnoreCase(CLOB_TYPE)); 503 504 isLongObject = (isCharacterLongObject || myType.equalsIgnoreCase(BINARY_TYPE) || 505 myType.equalsIgnoreCase(BLOB_TYPE) || 506 myType.equalsIgnoreCase(JAVA_OBJECT) || 507 myType.equalsIgnoreCase(LONGVARBINARY) || 508 myType.equalsIgnoreCase(VARBINARY_TYPE)); 509 510 setDefaultMask(); 511 512 } 513 514 523 public DBField(String myName, String myType, int newPrecision, 524 boolean myAllowNull, String myDescrip) 525 throws DBException { 526 this(myName, myType, 0, newPrecision, myAllowNull, myDescrip); 527 } 528 529 534 public boolean allowsNull() { 535 return allowNull; 536 } 537 538 543 public String getDescription() { 544 return description; 545 } 546 547 548 553 public String getDefaultValue() { 554 return defaultValue; 555 } 556 557 562 public void setDefaultValue(String newValue) { 563 this.defaultValue = newValue; 564 } 565 566 571 public String getFilterMethod() { 572 return filterMethod; 573 } 574 575 581 public String getLength() { 582 return String.valueOf(fieldSize); 583 } 584 585 590 public int getLengthInt() { 591 return fieldSize; 592 } 593 594 599 public String getLookupObject() { 600 return lookupObject; 601 } 602 603 610 public String getLookupDefinition() { 611 return lookupDefinition; 612 } 613 614 622 public String getLookupField() { 623 return this.lookupField; 624 } 625 626 631 public String getName() { 632 return fieldName; 633 } 634 635 640 public int getPrecision() { 641 return precision; 642 } 643 644 650 public String getTypeString() { 651 return expressoFieldTypeString; 652 } 653 654 659 public boolean isKey() { 660 return isKey; 661 } 662 663 668 public boolean isMultiValued() { 669 return isMultiValued; 670 } 671 672 677 public boolean isReadOnly() { 678 return isReadOnly || isAutoIncremented(); 679 } 680 681 687 public boolean isBooleanType() { 688 return this.isBoolean; 689 } 690 691 697 public boolean isCharacterLongObjectType() { 698 return this.isCharacterLongObject; 699 } 700 701 706 public boolean isLongObjectType() { 707 return this.isLongObject; 708 } 709 710 716 public boolean isLongBinaryType() { 717 return this.isLongBinary; 718 } 719 720 726 public boolean isLongCharacterType() { 727 return this.isLongCharacter; 728 } 729 730 736 public boolean isBinaryObjectType() { 737 return (isLongObject & !isCharacterLongObject & !isLongBinary); 738 } 739 740 746 public boolean isQuotedTextType() { 747 return isText; 748 } 749 750 756 public boolean isNumericType() { 757 return isNumeric; 758 } 759 760 766 public boolean isDateType() { 767 return isDate; 768 } 769 770 777 public boolean isDateOnlyType() { 778 return isDateOnly; 779 } 780 781 public boolean isFloatingPointType() { 782 return isFloatingPointType; 783 } 784 785 792 public boolean isTimeType() { 793 return isTime; 794 } 795 796 797 804 public boolean isDateTimeType() { 805 return isDateTime; 806 } 807 808 813 public boolean isSecret() { 814 return isSecret; 815 } 816 817 823 public boolean isHashed() { 824 return hashed; 825 } 826 827 833 public boolean isEncrypted() { 834 return encrypted; 835 } 836 837 842 public boolean isVirtual() { 843 return isVirtual; 844 } 845 846 853 public String setFilterMethod(String newMethod) 854 throws DBException { 855 856 if (!(newMethod.equals("standardFilter") || newMethod.equals("rawFilter") || 858 newMethod.equals("stripFilter"))) { 859 throw new DBException("Unknown Filter Method Name: " + newMethod); 860 } 861 862 String oldmethod = filterMethod; 863 filterMethod = newMethod; 864 return oldmethod; 865 } 866 867 868 873 public void setKey(boolean newKey) { 874 isKey = newKey; 875 } 876 877 886 public synchronized void setLookupObject(String objectName) { 887 lookupObject = objectName; 888 } 889 890 896 public synchronized void setLookupField(String lookupFieldName) { 897 lookupField = lookupFieldName; 898 } 899 900 907 public synchronized void setLookupDefinition(String definitionName) { 908 lookupDefinition = definitionName; 909 } 910 911 921 public synchronized void setMultiValued(boolean newMulti) { 922 isMultiValued = newMulti; 923 } 924 925 932 public synchronized void setHashed(boolean newValue) { 933 if (this.isQuotedTextType()) { 934 if (logCat.isDebugEnabled()) { 935 logCat.debug("Setting field " + fieldName + 936 " to hashed status"); 937 } 938 939 hashed = newValue; 940 } else { 941 throw new java.lang.IllegalArgumentException (fieldName + 942 " Field needs to be a string data type field"); 943 } 944 } 945 946 947 953 public synchronized void setEncrypted(boolean newValue) { 954 if (this.isQuotedTextType()) { 955 if (logCat.isDebugEnabled()) { 956 logCat.debug("Setting field " + fieldName + 957 " to encrypted status"); 958 } 959 960 encrypted = newValue; 961 } else { 962 throw new IllegalArgumentException ("Field needs to be a string data type field"); 963 } 964 } 965 966 967 975 public synchronized void setReadOnly() { 976 isReadOnly = true; 977 } 978 979 984 public boolean isAutoIncremented() { 985 return isAutoInc; 986 } 987 988 993 public synchronized void setSecret() { 994 isSecret = true; 995 } 996 997 1003 public synchronized void setVirtual(boolean newVirtual) { 1004 isVirtual = newVirtual; 1005 } 1006 1007 1014 public void setMask(Pattern newMask) { 1015 mask = newMask; 1016 } 1017 1018 1023 public void setDefaultMask() 1024 throws DBException { 1025 1044 1045 if (expressoFieldTypeString.equalsIgnoreCase(DBField.BIGINT_TYPE) || 1047 expressoFieldTypeString.equalsIgnoreCase(DBField.INTEGER_TYPE) || 1048 expressoFieldTypeString.equalsIgnoreCase(DBField.INT_TYPE) || 1049 expressoFieldTypeString.equalsIgnoreCase(DBField.LONG_TYPE) || 1050 expressoFieldTypeString.equalsIgnoreCase(DBField.SMALLINT_TYPE) || 1051 expressoFieldTypeString.equalsIgnoreCase(DBField.TINYINT_TYPE)) { 1052 Pattern p; 1053 try { 1054 p = patternCompiler.compile(INT_MASK, Perl5Compiler.READ_ONLY_MASK); 1055 } catch (MalformedPatternException mpe) { 1056 throw new DBException(mpe); 1057 } 1058 setMask(p); 1059 setAttribute(ATTRIBUTE_ERROR_MESSAGE, 1061 "You must enter a valid integer for field '" + 1062 description + 1063 "'"); 1064 } 1065 else if (expressoFieldTypeString.equalsIgnoreCase(DBField.DECIMAL_TYPE) || 1067 expressoFieldTypeString.equalsIgnoreCase(DBField.DOUBLE_TYPE) || 1068 expressoFieldTypeString.equalsIgnoreCase(DBField.FLOAT_TYPE) || 1069 expressoFieldTypeString.equalsIgnoreCase(DBField.NUMERIC_TYPE) || 1070 expressoFieldTypeString.equalsIgnoreCase(DBField.REAL_TYPE)) { 1071 1072 Pattern p; 1073 try { 1074 p = patternCompiler.compile(FLOAT_MASK, Perl5Compiler.READ_ONLY_MASK); 1075 } catch (MalformedPatternException mpe) { 1076 throw new DBException(mpe); 1077 } 1078 setMask(p); 1079 setAttribute(ATTRIBUTE_ERROR_MESSAGE, 1081 "You must enter a valid decimal for field '" + 1082 description + 1083 "'"); 1084 } 1085 else if (expressoFieldTypeString.equalsIgnoreCase(DBField.BIT_TYPE) || 1087 expressoFieldTypeString.equalsIgnoreCase(DBField.BOOLEAN_TYPE)) { 1088 1089 Pattern p; 1090 try { 1091 p = patternCompiler.compile(BOOLEAN_MASK, Perl5Compiler.READ_ONLY_MASK); 1092 } catch (MalformedPatternException mpe) { 1093 throw new DBException(mpe); 1094 } 1095 setMask(p); 1096 setAttribute(ATTRIBUTE_ERROR_MESSAGE, 1098 "You must enter a valid boolean for field '" + 1099 description + 1100 "'"); 1101 } 1102 else if (expressoFieldTypeString.equalsIgnoreCase(DBField.DATETIME_TYPE) || 1104 expressoFieldTypeString.equalsIgnoreCase(DBField.DATE_TYPE) || 1105 expressoFieldTypeString.equalsIgnoreCase(DBField.TIMESTAMP_TYPE) || 1106 expressoFieldTypeString.equalsIgnoreCase(DBField.TIME_TYPE)) { 1107 1108 Pattern p; 1109 try { 1110 p = patternCompiler.compile(DATE_MASK, Perl5Compiler.READ_ONLY_MASK); 1111 } catch (MalformedPatternException mpe) { 1112 throw new DBException(mpe); 1113 } 1114 setMask(p); 1115 setAttribute(ATTRIBUTE_ERROR_MESSAGE, 1117 "You must enter a valid date for field '" + 1118 description + 1119 "'"); 1120 } 1121 } 1122 1123 1128 public Pattern getMask() { 1129 return mask; 1130 } 1131 1132 1137 public synchronized void removeAttribute(String attribName) { 1138 if (attributes == null) { 1139 return; 1140 } 1141 1142 attributes.remove(attribName); 1143 } 1144 1145 1151 public synchronized void setAttribute(String attribName, 1152 Object attribValue) { 1153 if (attributes == null) { 1154 attributes = new Hashtable (); 1155 } 1156 1157 attributes.put(attribName, attribValue); 1158 } 1159 1160 1167 public Object getAttribute(String attribName) { 1168 if (attributes == null) { 1169 return null; 1170 } 1171 1172 return attributes.get(attribName); 1173 } 1174 1175 1181 public Iterator getAttributesIterator() { 1182 if (attributes == null) { 1183 return null; 1184 } 1185 1186 return new HashMap (attributes).keySet().iterator(); 1187 } 1188 1189 1190 1195 public java.util.Set getAllAttributes() { 1196 if (attributes == null) { 1197 return null; 1198 } 1199 1200 return new HashMap (attributes).keySet(); 1201 } 1202 1203 1204 1209 private void readObject(ObjectInputStream stream) 1210 throws IOException , ClassNotFoundException { 1211 if (logCat == null) { 1212 logCat = Logger.getLogger(DBField.class); 1213 } 1214 1215 stream.defaultReadObject(); 1216 } 1217 1218 1225 public Class getFilterClass() { 1226 return mFilterClass; 1227 } 1228 1229 1237 public void setFilterClass(Class filterClass) 1238 throws DBException { 1239 1240 mFilterClass = filterClass; 1241 } 1242 1243 1248 public boolean isMasked() { 1249 return mask != null; 1250 } 1251 1252 1253} 1254 | Popular Tags |