KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > DjRelation


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

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 JavaDoc
38 {
39   public static final int ONE_TO_MANY = 0;
40   public static final int ONE_TO_ONE = 1;
41
42   String JavaDoc _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 JavaDoc _restrictionExpression = "";
50   String JavaDoc _description;
51   long _internalId;
52
53   public DjRelation(String JavaDoc name, DjExtent masterExtent, DjExtent detailExtent, DjProperty detailProperty,
54                     boolean isContainedRelation, String JavaDoc 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 JavaDoc toString()
66   {
67     StringBuffer JavaDoc result = new StringBuffer JavaDoc(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 JavaDoc other)
89   {
90     if (!(other instanceof DjRelation)) return false;
91     DjRelation rd = (DjRelation) other;
92
93     String JavaDoc detailPropertyName1 = "";
94     if (_detailProperty != null) detailPropertyName1 = _detailProperty.getName();
95
96     String JavaDoc 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 JavaDoc detailPropertyName = "";
106     if (_detailProperty != null) detailPropertyName = _detailProperty.getName();
107
108     return detailPropertyName.hashCode();
109   }
110
111   public Object JavaDoc 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 JavaDoc x)
122     {
123       DjLogger.log(x);
124       throw new RuntimeException JavaDoc("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 JavaDoc getName()
171   {
172     return _name;
173   }
174
175   public String JavaDoc getObjectNameSingular()
176   {
177     return _detailExtent.getNameSingular();
178   }
179
180   public String JavaDoc getObjectNamePlural()
181   {
182     return _detailExtent.getNamePlural();
183   }
184
185   public String JavaDoc getObjectType()
186   {
187     return _detailExtent.getName();
188   }
189
190   public String JavaDoc 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 JavaDoc description)
216   {
217     _description = description;
218   }
219
220   public void setDetailProperty(DjProperty detailProperty)
221   {
222     _detailProperty = detailProperty;
223   }
224
225   public void setDetailProperty(String JavaDoc 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         // name the same but it's another property!
243
// fix this then, is very likely the result of a extent apply in the model editor
244
_detailProperty = _detailExtent.getProperty(_detailProperty.getName());
245       }
246     }
247     catch (Exception JavaDoc x)
248     {
249       // leave it invalid then, probably fixed by a secondary update
250
// Could be cause by edit screen, extent updated, property not yet.
251
}
252   }
253
254   public void setMasterExtent(DjExtent masterExtent)
255   {
256     _masterExtent = masterExtent;
257   }
258
259   public void setName(String JavaDoc name)
260   {
261     _name = name;
262   }
263
264   public String JavaDoc getRestrictionExpression()
265   {
266     return _restrictionExpression;
267   }
268
269   public void setRestrictionExpression(String JavaDoc 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     // Not a recursive relation? Then check the type with the manager's known list
299
if (!_masterExtent.getTypeName().equals(_detailExtent.getTypeName())) try
300     {
301       mgr.getType(_masterExtent.getTypeName());
302     }
303     catch (Exception JavaDoc 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 JavaDoc x)
314     {
315       // property must be deleted then. Kill this relation too then
316
_masterExtent.removeDetailRelation(this);
317       DjLogger.log(Messages.getString("DjRelation.DetailPropDeleted", _masterExtent.getName() + "." + getName()));
318       return;
319     }
320
321     // check te restriction expression by forcing a parse
322
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       // more than 1 relation mapped to this property
336
String JavaDoc 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     // make sure the type is correct
353

354     if (strictChecking)
355     {
356       // Now validate the restriction by forcing a parse for not yet parsed restrictions
357
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