1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2001 Michael Powers
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.access;
import net.wotonomy.foundation.NSArray;
import net.wotonomy.foundation.NSDictionary;
import net.wotonomy.foundation.NSMutableArray;
import net.wotonomy.foundation.NSMutableDictionary;
/**
* Represents a relationship from one entity to another. Relationships are unidirectional.
*
* @author ezamudio@nasoft.com
* @author $Author: cgruber $
* @version $Revision: 894 $
*/
public class EORelationship extends EOProperty implements EOPropertyListEncoding {
public static final int InnerJoin = 0;
public static final int FullOuterJoin = 1;
public static final int LeftOuterJoin = 2;
public static final int RightOuterJoin = 3;
protected String _name;
protected int _batchCount;
protected int _deleteRule;
protected int _joinSemantic;
protected EOEntity _destination;
protected EOEntity _entity;
protected NSMutableArray _joins = new NSMutableArray();
protected boolean _isMandatory;
protected boolean _isToMany;
protected boolean _isFlattened;
protected boolean _knowsIfFlattened;
protected boolean _ownsDestination;
protected boolean _propagatesPrimaryKey;
protected boolean _useBatchFaulting;
protected NSDictionary _userInfo = NSDictionary.EmptyDictionary;
protected NSDictionary _internalInfo = NSDictionary.EmptyDictionary;
protected NSDictionary plist;
protected String _definition;
public EORelationship() {
super();
}
public EORelationship(NSDictionary dict, Object obj) {
super();
_entity = (EOEntity)obj;
setName((String)dict.objectForKey("name"));
setToMany("Y".equals(dict.objectForKey("isToMany")));
setPropagatesPrimaryKey("Y".equals(dict.objectForKey("propagatesPrimaryKey")));
setIsMandatory("Y".equals(dict.objectForKey("isMandatory")));
setOwnsDestination("Y".equals(dict.objectForKey("ownsDestination")));
setDefinition((String)dict.objectForKey("definition"));
String delrule = (String)dict.objectForKey("deleteRule");
if (delrule != null) {
if (delrule.equals("EODeleteRuleCascade"))
setDeleteRule(0);
else if (delrule.equals("EODeleteRuleDeny"))
setDeleteRule(0);
else if (delrule.equals("EODeleteRuleNoAction"))
setDeleteRule(0);
else if (delrule.equals("EODeleteRuleNullify"))
setDeleteRule(0);
}
delrule = (String)dict.objectForKey("joinSemantic");
if (delrule != null) {
if (delrule.equals("EOInnerJoin"))
setJoinSemantic(InnerJoin);
else if (delrule.equals("EOFullOuterJoin"))
setJoinSemantic(FullOuterJoin);
else if (delrule.equals("EOLeftOuterJoin"))
setJoinSemantic(LeftOuterJoin);
else if (delrule.equals("EORightOuterJoin"))
setJoinSemantic(RightOuterJoin);
}
delrule = (String)dict.objectForKey("batchCount");
if (delrule != null)
setNumberOfToManyFaultsToBatchFetch(Integer.parseInt(delrule));
NSDictionary d = (NSDictionary)dict.objectForKey("userInfo");
if (d != null)
_userInfo = d;
d = (NSDictionary)dict.objectForKey("internalInfo");
if (d != null)
_internalInfo = d;
plist = dict;
}
public void setName(String name) {
_name = name;
}
public String name() {
return _name;
}
public void addJoin(EOJoin join) {
_joins.addObject(join);
}
public void removeJoin(EOJoin join) {
_joins.removeObject(join);
}
public EOEntity entity() {
return _entity;
}
public EOEntity destinationEntity() {
isFlattened();
if (_destination == null && plist != null) {
EOModel model = _entity.model();
EOModelGroup group = model.modelGroup();
String destEntity = (String)plist.objectForKey("destination");
if (group != null)
_destination = group.entityNamed(destEntity);
else
_destination = model.entityNamed(destEntity);
}
return _destination;
}
public void setOwnsDestination(boolean flag) {
_ownsDestination = flag;
}
public boolean ownsDestination() {
return _ownsDestination;
}
public void setToMany(boolean flag) {
_isToMany = flag;
}
public boolean isToMany() {
return _isToMany;
}
public void setIsMandatory(boolean flag) {
_isMandatory = flag;
}
public boolean isMandatory() {
return _isMandatory;
}
public void setPropagatesPrimaryKey(boolean flag) {
_propagatesPrimaryKey = flag;
}
public boolean propagatesPrimaryKey() {
return _propagatesPrimaryKey;
}
public void setDeleteRule(int value) {
_deleteRule = value;
}
public int deleteRule() {
return _deleteRule;
}
public void setJoinSemantic(int value) {
_joinSemantic = value;
}
public int joinSemantic() {
return _joinSemantic;
}
public void setNumberOfToManyFaultsToBatchFetch(int count) {
_batchCount = count;
}
public int numberOfToManyFaultsToBatchFetch() {
return _batchCount;
}
public NSArray joins() {
if (_joins.count() == 0 && plist != null) {
NSArray joins = (NSArray)plist.objectForKey("joins");
for (int i = 0; i < joins.count(); i++) {
NSDictionary d = (NSDictionary)joins.objectAtIndex(i);
String srcName = (String)d.objectForKey("sourceAttribute");
String dstName = (String)d.objectForKey("destinationAttribute");
EOAttribute a1 = _entity.attributeNamed(srcName);
EOAttribute a2 = destinationEntity().attributeNamed(dstName);
EOJoin j = new EOJoin(a1, a2);
addJoin(j);
}
}
return new NSArray(_joins);
}
public void setDefinition(String def) {
_definition = def;
}
public String definition() {
return _definition;
}
public boolean isFlattened() {
if (_knowsIfFlattened)
return _isFlattened;
_knowsIfFlattened = true;
if (definition() == null)
return false;
NSArray comps = NSArray.componentsSeparatedByString(definition(), ".");
if (comps.count() < 2)
return false;
EORelationship r = null;
EOEntity e = entity();
for (int i = 0; i < comps.count(); i++) {
String name = (String)comps.objectAtIndex(i);
r = e.relationshipNamed(name);
if (r == null)
return false;
e = r.destinationEntity();
}
_destination = e;
_isFlattened = true;
return _isFlattened;
}
public boolean isMultiHop() {
return false;
}
public String relationshipPath() {
if (isFlattened())
return _definition;
return null;
}
public void setUserInfo(NSDictionary value) {
_userInfo = value;
}
public NSDictionary userInfo() {
return _userInfo;
}
public void awakeWithPropertyList(NSDictionary plist) {
}
public void encodeIntoPropertyList(NSMutableDictionary dict) {
dict.setObjectForKey(name(), "name");
if (destinationEntity() != null && definition() == null)
dict.setObjectForKey(_destination.name(), "destination");
if (_internalInfo != null && _internalInfo.count() > 0)
dict.setObjectForKey(_internalInfo, "internalInfo");
if (_userInfo != null && _userInfo.count() > 0)
dict.setObjectForKey(_userInfo, "userInfo");
if (isToMany())
dict.setObjectForKey("Y", "isToMany");
switch (_joinSemantic) {
case InnerJoin:
dict.setObjectForKey("EOInnerJoin", "joinSemantic");
break;
case FullOuterJoin:
dict.setObjectForKey("EOFullOuterJoin", "joinSemantic");
break;
case LeftOuterJoin:
dict.setObjectForKey("EOLefOuterJoin", "joinSemantic");
break;
case RightOuterJoin:
dict.setObjectForKey("EORightOuterJoin", "joinSemantic");
break;
}
if (_batchCount > 0)
dict.setObjectForKey(new Integer(_batchCount), "batchCount");
if (definition() != null)
dict.setObjectForKey(definition(), "definition");
else {
NSMutableArray jarr = new NSMutableArray(joins().count());
for (int i = 0; i < _joins.count(); i++) {
EOJoin j = (EOJoin)_joins.objectAtIndex(i);
NSDictionary d = new NSDictionary(
new Object[]{ j.sourceAttribute().name(), j.destinationAttribute().name() },
new Object[]{ "sourceAttribute", "destinationAttribute" });
jarr.addObject(d);
}
dict.setObjectForKey(jarr, "joins");
}
}
}
/*
* $Log$
* Revision 1.2 2006/02/16 16:47:13 cgruber
* Move some classes in to "internal" packages and re-work imports, etc.
*
* Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
*
* Revision 1.1 2006/02/16 13:19:57 cgruber
* Check in all sources in eclipse-friendly maven-enabled packages.
*
* Revision 1.5 2003/08/11 19:38:27 chochos
* Can now read from a file and re-write to another file.
*
* Revision 1.4 2003/08/09 01:35:35 chochos
* implement EOPropertyListEncoding
*
* Revision 1.3 2003/08/08 06:51:53 chochos
* isFlattened() works
*
* Revision 1.2 2003/08/08 02:17:43 chochos
* main accessors are in place.
*
* Revision 1.1 2003/08/07 02:41:30 chochos
* a relationship that for the moment can be created from a property list.
*
*/
|