1 30 package com.genimen.djeneric.structure; 31 32 import com.genimen.djeneric.language.Messages; 33 import com.genimen.djeneric.repository.DjExtent; 34 import com.genimen.djeneric.repository.DjRelation; 35 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 36 37 55 public class RelationDescriptor implements Comparable 56 { 57 DjExtent _masterExtent; 58 DjExtent _detailExtent; 59 String _detailPropertyName; 60 String _relationName; 61 long _internalPropertyId; 62 long _internalRelationId; 63 64 public RelationDescriptor(DjRelation rel) 65 { 66 _masterExtent = rel.getMasterExtent(); 67 _detailExtent = rel.getDetailExtent(); 68 _detailPropertyName = rel.getDetailProperty().getName(); 69 _relationName = rel.getName(); 70 _internalPropertyId = rel.getDetailProperty().getInternalId(); 71 _internalRelationId = rel.getInternalId(); 72 } 73 74 public RelationDescriptor(DjRelation rel, DjExtent specificDetailImplementation) 75 { 76 _masterExtent = rel.getMasterExtent(); 77 _detailExtent = specificDetailImplementation; 78 _detailPropertyName = rel.getDetailProperty().getName(); 79 _relationName = rel.getName(); 80 _internalPropertyId = rel.getDetailProperty().getInternalId(); 81 _internalRelationId = rel.getInternalId(); 82 } 83 84 public DjRelation getRelation() throws ObjectNotDefinedException 85 { 86 DjRelation rel = getDetailExtent().getMasterRelationByInternalId(_internalRelationId); 87 88 if (rel != null) return rel; 89 90 92 DjRelation[] rels = getDetailExtent().getMasterRelations(); 93 for (int i = 0; i < rels.length; i++) 94 { 95 if (rels[i].getMasterExtent() == getMasterExtent() && rels[i].getDetailExtent() == getDetailExtent() 96 && rels[i].getDetailProperty().getName().equals(getDetailPropertyName())) return rels[i]; 97 } 98 throw new ObjectNotDefinedException(Messages.getString("RelationDescriptor.relationMissing", _relationName, 99 getMasterExtent().getName(), getDetailExtent().getName())); 100 } 101 102 public boolean equals(Object other) 103 { 104 if (other == this) return true; 105 if (!(other instanceof RelationDescriptor)) return false; 106 RelationDescriptor rd = (RelationDescriptor) other; 107 108 return (getMasterExtent() == rd.getMasterExtent()) && (getDetailExtent() == rd.getDetailExtent()) 109 && (getDetailPropertyName().equals(rd.getDetailPropertyName())); 110 } 111 112 public int hashCode() 113 { 114 int result = 0; 115 if (getMasterExtent() != null) result += getMasterExtent().hashCode(); 116 if (getDetailExtent() != null) result += getDetailExtent().hashCode(); 117 if (getDetailPropertyName() != null) result += getDetailPropertyName().hashCode(); 118 119 return result; 120 } 121 122 public int compareTo(Object o) 123 { 124 if (equals(o)) return 0; 125 return 1; 126 } 128 129 public String getDetailPropertyName() 130 { 131 try 132 { 133 return _detailExtent.getPropertyByInternalId(_internalPropertyId).getName(); 134 } 135 catch (ObjectNotDefinedException onde) 136 { 137 } 140 141 return _detailPropertyName; 142 } 143 144 public DjExtent getDetailExtent() 145 { 146 return _detailExtent; 147 } 148 149 public DjExtent getMasterExtent() 150 { 151 return _masterExtent; 152 } 153 154 public String getRelationName() 155 { 156 return _relationName; 157 } 158 159 public void setDetailPropertyName(String detailPropertyName) 160 { 161 _detailPropertyName = detailPropertyName; 162 } 163 164 public void setDetailExtent(DjExtent detailExtent) 165 { 166 _detailExtent = detailExtent; 167 } 168 169 public void setMasterExtent(DjExtent masterExtent) 170 { 171 _masterExtent = masterExtent; 172 } 173 174 public void setRelationName(String relationName) 175 { 176 _relationName = relationName; 177 } 178 179 public String toString() 180 { 181 return Messages.getString("RelationDescriptor.relation", getDetailExtent().getName(), getRelationName(), 182 getMasterExtent().getName()); 183 } 184 185 188 public boolean isvalid() 189 { 190 try 191 { 192 return getRelation() != null; 193 } 194 catch (ObjectNotDefinedException e) 195 { 196 return false; 197 } 198 } 199 200 } | Popular Tags |