1 30 package com.genimen.djeneric.repository; 31 32 import java.util.ArrayList ; 33 import java.util.Comparator ; 34 35 import com.genimen.djeneric.language.Messages; 36 import com.genimen.djeneric.repository.exceptions.CatalogException; 37 import com.genimen.djeneric.repository.exceptions.DjenericException; 38 import com.genimen.djeneric.repository.exceptions.DomainViolationException; 39 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 40 import com.genimen.djeneric.util.DjLogger; 41 42 47 public class DjProperty implements Cloneable 48 { 49 String _name; 50 String _prompt; 51 String _mapping; 52 String _alias; 53 String _defaultValue; 54 String _description; 55 DjPropertyType _type; 56 boolean _required; 57 boolean _partOfUID; 58 boolean _query = true; 59 DjExtent _extent; 60 int _seq; 61 int _sortOrder = 0; 62 ArrayList _propertyRestrictions = null; 63 64 boolean _mapped2longCache = false; 65 boolean _mapped2longCacheIsDetermined = false; 66 boolean _mapped2relationCache = false; 67 boolean _mapped2relationCacheIsDetermined = false; 68 69 boolean _partOfRestrictedPath = false; 70 71 long _internalId; 72 73 97 public DjProperty(String name, String mapping, String alias, String prompt, DjPropertyType type, boolean required, 98 int seq, String defaultValue, String description) throws DjenericException 99 { 100 _internalId = DjPersistenceManager.getNextInternalId(); 101 _extent = null; 102 setName(name); 103 setAlias(alias); 104 setPrompt(prompt); 105 setMapping(mapping); 106 setType(type); 107 setRequired(required); 108 setSeq(seq); 109 setDescription(description); 110 setDefaultValue(defaultValue); 111 } 112 113 118 public int getPropertyRestrictionCount() 119 { 120 if (_propertyRestrictions == null) return 0; 121 else return _propertyRestrictions.size(); 122 } 123 124 public boolean isRestricted() 125 { 126 return (_propertyRestrictions != null && _propertyRestrictions.size() > 0); 127 } 128 129 136 public DjPropertyRestriction getPropertyRestriction(int idx) 137 { 138 return (DjPropertyRestriction) _propertyRestrictions.get(idx); 139 } 140 141 148 public void addPropertyRestriction(DjPropertyRestriction pr) 149 { 150 if (_propertyRestrictions == null) _propertyRestrictions = new ArrayList (); 151 _propertyRestrictions.add(pr); 152 } 153 154 160 public void removePropertyRestriction(DjPropertyRestriction pr) 161 { 162 if (_propertyRestrictions == null) return; 163 _propertyRestrictions.remove(pr); 164 } 165 166 172 public void removePropertyRestriction(DjRestriction pr) 173 { 174 if (_propertyRestrictions == null) return; 175 for (int i = 0; i < getPropertyRestrictionCount(); i++) 176 { 177 if (getPropertyRestriction(i).getRestriction().equals(pr)) removePropertyRestriction(getPropertyRestriction(i)); 178 } 179 } 180 181 186 public DjExtent getExtent() 187 { 188 return _extent; 189 } 190 191 196 public Object clone() 197 { 198 try 199 { 200 DjProperty nc = new DjProperty(_name, _mapping, _alias, _prompt, _type, _required, _seq, _defaultValue, 201 _description); 202 nc.setSortOrder(getSortOrder()); 203 nc.setPartOfUID(isPartOfUID()); 204 nc.setQueryable(isQueryable()); 205 206 nc._internalId = _internalId; 207 return nc; 208 } 209 catch (Exception x) 210 { 211 DjLogger.log(x); 212 } 213 return null; 214 } 215 216 221 public long getInternalId() 222 { 223 return _internalId; 224 } 225 226 231 public int getSeq() 232 { 233 return _seq; 234 } 235 236 242 public void setSeq(int seq) 243 { 244 _seq = seq; 245 } 246 247 252 public int getSortOrder() 253 { 254 return _sortOrder; 255 } 256 257 263 public void setSortOrder(int s) 264 { 265 _sortOrder = s; 266 } 267 268 273 public String getName() 274 { 275 return _name; 276 } 277 278 283 public String getPrompt() 284 { 285 if (_prompt == null) return getName(); 286 return _prompt; 287 } 288 289 295 public void setPrompt(String prompt) 296 { 297 if (prompt != null) 298 { 299 if (prompt.trim().length() == 0) prompt = null; 300 else if (prompt.equals(_name)) prompt = null; 301 } 302 _prompt = prompt; 303 } 304 305 310 public String getAlias() 311 { 312 if (_alias == null) return getName(); 313 return _alias; 314 } 315 316 321 public String getDefaultValue() 322 { 323 return _defaultValue; 324 } 325 326 332 public void setDefaultValue(String value) 333 { 334 if (value != null && value.trim().length() == 0) value = null; 335 336 _defaultValue = value; 337 } 338 339 345 public void setAlias(String alias) 346 { 347 if (alias != null) 348 { 349 if (alias.trim().length() == 0) alias = null; 350 else if (alias.equals(_name)) alias = null; 351 } 352 _alias = alias; 353 } 354 355 360 public int getLength() 361 { 362 if (_type.getLength() == 0) 363 { 364 if (_type.getTypeCode() == DjDomain.STRING_TYPE) 365 { 366 if (getMapping().startsWith(DjPersistenceManager.MAPPING_STR)) return DjPersistenceManager.MAPPING_STR_MAX; 367 if (getMapping().startsWith(DjPersistenceManager.MAPPING_TXT)) return DjPersistenceManager.MAPPING_TXT_MAX; 368 if (getMapping().startsWith(DjPersistenceManager.MAPPING_LNG)) return DjPersistenceManager.MAPPING_LNG_MAX; 369 } 370 if (_type.getTypeCode() == DjDomain.BIGDECIMAL_TYPE) return 15; 371 if (_type.getTypeCode() == DjDomain.INT_TYPE) return 10; 372 if (_type.getTypeCode() == DjDomain.DATE_TYPE) return 15; 373 if (_type.getTypeCode() == DjDomain.LONG_TYPE) return 15; 374 } 375 return _type.getLength(); 376 } 377 378 383 public int getDecimals() 384 { 385 return _type.getDecimals(); 386 } 387 388 393 public String getDescription() 394 { 395 return _description; 396 } 397 398 404 public void setDescription(String descr) 405 { 406 _description = descr; 407 } 408 409 414 public String getMapping() 415 { 416 return _mapping; 417 } 418 419 424 public DjPropertyType getType() 425 { 426 return _type; 427 } 428 429 434 public String getNativeType() 435 { 436 return _type.getNativeType(); 437 } 438 439 public Class getNativeTypeClass() 440 { 441 return _type.getNativeTypeClass(); 442 } 443 444 449 public String getTypeName() 450 { 451 return _type.getTypeName(); 452 } 453 454 459 public int getTypeCode() 460 { 461 return _type.getTypeCode(); 462 } 463 464 470 public void setType(DjPropertyType type) 471 { 472 _type = type; 473 } 474 475 481 public void setQueryable(boolean b) 482 { 483 _query = b; 484 } 485 486 491 public boolean isQueryable() 492 { 493 return _query; 494 } 495 496 501 public boolean isRequired() 502 { 503 return _required; 504 } 505 506 512 513 public boolean isInternalTypeObjectBased() 514 { 515 return getTypeCode() == DjDomain.BIGDECIMAL_TYPE || getTypeCode() == DjDomain.BYTE_TYPE 516 || getTypeCode() == DjDomain.DATE_TYPE || getTypeCode() == DjDomain.STRING_TYPE 517 || getTypeCode() == DjDomain.DATE_TYPE; 518 } 519 520 526 public void setMapping(String mapping) 527 { 528 _mapped2longCacheIsDetermined = false; 529 _mapping = mapping.toLowerCase(); 530 } 531 532 540 public void setName(String name) throws CatalogException 541 { 542 if ((_name != null) && !_name.equalsIgnoreCase(name)) 543 { 544 if (_extent != null && _extent.hasProperty(name)) throw new CatalogException(Messages 545 .getString("DjProperty.DuplicateProperty", name, _extent.getName())); 546 } 547 _name = name; 548 if (_extent != null) _extent.updateHashes(); 549 } 550 551 557 public void setRequired(boolean required) 558 { 559 _required = required; 560 } 561 562 568 protected void setExtent(DjExtent tab) 569 { 570 _extent = tab; 571 } 572 573 578 public String toString() 579 { 580 return getName(); 581 } 582 583 592 public DjDomainValue[] getValidValues(DjSession session) throws DjenericException 593 { 594 if (getType() instanceof DjDomain) 595 { 596 DjDomain dom = (DjDomain) getType(); 597 return dom.getValidValues(); 598 } 599 else if (getType() instanceof DjExtent) 600 { 601 DjList masters = session.getObjects((DjExtent) getType()); 602 return DjDomain.convertToValueList(masters); 603 } 604 return null; 605 } 606 607 public boolean isMappedToLong() 608 { 609 if (_mapped2longCacheIsDetermined) return _mapped2longCache; 610 611 _mapped2longCache = getMapping().startsWith(DjPersistenceManager.MAPPING_LNG); 612 _mapped2longCacheIsDetermined = true; 613 return _mapped2longCache; 614 } 615 616 public boolean isMappedToRelation() 617 { 618 if (_mapped2relationCacheIsDetermined) return _mapped2relationCache; 619 620 _mapped2relationCache = getMapping().startsWith(DjPersistenceManager.MAPPING_REL); 621 _mapped2relationCacheIsDetermined = true; 622 return _mapped2relationCache; 623 } 624 625 635 protected void validate(DjPersistenceManager mgr, boolean strictChecking) throws CatalogException 636 { 637 if (getName().trim().length() == 0) throw new CatalogException(Messages.getString("DjProperty.EmptyName", _extent 638 .getName())); 639 if (getType() == null) throw new CatalogException(Messages.getString("DjProperty.NoTypeSet", _extent.getName() 640 + "." + getName())); 641 642 if (!DjPersistenceManager.isValidName(getName())) 643 { 644 throw new CatalogException(Messages.getString("DjProperty.InvalidName", _extent.getName(), getName())); 645 } 646 if (!DjPersistenceManager.isValidName(getAlias())) 647 { 648 throw new CatalogException(Messages.getString("DjProperty.InvalidAlias", _extent.getName() + "." + getName(), 649 getAlias())); 650 } 651 652 if (!getType().getTypeName().equals(getExtent().getTypeName())) try 654 { 655 mgr.getType(getType().getTypeName()); 656 } 657 catch (Exception x) 658 { 659 throw new CatalogException(Messages.getString("DjProperty.InvalidType", _extent.getName() + "." + getName(), 660 getType().getTypeName())); 661 } 662 663 try 664 { 665 String map = getMapping().toLowerCase(); 666 if (map.equalsIgnoreCase(DjPersistenceManager.MAPPING_OBJECT_ID)) 667 { 668 setType(mgr.getDomain(DjPersistenceManager.INTERNAL_ID_COLUMN_TYPE)); 669 setRequired(true); 670 } 671 } 672 catch (ObjectNotDefinedException ox) 673 { 674 throw new CatalogException(Messages.getString("global.IdNotMapped", getExtent().getName(), 675 DjPersistenceManager.MAPPING_OBJECT_ID)); 676 } 677 678 checkMapping(strictChecking); 679 if (strictChecking && getDefaultValue() != null) 680 { 681 if (getType() instanceof DjDomain) 682 { 683 DjDomain dom = (DjDomain) getType(); 684 try 685 { 686 dom.validateValue(getDefaultValue()); 687 } 688 catch (DomainViolationException dve) 689 { 690 throw new CatalogException(Messages.getString("DjProperty.InvalidDefault", _extent.getName() + "." 691 + getName(), dve.getMessage())); 692 } 693 } 694 else 695 { 696 throw new CatalogException(Messages.getString("DjProperty.RelNoDefault", _extent.getName() + "." + getName())); 697 } 698 699 } 700 } 701 702 private void checkMapping(boolean strictChecking) throws CatalogException 703 { 704 String map = getMapping().toLowerCase(); 705 706 if (!map.startsWith(DjPersistenceManager.MAPPING_OBJECT_ID) && !map.startsWith(DjPersistenceManager.MAPPING_STR) 707 && !map.startsWith(DjPersistenceManager.MAPPING_NUM) && !map.startsWith(DjPersistenceManager.MAPPING_TXT) 708 && !map.startsWith(DjPersistenceManager.MAPPING_DAT) && !map.startsWith(DjPersistenceManager.MAPPING_LNG) 709 && !map.startsWith(DjPersistenceManager.MAPPING_REL)) 710 { 711 throw new CatalogException(Messages.getString("DjProperty.InvalidMapping", _extent.getName() + "." + getName(), 712 map)); 713 } 714 715 if (strictChecking) 716 { 717 719 if (map.startsWith(DjPersistenceManager.MAPPING_LNG) 721 && ((getTypeCode() != DjDomain.BYTE_TYPE) && (getTypeCode() != DjDomain.STRING_TYPE))) 722 { 723 throw new CatalogException(Messages.getString("DjProperty.WrongLongMapping", _extent.getName() + "." 724 + getName(), map)); 725 } 726 727 if (map.startsWith(DjPersistenceManager.MAPPING_STR) && getTypeCode() != DjDomain.STRING_TYPE) 729 { 730 throw new CatalogException(Messages.getString("DjProperty.WrongStringMapping", _extent.getName() + "." 731 + getName(), map)); 732 } 733 734 if (map.startsWith(DjPersistenceManager.MAPPING_TXT) && getTypeCode() != DjDomain.STRING_TYPE) 736 { 737 throw new CatalogException(Messages.getString("DjProperty.WrongStringMapping", _extent.getName() + "." 738 + getName(), map)); 739 } 740 741 if (getTypeCode() == DjDomain.STRING_TYPE 743 && !(map.startsWith(DjPersistenceManager.MAPPING_TXT) || map.startsWith(DjPersistenceManager.MAPPING_STR) || map 744 .startsWith(DjPersistenceManager.MAPPING_LNG))) 745 { 746 throw new CatalogException(Messages.getString("DjProperty.WrongStringMapping", _extent.getName() + "." 747 + getName(), map)); 748 } 749 750 752 if (map.startsWith(DjPersistenceManager.MAPPING_DAT) && getTypeCode() != DjDomain.DATE_TYPE) 754 { 755 throw new CatalogException(Messages.getString("DjProperty.WrongDateMapping", _extent.getName() + "." 756 + getName(), map)); 757 } 758 759 if (!map.startsWith(DjPersistenceManager.MAPPING_DAT) && getTypeCode() == DjDomain.DATE_TYPE) 760 { 761 throw new CatalogException(Messages.getString("DjProperty.WrongDateMapping2", _extent.getName() + "." 762 + getName(), map, 763 DjPersistenceManager.MAPPING_DAT)); 764 } 765 766 } 767 768 if (map.startsWith(DjPersistenceManager.MAPPING_STR) && getLength() > DjPersistenceManager.MAPPING_STR_MAX) 769 { 770 throw new CatalogException(Messages.getString("DjProperty.MaxLenExceeded", _extent.getName() + "." + getName(), 771 map, String.valueOf(DjPersistenceManager.MAPPING_STR_MAX))); 772 } 773 774 if (map.startsWith(DjPersistenceManager.MAPPING_NUM) && getLength() > DjPersistenceManager.MAPPING_NUM_MAX) 775 { 776 throw new CatalogException(Messages.getString("DjProperty.MaxLenExceeded", _extent.getName() + "." + getName(), 777 map, String.valueOf(DjPersistenceManager.MAPPING_NUM_MAX))); 778 } 779 780 if (map.startsWith(DjPersistenceManager.MAPPING_REL)) 781 { 782 if (getExtent().getMasterRelationByPropertyName(getName()) == null) throw new CatalogException(Messages 783 .getString("DjProperty.MappedToRelButNorel", _extent.getName() + "." + getName(), map)); 784 } 785 786 if (map.startsWith(DjPersistenceManager.MAPPING_TXT) && getLength() > DjPersistenceManager.MAPPING_TXT_MAX) 787 { 788 throw new CatalogException(Messages.getString("DjProperty.MaxLenExceeded", _extent.getName() + "." + getName(), 789 map, String.valueOf(DjPersistenceManager.MAPPING_TXT_MAX))); 790 } 791 } 792 793 798 public static Comparator getDefaultComparator() 799 { 800 return new PropertyComparator(); 801 } 802 803 public boolean mappingIsValid() 804 { 805 try 806 { 807 checkMapping(true); 808 return true; 809 } 810 catch (CatalogException ce) 811 { } 813 return false; 814 } 815 816 public void setIsPartOfRestrictedPath(boolean b) 817 { 818 _partOfRestrictedPath = b; 819 } 820 821 824 public boolean isPartOfRestrictedPath() 825 { 826 return _partOfRestrictedPath; 827 } 828 829 public boolean isPartOfUID() 830 { 831 return _partOfUID; 832 } 833 834 public void setPartOfUID(boolean partOfUID) 835 { 836 _partOfUID = partOfUID; 837 } 838 839 } 840 841 class PropertyComparator implements Comparator 842 { 843 public int compare(Object o1, Object o2) 844 { 845 DjProperty f1 = (DjProperty) o1; 846 DjProperty f2 = (DjProperty) o2; 847 848 return f1.getSeq() - f2.getSeq(); 849 } 850 } 851 852 class PropertySortOrderComparator implements Comparator 853 { 854 public int compare(Object o1, Object o2) 855 { 856 DjProperty f1 = (DjProperty) o1; 857 DjProperty f2 = (DjProperty) o2; 858 859 return f1.getSortOrder() - f2.getSortOrder(); 860 } 861 } | Popular Tags |