| 1 27 package com.genimen.djeneric.repository; 28 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 import java.util.Collections ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.StringTokenizer ; 35 36 import com.genimen.djeneric.language.Messages; 37 import com.genimen.djeneric.repository.exceptions.CatalogException; 38 import com.genimen.djeneric.repository.exceptions.DjenericException; 39 import com.genimen.djeneric.repository.exceptions.DomainViolationException; 40 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 41 import com.genimen.djeneric.util.DjLogger; 42 43 52 53 public abstract class DjExtent implements Cloneable , DjPropertyType 54 { 55 64 public abstract boolean isEmpty(DjSession session) throws DjenericException; 65 66 71 public abstract Object clone(); 72 73 String _name; 74 String _descriptorExpression = ""; 75 String _alias; 76 String _nameSingular; 77 String _namePlural; 78 String _title; 79 String _internalCode; 80 String _objectType; 81 String _description; 82 boolean _large = false; 83 boolean _transient = false; 84 85 DjExtent _super = null; 86 87 boolean _shouldUseAlias = true; 88 long _internalId; 89 90 ArrayList _personalProperties = new ArrayList (); 91 ArrayList _specializations = new ArrayList (); 92 93 HashMap _propertiesByName = new HashMap (); 94 HashMap _propertiesByInternalId = new HashMap (); 95 HashMap _indexesByName = new HashMap (); 96 ArrayList _personalDetailRelations = new ArrayList (); 97 ArrayList _personalMasterRelations = new ArrayList (); 98 String [] _propertyNames = new String [0]; 99 DjProperty[] _properties = new DjProperty[0]; 100 DjPackage _package = null; 101 102 124 public DjExtent(String objectType, String name, String alias, String internalCode, String title, String nameSingular, 125 String namePlural) 126 127 { 128 _internalId = DjPersistenceManager.getNextInternalId(); 129 _objectType = objectType; 130 _name = name; 131 _alias = alias; 132 _nameSingular = nameSingular; 133 _namePlural = namePlural; 134 _title = title; 135 136 _internalCode = internalCode; 137 } 138 139 public DjExtent resolveType(String instPath) throws ObjectNotDefinedException 140 { 141 if (instPath == null) return this; 142 143 int idx = instPath.indexOf("."); 144 145 String basePropName = instPath; 146 String restPath = null; 147 if (idx != -1) 148 { 149 basePropName = instPath.substring(0, idx); 150 restPath = instPath.substring(idx + 1); 151 } 152 153 if (hasProperty(basePropName)) 154 { 155 DjProperty prop = getProperty(basePropName); 156 if (!(prop.getType() instanceof DjExtent)) throw new ObjectNotDefinedException(Messages 157 .getString("DjObject.PropertyInPathNotObject", basePropName)); 158 159 return ((DjExtent) prop.getType()).resolveType(restPath); 160 } 161 162 DjRelation rel = getDetailRelation(basePropName); 163 return rel.getDetailExtent().resolveType(restPath); 164 } 165 166 172 public void setSuper(DjExtent superExtent) 173 { 174 if (_super != null) _super.removeSpecialization(this); 175 176 if (superExtent != null) 177 { 178 DjProperty idprop = findPropertyByMapping(DjPersistenceManager.MAPPING_OBJECT_ID); 179 DjProperty superIdprop = superExtent.findPropertyByMapping(DjPersistenceManager.MAPPING_OBJECT_ID); 180 if (idprop != null && superIdprop != null) 181 { 182 removeProperty(idprop); 183 } 184 superExtent.addSpecialization(this); 185 } 186 _super = superExtent; 187 188 updateHashes(); 189 } 190 191 199 public int[] getPropertySortIndices() 200 { 201 DjProperty[] props = getProperties(); 202 ArrayList lst = new ArrayList (); 203 for (int i = 0; i < props.length; i++) 204 { 205 if (props[i].getSortOrder() != 0) lst.add(props[i]); 206 } 207 Collections.sort(lst, new PropertySortOrderComparator()); 208 int[] result = new int[lst.size()]; 209 210 try 211 { 212 for (int i = 0; i < lst.size(); i++) 213 { 214 result[i] = getPropertyIndex(((DjProperty) lst.get(i)).getName()); 215 if (((DjProperty) lst.get(i)).getSortOrder() < 0) result[i] = -result[i]; 216 } 217 } 218 catch (ObjectNotDefinedException onde) 219 { 220 DjLogger.log(onde); 223 } 224 return result; 225 } 226 227 private void addSpecialization(DjExtent sub) 228 { 229 _specializations.add(sub); 230 } 231 232 private void removeSpecialization(DjExtent sub) 233 { 234 _specializations.remove(sub); 235 } 236 237 243 public boolean isPartOfInheritanceChain() 244 { 245 return (getSuper() != null) || (getSpecializations().length > 0); 246 } 247 248 256 public boolean isInstanceof(DjExtent someType) 257 { 258 if (someType == this) return true; 259 260 if (someType == null) return false; 261 262 if (getSuper() != null) return getSuper().isInstanceof(someType); 263 return false; 264 } 265 266 public boolean isInstanceof(String objectTypeName) 267 { 268 if (getObjectType().equals(objectTypeName)) return true; 269 if (getQualifiedObjectType().equals(objectTypeName)) return true; 270 271 if (getSuper() != null) return getSuper().isInstanceof(objectTypeName); 272 273 return false; 274 } 275 276 281 public DjExtent[] getSpecializations() 282 { 283 return (DjExtent[]) _specializations.toArray(new DjExtent[0]); 284 } 285 286 291 public DjExtent[] getAllSpecializations() 292 { 293 ArrayList result = new ArrayList (); 294 DjExtent[] children = getSpecializations(); 295 for (int i = 0; i < children.length; i++) 296 result.add(children[i]); 297 298 for (int i = 0; i < children.length; i++) 299 { 300 DjExtent[] specs = children[i].getAllSpecializations(); 301 for (int j = 0; j < specs.length; j++) 302 result.add(specs[j]); 303 } 304 305 return (DjExtent[]) result.toArray(new DjExtent[0]); 306 } 307 308 313 public DjExtent getSuper() 314 { 315 return _super; 316 } 317 318 325 public String getTypeName() 326 { 327 return _objectType; 328 } 329 330 public String getTypeClassName() 331 { 332 return getQualifiedObjectType(); 333 } 334 335 341 public String getNativeType() 342 { 343 return "long"; 344 } 345 346 353 public int getTypeCode() 354 { 355 return DjDomain.LONG_TYPE; 356 } 357 358 365 public int getLength() 366 { 367 return DjPersistenceManager.MAPPING_ID_COLUMN_SIZE; 368 } 369 370 376 public int getDecimals() 377 { 378 return 0; 379 } 380 381 387 public String getFormatMask() 388 { 389 return ""; 390 } 391 392 399 public void setDescription(String descr) 400 { 401 _description = descr; 402 } 403 404 410 public String getDescription() 411 { 412 return _description; 413 } 414 415 421 public String getDescriptorExpression() 422 { 423 if (_descriptorExpression.length() != 0) return _descriptorExpression; 424 if (getSuper() != null) return getSuper().getDescriptorExpression(); 425 426 return ""; 427 } 428 429 434 public String getPersonalDescriptorExpression() 435 { 436 return _descriptorExpression; 437 } 438 439 448 public void setDescriptorExpression(String expr) 449 { 450 if (expr == null) expr = ""; 451 if (getSuper() != null && getSuper().getDescriptorExpression().equals(expr)) expr = ""; 452 453 _descriptorExpression = expr; 454 } 455 456 461 public void updateHashes() 462 { 463 ArrayList props = getPropertiesList(); 464 465 _propertiesByName = new HashMap (); 466 _indexesByName = new HashMap (); 467 _propertiesByInternalId = new HashMap (); 468 _properties = (DjProperty[]) props.toArray(new DjProperty[0]); 469 _propertyNames = new String [props.size()]; 470 471 for (int i = 0; i < _properties.length; i++) 472 { 473 _propertyNames[i] = _properties[i].getName(); 474 _propertiesByName.put(_propertyNames[i], _properties[i]); 475 _indexesByName.put(_propertyNames[i], new Integer (i)); 476 _propertiesByInternalId.put(new Long (_properties[i].getInternalId()), _properties[i]); 477 } 478 } 479 480 483 private void resetInternalData() 484 { 485 _personalProperties = new ArrayList (); 486 _propertiesByName = new HashMap (); 487 _propertiesByInternalId = new HashMap (); 488 _indexesByName = new HashMap (); 489 _personalDetailRelations = new ArrayList (); 490 _personalMasterRelations = new ArrayList (); 491 _propertyNames = new String [0]; 492 _properties = new DjProperty[0]; 493 } 494 495 504 public void copyInto(DjExtent destinationExtent) throws DjenericException 505 { 506 destinationExtent.resetInternalData(); 507 copyPropertiesInto(destinationExtent); 508 } 509 510 520 protected void copyPropertiesInto(DjExtent nt) throws DjenericException 521 { 522 nt.setAlias(getAlias()); 523 nt.setInternalCode(getInternalCode()); 524 nt.setDescriptorExpression(getDescriptorExpression()); 525 nt.setName(getName()); 526 nt.setNamePlural(getNamePlural()); 527 nt.setNameSingular(getNameSingular()); 528 nt.setObjectType(getObjectType()); 529 nt.setShouldUseAlias(_shouldUseAlias); 530 nt.setTitle(getTitle()); 531 nt.setDescription(getDescription()); 532 nt.setSuper(getSuper()); 533 nt._specializations.addAll(_specializations); 534 nt._large = _large; 535 nt._transient = _transient; 536 nt._package = _package; 537 538 for (int i = 0; i < _personalProperties.size(); i++) 539 { 540 DjProperty org = (DjProperty) _personalProperties.get(i); 541 DjProperty prop = (DjProperty) org.clone(); 542 try 543 { 544 nt.addProperty(prop); 545 } 546 catch (Exception x) 547 { 548 throw new DjenericException(x); 549 } 550 } 551 552 DjRelation[] rels = getPersonalDetailRelations(); 553 for (int i = 0; i < rels.length; i++) 554 { 555 DjRelation rel = (DjRelation) rels[i].clone(); 556 rel.setMasterExtent(nt); 557 nt.addDetailRelation(rel); 558 } 559 560 rels = getPersonalMasterRelations(); 561 for (int i = 0; i < rels.length; i++) 562 { 563 DjRelation rel = (DjRelation) rels[i].clone(); 564 rel.setDetailExtent(nt); 565 nt.addMasterRelation(rel); 566 } 567 568 } 569 570 582 public void addDetailRelation(DjRelation d) 583 { 584 _personalDetailRelations.add(d); 585 } 586 587 599 public void removeDetailRelation(DjRelation d) 600 { 601 _personalDetailRelations.remove(d); 602 } 603 604 607 public void removeAllDetailRelations() 608 { 609 _personalDetailRelations.clear(); 610 } 611 612 624 public void addMasterRelation(DjRelation d) 625 { 626 _personalMasterRelations.add(d); 627 } 628 629 641 public void removeMasterRelation(DjRelation d) 642 { 643 _personalMasterRelations.remove(d); 644 } 645 646 653 public DjRelation getDetailRelation(int idx) 654 { 655 return getDetailRelations()[idx]; 656 } 657 658 667 public DjRelation getDetailRelation(String name) throws ObjectNotDefinedException 668 { 669 DjRelation[] rels = getDetailRelations(); 670 for (int i = 0; i < rels.length; i++) 671 { 672 if (rels[i].getName().equals(name)) return rels[i]; 673 } 674 throw new ObjectNotDefinedException(Messages.getString("DjExtent.RelationNotDefined", name, getName())); 675 } 676 677 684 public DjRelation[] getDetailRelations() 685 { 686 return (DjRelation[]) getDetailRelationsAsArrayList().toArray(new DjRelation[0]); 687 } 688 689 694 public DjRelation[] getPersonalDetailRelations() 695 { 696 return (DjRelation[]) _personalDetailRelations.toArray(new DjRelation[0]); 697 } 698 699 704 protected ArrayList getDetailRelationsAsArrayList() 705 { 706 if (_super == null) return _personalDetailRelations; 707 ArrayList all = new ArrayList (); 708 all.addAll(_super.getDetailRelationsAsArrayList()); 709 all.addAll(_personalDetailRelations); 710 return all; 711 } 712 713 719 public int getDetailRelationCount() 720 { 721 int length = 0; 722 if (_super != null) length = _super.getDetailRelationCount(); 723 length += _personalDetailRelations.size(); 724 return length; 725 } 726 727 733 public DjRelation[] getMasterRelations() 734 { 735 return (DjRelation[]) getMasterRelationsAsArrayList().toArray(new DjRelation[0]); 736 } 737 738 743 public DjRelation[] getPersonalMasterRelations() 744 { 745 return (DjRelation[]) _personalMasterRelations.toArray(new DjRelation[0]); 746 } 747 748 753 protected ArrayList getMasterRelationsAsArrayList() 754 { 755 if (_super == null) return _personalMasterRelations; 756 ArrayList all = new ArrayList (); 757 all.addAll(_super.getMasterRelationsAsArrayList()); 758 all.addAll(_personalMasterRelations); 759 return all; 760 } 761 762 767 public int getMasterRelationCount() 768 { 769 int length = 0; 770 if (_super != null) length = _super.getMasterRelationCount(); 771 length += _personalMasterRelations.size(); 772 return length; 773 } 774 775 783 public DjRelation getMasterRelationByPropertyName(String propertyName) 784 { 785 DjRelation[] masters = getMasterRelations(); 786 for (int i = 0; i < masters.length; i++) 787 { 788 if (masters[i].getDetailProperty() != null) 789 { 790 if (masters[i].getDetailProperty().getName().equals(propertyName)) 791 { 792 return masters[i]; 793 } 794 } 795 } 796 return null; 797 } 798 799 public DjRelation getMasterRelationByInternalId(long internalId) 800 { 801 DjRelation[] masters = getMasterRelations(); 802 for (int i = 0; i < masters.length; i++) 803 { 804 if (masters[i].getInternalId() == internalId) return masters[i]; 805 } 806 return null; 807 } 808 809 816 public DjRelation getMasterRelation(int idx) 817 { 818 return getMasterRelations()[idx]; 819 } 820 821 826 public String getObjectType() 827 { 828 return _objectType; 829 } 830 831 public String getQualifiedObjectType() 832 { 833 if (getPackage() == null) return getObjectType(); 834 return getPackage().getName() + "." + getObjectType(); 835 } 836 837 public String getQualifiedExtentName() 838 { 839 if (getPackage() == null) return getName(); 840 return getPackage().getName() + "." + getName(); 841 } 842 843 849 public void setObjectType(String objectType) 850 { 851 _objectType = objectType; 852 } 853 854 859 public String getName() 860 { 861 return _name; 862 } 863 864 870 public void setName(String name) 871 { 872 _name = name; 873 } 874 875 881 public String getNameSingular() 882 { 883 return _nameSingular; 884 } 885 886 893 public void setNameSingular(String nameSingular) 894 { 895 _nameSingular = nameSingular; 896 } 897 898 904 public String getNamePlural() 905 { 906 return _namePlural; 907 } 908 909 916 public void setNamePlural(String namePlural) 917 { 918 _namePlural = namePlural; 919 } 920 921 927 public String getTitle() 928 { 929 return _title; 930 } 931 932 939 public void setTitle(String title) 940 { 941 _title = title; 942 } 943 944 949 public String getAlias() 950 { 951 return _alias; 952 } 953 954 960 public void setAlias(String alias) 961 { 962 _alias = alias; 963 } 964 965 971 boolean shouldUseAlias() 972 { 973 return _shouldUseAlias; 974 } 975 976 983 void setShouldUseAlias(boolean b) 984 { 985 _shouldUseAlias = b; 986 } 987 988 994 public String getInternalCode() 995 { 996 return _internalCode; 997 } 998 999 1008 public void setInternalCode(String internalCode) 1009 { 1010 _internalCode = internalCode; 1011 } 1012 1013 1023 public void addProperty(DjProperty prop) throws CatalogException 1024 { 1025 String key = prop.getName().toLowerCase(); 1026 1027 if (_propertiesByName.containsKey(key)) 1028 { 1029 throw new CatalogException(Messages.getString("DjExtent.PropertyMultiple", prop.getName(), _name)); 1030 } 1031 prop.setExtent(this); 1032 _personalProperties.add(prop); 1033 1034 updateHashes(); 1035 } 1036 1037 1043 public void removeProperty(DjProperty prop) 1044 { 1045 _personalProperties.remove(prop); 1046 updateHashes(); 1047 } 1048 1049 1054 public int getPropertyCount() 1055 { 1056 return _propertyNames.length; 1057 } 1058 1059 |