KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > structure > RelationDescriptor


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
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * 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, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

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 /*
38  * A relationDescriptor defines the relation between two ExtentNode's just like
39  * a DjRelation does between tables. BUT: This class is used to DESCRIBE a
40  * relation rather than using a pointer to a relation directly. This is needed
41  * because a pointer could possibly reference a relation that has become
42  * invalid because of operations in the diagrammer / extent editor. A
43  * descriptor stores the names of relevant data, using (combinations) of these
44  * names the relation that was originally meant can be retrieved. Algorithm:
45  * first lookup the relation by name in the list of detail relations of the
46  * extent If that fails, the relation might have been renamed. In that case
47  * find the detail relation by the pointer of the detail extent; in combination
48  * with the detail column NAME. (The name of the column is used here because of
49  * the nature of how the extent editor works: only the extent is updated,
50  * columns and relations are replaced inside the extent on apply) If this all
51  * fails the relation is either completely changed or deleted. In both cases
52  * the link is consodered broken and the details should be removed from the
53  * extent in the navigator.
54  */

55 public class RelationDescriptor implements Comparable JavaDoc
56 {
57   DjExtent _masterExtent;
58   DjExtent _detailExtent;
59   String JavaDoc _detailPropertyName;
60   String JavaDoc _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     // Not found? Try lookup based on detailproperty and extents
91

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 JavaDoc 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 JavaDoc o)
123   {
124     if (equals(o)) return 0;
125     return 1;
126     // we do not support ordering yet
127
}
128
129   public String JavaDoc getDetailPropertyName()
130   {
131     try
132     {
133       return _detailExtent.getPropertyByInternalId(_internalPropertyId).getName();
134     }
135     catch (ObjectNotDefinedException onde)
136     {
137       // Detail property was deleted. Return the original name and try to
138
// recover
139
}
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 JavaDoc getRelationName()
155   {
156     return _relationName;
157   }
158
159   public void setDetailPropertyName(String JavaDoc 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 JavaDoc relationName)
175   {
176     _relationName = relationName;
177   }
178
179   public String JavaDoc toString()
180   {
181     return Messages.getString("RelationDescriptor.relation", getDetailExtent().getName(), getRelationName(),
182                               getMasterExtent().getName());
183   }
184
185   /**
186    * @return
187    */

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