1 package org.apache.ojb.broker.metadata;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.Vector;
22
23 import org.apache.commons.lang.SystemUtils;
24 import org.apache.ojb.broker.PersistenceBrokerException;
25 import org.apache.ojb.broker.accesslayer.QueryCustomizer;
26
27
28
29
30
31
32
33
34
35
36
37 public class CollectionDescriptor extends ObjectReferenceDescriptor
38 {
39 private static final long serialVersionUID = -8570280662286424937L;
40
41
42
43
44
45
46 private Class collectionClass = null;
47
48
49
50 private Collection m_orderby = new ArrayList();
51
52
53
54 private String indirectionTable = null;
55 private Vector fksToItemClass = null;
56 private Vector fksToThisClass = null;
57 private String[] fksToItemClassAry;
58 private String[] fksToThisClassAry;
59 private QueryCustomizer m_queryCustomizer;
60 private Boolean m_hasProxyItems;
61
62 public CollectionDescriptor(ClassDescriptor descriptor)
63 {
64 super(descriptor);
65 }
66
67 public String[] getFksToThisClass()
68 {
69 if (fksToThisClassAry == null)
70 {
71 fksToThisClassAry = (String[]) fksToThisClass.toArray(
72 new String[fksToThisClass.size()]);
73 }
74 return fksToThisClassAry;
75 }
76
77 public void setFksToThisClass(Vector fksToThisClass)
78 {
79 this.fksToThisClass = fksToThisClass;
80 fksToThisClassAry = null;
81 }
82
83
84
85
86 public void addFkToThisClass(String column)
87 {
88 if (fksToThisClass == null)
89 {
90 fksToThisClass = new Vector();
91 }
92 fksToThisClass.add(column);
93 fksToThisClassAry = null;
94 }
95
96
97
98
99 public void addFkToItemClass(String column)
100 {
101 if (fksToItemClass == null)
102 {
103 fksToItemClass = new Vector();
104 }
105 fksToItemClass.add(column);
106 fksToItemClassAry = null;
107 }
108
109
110
111
112
113 public Class getCollectionClass()
114 {
115 return collectionClass;
116 }
117
118
119
120
121
122 public void setCollectionClass(Class c)
123 {
124 collectionClass = c;
125 }
126
127
128
129
130 public String getCollectionClassName()
131 {
132 return collectionClass != null ? collectionClass.getName() : null;
133 }
134
135 public String getIndirectionTable()
136 {
137 return indirectionTable;
138 }
139
140 public void setIndirectionTable(String indirectionTable)
141 {
142 this.indirectionTable = indirectionTable;
143 }
144
145 public String[] getFksToItemClass()
146 {
147 if (fksToItemClassAry == null)
148 {
149 fksToItemClassAry = (String[]) fksToItemClass.toArray(
150 new String[fksToItemClass.size()]);
151 }
152 return fksToItemClassAry;
153 }
154
155 public void setFksToItemClass(Vector fksToItemClass)
156 {
157 this.fksToItemClass = fksToItemClass;
158 fksToItemClassAry = null;
159 }
160
161 public boolean isMtoNRelation()
162 {
163 return (indirectionTable != null);
164 }
165
166
167
168
169
170
171 public void addOrderBy(String fieldName, boolean sortAscending)
172 {
173 if (fieldName != null)
174 {
175 m_orderby.add(new FieldHelper(fieldName, sortAscending));
176 }
177 }
178
179
180
181
182
183 public Collection getOrderBy()
184 {
185 return m_orderby;
186 }
187
188 protected int getCascadeDeleteValue(String cascade)
189 {
190 if(cascade.equalsIgnoreCase("false") && isMtoNRelation())
191 {
192
193
194
195
196 return CASCADE_LINK;
197 }
198 return super.getCascadeDeleteValue(cascade);
199 }
200
201
202
203
204 public String toXML()
205 {
206 RepositoryTags tags = RepositoryTags.getInstance();
207 String eol = SystemUtils.LINE_SEPARATOR;
208
209
210 String result = " " + tags.getOpeningTagNonClosingById(COLLECTION_DESCRIPTOR) + eol;
211
212
213
214 result += " " + tags.getAttribute(FIELD_NAME,this.getAttributeName()) + eol;
215
216
217 if (getCollectionClassName() != null)
218 {
219 result += " " + tags.getAttribute(COLLECTION_CLASS,this.getCollectionClassName()) + eol;
220 }
221
222
223 result += " " + tags.getAttribute(ITEMS_CLASS,this.getItemClassName()) + eol;
224
225
226 if (isMtoNRelation())
227 {
228 result += " " + tags.getAttribute(INDIRECTION_TABLE,getIndirectionTable()) + eol;
229 }
230
231
232 if (isLazy())
233 {
234 result += " " + tags.getAttribute(PROXY_REFERENCE,"true") + eol;
235 result += " " + tags.getAttribute(PROXY_PREFETCHING_LIMIT, "" + this.getProxyPrefetchingLimit()) + eol;
236 }
237
238
239 if (isRefresh())
240 {
241 result += " " + tags.getAttribute(REFRESH,"true") + eol;
242 }
243
244
245 result += " " + tags.getAttribute(AUTO_RETRIEVE, "" + getCascadeRetrieve()) + eol;
246
247
248 result += " " + tags.getAttribute(AUTO_UPDATE, getCascadeAsString(getCascadingStore())) + eol;
249
250
251 result += " " + tags.getAttribute(AUTO_DELETE, getCascadeAsString(getCascadingDelete())) + eol;
252
253
254 if (getOtmDependent())
255 {
256 result += " " + tags.getAttribute(OTM_DEPENDENT, "true") + eol;
257 }
258
259
260 result += " >" + eol;
261
262
263
264 for (int i=0;i<getForeignKeyFields().size();i++)
265 {
266 Object obj = getForeignKeyFields().get(i);
267 if (obj instanceof Integer)
268 {
269 String fkId = obj.toString();
270 result += " " + tags.getOpeningTagNonClosingById(INVERSE_FK) + " ";
271 result += tags.getAttribute(FIELD_ID_REF, fkId) + "/>" + eol;
272 }
273 else
274 {
275 String fk = (String) obj;
276 result += " " + tags.getOpeningTagNonClosingById(INVERSE_FK) + " ";
277 result += tags.getAttribute(FIELD_REF, fk) + "/>" + eol;
278 }
279 }
280
281
282
283 if (isMtoNRelation())
284 {
285
286 for (int i=0;i<getFksToThisClass().length;i++)
287 {
288 String fkId = getFksToThisClass()[i];
289 result += " " + tags.getOpeningTagNonClosingById(FK_POINTING_TO_THIS_CLASS) + " ";
290 result += tags.getAttribute(COLUMN_NAME, fkId) + "/>" + eol;
291 }
292
293
294 for (int i=0;i<getFksToItemClass().length;i++)
295 {
296 String fkId = getFksToItemClass()[i];
297 result += " " + tags.getOpeningTagNonClosingById(FK_POINTING_TO_ITEMS_CLASS) + " ";
298 result += tags.getAttribute(COLUMN_NAME, fkId) + "/>" + eol;
299 }
300 }
301
302
303 result += " " + tags.getClosingTagById(COLLECTION_DESCRIPTOR) + eol;
304 return result;
305 }
306
307
308
309
310 public QueryCustomizer getQueryCustomizer()
311 {
312 return m_queryCustomizer;
313 }
314
315
316
317
318
319 public void setQueryCustomizer(QueryCustomizer queryCustomizer)
320 {
321 m_queryCustomizer = queryCustomizer;
322 }
323
324 public boolean hasProxyItems() throws PersistenceBrokerException
325 {
326 if (m_hasProxyItems == null)
327 {
328 DescriptorRepository repo = getClassDescriptor().getRepository();
329 ClassDescriptor cld = repo.getDescriptorFor(getItemClass());
330 if (cld.getProxyClass() != null)
331 {
332 m_hasProxyItems = Boolean.TRUE;
333 }
334 else
335 {
336 Collection extents = cld.getExtentClasses();
337 m_hasProxyItems = Boolean.FALSE;
338 for (Iterator it = extents.iterator(); it.hasNext(); )
339 {
340 Class ext = (Class) it.next();
341 ClassDescriptor cldExt = repo.getDescriptorFor(ext);
342 if (cldExt.getProxyClass() != null)
343 {
344 m_hasProxyItems = Boolean.TRUE;
345 break;
346 }
347 }
348 }
349 }
350
351 return (m_hasProxyItems.booleanValue());
352 }
353 }