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 1066 public String getPropertyName(int idx) 1067 { 1068 return _propertyNames[idx]; 1069 } 1070 1071 1078 public DjProperty getProperty(int idx) 1079 { 1080 return _properties[idx]; 1081 } 1082 1083 1094 public DjProperty getPropertyByInternalId(long id) throws ObjectNotDefinedException 1095 { 1096 DjProperty prop = (DjProperty) _propertiesByInternalId.get(new Long (id)); 1097 if (prop != null) return prop; 1098 throw new ObjectNotDefinedException(Messages.getString("DjExtent.InternalNotFound", String.valueOf(id))); 1099 } 1100 1101 1106 protected long getInternalId() 1107 { 1108 return _internalId; 1109 } 1110 1111 1117 protected void setInternalId(long internalId) 1118 { 1119 _internalId = internalId; 1120 } 1121 1122 1127 public String [] getPropertyNames() 1128 { 1129 return _propertyNames; 1130 } 1131 1132 1141 public int getPropertyIndex(String propName) throws ObjectNotDefinedException 1142 { 1143 Integer itg = (Integer ) _indexesByName.get(propName); 1144 if (itg != null) return itg.intValue(); 1145 1146 throw new ObjectNotDefinedException(Messages.getString("DjExtent.NamedPropNotFound", propName, _name)); 1147 } 1148 1149 1158 public DjProperty getProperty(String propName) throws ObjectNotDefinedException 1159 { 1160 int idx = propName.lastIndexOf("."); 1161 if (idx == -1) return doGetProperty(propName); 1162 1163 String instPath = propName.substring(0, idx); 1164 propName = propName.substring(idx + 1); 1165 return resolveType(instPath).doGetProperty(propName); 1166 } 1167 1168 private DjProperty doGetProperty(String propName) throws ObjectNotDefinedException 1169 { 1170 DjProperty prop = (DjProperty) _propertiesByName.get(propName); 1171 if (prop == null) throw new ObjectNotDefinedException(Messages.getString("DjExtent.NamedPropNotFound", propName, 1172 _name)); 1173 return prop; 1174 } 1175 1176 1186 public DjProperty getPropertyByMapping(String mapName) throws ObjectNotDefinedException 1187 { 1188 DjProperty prop = findPropertyByMapping(mapName); 1189 if (prop == null) throw new ObjectNotDefinedException(Messages.getString("DjExtent.MapNotFound", mapName, _name)); 1190 return prop; 1191 } 1192 1193 1201 public DjProperty findPropertyByMapping(String mapName) 1202 { 1203 Iterator it = _propertiesByName.values().iterator(); 1204 while (it.hasNext()) 1205 { 1206 DjProperty prop = (DjProperty) it.next(); 1207 if (prop.getMapping().equalsIgnoreCase(mapName)) return prop; 1208 } 1209 return null; 1210 } 1211 1212 1217 public DjProperty[] getProperties() 1218 { 1219 return _properties; 1220 } 1221 1222 1227 public DjProperty[] getPersonalProperties() 1228 { 1229 DjProperty[] result = (DjProperty[]) _personalProperties.toArray(new DjProperty[0]); 1230 Arrays.sort(result, new PropertyComparator()); 1231 return result; 1232 } 1233 1234 1239 public ArrayList getPropertiesList() 1240 { 1241 ArrayList result = new ArrayList (); 1242 if (_super != null) result.addAll(_super.getPropertiesList()); 1243 result.addAll(_personalProperties); 1244 return result; 1245 } 1246 1247 1254 public boolean isInherited(DjProperty prop) 1255 { 1256 return !_personalProperties.contains(prop); 1257 } 1258 1259 1266 public boolean isInherited(DjRelation rel) 1267 { 1268 return !_personalDetailRelations.contains(rel) && !_personalMasterRelations.contains(rel); 1269 } 1270 1271 1277 public DjProperty[] getPropertiesSorted() 1278 { 1279 Iterator it = _propertiesByName.values().iterator(); 1280 ArrayList v = new ArrayList (); 1281 while (it.hasNext()) 1282 { 1283 v.add(it.next()); 1284 } 1285 1286 Collections.sort(v, new PropertyComparator()); 1287 return (DjProperty[]) v.toArray(new DjProperty[0]); 1288 } 1289 1290 1294 public void sortProperties() 1295 { 1296 Collections.sort(_personalProperties, new PropertyComparator()); 1297 updateHashes(); 1298 } 1299 1300 1305 public String getPropertyListString() 1306 { 1307 StringBuffer result = new StringBuffer (100); 1308 1309 Iterator it = _propertiesByName.values().iterator(); 1310 while (it.hasNext()) 1311 { 1312 Object o = it.next(); 1313 result.append(((DjProperty) o).getName()); 1314 if (it.hasNext()) result.append(",\n"); 1315 } 1316 return result.toString(); 1317 } 1318 1319 1327 public boolean hasProperty(String propertyName) 1328 { 1329 return _propertiesByName.containsKey(propertyName); 1330 } 1331 1332 1339 public boolean hasProperty(long internalId) 1340 { 1341 for (int i = 0; i < getPropertyCount(); i++) 1342 { 1343 if (getProperty(i).getInternalId() == internalId) return true; 1344 } 1345 return false; 1346 } 1347 1348 1355 public boolean hasRelation(String relationName) 1356 { 1357 return hasDetailRelation(relationName) || hasMasterRelation(relationName); 1358 } 1359 1360 public boolean hasDetailRelation(String relationName) 1361 { 1362 for (int i = 0; i < getDetailRelationCount(); i++) 1363 { 1364 if (getDetailRelation(i).getName().equalsIgnoreCase(relationName)) return true; 1365 } 1366 return false; 1367 } 1368 1369 public boolean hasMasterRelation(String relationName) 1370 { 1371 for (int i = 0; i < getMasterRelationCount(); i++) 1372 { 1373 if (getMasterRelation(i).getName().equalsIgnoreCase(relationName)) return true; 1374 } 1375 return false; 1376 } 1377 1378 public boolean hasRelation(String relationName, DjExtent otherEnd) 1379 { 1380 for (int i = 0; i < getDetailRelationCount(); i++) 1381 { 1382 if (getDetailRelation(i).getName().equalsIgnoreCase(relationName) 1383 && getDetailRelation(i).getDetailExtent() == otherEnd) return true; 1384 } 1385 for (int i = 0; i < getMasterRelationCount(); i++) 1386 { 1387 if (getMasterRelation(i).getName().equalsIgnoreCase(relationName) 1388 && getMasterRelation(i).getMasterExtent() == otherEnd) return true; 1389 } 1390 return false; 1391 } 1392 1393 1402 public DjProperty getIdProperty() throws ObjectNotDefinedException 1403 { 1404 return getPropertyByMapping(DjPersistenceManager.MAPPING_OBJECT_ID); 1405 } 1406 1407 1412 public String toString() 1413 { 1414 return getName(); 1415 } 1416 1417 1423 public int getComponentType() 1424 { 1425 return DjDomain.COMP_COMBOBOX; 1426 } 1427 1428 1434 public String getComponentTypeName() 1435 { 1436 return DjDomain.int2componentType(getComponentType()); 1437 } 1438 1439 public void autoAdjustMappings(DjProperty[] forTheseProperties) throws ObjectNotDefinedException, CatalogException 1440 { 1441 for (int i = 0; i < forTheseProperties.length; i++) 1442 { 1443 DjProperty prop = getProperty(forTheseProperties[i].getName()); 1444 1445 if (prop.getMapping().equals(DjPersistenceManager.MAPPING_OBJECT_ID) || isInherited(prop)) continue; 1448 try 1449 { 1450 DjRelation rel = getMasterRelationByPropertyName(prop.getName()); 1451 if (rel != null) 1452 { 1453 prop.setType(rel.getMasterExtent()); 1454 } 1455 1456 if (rel == null && prop.getType() instanceof DjExtent) 1457 { 1458 throw new CatalogException(Messages.getString("DjExtent.InvalidPropertyType", prop.getName(), prop.getType() 1459 .toString())); 1460 } 1461 1462 if (prop.getType() instanceof DjExtent) autoMapRelation(prop); 1463 else autoMapNormalProperty(prop); 1464 } 1465 catch (CatalogException ce) 1466 { 1467 throw new CatalogException(prop.getName() + ": " + ce.getMessage()); 1468 } 1469 } 1470 } 1471 1472 private void autoMapNormalProperty(DjProperty prop) throws CatalogException 1473 { 1474 int typeCode = prop.getTypeCode(); 1475 if (typeCode == DjDomain.BIGDECIMAL_TYPE || typeCode == DjDomain.INT_TYPE || typeCode == DjDomain.LONG_TYPE) 1476 { 1477 if (!prop.getMapping().startsWith(DjPersistenceManager.MAPPING_NUM)) 1478 { 1479 prop.setMapping(findNextFreeMapping(DjPersistenceManager.MAPPING_NUM)); 1480 } 1481 } 1482 else if (prop.getTypeCode() == DjDomain.BYTE_TYPE) 1483 { 1484 if (!prop.getMapping().startsWith(DjPersistenceManager.MAPPING_LNG)) 1485 { 1486 prop.setMapping(findNextFreeMapping(DjPersistenceManager.MAPPING_LNG)); 1487 } 1488 } 1489 else if (prop.getTypeCode() == DjDomain.DATE_TYPE) 1490 { 1491 if (!prop.getMapping().startsWith(DjPersistenceManager.MAPPING_DAT)) 1492 { 1493 prop.setMapping(findNextFreeMapping(DjPersistenceManager.MAPPING_DAT)); 1494 } 1495 } 1496 else if (prop.getTypeCode() == DjDomain.STRING_TYPE) 1497 { 1498 int length = prop.getLength(); 1499 if (length <= DjPersistenceManager.MAPPING_STR_MAX) 1500 { 1501 prop.setMapping(findNextFreeMapping(DjPersistenceManager.MAPPING_STR)); 1502 } 1503 else if (length <= DjPersistenceManager.MAPPING_TXT_MAX) 1504 { 1505 prop.setMapping(findNextFreeMapping(DjPersistenceManager.MAPPING_TXT)); 1506 } 1507 else 1508 { 1509 prop.setMapping(findNextFreeMapping(DjPersistenceManager.MAPPING_LNG)); 1510 } 1511 } 1512 } 1513 1514 private void autoMapRelation(DjProperty prop) throws CatalogException 1515 { 1516 prop.setMapping(findNextFreeMapping(DjPersistenceManager.MAPPING_REL)); 1517 } 1518 1519 private boolean propertyIsMapped(DjExtent extent, String map) 1520 { 1521 try 1522 { 1523 extent.getPropertyByMapping(map); 1524 return true; 1525 } 1526 catch (ObjectNotDefinedException ond) 1527 { 1528 } 1530 DjExtent[] specializations = extent.getSpecializations(); 1531 for (int i = 0; i < specializations.length; i++) 1532 { 1533 if (propertyIsMapped(specializations[i], map)) return true; 1534 } 1535 return false; 1536 } 1537 1538 public String findNextFreeMapping(String startCode) throws CatalogException 1539 { 1540 int sz = DjPersistenceManager.getMaxMappingCount(startCode); 1541 1542 for (int i = 1; i <= sz; i++) 1543 { 1544 String code = startCode; 1545 if (i < 10) code += "0"; 1546 code += i; 1547 if (!propertyIsMapped(this, code)) return code; 1548 } 1549 throw new CatalogException(Messages.getString("DjExtent.NoMoreMappings", startCode)); 1550 } 1551 1552 1560 public void validateValue(Object value) throws DomainViolationException 1561 { 1562 if (value == null) return; 1563 1564 if (value instanceof DjObject) 1565 { 1566 DjObject obj = (DjObject) value; 1567 if (!obj.getExtent().isInstanceof(this)) throw new DomainViolationException(Messages 1568 .getString("DjExtent.ValueNotOfType", getObjectType())); 1569 } 1570 else if (!(value instanceof Long )) throw new DomainViolationException(Messages.getString("DjExtent.ValueNotOfType", 1571 getObjectType())); 1572 } 1573 1574 1580 public void validateDescriptorExpression() throws CatalogException 1581 { 1582 validateDescriptorExpression(getDescriptorExpression()); 1583 } 1584 1585 public void validateDescriptorExpression(String expr) throws CatalogException 1586 { 1587 try 1588 { 1589 if (expr == null || expr.trim().length() == 0) 1590 { 1591 return; 1592 } 1593 1594 StringTokenizer tok = new StringTokenizer (expr, "+"); 1595 1596 while (tok.hasMoreElements()) 1597 { 1598 String subExpr = tok.nextToken(); 1599 validateSingleDescriptorExpr(subExpr); 1600 } 1601 } 1602 catch (Exception x) 1603 { 1604 throw new CatalogException(Messages.getString("DjExtent.InvalidDescriptor", getName(), getDescriptorExpression(), 1605 x.getMessage())); 1606 } 1607 } 1608 1609 private void validateSingleDescriptorExpr(String expr) throws DjenericException 1610 { 1611 expr = expr.trim(); 1612 1613 if (expr.length() == 0) return; 1615 if (expr.startsWith("\"") && expr.endsWith("\"") && expr.length() > 1) return; 1616 1617 getProperty(expr); 1619 } 1620 1621 public DjProperty[] getPropertiesWithInvalidMapping() 1622 { 1623 ArrayList result = new ArrayList (); 1624 1625 DjProperty[] cols = getProperties(); 1626 for (int i = 0; i < cols.length; i++) 1627 { 1628 if (!cols[i].mappingIsValid()) result.add(cols[i]); 1629 1630 for (int j = 0; j < cols.length; j++) 1631 { 1632 if (cols[i].getMapping().equals(cols[j].getMapping()) && !result.contains(cols[i]) && !result.contains(cols[j]) 1633 && j != i) 1634 { 1635 if (!isInherited(cols[i])) result.add(cols[i]); 1636 else if (!isInherited(cols[j])) result.add(cols[j]); 1637 } 1638 } 1639 } 1640 1641 return (DjProperty[]) result.toArray(new DjProperty[0]); 1642 } 1643 1644 1656 public void validate(DjPersistenceManager mgr, boolean strictChecking) throws CatalogException 1657 { 1658 if (getName().trim().length() == 0) throw new CatalogException(Messages.getString("DjExtent.EmptyName")); 1659 if (getNamePlural().trim().length() == 0) throw new CatalogException(Messages.getString("DjExtent.PluralEmpty", 1660 getName())); 1661 if (getNameSingular().trim().length() == 0) throw new CatalogException(Messages.getString("DjExtent.SingularEmpty", 1662 getName())); 1663 if (getObjectType().trim().length() == 0) throw new CatalogException(Messages.getString("DjExtent.TypeEmpty", 1664 getName())); 1665 if (getTitle().trim().length() == 0) throw new CatalogException(Messages 1666 .getString("DjExtent.TitleEmpty", getName())); 1667 if (getInternalCode().trim().length() == 0) throw new CatalogException(Messages.getString("DjExtent.InternalEmpty", 1668 getName())); 1669 if (getInternalCode().length() > DjPersistenceManager.MAX_INTERNAL_CODE_LENGTH) throw new CatalogException(Messages 1670 .getString("DjExtent.AliasTooLong", getName(), String.valueOf(DjPersistenceManager.MAX_INTERNAL_CODE_LENGTH))); 1671 if (mgr.isKnownDomain(getObjectType())) throw new CatalogException(Messages.getString("DjExtent.TypeNotUnique", 1672 getName(), getObjectType())); 1673 1674 setName(getName().trim()); 1675 setAlias(getAlias().trim()); 1676 setObjectType(getObjectType().trim()); 1677 1678 DjProperty idprop = null; 1679 try 1680 { 1681 idprop = getPropertyByMapping(DjPersistenceManager.MAPPING_OBJECT_ID); 1682 if (idprop.getTypeCode() != DjDomain.LONG_TYPE) 1683 { 1684 throw new CatalogException(Messages.getString("DjExtent.IDNotLong", idprop.getName(), getName())); 1685 } 1686 1687 } 1688 catch (ObjectNotDefinedException x) 1689 { 1690 try 1691 { 1692 createIdProperty(mgr); 1693 } 1694 catch (DjenericException dje) 1695 { 1696 DjLogger.log(dje); 1697 throw new CatalogException(Messages.getString("global.IdNotMapped", getName(), 1698 DjPersistenceManager.MAPPING_OBJECT_ID)); 1699 } 1700 } 1701 1702 if (strictChecking) validateDescriptorExpression(); 1703 1704 if (!DjPersistenceManager.isValidName(getName())) 1705 { 1706 throw new CatalogException(Messages.getString("DjExtent.InvalidExtentName", getName())); 1707 } 1708 if (!DjPersistenceManager.isValidName(getObjectType())) 1709 { 1710 throw new CatalogException(Messages.getString("DjExtent.InvalidExtentType", getName(), getObjectType())); 1711 } 1712 if (!DjPersistenceManager.isValidName(getAlias())) 1713 { 1714 throw new CatalogException(Messages.getString("DjExtent.InvalidAlias", getName(), getAlias())); 1715 } 1716 1717 DjProperty[] props = getProperties(); 1718 for (int i = 0; i < props.length; i++) 1719 { 1720 for (int j = 0; j < props.length; j++) 1721 { 1722 if ((props[i] != props[j]) && (props[i].getAlias().equalsIgnoreCase(props[j].getAlias()))) 1723 { 1724 throw new CatalogException(Messages.getString("DjExtent.PropAliasNotUnique", getName(), props[j].getName())); 1725 } 1726 if ((props[i] != props[j]) && (props[i].getName().equalsIgnoreCase(props[j].getName()))) 1727 { 1728 throw new CatalogException(Messages.getString("DjExtent.PropNameNotUnique", getName(), props[j].getName())); 1729 } 1730 if ((props[i] != props[j]) && (props[i].getMapping().equalsIgnoreCase(props[j].getMapping()))) 1731 { 1732 throw new CatalogException(Messages.getString("DjExtent.DuplicateMapping", getName() + "." 1733 + props[j].getName(), props[i] 1734 .getMapping(), props[j].getName())); 1735 } 1736 } 1737 } 1738 for (int i = 0; i < props.length; i++) 1739 { 1740 props[i].validate(mgr, strictChecking); 1741 } 1742 1743 DjRelation[] rels = getMasterRelations(); 1744 for (int i = 0; i < rels.length; i++) 1745 { 1746 rels[i].validate(mgr, strictChecking); 1747 DjProperty detailProp = rels[i].getDetailProperty(); 1748 1749 if (!detailProp.getMapping().startsWith(DjPersistenceManager.MAPPING_REL)) 1750 { 1751 throw new CatalogException(Messages.getString("DjExtent.NotRelMapped", getName() + "." + rels[i].getName(), 1752 detailProp.getName(), DjPersistenceManager.MAPPING_REL, 1753 detailProp.getMapping())); 1754 } 1755 for (int j = 0; j < rels.length; j++) 1756 { 1757 if (rels[i] != rels[j]) 1758 { 1759 if ((rels[i].getDetailExtent() == rels[j].getDetailExtent()) 1760 && (rels[i].getDetailProperty() == rels[j].getDetailProperty())) 1761 { 1762 throw new CatalogException(Messages.getString("DjExtent.DuplicateRel", getName(), rels[i].getDetailExtent() 1763 .getName(), rels[i].getDetailProperty().getName())); 1764 } 1765 } 1766 } 1767 } 1768 1769 rels = getDetailRelations(); 1770 for (int i = 0; i < rels.length; i++) 1771 { 1772 for (int j = 0; j < rels.length; j++) 1773 { 1774 if (rels[i] != rels[j]) 1775 { 1776 if (rels[i].getName().equals(rels[j].getName()) && strictChecking) 1777 { 1778 String msg = Messages.getString("DjExtent.DupRelName", getName(), rels[i].getName()); 1779 1780 if (!rels[i].getMasterExtent().getName().equals(rels[j].getMasterExtent().getName())) 1781 { 1782 msg += "\n" 1783 + Messages.getString("DjExtent.RelsDefinedIn", rels[i].getDetailExtent().getName(), rels[j] 1784 .getDetailExtent().getName()); 1785 } 1786 throw new CatalogException(msg); 1787 } 1788 } 1789 } 1790 } 1791 1792 if (getSuper() != null) 1793 { 1794 try 1795 { 1796 mgr.getType(getSuper().getTypeName()); 1797 } 1798 catch (Exception x) 1799 { 1800 throw new CatalogException(Messages.getString("DjExtent.InvalidSuperType", getName(), getSuper().getTypeName())); 1801 } 1802 } 1803 } 1804 1805 public String getDefaultMasterRelationName() 1806 { 1807 String relationName = getNamePlural(); 1808 if (relationName == null || relationName.trim().length() == 0) 1809 { 1810 relationName = getObjectType(); 1811 } 1812 relationName = relationName.trim(); 1813 1814 for (int i = 1; i < relationName.length(); i++) 1815 { 1816 if (relationName.charAt(i - 1) == ' ') relationName = relationName.substring(0, i) 1817 + relationName.toUpperCase().substring(i, i + 1) 1818 + relationName.substring(i + 1); 1819 } 1820 StringBuffer result = new StringBuffer (); 1821 for (int i = 0; i < relationName.length(); i++) 1822 { 1823 char ch = relationName.charAt(i); 1824 if (Character.isJavaIdentifierPart(ch)) result.append(ch); 1825 } 1826 1827 if (result.length() > 0) relationName = result.toString(); 1828 1829 if (relationName.length() > 0) 1830 { 1831 relationName = relationName.toLowerCase().charAt(0) + relationName.substring(1); 1832 } 1833 return relationName; 1834 } 1835 1836 1839 public boolean isLarge() 1840 { 1841 return _large; 1842 } 1843 1844 1847 public void setLarge(boolean b) 1848 { 1849 _large = b; 1850 } 1851 1852 public DjProperty createIdProperty(DjPersistenceManager mgr) throws DjenericException 1853 { 1854 DjProperty prop = new DjProperty("id", DjPersistenceManager.MAPPING_OBJECT_ID, "id", Messages 1855 .getString("ModelEditor.ObjectID"), mgr.getType(DjPersistenceManager.INTERNAL_ID_COLUMN_TYPE), true, 0, null, 1856 Messages.getString("ModelEditor.ObjectID")); 1857 1858 prop.setQueryable(false); 1859 addProperty(prop); 1860 return prop; 1861 } 1862 1863 1866 public void fixAlias() 1867 { 1868 StringBuffer alias = new StringBuffer (getInternalCode()); 1869 if (alias.length() == 0) alias.append(getName()); 1870 1871 if (alias.length() > DjPersistenceManager.MAX_INTERNAL_CODE_LENGTH) 1872 { 1873 int i = 0; 1874 while (i < alias.length()) 1875 { 1876 if ("aAeEiIoOuU".indexOf(alias.charAt(i)) != -1) alias.deleteCharAt(i); 1877 else i++; 1878 } 1879 if (alias.length() > DjPersistenceManager.MAX_INTERNAL_CODE_LENGTH) 1880 { 1881 alias = new StringBuffer (alias.substring(0, DjPersistenceManager.MAX_INTERNAL_CODE_LENGTH)); 1882 } 1883 } 1884 setInternalCode(alias.toString()); 1885 setAlias(alias.toString()); 1886 } 1887 1888 public void clearSpecializations() 1889 { 1890 _specializations.clear(); 1891 1892 } 1893 1894 public void refreshSpecializations() 1895 { 1896 setSuper(getSuper()); 1897 } 1898 1899 public boolean isTransient() 1900 { 1901 return _transient; 1902 } 1903 1904 public void setTransient(boolean transient1) 1905 { 1906 _transient = transient1; 1907 } 1908 1909 public DjPackage getPackage() 1910 { 1911 return _package; 1912 } 1913 1914 public void setPackage(DjPackage package1) 1915 { 1916 _package = package1; 1917 } 1918 1919 public String getPropertyType(String propertyPath) throws ObjectNotDefinedException 1920 { 1921 DjProperty prop; 1922 int idx = propertyPath.indexOf("."); 1923 if (idx == -1) 1924 { 1925 try 1926 { 1927 prop = getProperty(propertyPath); 1928 } 1929 catch (ObjectNotDefinedException onde) 1930 { 1931 try 1932 { 1933 DjExtent detail = getDetailRelation(propertyPath).getDetailExtent(); 1934 return detail.getQualifiedObjectType(); 1935 } 1936 catch (ObjectNotDefinedException onde2) 1937 { 1938 throw onde; 1939 } 1940 } 1941 } 1942 else 1943 { 1944 idx = propertyPath.lastIndexOf("."); 1945 String propName = propertyPath.substring(idx + 1); 1946 String instPath = propertyPath.substring(0, idx); 1947 DjExtent ext = resolveType(instPath); 1948 prop = ext.getProperty(propName); 1949 } 1950 if (prop.getType() instanceof DjExtent) 1951 { 1952 return ((DjExtent) prop.getType()).getQualifiedObjectType(); 1953 } 1954 else 1955 { 1956 return prop.getNativeTypeClass().getName(); 1957 } 1958 } 1959 1960 protected void deleteInvalidRelationProperties() 1961 { 1962 DjProperty[] props = getProperties(); 1963 for (int p = 0; p < props.length; p++) 1964 { 1965 if (props[p].isMappedToRelation() && getMasterRelationByPropertyName(props[p].getName()) == null) 1966 { 1967 DjLogger.log("Removed invalid relprop: " + props[p]); 1968 removeProperty(props[p]); 1969 } 1970 } 1971 } 1972 1973 public boolean hasUidDefined() 1974 { 1975 for (int i = 0; i < getPropertyCount(); i++) 1976 if (getProperty(i).isPartOfUID()) return true; 1977 return false; 1978 } 1979} | Popular Tags |