1 27 package com.genimen.djeneric.repository; 28 29 import java.io.UnsupportedEncodingException ; 30 import java.math.BigDecimal ; 31 import java.text.ParseException ; 32 import java.text.SimpleDateFormat ; 33 import java.util.ArrayList ; 34 import java.util.Arrays ; 35 import java.util.Date ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 import java.util.StringTokenizer ; 39 40 import com.genimen.djeneric.language.Messages; 41 import com.genimen.djeneric.repository.exceptions.CanNotDeleteException; 42 import com.genimen.djeneric.repository.exceptions.DjenericException; 43 import com.genimen.djeneric.repository.exceptions.DomainViolationException; 44 import com.genimen.djeneric.repository.exceptions.ObjectDeletedException; 45 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 46 import com.genimen.djeneric.repository.exceptions.PropertyFormatException; 47 import com.genimen.djeneric.repository.exceptions.PropertyRequiredException; 48 import com.genimen.djeneric.repository.exceptions.RestrictionFailedException; 49 import com.genimen.djeneric.util.DjLogger; 50 51 57 public abstract class DjObject implements DjValueObject 58 { 59 66 protected abstract void update() throws DjenericException; 67 68 74 protected abstract void insert() throws DjenericException; 75 76 82 protected abstract void delete() throws DjenericException; 83 84 90 public abstract void reload() throws DjenericException; 91 92 94 97 public final static int STRING_TYPE = DjDomain.STRING_TYPE; 98 101 public final static int INT_TYPE = DjDomain.INT_TYPE; 102 105 public final static int LONG_TYPE = DjDomain.LONG_TYPE; 106 109 public final static int DATE_TYPE = DjDomain.DATE_TYPE; 110 113 public final static int BIGDECIMAL_TYPE = DjDomain.BIGDECIMAL_TYPE; 114 115 private DjExtent _extent; 116 private boolean _isTemporary = false; 117 private boolean _isPersisted = false; 118 private boolean _isBeingLoaded = false; 119 private boolean _isMarkedForDelete = false; 120 private boolean _isModified = false; 121 private boolean _ignoreModifications = false; 122 private String _descriptorExpression = null; 123 private boolean _transient = false; 124 private boolean _alreadyHitDuringCurrentTransaction = false; 125 static SimpleDateFormat sf = new SimpleDateFormat (Messages 126 .getString("global.DateFormatMask")); 127 private boolean _isDetail = false; 128 private boolean _isDetailOfNewMaster; 129 130 private DjAssociation[] _detailAssociations = null; 131 private DjAssociation[] _masterAssociations = null; 132 private Object [] _propertyValues; 133 private Object [] _originalPropertyValues; 134 135 private DjSession _session; 136 137 147 protected DjObject(DjSession session, DjExtent extent) throws DjenericException 148 { 149 _propertyValues = new Object [extent.getPropertyCount()]; 150 _originalPropertyValues = new Object [extent.getPropertyCount()]; 151 152 _extent = extent; 153 _session = session; 154 setDescriptorExpression(extent.getDescriptorExpression()); 155 initializeDefaultValues(); 156 } 157 158 164 public DjObject shallowCopy() throws DjenericException 165 { 166 DjObject obj = getSession().createObject(getExtent()); 167 168 DjProperty id = getExtent().getIdProperty(); 169 for (int i = 0; i < getPropertyCount(); i++) 170 { 171 if (getProperty(i) == id) continue; 172 obj.set(i, get(i)); 173 } 174 175 return obj; 176 } 177 178 protected void initializeDefaultValues() throws PropertyFormatException, DjenericException 179 { 180 try 181 { 182 setIgnoreModifications(true); 183 DjProperty[] properties = getExtent().getProperties(); 184 for (int i = 0; i < properties.length; i++) 185 { 186 if (properties[i].getDefaultValue() != null) 187 { 188 setString(i, properties[i].getDefaultValue()); 189 } 190 } 191 } 192 finally 193 { _isModified = false; 196 setIgnoreModifications(false); 197 } 198 } 199 200 210 protected DjObject(DjSession session, String objectType) throws DjenericException 211 { 212 this(session, session.getPersistenceManager().getExtentByObjectType(objectType)); 213 } 214 215 221 protected void setPersisted(boolean b) 222 { 223 _isPersisted = b; 224 } 225 226 232 void setHitDuringCurrentTransaction(boolean b) 233 { 234 _alreadyHitDuringCurrentTransaction = b; 235 } 236 237 242 boolean isHitDuringCurrentTransaction() 243 { 244 return _alreadyHitDuringCurrentTransaction; 245 } 246 247 253 protected void trace(String msg) 254 { 255 _session.trace(msg); 256 } 257 258 265 public boolean shouldTrace(int level) 266 { 267 return _session.shouldTrace(level); 268 } 269 270 276 protected void setBeingLoaded(boolean b) 277 { 278 _isBeingLoaded = b; 279 } 280 281 287 protected void setModified(boolean b) throws DjenericException 288 { 289 if (b != _isModified) 290 { 291 _isModified = b; 292 293 if (!isTransient() && !_ignoreModifications && !_isBeingLoaded) getSession().notifyModified(this, _isModified); 295 } 296 } 297 298 public void setTransient(boolean b) throws DjenericException 299 { 300 boolean wastransient = _transient; 301 302 _transient = b; 305 306 if (wastransient && !b) 308 { 309 getSession().notifyModified(this, isModified()); 310 if (getLong(getIdPropertyName()) < 0) setNull(getIdPropertyName()); 313 } 314 else if (!_isTemporary && b) getSession().cacheObject(this); 315 } 316 317 322 public boolean isTransient() 323 { 324 return _transient; 325 } 326 327 335 protected void setIgnoreModifications(boolean b) 336 { 337 _ignoreModifications = b; 338 } 339 340 345 public boolean isMarkedForDelete() 346 { 347 return _isMarkedForDelete; 348 } 349 350 359 public boolean isInstanceOf(String typeName) throws ObjectNotDefinedException 360 { 361 return isInstanceOf(getSession().getPersistenceManager().getExtentByObjectType(typeName)); 362 } 363 364 371 public boolean isInstanceOf(DjExtent ext) 372 { 373 DjExtent sup = getExtent(); 374 while (sup != null) 375 { 376 if (sup == ext) return true; 377 sup = sup.getSuper(); 378 } 379 return false; 380 } 381 382 public boolean equals(Object o) 383 { 384 if (o == null) return false; 385 if (!(o instanceof DjObject)) return false; 386 387 DjObject obj = (DjObject) o; 388 if (obj.getExtent() != getExtent()) return false; 389 390 for (int i = 0; i < _propertyValues.length; i++) 391 { 392 if (obj._propertyValues[i] == null && _propertyValues[i] != null) return false; 393 if (obj._propertyValues[i] != null && _propertyValues[i] == null) return false; 394 if (obj._propertyValues[i] != null && _propertyValues[i] != null 395 && !obj._propertyValues[i].equals(_propertyValues[i])) return false; 396 } 397 return true; 398 } 399 400 public int hashCode() 401 { 402 int result = 0; 403 404 String [] names = getPropertyNames(); 405 for (int i = 0; i < names.length; i++) 406 { 407 Object o = _propertyValues[i]; 408 if (o != null) result += o.hashCode(); 409 } 410 return result; 411 } 412 413 418 public DjExtent getExtent() 419 { 420 return _extent; 421 } 422 423 428 protected Object [] getOriginalPropertyValues() 429 { 430 return _originalPropertyValues; 431 } 432 433 438 protected Object [] getPropertyValues() 439 { 440 return _propertyValues; 441 } 442 443 448 public DjSession getSession() 449 { 450 return _session; 451 } 452 453 456 protected void updateOriginalPropertyValues() 457 { 458 _originalPropertyValues = (Object []) _propertyValues.clone(); 459 } 462 463 469 protected void setIsDetail(boolean b) 470 { 471 _isDetail = b; 472 } 473 474 479 protected boolean isDetail() 480 { 481 return _isDetail; 482 } 483 484 String _idCol = null; 485 486 public boolean isPropertyChanged(String propertyName) throws ObjectNotDefinedException 487 { 488 return isPropertyChanged(getPropertyIndex(propertyName)); 489 } 490 491 public boolean isPropertyChanged(int i) 492 { 493 if (_propertyValues[i] == null && _originalPropertyValues[i] != null) return true; 494 if (_propertyValues[i] != null && _originalPropertyValues[i] == null) return true; 495 return !_propertyValues[i].equals(_originalPropertyValues[i]); 496 } 497 498 505 public String getIdPropertyName() throws ObjectNotDefinedException 506 { 507 if (_idCol == null) 508 { 509 _idCol = _extent.getIdProperty().getName(); 510 } 511 return _idCol; 512 } 513 514 521 public long getObjectId() throws DjenericException 522 { 523 determineObjectIdIfNeeded(); 524 return getLong(getIdPropertyName()); 525 } 526 527 532 public String getDescriptorExpression() 533 { 534 return _descriptorExpression; 535 } 536 537 543 public void setDescriptorExpression(String descriptorExpression) 544 { 545 _descriptorExpression = descriptorExpression; 546 } 547 548 553 public String getDescriptor() 554 { 555 String result = evaluateDescriptorExpression(_descriptorExpression); 556 if (result == null) return ""; 557 return result; 558 } 559 560 567 private String getDescriptorAvoidingRecursion(DjObject origin) 568 { 569 if (origin == this) return "Recursion Detected"; 570 return getDescriptor(); 571 } 572 573 580 public String getDescriptor(String expr) 581 { 582 if (expr == null || expr.trim().length() == 0) expr = _descriptorExpression; 583 584 String result = evaluateDescriptorExpression(expr); 585 if (result == null) return ""; 586 return result; 587 } 588 589 private String evaluateDescriptorExpression(String expr) 590 { 591 String orgExpr = expr; 592 try 593 { 594 if (expr == null || expr.trim().length() == 0) 595 { 596 return getObjectNameSingular() + " " + getObjectId(); 597 } 598 599 StringTokenizer tok = new StringTokenizer (expr, "+"); 600 StringBuffer result = new StringBuffer (); 601 602 while (tok.hasMoreElements()) 603 { 604 String subExpr = tok.nextToken(); 605 String single = evaluateSingleDescriptorField(subExpr); 606 if (single != null) 607 { 608 result.append(single); 609 } 610 } 611 612 if (result.length() == 0) result.append(Messages.getString("DjObject.NewObjectDescriptor", getExtent() 613 .getNameSingular())); 614 return result.toString(); 615 } 616 catch (ObjectDeletedException ode) 617 { 618 return Messages.getString("global.Deleted"); 619 } 620 catch (Exception x) 621 { 622 DjLogger.log(x); 623 return Messages.getString("DjObject.DescrEvalError", orgExpr); 624 } 625 } 626 627 private String evaluateSingleDescriptorField(String expr) throws DjenericException 628 { 629 expr = expr.trim(); 630 631 if (expr.length() == 0) return ""; 633 if (expr.charAt(0) == '"') 634 { 635 if (expr.endsWith("\"")) return expr.substring(1, expr.length() - 1); 636 return expr.substring(1); 637 } 638 639 DjProperty prop = getProperty(expr); 640 DjPropertyType type = prop.getType(); 641 642 if (type instanceof DjDomain) 643 { 644 DjDomain dom = (DjDomain) type; 645 if (dom.hasValidValues()) 646 { 647 return dom.translateCode(getString(expr)); 648 } 649 } 650 if (type instanceof DjExtent) 651 { 652 DjObject getFrom = (DjObject) get(expr); 653 if (getFrom == null) return null; 654 else return getFrom.getDescriptorAvoidingRecursion(this); 655 } 656 return getString(expr); 657 } 658 659 public String evaluateStringExpression(String expr) 660 { 661 String orgExpr = expr; 662 try 663 { 664 if (expr == null || expr.trim().length() == 0) 665 { 666 return null; 667 } 668 669 StringTokenizer tok = new StringTokenizer (expr, "+"); 670 StringBuffer result = new StringBuffer (); 671 672 while (tok.hasMoreElements()) 673 { 674 String subExpr = tok.nextToken(); 675 String single = evaluateSingleDescriptorField(subExpr); 676 if (single == null) 677 { 678 return null; 679 } 680 result.append(single); 681 } 682 if (result.length() == 0) return null; 683 684 return result.toString(); 685 } 686 catch (Exception x) 687 { 688 DjLogger.log(x); 689 return Messages.getString("DjObject.DescrEvalError", orgExpr); 690 } 691 } 692 693 public Object evaluateObjectExpression(String expr) throws DjenericException 694 { 695 if (expr == null || expr.trim().length() == 0) 696 { 697 return null; 698 } 699 700 String orgExpr = expr; 701 DjObject getFrom = this; 702 703 expr = expr.trim(); 704 705 if (expr.length() == 0) return null; 706 707 int idx = expr.indexOf("."); 708 while (idx != -1) 709 { 710 String sub = expr.substring(0, idx); 711 Object o = get(sub); 712 if (o == null) return null; 713 if (!(o instanceof DjObject)) 715 { 716 System.err.println(Messages.getString("DjObject.DescrEvalErrorFor", orgExpr, getExtent().getName())); 717 System.err.println(Messages.getString("DjObject.CannotTraverse", sub)); 718 throw new DjenericException(Messages.getString("DjObject.NotDjObject", sub)); 719 } 720 getFrom = (DjObject) o; 721 expr = expr.substring(idx + 1); 722 idx = expr.indexOf("."); 723 } 724 return getFrom; 725 } 726 727 732 public String getQualifiedObjectType() 733 { 734 return _extent.getQualifiedObjectType(); 735 } 736 737 742 public String getObjectNameSingular() 743 { 744 return _extent.getNameSingular(); 745 } 746 747 752 public String getObjectNamePlural() 753 { 754 return _extent.getNamePlural(); 755 } 756 757 762 public DjAssociation[] getDetailAssociations() 763 { 764 if (_detailAssociations == null) 765 { 766 ArrayList directAssocs = new ArrayList (); 767 768 ArrayList result = new ArrayList (); 769 770 DjRelation[] rels = _extent.getDetailRelations(); 771 for (int i = 0; i < rels.length; i++) 772 { 773 DjExtent detailExtent = rels[i].getDetailExtent(); 774 ArrayList allDetails = new ArrayList (); 775 allDetails.add(detailExtent); 776 directAssocs.add(detailExtent); 777 778 DjExtent[] allSpecs = detailExtent.getAllSpecializations(); 779 for (int d = 0; d < allSpecs.length; d++) 780 allDetails.add(allSpecs[d]); 781 782 Iterator it = allDetails.iterator(); 783 while (it.hasNext()) 784 { 785 DjExtent currentDetail = (DjExtent) it.next(); 786 DjAssociation a = _session.getPersistenceManager().createAssociation(_session, this, rels[i], currentDetail); 787 if (!directAssocs.contains(currentDetail)) a.setSpecializationOfActualDetailExtent(true); 788 result.add(a); 789 } 790 } 791 _detailAssociations = (DjAssociation[]) result.toArray(new DjAssociation[0]); 792 } 793 794 return _detailAssociations; 795 } 796 797 806 public DjAssociation getDetailAssociationByName(String assocName, DjExtent specializedDetailExtent) 807 throws ObjectNotDefinedException 808 { 809 if (_detailAssociations == null) 810 { 811 getDetailAssociations(); 812 } 813 for (int i = 0; i < _detailAssociations.length; i++) 814 { 815 if (_detailAssociations[i].getName().equals(assocName) 816 && _detailAssociations[i].getDetailExtent() == specializedDetailExtent) 817 { 818 return _detailAssociations[i]; 819 } 820 } 821 throw new ObjectNotDefinedException(Messages.getString("DjObject.AssociationNotDefined", assocName)); 822 } 823 824 833 public DjAssociation getDetailAssociationByName(String assocName) throws ObjectNotDefinedException 834 { 835 if (_detailAssociations == null) 836 { 837 getDetailAssociations(); 838 } 839 for (int i = 0; i < _detailAssociations.length; i++) 840 { 841 if (_detailAssociations[i].getName().equals(assocName) 842 && !_detailAssociations[i].isSpecializationOfActualDetailExtent()) 843 { 844 return _detailAssociations[i]; 845 } 846 } 847 throw new ObjectNotDefinedException(Messages.getString("DjObject.AssociationNotDefined", assocName)); 848 } 849 850 855 public DjAssociation[] getMasterAssociations() 856 { 857 if (_masterAssociations == null) 858 { 859 DjRelation[] rels = _extent.getMasterRelations(); 860 _masterAssociations = new DjAssociation[rels.length]; 861 for (int i = 0; i < rels.length; i++) 862 { 863 DjAssociation ass = _session.getPersistenceManager().createAssociation(_session, this, rels[i], 864 rels[i].getDetailExtent()); 865 _masterAssociations[i] = ass; 866 } 867 } 868 return _masterAssociations; 869 } 870 871 880 public DjAssociation getMasterAssociationByName(String assocName) throws ObjectNotDefinedException 881 { 882 if (_masterAssociations == null) 883 { 884 getMasterAssociations(); 885 } 886 for (int i = 0; i < _masterAssociations.length; i++) 887 { 888 if (_masterAssociations[i].getName().equals(assocName)) 889 { 890 return _masterAssociations[i]; 891 } 892 } 893 throw new ObjectNotDefinedException(Messages.getString("DjObject.AssociationNotDefined", assocName)); 894 } 895 896 901 protected void synchronizeAssociations() throws DjenericException 902 { 903 if (!isTransient() && !isMarkedForDelete()) 904 { 905 DjAssociation ass[] = getDetailAssociations(); 906 for (int a = 0; a < ass.length; a++) 907 { 908 ass[a].synchronizeWithSession(); 909 } 910 } 911 } 912 913 920 public DjAssociation getMasterAssociationByPropertyName(int propIdx) 921 { 922 return getMasterAssociationByPropertyName(getPropertyName(propIdx)); 923 } 924 925 932 public DjAssociation getMasterAssociationByPropertyName(String propertyName) 933 { 934 DjAssociation[] assocs = getMasterAssociations(); 935 for (int i = 0; i < assocs.length; i++) 936 { 937 if (assocs[i].getRelation().getDetailProperty().getName().equals(propertyName)) 938 { 939 return assocs[i]; 940 } 941 } 942 return null; 943 } 944 945 953 public void checkRequiredProperties() throws PropertyRequiredException 954 { 955 ArrayList missingProperties = null; 956 957 for (int i = 0; i < getPropertyCount(); i++) 958 { 959 if (_extent.getProperty(i).isRequired() && isNull(i)) 960 { 961 if (missingProperties == null) 962 { 963 missingProperties = new ArrayList (); 964 } 965 missingProperties.add(getProperty(i).getName()); 966 } 967 } 968 if (missingProperties != null) 969 { 970 throw new PropertyRequiredException(Messages.getString("global.RequiredPropNull"), this, missingProperties); 971 } 972 } 973 974 977 public boolean hasProperty(String propertyName) 978 { 979 return _extent.hasProperty(propertyName); 980 } 981 982 private DjObject resolveInstance(String instPath, boolean implicitCreate) throws DjenericException 983 { 984 if (instPath == null) return this; 985 986 int idx = instPath.indexOf("."); 987 988 String basePropName = instPath; 989 String restPath = null; 990 if (idx != -1) 991 { 992 basePropName = instPath.substring(0, idx); 993 restPath = instPath.substring(idx + 1); 994 } 995 996 if (_extent.hasProperty(basePropName)) 997 { 998 DjProperty prop = _extent.getProperty(basePropName); 999 if (!(prop.getType() instanceof DjExtent)) throw new ObjectNotDefinedException(Messages 1000 .getString("DjObject.PropertyInPathNotObject", basePropName)); 1001 1002 DjObject next = (DjObject) get(basePropName); 1003 if (next == null) 1004 { 1005 if (implicitCreate) 1006 { 1007 next = getSession().createObject((DjExtent) prop.getType()); 1008 set(basePropName, next); 1009 } 1010 else return null; 1011 } 1012 return next.resolveInstance(restPath, implicitCreate); 1013 } 1014 1015 DjAssociation rel = getDetailAssociationByName(basePropName); 1016 if (!rel.isOneToOne()) throw new ObjectNotDefinedException(Messages.getString("DjObject.RelationNot11", rel 1017 .getName(), instPath)); 1018 1019 if (rel.getObjects().size() == 0) 1020 { 1021 if (implicitCreate) return rel.createNew().resolveInstance(restPath, implicitCreate); 1022 else return null; 1023 } 1024 1025 return rel.getObjects().getDjenericObjectAt(0).resolveInstance(restPath, implicitCreate); 1026 } 1027 1028 1037 public int getMaxLength(String propertyName) throws DjenericException 1038 { 1039 int idx = propertyName.lastIndexOf("."); 1040 if (idx != -1) 1041 { 1042 String propName = propertyName.substring(idx + 1); 1043 String instPath = propertyName.substring(0, idx); 1044 1045 DjObject dest = resolveInstance(instPath, false); 1046 if (dest == null) throw new DjenericException(Messages.getString("ObjectPath.IdentifierNull", propertyName)); 1047 return dest.getMaxLength(propName); 1048 } 1049 else return getMaxLength(_extent.getPropertyIndex(propertyName)); 1050 } 1051 1052 1059 public int getMaxLength(int propIdx) 1060 { 1061 return _extent.getProperty(propIdx).getLength(); 1062 } 1063 1064 1073 public int getDecimals(String propertyName) throws DjenericException 1074 { 1075 int idx = propertyName.lastIndexOf("."); 1076 if (idx != -1) 1077 { 1078 String propName = propertyName.substring(idx + 1); 1079 String instPath = propertyName.substring(0, idx); 1080 DjObject dest = resolveInstance(instPath, false); 1081 if (dest == null) throw new DjenericException(Messages.getString("ObjectPath.IdentifierNull", propertyName)); 1082 return dest.getMaxLength(propName); 1083 } 1084 else return getDecimals(_extent.getPropertyIndex(propertyName)); 1085 } 1086 1087 1094 public int getDecimals(int propIdx) 1095 { 1096 return _extent.getProperty(propIdx).getDecimals(); 1097 } 1098 1099 1108 public int getTypeCode(String propertyName) throws DjenericException 1109 { 1110 return _extent.getProperty(propertyName).getTypeCode(); 1111 } 1112 1113 1120 public int getTypeCode(int propIdx) 1121 { 1122 return _extent.getProperty(propIdx).getTypeCode(); 1123 } 1124 1125 1134 public DjPropertyType getType(String propertyName) throws DjenericException 1135 { 1136 return _extent.getProperty(propertyName).getType(); 1137 } 1138 1139 1148 public DjPropertyType getType(int propIdx) 1149 { 1150 return _extent.getProperty(propIdx).getType(); 1151 } 1152 1153 1162 public String getTypeName(String propertyName) throws DjenericException 1163 { 1164 return _extent.getProperty(propertyName).getTypeName(); 1165 } 1166 1167 1176 public String getTypeName(int propIdx) 1177 { 1178 return _extent.getProperty(propIdx).getTypeName(); 1179 } 1180 1181 1190 public boolean isRequired(String propertyName) throws DjenericException 1191 { 1192 int idx = propertyName.lastIndexOf("."); 1193 if (idx != -1) 1194 { 1195 String propName = propertyName.substring(idx + 1); 1196 String instPath = propertyName.substring(0, idx); 1197 DjObject dest = resolveInstance(instPath, false); 1198 if (dest == null) throw new DjenericException(Messages.getString("ObjectPath.IdentifierNull", propertyName)); 1199 return dest.isRequired(propName); 1200 } 1201 else return isRequired(_extent.getPropertyIndex(propertyName)); 1202 } 1203 1204 1213 public boolean isRequired(int propIdx) 1214 { 1215 return _extent.getProperty(propIdx).isRequired(); 1216 } 1217 1218 1223 public int getPropertyCount() 1224 { 1225 return _extent.getPropertyCount(); 1226 } 1227 1228 1233 public String [] getPropertyNames() 1234 { 1235 return _extent.getPropertyNames(); 1236 } 1237 1238 1245 public String getPropertyName(int idx) 1246 { 1247 return _extent.getPropertyName(idx); 1248 } 1249 1250 1257 public DjProperty getProperty(int idx) 1258 { 1259 return _extent.getProperty(idx); 1260 } 1261 1262 1271 public DjProperty getProperty(String propertyName) throws ObjectNotDefinedException 1272 { 1273 return _extent.getProperty(propertyName); 1274 } 1275 1276 1285 public int getPropertyIndex(String propertyName) throws ObjectNotDefinedException 1286 { 1287 return _extent.getPropertyIndex(propertyName); 1288 } 1289 1290 1299 public DjDomainValue[] getValidValues(String propertyName) throws DjenericException 1300 { 1301 int idx = propertyName.lastIndexOf("."); 1302 if (idx != -1) 1303 { 1304 String propName = propertyName.substring(idx + 1); 1305 String instPath = propertyName.substring(0, idx); 1306 DjObject dest = resolveInstance(instPath, false); 1307 if (dest == null) throw new DjenericException(Messages.getString("ObjectPath.IdentifierNull", propertyName)); 1308 return dest.getValidValues(propName); 1309 } 1310 else return getValidValues(_extent.getPropertyIndex(propertyName)); 1311 } 1312 1313 1322 public DjDomainValue[] getValidValues(int propIdx) throws DjenericException 1323 { 1324 DjAssociation assoc = getMasterAssociationByPropertyName(propIdx); 1325 if (assoc == null) 1326 { 1327 DjProperty prop = getProperty(propIdx); 1329 1330 return prop.getValidValues(getSession()); 1331 } 1332 1333 DjExtent masterExtent = assoc.getRelation().getMasterExtent(); 1334 1336 DjRestriction restriction = assoc.getRestriction(); 1337 String listKey = masterExtent.getName(); 1338 1339 if (restriction != null) 1340 { 1341 long id = 0; 1342 1343 try 1344 { 1345 id = restriction.evaluate(this); 1346 } 1347 catch (RestrictionFailedException rfe) 1348 { 1349 } 1350 1351 listKey += getObjectId() + assoc.getName() + id; 1352 } 1353 1354 DjDomainValue[] valids = _session.getValidValues(listKey); 1355 if (valids == null) 1356 { 1357 valids = DjDomain.convertToValueList(_session.getValidMasterObjects(this, masterExtent, restriction, false)); 1358 _session.registerValidValues(listKey, valids); 1359 } 1360 return valids; 1361 } 1362 1363 1369 public void validate() throws DjenericException 1370 { 1371 boolean checkIntegrity = getSession().getPersistenceManager().isCheckReferentialIntegrity(); 1372 for (int i = 0; i < getPropertyCount(); i++) 1373 { 1374 DjPropertyType type = getProperty(i).getType(); 1375 1376 boolean shouldCheck = (type instanceof DjDomain) || (checkIntegrity && type instanceof DjExtent); 1378 1379 if (shouldCheck) checkDomainValue(i, get(i)); 1380 } 1381 } 1384 1385 1388 1396 public void setNull(String propertyName) throws ObjectNotDefinedException, DjenericException 1397 { 1398 int idx = propertyName.lastIndexOf("."); 1399 if (idx != -1) 1400 { 1401 String propName = propertyName.substring(idx + 1); 1402 String instPath = propertyName.substring(0, idx); 1403 resolveInstance(instPath, true).setNull(propName); 1404 } 1405 else setNull(_extent.getPropertyIndex(propertyName)); 1406 } 1407 1408 1414 public void setNull(int propIdx) throws DjenericException 1415 { 1416 if (_propertyValues[propIdx] != null) 1417 { 1418 if (!_isBeingLoaded && !isTransient() && getProperty(propIdx).getType() instanceof DjExtent) 1423 { 1424 Long oldValue = (Long ) _propertyValues[propIdx]; 1425 1426 DjObject obj = getSession().getFromSession(oldValue.longValue()); 1427 if (obj != null) obj.synchronizeAssociations(); 1428 } 1429 _propertyValues[propIdx] = null; 1430 setModified(true); 1431 } 1432 } 1433 1434 1443 public boolean isNull(String propertyName) throws ObjectNotDefinedException, DjenericException 1444 { 1445 int idx = propertyName.lastIndexOf("."); 1446 if (idx != -1) 1447 { 1448 String propName = propertyName.substring(idx + 1); 1449 String instPath = propertyName.substring(0, idx); 1450 DjObject dest = resolveInstance(instPath, false); 1451 if (dest == null) return true; 1452 return dest.isNull(propName); 1453 } 1454 else return isNull(_extent.getPropertyIndex(propertyName)); 1455 } 1456 1457 1464 public boolean isNull(int propIdx) 1465 { 1466 return _propertyValues[propIdx] == null; 1467 } 1468 1469 1482 public Object get(String propertyName) throws ObjectNotDefinedException, DjenericException 1483 { 1484 int idx = propertyName.lastIndexOf("."); 1485 if (idx != -1) 1486 { 1487 String propName = propertyName.substring(idx + 1); 1488 String instPath = propertyName.substring(0, idx); 1489 DjObject dest = resolveInstance(instPath, false); 1490 if (dest == null) return null; 1491 return dest.get(propName); 1492 } 1493 else try 1494 { 1495 return get(_extent.getPropertyIndex(propertyName)); 1496 } 1497 catch (ObjectNotDefinedException onde) 1498 { 1499 String assocName = propertyName; 1500 boolean isIndexed = false; 1501 int indexValue = 0; 1502 if (assocName.indexOf('[') != -1) 1503 { 1504 isIndexed = true; 1505 int brackIdx = assocName.indexOf('['); 1506 int brackEndIdx = assocName.indexOf(']'); 1507 indexValue = Integer.parseInt(assocName.substring(brackIdx + 1, brackEndIdx)); 1508 assocName = assocName.substring(0, brackIdx); 1509 } 1510 1511 DjAssociation detail = getDetailAssociationByName(assocName); 1512 DjList result = detail.getObjects(); 1513 if (isIndexed) return result.getDjenericObjectAt(indexValue); 1514 1515 if (detail.isOneToMany()) return result; 1516 if (result.size() == 0) return null; 1517 return result.getDjenericObjectAt(0); 1519 } 1520 } 1521 1522 1531 public Object get(int propIdx) throws DjenericException 1532 { 1533 if (isNull(propIdx)) 1534 { 1535 return null; 1536 } 1537 1538 DjPropertyType tp = _extent.getProperty(propIdx).getType(); 1539 if (tp instanceof DjExtent) 1540 { 1541 return _session.getObject((DjExtent) tp, getLong(propIdx)); 1542 } 1543 else 1544 { 1545 return _propertyValues[propIdx]; 1546 } 1547 } 1548 1549 1563 public void set(String propertyName, Object value) throws ObjectNotDefinedException, PropertyFormatException, 1564 DjenericException 1565 { 1566 int idx = propertyName.lastIndexOf("."); 1567 if (idx != -1) 1568 { 1569 String propName = propertyName.substring(idx + 1); 1570 String instPath = propertyName.substring(0, idx); 1571 resolveInstance(instPath, true).set(propName, value); 1572 } 1573 else 1574 { 1575 try 1576 { 1577 set(_extent.getPropertyIndex(propertyName), value); 1578 } 1579 catch (ObjectNotDefinedException onde) 1580 { 1581 if (!(value instanceof DjObject) && !(value instanceof DjList)) throw onde; 1582 1583 String assocName = propertyName; 1584 boolean isIndexed = false; 1585 int indexValue = 0; 1586 if (assocName.indexOf('[') != -1) 1587 { 1588 isIndexed = true; 1589 int brackIdx = assocName.indexOf('['); 1590 int brackEndIdx = assocName.indexOf(']'); 1591 indexValue = Integer.parseInt(assocName.substring(brackIdx + 1, brackEndIdx)); 1592 assocName = assocName.substring(0, brackIdx); 1593 } 1594 1595 DjAssociation detail = getDetailAssociationByName(assocName); 1596 1597 if (isIndexed) 1598 { 1599 if (value instanceof DjList) throw new DjenericException(Messages.getString("DjObject.CannotAssignList", 1600 propertyName)); 1601 detail.replaceDetail(indexValue, (DjObject) value); 1602 } 1603 else 1604 { 1605 DjList theValues; 1606 if (value instanceof DjObject) 1607 { 1608 theValues = new DjList(); 1609 theValues.add(value); 1610 } 1611 else 1612 { 1613 theValues = (DjList) value; 1614 if ((theValues.size() > 1) && detail.isOneToOne()) 1615 { 1616 throw new DjenericException(Messages.getString("DjObject.RelationNot11", propertyName, propertyName)); 1617 } 1618 } 1619 1620 detail.markAllDeletedExcluding(theValues); 1621 detail.addAll(theValues); 1622 } 1623 } 1624 } 1625 } 1626 1627 1641 public void set(int propIdx, Object value) throws PropertyFormatException, ObjectNotDefinedException, 1642 DjenericException 1643 { 1644 if (value == null) 1645 { 1646 setNull(propIdx); 1647 return; 1648 } 1649 DjProperty prop = _extent.getProperty(propIdx); 1650 1651 if (prop.getType() instanceof DjExtent) 1652 { 1653 if (!(value instanceof DjObject)) 1654 { 1655 throw new PropertyFormatException(Messages.getString("DjObject.InvalidObjectType", value.getClass().getName()), 1656 this, _extent.getPropertyName(propIdx)); 1657 } 1658 1659 DjObject po = (DjObject) value; 1660 po.determineObjectIdIfNeeded(); 1661 1662 setLong(propIdx, po.getObjectId()); 1663 1664 } 1665 else 1666 { 1667 if (value instanceof byte[] && prop.getTypeCode() == DjDomain.BYTE_TYPE) setBytes(propIdx, (byte[]) value); 1668 else if (value instanceof byte[] && prop.getTypeCode() == DjDomain.STRING_TYPE) setString(propIdx, new String ( 1669 (byte[]) value)); 1670 else 1671 if (!(value instanceof String || value instanceof Long || value instanceof Integer 1677 || value instanceof java.util.Date || value instanceof BigDecimal ) 1678 && (prop.getTypeCode() != DjDomain.STRING_TYPE)) 1679 { 1680 throw new PropertyFormatException(Messages.getString("DjObject.InvalidObjectType", value.getClass().getName()), 1681 this, _extent.getPropertyName(propIdx)); 1682 } 1683 else setString(propIdx, value.toString()); 1684 } 1685 } 1686 1687 1699 public void setString(String propertyName, String value) throws PropertyFormatException, ObjectNotDefinedException, 1700 DjenericException 1701 { 1702 int idx = propertyName.lastIndexOf("."); 1703 if (idx != -1) 1704 { 1705 String propName = propertyName.substring(idx + 1); 1706 String instPath = propertyName.substring(0, idx); 1707 resolveInstance(instPath, true).setString(propName, value); 1708 } 1709 else setString(_extent.getPropertyIndex(propertyName), value); 1710 } 1711 1712 1722 public void setString(int propIdx, String value) throws PropertyFormatException, DjenericException 1723 { 1724 if (value == null) 1725 { 1726 setNull(propIdx); 1727 return; 1728 } 1729 int propType = getTypeCode(propIdx); 1730 1731 if (propType == DjDomain.STRING_TYPE) 1732 { 1733 if (isNull(propIdx) || !_propertyValues[propIdx].equals(value)) 1734 { 1735 DjPropertyType tp = getType(propIdx); 1736 if (tp instanceof DjDomain) 1737 { 1738 DjDomain dom = (DjDomain) tp; 1739 if (dom.getCaseConversion().equals(DjDomain.CASE_UPPER)) value = value.toUpperCase(); 1740 else if (dom.getCaseConversion().equals(DjDomain.CASE_LOWER)) value = value.toLowerCase(); 1741 } 1742 checkDomainValue(propIdx, value); 1743 _propertyValues[propIdx] = value; 1744 setModified(true); 1745 } 1746 return; 1747 } 1748 1749 if (value.trim().length() == 0) 1751 { 1752 setNull(propIdx); 1753 } 1754 else if (propType == DjDomain.BYTE_TYPE) 1755 { 1756 try 1757 { 1758 setBytes(propIdx, value.getBytes(DjPersistenceManager.ENCODING_METHOD)); 1759 } 1760 catch (java.io.UnsupportedEncodingException uce) 1761 { 1762 throw new PropertyFormatException(uce.getMessage(), this, getPropertyName(propIdx)); 1763 } 1764 } 1765 else if (propType == DjDomain.BIGDECIMAL_TYPE) 1766 { 1767 setBigDecimal(propIdx, new BigDecimal (value)); 1768 } 1769 else if (propType == DjDomain.DATE_TYPE) 1770 { 1771 String mask = getFormatMask(propIdx); 1772 try 1773 { 1774 setDate(propIdx, convertString2Date(value, mask)); 1775 } 1776 catch (ParseException pex) 1777 { 1778 throw new PropertyFormatException(Messages.getString("DjObject.FormatError", pex.getMessage(), mask), this, 1779 getPropertyName(propIdx)); 1780 } 1781 } 1782 else if (propType == DjDomain.INT_TYPE) 1783 { 1784 setInt(propIdx, new Integer (value)); 1785 } 1786 else if (propType == DjDomain.LONG_TYPE) 1787 { 1788 setLong(propIdx, new Long (value)); 1789 } 1790 else 1791 { 1792 throw new PropertyFormatException(Messages.getString("DjObject.setStringError", String.valueOf(propType)), this, 1793 getPropertyName(propIdx)); 1794 } 1795 } 1796 1797 1806 public String getString(String propertyName) throws ObjectNotDefinedException, DjenericException 1807 { 1808 int idx = propertyName.lastIndexOf("."); 1809 if (idx != -1) 1810 { 1811 String propName = propertyName.substring(idx + 1); 1812 String instPath = propertyName.substring(0, idx); 1813 1814 DjObject dest = resolveInstance(instPath, false); 1815 if (dest == null) return null; 1816 return dest.getString(propName); 1817 } 1818 else return getString(_extent.getPropertyIndex(propertyName)); 1819 } 1820 1821 1828 public String getFormatMask(int propIdx) 1829 { 1830 String mask = null; 1831 DjPropertyType tp = getProperty(propIdx).getType(); 1832 if (tp instanceof DjDomain) 1833 { 1834 DjDomain dom = (DjDomain) tp; 1835 mask = dom.getFormatMask(); 1836 if (mask != null && mask.length() == 0) mask = null; 1837 } 1838 return mask; 1839 } 1840 1841 1848 public String getString(int propIdx) 1849 { 1850 if (isNull(propIdx)) 1851 { 1852 return null; 1853 } 1854 Object val = _propertyValues[propIdx]; 1855 if (val == null) 1856 { 1857 return null; 1858 } 1859 1860 try 1861 { 1862 if (getTypeCode(propIdx) == DjDomain.DATE_TYPE) 1863 { 1864 return convertDate2String(getDate(propIdx), getFormatMask(propIdx)); 1865 } 1866 } 1867 catch (Exception x) 1868 { 1869 DjLogger.log(x); 1870 } 1871 1872 if (getTypeCode(propIdx) == DjDomain.BYTE_TYPE) 1873 { 1874 try 1875 { 1876 return new String (getBytes(propIdx), DjPersistenceManager.ENCODING_METHOD); 1877 } 1878 catch (UnsupportedEncodingException uee) 1879 { 1880 DjLogger.log(uee); 1881 return new String (getBytes(propIdx)); 1883 } 1884 } 1885 1886 return val.toString(); 1887 } 1888 1889 public String getOriginalValueString(String propertyName) throws ObjectNotDefinedException 1890 { 1891 return getOriginalValueString(getPropertyIndex(propertyName)); 1892 } 1893 1894 public String getOriginalValueString(int propIdx) 1895 { 1896 Object val = _originalPropertyValues[propIdx]; 1897 if (val == null) 1898 { 1899 return null; 1900 } 1901 1902 try 1903 { 1904 if (getTypeCode(propIdx) == DjDomain.DATE_TYPE) 1905 { 1906 return convertDate2String((Date ) _originalPropertyValues[propIdx], getFormatMask(propIdx)); 1907 } 1908 } 1909 catch (Exception x) 1910 { 1911 DjLogger.log(x); 1912 } 1913 1914 if (getTypeCode(propIdx) == DjDomain.BYTE_TYPE) 1915 { 1916 try 1917 { 1918 return new String ((byte[]) _originalPropertyValues[propIdx], DjPersistenceManager.ENCODING_METHOD); 1919 } 1920 catch (UnsupportedEncodingException uee) 1921 { 1922 DjLogger.log(uee); 1923 return new String ((byte[]) _originalPropertyValues[propIdx]); 1925 } 1926 } 1927 1928 return val.toString(); 1929 } 1930 1931 1943 public void setBytes(String propertyName, byte[] value) throws PropertyFormatException, ObjectNotDefinedException, 1944 DjenericException 1945 { 1946 int idx = propertyName.lastIndexOf("."); 1947 if (idx != -1) 1948 { 1949 String propName = propertyName.substring(idx + 1); 1950 String instPath = propertyName.substring(0, idx); 1951 resolveInstance(instPath, true).setBytes(propName, value); 1952 } 1953 else setBytes(_extent.getPropertyIndex(propertyName), value); 1954 } 1955 1956 1966 public void setBytes(int propIdx, byte[] value) throws PropertyFormatException, DjenericException 1967 { 1968 if (value == null) 1969 { 1970 setNull(propIdx); 1971 return; 1972 } 1973 int propType = getTypeCode(propIdx); 1974 1975 if (propType == DjDomain.BYTE_TYPE) 1976 { 1977 if (isNull(propIdx) || !Arrays.equals(value, (byte[]) _propertyValues[propIdx])) 1978 { 1979 checkDomainValue(propIdx, value); 1980 _propertyValues[propIdx] = value; 1981 setModified(true); 1982 } 1983 } 1984 else if (propType == DjDomain.STRING_TYPE) 1985 { 1986 setString(propIdx, new String (value)); 1987 } 1988 else 1989 { 1990 throw new PropertyFormatException(Messages.getString("DjObject.setBytesError", String.valueOf(propType)), this, 1991 getPropertyName(propIdx)); 1992 } 1993 1994 } 1995 1996 2008 public void setBigDecimal(String propertyName, BigDecimal value) throws PropertyFormatException, 2009 ObjectNotDefinedException, DjenericException 2010 { 2011 int idx = propertyName.lastIndexOf("."); 2012 if (idx != -1) 2013 { 2014 String propName = propertyName.substring(idx + 1); 2015 String instPath = propertyName.substring(0, idx); 2016 resolveInstance(instPath, true).setBigDecimal(propName, value); 2017 } 2018 else setBigDecimal(_extent.getPropertyIndex(propertyName), value); 2019 } 2020 2021 private void checkDomainValue(int propIdx, Object value) throws PropertyFormatException 2022 { 2023 if (_isBeingLoaded || value == null) return; 2024 2025 DjProperty theProp = getExtent().getProperty(propIdx); 2026 try 2027 { 2028 theProp.getType().validateValue(value); 2029 } 2030 catch (DomainViolationException dve) 2031 { 2032 throw new PropertyFormatException(dve.getMessage(), this, getPropertyName(propIdx)); 2033 } 2034 } 2035 2036 2046 public void setBigDecimal(int propIdx, BigDecimal value) throws PropertyFormatException, DjenericException 2047 { 2048 if (value == null) 2049 { 2050 setNull(propIdx); 2051 return; 2052 } 2053 2054 int propType = getTypeCode(propIdx); 2055 2056 if (propType == DjDomain.BIGDECIMAL_TYPE) 2057 { 2058 if (isNull(propIdx) || !_propertyValues[propIdx].equals(value)) 2059 { 2060 checkDomainValue(propIdx, value); 2061 _propertyValues[propIdx] = value; 2062 setModified(true); 2063 } 2064 } 2065 else if (propType == DjDomain.STRING_TYPE) 2066 { 2067 setString(propIdx, value.toString()); 2068 } 2069 else if (propType == DjDomain.DATE_TYPE) 2070 { 2071 setDate(propIdx, new Date (value.longValue())); 2072 } 2073 else if (propType == DjDomain.INT_TYPE) 2074 { 2075 setInt(propIdx, new Integer (value.toString())); 2076 } 2077 else if (propType == DjDomain.LONG_TYPE) 2078 { 2079 setLong(propIdx, new Long (value.toString())); 2080 } 2081 else 2082 { 2083 throw new PropertyFormatException(Messages.getString("DjObject.setBigDecimalError", String.valueOf(propType)), 2084 this, getPropertyName(propIdx)); 2085 } 2086 2087 } 2088 2089 2098 public BigDecimal getBigDecimal(String propertyName) throws ObjectNotDefinedException, DjenericException 2099 { 2100 int idx = propertyName.lastIndexOf("."); 2101 if (idx != -1) 2102 { 2103 String propName = propertyName.substring(idx + 1); 2104 String instPath = propertyName.substring(0, idx); 2105 DjObject dest = resolveInstance(instPath, false); 2106 if (dest == null) return null; 2107 return dest.getBigDecimal(propName); 2108 } 2109 else return getBigDecimal(_extent.getPropertyIndex(propertyName)); 2110 } 2111 2112 2119 public BigDecimal getBigDecimal(int propIdx) 2120 { 2121 if (isNull(propIdx)) 2122 { 2123 return null; 2124 } 2125 Object val = _propertyValues[propIdx]; 2126 if (val == null) 2127 { 2128 return null; 2129 } 2130 2131 if (val instanceof BigDecimal ) 2132 { 2133 return (BigDecimal ) val; 2134 } 2135 2136 return new BigDecimal (val.toString()); 2137 } 2138 2139 2148 public byte[] getBytes(String propertyName) throws ObjectNotDefinedException, DjenericException 2149 { 2150 int idx = propertyName.lastIndexOf("."); 2151 if (idx != -1) 2152 { 2153 String propName = propertyName.substring(idx + 1); 2154 String instPath = propertyName.substring(0, idx); 2155 DjObject dest = resolveInstance(instPath, false); 2156 if (dest == null) return null; 2157 return dest.getBytes(propName); 2158 } 2159 else return getBytes(_extent.getPropertyIndex(propertyName)); 2160 } 2161 2162 2169 public byte[] getBytes(int propIdx) 2170 { 2171 if (isNull(propIdx)) 2172 { 2173 return null; 2174 } 2175 Object val = _propertyValues[propIdx]; 2176 if (val == null) 2177 { 2178 return null; 2179 } 2180 2181 if (val instanceof byte[]) 2182 { 2183 return (byte[]) val; 2184 } 2185 2186 try 2187 { 2188 return val.toString().getBytes(DjPersistenceManager.ENCODING_METHOD); 2189 } 2190 catch (UnsupportedEncodingException uee) 2191 { 2192 DjLogger.log(uee); 2193 return val.toString().getBytes(); 2194 } 2195 } 2196 2197 2209 public void setInt(String propertyName, int value) throws PropertyFormatException, ObjectNotDefinedException, 2210 DjenericException 2211 { 2212 int idx = propertyName.lastIndexOf("."); 2213 if (idx != -1) 2214 { 2215 String propName = propertyName.substring(idx + 1); 2216 String instPath = propertyName.substring(0, idx); 2217 resolveInstance(instPath, true).setInt(propName, value); 2218 } 2219 else setInt(_extent.getPropertyIndex(propertyName), new Integer (value)); 2220 } 2221 2222 2232 public void setInt(int propIdx, Integer value) throws PropertyFormatException, DjenericException 2233 { 2234 int propType = getTypeCode(propIdx); 2235 2236 if (propType == DjDomain.INT_TYPE) 2237 { 2238 if (isNull(propIdx) || !_propertyValues[propIdx].equals(value)) 2239 { 2240 checkDomainValue(propIdx, value); 2241 _propertyValues[propIdx] = value; 2242 setModified(true); 2243 } 2244 } 2245 else if (propType == DjDomain.BIGDECIMAL_TYPE) 2246 { 2247 setBigDecimal(propIdx, new BigDecimal (value.intValue())); 2248 } 2249 else if (propType == DjDomain.STRING_TYPE) 2250 { 2251 setString(propIdx, value.toString()); 2252 } 2253 else if (propType == DjDomain.DATE_TYPE) 2254 { 2255 setDate(propIdx, new Date (value.longValue())); 2256 } 2257 else if (propType == DjDomain.LONG_TYPE) 2258 { 2259 setLong(propIdx, new Long (value.longValue())); 2260 } 2261 else 2262 { 2263 throw new PropertyFormatException(Messages.getString("DjObject.setIntError", String.valueOf(propType)), this, 2264 getPropertyName(propIdx)); 2265 } 2266 2267 } 2268 2269 2281 public void setInt(String propertyName, Integer value) throws PropertyFormatException, ObjectNotDefinedException, 2282 DjenericException 2283 { 2284 int idx = propertyName.lastIndexOf("."); 2285 if (idx != -1) 2286 { 2287 String propName = propertyName.substring(idx + 1); 2288 String instPath = propertyName.substring(0, idx); 2289 resolveInstance(instPath, true).setInt(propName, value); 2290 } 2291 else setInt(_extent.getPropertyIndex(propertyName), value); 2292 } 2293 2294 2304 public void setInt(int propIdx, int value) throws PropertyFormatException, DjenericException 2305 { 2306 setInt(propIdx, new Integer (value)); 2307 } 2308 2309 2318 public int getInt(String propertyName) throws ObjectNotDefinedException, DjenericException 2319 { 2320 int idx = propertyName.lastIndexOf("."); 2321 if (idx != -1) 2322 { 2323 String propName = propertyName.substring(idx + 1); 2324 String instPath = propertyName.substring(0, idx); 2325 DjObject dest = resolveInstance(instPath, false); 2326 if (dest == null) throw new DjenericException(Messages.getString("ObjectPath.IdentifierNull", propertyName)); 2327 return dest.getInt(propName); 2328 } 2329 else return getInt(_extent.getPropertyIndex(propertyName)); 2330 } 2331 2332 2339 public int getInt(int propIdx) 2340 { 2341 Object val = _propertyValues[propIdx]; 2342 if (val == null) 2343 { 2344 return 0; 2345 } 2346 2347 if (val instanceof Integer ) 2348 { 2349 return ((Integer ) val).intValue(); 2350 } 2351 2352 return Integer.parseInt(val.toString()); 2353 } 2354 2355 2367 public void setLong(String propertyName, long value) throws PropertyFormatException, ObjectNotDefinedException, 2368 DjenericException 2369 { 2370 int idx = propertyName.lastIndexOf("."); 2371 if (idx != -1) 2372 { 2373 String propName = propertyName.substring(idx + 1); 2374 String instPath = propertyName.substring(0, idx); 2375 resolveInstance(instPath, true).setLong(propName, value); 2376 } 2377 else setLong(_extent.getPropertyIndex(propertyName), new Long (value)); 2378 } 2379 2380 2390 public void setLong(int propIdx, Long value) throws PropertyFormatException, DjenericException 2391 { 2392 2393 int propType = getTypeCode(propIdx); 2394 2395 if (propType == DjDomain.LONG_TYPE) 2396 { 2397 if (isNull(propIdx) || !_propertyValues[propIdx].equals(value)) 2398 { 2399 checkDomainValue(propIdx, value); 2400 Long oldValue = (Long ) _propertyValues[propIdx]; 2401 _propertyValues[propIdx] = value; 2402 setModified(true); 2403 2404 try 2405 { if (!_isBeingLoaded && !isTransient() && getProperty(propIdx).getType() instanceof DjExtent) 2410 { 2411 if (value != null) 2412 { 2413 DjObject obj = getSession().getFromSession(value.longValue()); 2414 if (obj != null) obj.synchronizeAssociations(); 2415 } 2416 2417 if (oldValue != null) 2418 { 2419 DjObject obj = getSession().getFromSession(oldValue.longValue()); 2420 if (obj != null) obj.synchronizeAssociations(); 2421 } 2422 } 2423 } 2424 catch (DjenericException dje) 2425 { 2426 DjLogger.log(dje); 2427 throw new PropertyFormatException(dje.getMessage(), this, getProperty(propIdx).getName()); 2428 } 2429 } 2430 } 2431 else if (propType == DjDomain.BIGDECIMAL_TYPE) 2432 { 2433 setBigDecimal(propIdx, new BigDecimal (value.longValue())); 2434 } 2435 else if (propType == DjDomain.STRING_TYPE) 2436 { 2437 setString(propIdx, value.toString()); 2438 } 2439 else if (propType == DjDomain.DATE_TYPE) 2440 { 2441 setDate(propIdx, new Date (value.longValue())); 2442 } 2443 else if (propType == DjDomain.INT_TYPE) 2444 { 2445 setInt(propIdx, new Integer (value.intValue())); 2446 } 2447 else 2448 { 2449 throw new PropertyFormatException(Messages.getString("DjObject.setLongError", String.valueOf(propType)), this, 2450 getPropertyName(propIdx)); 2451 } 2452 } 2453 2454 2466 public void setLong(String propertyName, Long value) throws PropertyFormatException, ObjectNotDefinedException, 2467 DjenericException 2468 { 2469 int idx = propertyName.lastIndexOf("."); 2470 if (idx != -1) 2471 { 2472 String propName = propertyName.substring(idx + 1); 2473 String instPath = propertyName.substring(0, idx); 2474 resolveInstance(instPath, true).setLong(propName, value); 2475 } 2476 else setLong(_extent.getPropertyIndex(propertyName), value); 2477 } 2478 2479 2489 public void setLong(int propIdx, long value) throws PropertyFormatException, DjenericException 2490 { 2491 setLong(propIdx, new Long (value)); 2492 } 2493 2494 2503 public long getLong(String propertyName) throws ObjectNotDefinedException, DjenericException 2504 { 2505 int idx = propertyName.lastIndexOf("."); 2506 if (idx != -1) 2507 { 2508 String propName = propertyName.substring(idx + 1); 2509 String instPath = propertyName.substring(0, idx); 2510 DjObject dest = resolveInstance(instPath, false); 2511 if (dest == null) throw new DjenericException(Messages.getString("ObjectPath.IdentifierNull", propertyName)); 2512 return dest.getLong(propName); 2513 } 2514 else return getLong(_extent.getPropertyIndex(propertyName)); 2515 } 2516 2517 2524 public long getLong(int propIdx) 2525 { 2526 Object val = _propertyValues[propIdx]; 2527 if (val == null) 2528 { 2529 return 0; 2530 } 2531 2532 if (val instanceof Long ) 2533 { 2534 return ((Long ) val).longValue(); 2535 } 2536 2537 return Long.parseLong(val.toString()); 2538 } 2539 2540 2552 public void setDate(String propertyName, java.util.Date dt) throws PropertyFormatException, 2553 ObjectNotDefinedException, DjenericException 2554 { 2555 int idx = propertyName.lastIndexOf("."); 2556 if (idx != -1) 2557 { 2558 String propName = propertyName.substring(idx + 1); 2559 String instPath = propertyName.substring(0, idx); 2560 resolveInstance(instPath, true).setDate(propName, dt); 2561 } 2562 else setDate(getPropertyIndex(propertyName), dt); 2563 } 2564 2565 2575 public void setDate(int propIdx, java.util.Date dt) throws PropertyFormatException, DjenericException 2576 { 2577 if (dt == null) 2578 { 2579 setNull(propIdx); 2580 return; 2581 } 2582 int propType = getTypeCode(propIdx); 2583 2584 if (propType == DjDomain.BIGDECIMAL_TYPE) 2585 { 2586 setBigDecimal(propIdx, new BigDecimal (dt.getTime())); 2587 2588 } 2589 else if (propType == DjDomain.STRING_TYPE) 2590 { 2591 setString(propIdx, dt.toString()); 2592 } 2593 else if (propType == DjDomain.DATE_TYPE) 2594 { 2595 if (isNull(propIdx) || !_propertyValues[propIdx].equals(dt)) 2596 { 2597 checkDomainValue(propIdx, dt); 2598 _propertyValues[propIdx] = dt; 2599 setModified(true); 2600 } 2601 } 2602 else if (propType == DjDomain.LONG_TYPE) 2603 { 2604 setLong(propIdx, dt.getTime()); 2605 } 2606 else 2607 { 2608 throw new PropertyFormatException(Messages.getString("DjObject.setDateError", String.valueOf(propType)), this, 2609 getPropertyName(propIdx)); 2610 } 2611 } 2612 2613 2622 public java.util.Date getDate(String propertyName) throws ObjectNotDefinedException, DjenericException 2623 { 2624 if (isNull(propertyName)) 2625 { 2626 return null; 2627 } 2628 return new java.util.Date (getLong(propertyName)); 2629 } 2630 2631 2638 public java.util.Date getDate(int propIdx) 2639 { 2640 if (isNull(propIdx)) 2641 { 2642 return null; 2643 } 2644 Object val = _propertyValues[propIdx]; 2645 if (val == null) 2646 { 2647 return null; 2648 } 2649 2650 if (val instanceof Date ) 2651 { 2652 return (Date ) val; 2653 } 2654 2655 return new Date (new Long (val.toString()).longValue()); 2656 } 2657 2658 2663 public String toString() 2664 { 2665 return getDescriptor(); 2666 } 2667 2668 2671 2676 public boolean isModified() 2677 { 2678 return _isModified; 2679 } 2680 2681 2688 protected DjExtent restrictingObjectsExist() throws DjenericException 2689 { 2690 return restrictingObjectsExist(new HashMap ()); 2691 } 2692 2693 protected DjExtent restrictingObjectsExist(HashMap hitlist) throws DjenericException 2694 { 2695 if (hitlist.containsKey(this)) return null; 2696 hitlist.put(this, this); 2697 2698 DjAssociation[] assocs = getDetailAssociations(); 2699 2700 for (int i = 0; i < assocs.length; i++) 2701 { 2702 if (!assocs[i].isDetailsContained()) 2703 { 2704 if (assocs[i].detailsExist()) return assocs[i].getDetailExtent(); 2706 } 2707 else 2708 { 2709 if (assocs[i].getRelation().getDetailExtent().getDetailRelations().length != 0) 2712 { 2713 DjList details = assocs[i].getObjects(); 2718 for (int d = 0; d < details.size(); d++) 2719 { 2720 DjObject detail = details.getDjenericObjectAt(d); 2721 DjExtent x = detail.restrictingObjectsExist(hitlist); 2722 if (x != null) return x; 2723 } 2724 } 2725 } 2726 } 2727 return null; 2728 } 2729 2730 2738 protected void throwIfDeleteRestricted() throws DjenericException, CanNotDeleteException 2739 { 2740 DjExtent inExtent = restrictingObjectsExist(); 2741 2742 if (inExtent != null) 2743 { 2744 throw new CanNotDeleteException(Messages.getString("DjObject.CanNotDelete", inExtent.getNameSingular(), 2745 getExtent().getNameSingular()), this, inExtent); 2746 } 2747 } 2748 2749 2754 public boolean isNew() 2755 { 2756 return !_isPersisted && !isMarkedForDelete(); 2757 } 2758 2759 2764 public boolean isPersisted() 2765 { 2766 return _isPersisted; 2767 } 2768 2769 2774 public boolean isEmpty() 2775 { 2776 return isNew() && !isModified(); 2777 } 2778 2779 2784 protected boolean hasDetailsLoaded() 2785 { 2786 if (_detailAssociations == null) 2787 { 2788 return false; 2789 } 2790 for (int i = 0; i < _detailAssociations.length; i++) 2791 { 2792 if (_detailAssociations[i].isDetailsLoaded()) 2793 { 2794 return true; 2795 } 2796 } 2797 return false; 2798 } 2799 2800 2808 public void markForDelete() throws DjenericException, CanNotDeleteException 2809 { 2810 markForDelete(new HashMap ()); 2811 } 2812 2813 public void markForDelete(HashMap hitlist) throws DjenericException, CanNotDeleteException 2814 { 2815 if (hitlist.containsKey(this)) return; 2816 hitlist.put(this, this); 2817 2818 if (_isMarkedForDelete) return; 2819 2820 if (getSession().getPersistenceManager().isCheckReferentialIntegrity()) 2821 { 2822 throwIfDeleteRestricted(); 2824 2825 DjAssociation[] assocs = getDetailAssociations(); 2826 for (int i = 0; i < assocs.length; i++) 2827 { 2828 if (assocs[i].isDetailsContained()) 2829 { 2830 DjList details = assocs[i].getObjects(); 2832 for (int d = 0; d < details.size(); d++) 2833 { 2834 DjObject detail = details.getDjenericObjectAt(d); 2835 detail.markForDelete(hitlist); 2836 } 2837 } 2838 } 2839 } 2840 _isMarkedForDelete = true; 2841 if (shouldTrace(DjPersistenceManager.TRACE_FLOW)) trace("Marked for delete: " + getExtent().getObjectType() 2842 + "(id=" + getObjectId() + ") " + toString()); 2843 setModified(true); 2844 } 2845 2846 2852 protected void determineObjectIdIfNeeded() throws DjenericException 2853 { 2854 String ipn = getIdPropertyName(); 2855 2856 if (isNull(ipn)) 2857 { 2858 if (isTransient()) setLong(ipn, getSession().getNextTransientObjectId()); 2859 else setLong(ipn, getSession().getPersistenceManager().getNextObjectId()); 2860 } 2861 } 2862 2863 2869 2870 private void applyBottomUp() throws DjenericException 2871 { 2872 applyBottomUp(new HashMap ()); 2873 } 2874 2875 private void applyBottomUp(HashMap hitlist) throws DjenericException 2876 { 2877 if (hitlist.containsKey(this)) return; 2878 hitlist.put(this, this); 2879 2880 determineObjectIdIfNeeded(); 2881 if (_detailAssociations != null) 2882 { 2883 for (int i = 0; i < _detailAssociations.length; i++) 2885 { 2886 if (_detailAssociations[i].isDetailsLoaded()) 2887 { 2888 DjList details = _detailAssociations[i].getObjects(); 2889 for (int d = 0; d < details.size(); d++) 2890 { 2891 DjObject detail = details.getDjenericObjectAt(d); 2892 detail.applyBottomUp(hitlist); 2893 } 2894 } 2895 } 2896 } 2897 2898 if (!isHitDuringCurrentTransaction() && _isPersisted && isMarkedForDelete()) 2899 { 2900 delete(); 2901 setHitDuringCurrentTransaction(true); 2902 } 2903 } 2904 2905 2911 protected void updateDetailFlags() throws DjenericException 2912 { 2913 updateDetailFlags(new HashMap ()); 2914 } 2915 2916 protected void updateDetailFlags(HashMap hitlist) throws DjenericException 2917 { 2918 if (hitlist.containsKey(this)) return; 2919 hitlist.put(this, this); 2920 2921 if (_detailAssociations != null) 2922 { 2923 for (int i = 0; i < _detailAssociations.length; i++) 2924 { 2925 if (_detailAssociations[i].isDetailsLoaded()) 2926 { 2927 DjList details = _detailAssociations[i].getObjects(); 2928 for (int d = 0; d < details.size(); d++) 2929 { 2930 DjObject detail = details.getDjenericObjectAt(d); 2931 detail.setIsDetail(true); 2932 detail.updateDetailFlags(hitlist); 2933 if (shouldTrace(DjPersistenceManager.TRACE_FLOW)) trace("Marked as a detail: " 2934 + detail.getExtent().getObjectType() + "(id=" 2935 + detail.getObjectId() + ") " + detail.toString()); 2936 } 2937 } 2938 } 2939 } 2940 } 2941 2942 2948 protected void updateDetailOfnewMasterFlags() throws DjenericException 2949 { 2950 updateDetailOfnewMasterFlags(new HashMap ()); 2951 } 2952 2953 protected void updateDetailOfnewMasterFlags(HashMap hitlist) throws DjenericException 2954 { 2955 if (hitlist.containsKey(this)) return; 2956 hitlist.put(this, this); 2957 2958 if (!isNew() || isHitDuringCurrentTransaction()) return; 2959 2960 if (_detailAssociations != null) 2961 { 2962 for (int i = 0; i < _detailAssociations.length; i++) 2963 { 2964 if (_detailAssociations[i].isDetailsLoaded()) 2965 { 2966 DjList details = _detailAssociations[i].getObjects(); 2967 for (int d = 0; d < details.size(); d++) 2968 { 2969 DjObject detail = details.getDjenericObjectAt(d); 2970 detail.setDetailOfNewMaster(true); 2971 detail.updateDetailOfnewMasterFlags(hitlist); 2972 if (shouldTrace(DjPersistenceManager.TRACE_FLOW)) trace("Marked as a detail of a new Object: " 2973 + detail.getExtent().getObjectType() + "(id=" 2974 + detail.getObjectId() + ") " + detail.toString()); 2975 } 2976 } 2977 } 2978 } 2979 } 2980 2981 2987 public void updateDetailObjectLinks() throws DjenericException 2988 { 2989 updateDetailObjectLinks(new HashMap ()); 2990 } 2991 2992 protected void updateDetailObjectLinks(HashMap hitlist) throws DjenericException 2993 { 2994 if (hitlist.containsKey(this)) return; 2995 hitlist.put(this, this); 2996 2997 determineObjectIdIfNeeded(); 2998 if (_detailAssociations != null) 2999 { 3000 for (int i = 0; i < _detailAssociations.length; i++) 3002 { 3003 if (_detailAssociations[i].isDetailsLoaded()) 3004 { 3005 DjList details = _detailAssociations[i].getObjects(); 3006 for (int d = 0; d < details.size(); d++) 3007 { 3008 DjObject detail = details.getDjenericObjectAt(d); 3009 if (detail.isModified() || detail.isNew()) 3013 { 3014 DjRelation rel = _detailAssociations[i].getRelation(); 3015 detail.setLong(rel.getDetailProperty().getName(), getObjectId()); 3017 } 3019 detail.updateDetailObjectLinks(hitlist); 3020 } 3021 } 3022 } 3023 } 3024 } 3025 3026 3032 private void applyTopDown() throws DjenericException 3033 { 3034 applyTopDown(new HashMap ()); 3035 } 3036 3037 private void applyTopDown(HashMap hitlist) throws DjenericException 3038 { 3039 3040 if (hitlist.containsKey(this)) return; 3041 hitlist.put(this, this); 3042 3043 determineObjectIdIfNeeded(); 3044 3045 if (isModified() && !isMarkedForDelete()) 3046 { 3047 if (_isPersisted) 3048 { 3049 if (!isHitDuringCurrentTransaction()) 3050 { 3051 update(); 3052 setHitDuringCurrentTransaction(true); 3053 } 3054 } 3055 else 3056 { 3057 if (!isHitDuringCurrentTransaction()) 3058 { 3059 insert(); 3060 setHitDuringCurrentTransaction(true); 3061 } 3062 } 3063 } 3064 if (_detailAssociations != null) 3065 { 3066 for (int i = 0; i < _detailAssociations.length; i++) 3068 { 3069 if (_detailAssociations[i].isDetailsLoaded()) 3070 { 3071 DjList details = _detailAssociations[i].getObjects(); 3072 for (int d = 0; d < details.size(); d++) 3073 { 3074 DjObject detail = details.getDjenericObjectAt(d); 3075 if (detail.isModified() || detail.isNew()) 3078 { 3079 DjRelation rel = _detailAssociations[i].getRelation(); 3080 detail.setLong(rel.getDetailProperty().getName(), getObjectId()); 3083 } 3084 detail.applyTopDown(hitlist); 3085 } 3086 } 3087 } 3088 } 3089 } 3090 3091 3100 protected void applyDeletes() throws DjenericException 3101 { 3102 if (isTransient() || (isEmpty() && !hasDetailsLoaded())) 3104 { 3105 return; 3106 } 3107 3108 applyBottomUp(); 3109 } 3110 3111 3120 protected void applyUpdates() throws DjenericException 3121 { 3122 if (isTransient() || (isEmpty() && !hasDetailsLoaded())) 3124 { 3125 return; 3126 } 3127 3128 applyTopDown(); 3129 } 3130 3131 3134 protected void notifyStartTransaction() 3135 { 3136 setHitDuringCurrentTransaction(false); 3137 } 3138 3139 3145 protected void notifyApplySuccesfull() throws DjenericException 3146 { 3147 notifyApplySuccesfull(new HashMap ()); 3148 } 3149 3150 protected void notifyApplySuccesfull(HashMap hitlist) throws DjenericException 3151 { 3152 if (hitlist.containsKey(this)) return; 3153 hitlist.put(this, this); 3154 3155 if (isMarkedForDelete()) 3156 { 3157 _session.unregisterDjenericObject(this); 3159 _isPersisted = false; 3160 setTransient(true); 3163 } 3164 else 3165 { 3166 _isPersisted = true; 3167 } 3168 setModified(false); 3170 _isMarkedForDelete = false; 3172 3173 _originalPropertyValues = (Object []) _propertyValues.clone(); 3176 3177 if (_detailAssociations != null) 3178 { 3179 for (int i = 0; i < _detailAssociations.length; i++) 3181 { 3182 if (_detailAssociations[i].isDetailsLoaded()) 3183 { 3184 DjList details = _detailAssociations[i].getObjects(); 3185 for (int d = 0; d < details.size(); d++) 3186 { 3187 DjObject detail = details.getDjenericObjectAt(d); 3188 detail.notifyApplySuccesfull(hitlist); 3189 } 3190 } 3191 } 3192 } 3193 } 3194 3195 3206 protected Date convertString2Date(String dateString, String mask) throws ParseException 3207 { 3208 if (mask == null) return sf.parse(dateString); 3209 3210 SimpleDateFormat m = new SimpleDateFormat (mask); 3211 return m.parse(dateString); 3212 } 3213 3214 3223 protected String convertDate2String(Date date, String mask) 3224 { 3225 if (mask == null) return sf.format(date); 3226 3227 SimpleDateFormat m = new SimpleDateFormat (mask); 3228 return m.format(date); 3229 } 3230 3231 public boolean exists() 3234 { 3235 return true; 3236 } 3237 3238 public void setDetailOfNewMaster(boolean b) 3239 { 3240 _isDetailOfNewMaster = b; 3241 3242 } 3243 3244 public boolean isDetailOfNewMaster() 3245 { 3246 return _isDetailOfNewMaster; 3247 } 3248 3249 protected boolean isTemporary() 3250 { 3251 return _isTemporary; 3252 } 3253 3254 protected void setTemporary(boolean isTemporary) 3255 { 3256 _isTemporary = isTemporary; 3257 } 3258 3259 public DjUid getUID() throws DjenericException 3260 { 3261 DjUid uid = new DjUid(getExtent()); 3262 3263 for (int i = 0; i < getPropertyCount(); i++) 3264 { 3265 DjProperty prop = getProperty(i); 3266 if (prop.isPartOfUID()) 3267 { 3268 if (prop.getType() instanceof DjDomain) 3269 { 3270 String value = getString(prop.getName()); 3271 if (value != null) uid.setProperty(prop.getName(), value); 3272 } 3273 else 3274 { 3275 DjObject master = (DjObject) get(prop.getName()); 3276 if (master != null) uid.setProperty(prop.getName(), master.getUID()); 3277 } 3278 } 3279 } 3280 uid.setAssociatedObjectid(getObjectId()); 3281 uid.setDescriptor(toString()); 3282 return uid; 3283 } 3284} | Popular Tags |