1 30 package com.genimen.djeneric.repository; 31 32 import com.genimen.djeneric.language.Messages; 33 import com.genimen.djeneric.repository.exceptions.CatalogException; 34 import com.genimen.djeneric.repository.exceptions.DjenericException; 35 import com.genimen.djeneric.util.DjLogger; 36 37 public class DjRelation implements Cloneable 38 { 39 public static final int ONE_TO_MANY = 0; 40 public static final int ONE_TO_ONE = 1; 41 42 String _name; 43 DjExtent _detailExtent; 44 DjExtent _masterExtent; 45 DjProperty _detailProperty; 46 DjRestriction _restriction = null; 47 boolean _isContainedRelation; 48 int _unicity = ONE_TO_MANY; 49 String _restrictionExpression = ""; 50 String _description; 51 long _internalId; 52 53 public DjRelation(String name, DjExtent masterExtent, DjExtent detailExtent, DjProperty detailProperty, 54 boolean isContainedRelation, String description) 55 { 56 _internalId = DjPersistenceManager.getNextInternalId(); 57 _name = name; 58 _masterExtent = masterExtent; 59 _detailExtent = detailExtent; 60 _detailProperty = detailProperty; 61 _isContainedRelation = isContainedRelation; 62 _description = description; 63 } 64 65 public String toString() 66 { 67 StringBuffer result = new StringBuffer (100); 68 if (_masterExtent != null) result.append(_masterExtent.toString()); 69 else result.append("null"); 70 71 result.append("-<"); 72 73 if (_detailExtent != null) result.append(_detailExtent.toString()); 74 else result.append("null"); 75 76 result.append("."); 77 78 if (_detailProperty != null) result.append(_detailProperty.toString()); 79 else result.append("null"); 80 81 result.append(" "); 82 83 if (_name != null) result.append("(" + _name + ")"); 84 85 return result.toString(); 86 } 87 88 public boolean equals(Object other) 89 { 90 if (!(other instanceof DjRelation)) return false; 91 DjRelation rd = (DjRelation) other; 92 93 String detailPropertyName1 = ""; 94 if (_detailProperty != null) detailPropertyName1 = _detailProperty.getName(); 95 96 String detailPropertyName2 = ""; 97 if (rd._detailProperty != null) detailPropertyName2 = rd._detailProperty.getName(); 98 99 return _detailExtent == rd._detailExtent && _masterExtent == rd._masterExtent 100 && detailPropertyName1.equals(detailPropertyName2); 101 } 102 103 public int hashCode() 104 { 105 String detailPropertyName = ""; 106 if (_detailProperty != null) detailPropertyName = _detailProperty.getName(); 107 108 return detailPropertyName.hashCode(); 109 } 110 111 public Object clone() 112 { 113 DjRelation nr = new DjRelation(getName(), getMasterExtent(), getDetailExtent(), getDetailProperty(), 114 isDetailsContained(), getDescription()); 115 setInternalId(getInternalId()); 116 117 try 118 { 119 copyPropertiesInto(nr); 120 } 121 catch (Exception x) 122 { 123 DjLogger.log(x); 124 throw new RuntimeException ("Internal error: DjRelation.clone() failed"); 125 } 126 return nr; 127 } 128 129 protected void copyPropertiesInto(DjRelation nr) 130 { 131 nr.setName(getName()); 132 nr.setDetailExtent(getDetailExtent()); 133 nr.setMasterExtent(getMasterExtent()); 134 nr.setDetailProperty(getDetailProperty()); 135 nr.setDetailsContained(isDetailsContained()); 136 nr.setRestrictionExpression(getRestrictionExpression()); 137 nr.setDescription(getDescription()); 138 nr.setUnicity(getUnicity()); 139 nr._internalId = _internalId; 140 } 141 142 public DjRestriction getRestriction() throws CatalogException 143 { 144 if (_restriction == null) 145 { 146 if (_restrictionExpression.trim().length() > 0) 147 { 148 _restriction = new DjRestriction(this, _restrictionExpression); 149 } 150 } 151 return _restriction; 152 } 153 154 public void validateRestriction() throws CatalogException 155 { 156 setRestrictionExpression(getRestrictionExpression()); 157 getRestriction(); 158 } 159 160 public long getInternalId() 161 { 162 return _internalId; 163 } 164 165 protected void setInternalId(long internalId) 166 { 167 _internalId = internalId; 168 } 169 170 public String getName() 171 { 172 return _name; 173 } 174 175 public String getObjectNameSingular() 176 { 177 return _detailExtent.getNameSingular(); 178 } 179 180 public String getObjectNamePlural() 181 { 182 return _detailExtent.getNamePlural(); 183 } 184 185 public String getObjectType() 186 { 187 return _detailExtent.getName(); 188 } 189 190 public String getDescription() 191 { 192 return _description; 193 } 194 195 public DjExtent getMasterExtent() 196 { 197 return _masterExtent; 198 } 199 200 public DjExtent getDetailExtent() 201 { 202 return _detailExtent; 203 } 204 205 public DjProperty getDetailProperty() 206 { 207 return _detailProperty; 208 } 209 210 public boolean isDetailsContained() 211 { 212 return _isContainedRelation; 213 } 214 215 public void setDescription(String description) 216 { 217 _description = description; 218 } 219 220 public void setDetailProperty(DjProperty detailProperty) 221 { 222 _detailProperty = detailProperty; 223 } 224 225 public void setDetailProperty(String detailPropertyName) throws DjenericException 226 { 227 _detailProperty = _detailExtent.getProperty(detailPropertyName); 228 } 229 230 public void setDetailsContained(boolean detailsContained) 231 { 232 _isContainedRelation = detailsContained; 233 } 234 235 public void setDetailExtent(DjExtent detailExtent) 236 { 237 _detailExtent = detailExtent; 238 try 239 { 240 if (_detailExtent.hasProperty(_detailProperty.getName()) && _detailProperty.getExtent() != _detailExtent) 241 { 242 _detailProperty = _detailExtent.getProperty(_detailProperty.getName()); 245 } 246 } 247 catch (Exception x) 248 { 249 } 252 } 253 254 public void setMasterExtent(DjExtent masterExtent) 255 { 256 _masterExtent = masterExtent; 257 } 258 259 public void setName(String name) 260 { 261 _name = name; 262 } 263 264 public String getRestrictionExpression() 265 { 266 return _restrictionExpression; 267 } 268 269 public void setRestrictionExpression(String restrictionExpression) 270 { 271 if (_restriction != null) _restriction.unregister(); 272 _restriction = null; 273 if (restrictionExpression == null) restrictionExpression = ""; 274 _restrictionExpression = restrictionExpression.trim(); 275 } 276 277 protected void validate(DjPersistenceManager mgr, boolean strictChecking) throws CatalogException 278 { 279 if (_masterExtent == null) throw new CatalogException(Messages.getString("DjRelation.MasterNotSet", getName())); 280 if (_detailExtent == null) throw new CatalogException(Messages.getString("DjRelation.DetailNotSet", _masterExtent 281 .getName() 282 + "." 283 + getName())); 284 if (_detailProperty == null) throw new CatalogException(Messages.getString("DjRelation.DetailPropNotSet", 285 _masterExtent.getName() + "." 286 + getName())); 287 288 if (getName().trim().length() == 0) throw new CatalogException(Messages.getString("DjRelation.RelnameNull", 289 _masterExtent.getName() + "." 290 + getName())); 291 292 if (strictChecking) if (!DjPersistenceManager.isValidName(getName())) 293 { 294 throw new CatalogException(Messages.getString("DjRelation.InvalidName", getDetailExtent().getName() + "." 295 + getName())); 296 } 297 298 if (!_masterExtent.getTypeName().equals(_detailExtent.getTypeName())) try 300 { 301 mgr.getType(_masterExtent.getTypeName()); 302 } 303 catch (Exception x) 304 { 305 throw new CatalogException(Messages.getString("DjRelation.InvalidMaster", getName(), _masterExtent.getTypeName())); 306 } 307 308 try 309 { 310 DjProperty col = _detailExtent.getPropertyByInternalId(_detailProperty._internalId); 311 _detailProperty = col; 312 } 313 catch (Exception x) 314 { 315 _masterExtent.removeDetailRelation(this); 317 DjLogger.log(Messages.getString("DjRelation.DetailPropDeleted", _masterExtent.getName() + "." + getName())); 318 return; 319 } 320 321 try 323 { 324 if (strictChecking) validateRestriction(); 325 } 326 catch (CatalogException x) 327 { 328 throw new CatalogException(Messages.getString("global.Restriction", _masterExtent.getName() + "." + getName(), x 329 .getMessage())); 330 } 331 332 DjRelation[] otherRels = mgr.getRelationsForProperty(_detailExtent.getName(), _detailProperty.getName()); 333 if (otherRels.length > 1) 334 { 335 String rels = ""; 337 for (int i = 0; i < otherRels.length; i++) 338 { 339 if (i != 0) rels += " " + Messages.getString("global.and") + " "; 340 rels += Messages.getString("DjRelation.Relation") + " " + otherRels[i].getMasterExtent().getName() + "." 341 + otherRels[i].getName(); 342 } 343 throw new CatalogException(Messages.getString("DjRelation.MultiMapping", rels, _detailProperty.getName())); 344 } 345 if (!_detailExtent.hasProperty(_detailProperty.getName())) 346 { 347 throw new CatalogException(Messages.getString("DjRelation.InvalidDetailProp", getName(), _masterExtent.getName(), 348 _detailExtent.getName(), _detailProperty.getName(), _detailExtent 349 .getName())); 350 } 351 _detailProperty.setType(_masterExtent); 352 354 if (strictChecking) 355 { 356 try 358 { 359 getRestriction(); 360 } 361 catch (CatalogException ce) 362 { 363 throw new CatalogException(Messages.getString("global.InvalidRestriction", _detailExtent.getName() + "." 364 + getName(), ce.getMessage())); 365 } 366 } 367 } 368 369 public int getUnicity() 370 { 371 return _unicity; 372 } 373 374 public void setUnicity(int unicity) 375 { 376 _unicity = unicity; 377 } 378 379 public boolean isOneToOne() 380 { 381 return _unicity == ONE_TO_ONE; 382 } 383 384 public boolean isOneToMany() 385 { 386 return _unicity == ONE_TO_MANY; 387 } 388 } | Popular Tags |