1 | |
package org.apache.ojb.broker.util; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import java.sql.PreparedStatement; |
19 | |
import java.sql.ResultSet; |
20 | |
import java.sql.SQLException; |
21 | |
import java.util.ArrayList; |
22 | |
import java.util.Collection; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.List; |
25 | |
import java.util.Map; |
26 | |
import java.util.StringTokenizer; |
27 | |
|
28 | |
import org.apache.commons.collections.CollectionUtils; |
29 | |
import org.apache.commons.collections.iterators.ArrayIterator; |
30 | |
import org.apache.commons.collections.map.ReferenceIdentityMap; |
31 | |
import org.apache.ojb.broker.Identity; |
32 | |
import org.apache.ojb.broker.ManageableCollection; |
33 | |
import org.apache.ojb.broker.MtoNImplementor; |
34 | |
import org.apache.ojb.broker.OJBRuntimeException; |
35 | |
import org.apache.ojb.broker.PBKey; |
36 | |
import org.apache.ojb.broker.PersistenceBrokerException; |
37 | |
import org.apache.ojb.broker.accesslayer.StatementManagerIF; |
38 | |
import org.apache.ojb.broker.accesslayer.sql.SqlExistStatement; |
39 | |
import org.apache.ojb.broker.core.PersistenceBrokerImpl; |
40 | |
import org.apache.ojb.broker.core.ValueContainer; |
41 | |
import org.apache.ojb.broker.core.proxy.IndirectionHandler; |
42 | |
import org.apache.ojb.broker.core.proxy.ProxyHelper; |
43 | |
import org.apache.ojb.broker.metadata.ClassDescriptor; |
44 | |
import org.apache.ojb.broker.metadata.CollectionDescriptor; |
45 | |
import org.apache.ojb.broker.metadata.FieldDescriptor; |
46 | |
import org.apache.ojb.broker.metadata.FieldHelper; |
47 | |
import org.apache.ojb.broker.metadata.MetadataException; |
48 | |
import org.apache.ojb.broker.metadata.MetadataManager; |
49 | |
import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor; |
50 | |
import org.apache.ojb.broker.metadata.fieldaccess.PersistentField; |
51 | |
import org.apache.ojb.broker.platforms.Platform; |
52 | |
import org.apache.ojb.broker.query.Criteria; |
53 | |
import org.apache.ojb.broker.query.MtoNQuery; |
54 | |
import org.apache.ojb.broker.query.Query; |
55 | |
import org.apache.ojb.broker.query.QueryByCriteria; |
56 | |
import org.apache.ojb.broker.query.QueryBySQL; |
57 | |
import org.apache.ojb.broker.query.ReportQueryByCriteria; |
58 | |
import org.apache.ojb.broker.query.ReportQueryByMtoNCriteria; |
59 | |
import org.apache.ojb.broker.util.logging.LoggerFactory; |
60 | |
import org.apache.ojb.broker.util.sequence.SequenceManagerException; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public class BrokerHelper |
73 | |
{ |
74 | |
public static final String REPOSITORY_NAME_SEPARATOR = "#"; |
75 | |
private PersistenceBrokerImpl m_broker; |
76 | |
|
77 | |
public BrokerHelper(PersistenceBrokerImpl broker) |
78 | |
{ |
79 | |
this.m_broker = broker; |
80 | |
} |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public static PBKey extractAllTokens(String name) |
90 | |
{ |
91 | |
if(name == null) |
92 | |
{ |
93 | |
throw new PersistenceBrokerException("Could not extract PBKey, given argument is 'null'"); |
94 | |
} |
95 | |
String user = null; |
96 | |
String passwd = null; |
97 | |
StringTokenizer tok = new StringTokenizer(name, REPOSITORY_NAME_SEPARATOR); |
98 | |
String dbName = tok.nextToken(); |
99 | |
if(tok.hasMoreTokens()) |
100 | |
{ |
101 | |
user = tok.nextToken(); |
102 | |
if(user != null && user.trim().equals("")) |
103 | |
{ |
104 | |
user = null; |
105 | |
} |
106 | |
} |
107 | |
if(tok.hasMoreTokens()) |
108 | |
{ |
109 | |
if(user != null) |
110 | |
passwd = tok.nextToken(); |
111 | |
} |
112 | |
if(user != null && passwd == null) |
113 | |
{ |
114 | |
passwd = ""; |
115 | |
} |
116 | |
return new PBKey(dbName, user, passwd); |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public static PBKey crossCheckPBKey(PBKey key) |
125 | |
{ |
126 | |
if(key.getUser() == null) |
127 | |
{ |
128 | |
PBKey defKey = MetadataManager.getInstance().connectionRepository().getStandardPBKeyForJcdAlias(key.getAlias()); |
129 | |
if(defKey != null) |
130 | |
{ |
131 | |
return defKey; |
132 | |
} |
133 | |
} |
134 | |
return key; |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
private ClassDescriptor getRealClassDescriptor(ClassDescriptor aCld, Object anObj) |
142 | |
{ |
143 | |
ClassDescriptor result; |
144 | |
|
145 | |
if(aCld.getClassOfObject() == ProxyHelper.getRealClass(anObj)) |
146 | |
{ |
147 | |
result = aCld; |
148 | |
} |
149 | |
else |
150 | |
{ |
151 | |
result = aCld.getRepository().getDescriptorFor(anObj.getClass()); |
152 | |
} |
153 | |
|
154 | |
return result; |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
public ValueContainer[] getKeyValues(ClassDescriptor cld, Object objectOrProxy, boolean convertToSql) throws PersistenceBrokerException |
168 | |
{ |
169 | |
IndirectionHandler handler = ProxyHelper.getIndirectionHandler(objectOrProxy); |
170 | |
|
171 | |
if(handler != null) |
172 | |
{ |
173 | |
return getKeyValues(cld, handler.getIdentity(), convertToSql); |
174 | |
} |
175 | |
else |
176 | |
{ |
177 | |
ClassDescriptor realCld = getRealClassDescriptor(cld, objectOrProxy); |
178 | |
return getValuesForObject(realCld.getPkFields(), objectOrProxy, convertToSql); |
179 | |
} |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public ValueContainer[] getKeyValues(ClassDescriptor cld, Identity oid) throws PersistenceBrokerException |
191 | |
{ |
192 | |
return getKeyValues(cld, oid, true); |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public ValueContainer[] getKeyValues(ClassDescriptor cld, Identity oid, boolean convertToSql) throws PersistenceBrokerException |
204 | |
{ |
205 | |
FieldDescriptor[] pkFields = cld.getPkFields(); |
206 | |
ValueContainer[] result = new ValueContainer[pkFields.length]; |
207 | |
Object[] pkValues = oid.getPrimaryKeyValues(); |
208 | |
|
209 | |
try |
210 | |
{ |
211 | |
for(int i = 0; i < result.length; i++) |
212 | |
{ |
213 | |
FieldDescriptor fd = pkFields[i]; |
214 | |
Object cv = pkValues[i]; |
215 | |
if(convertToSql) |
216 | |
{ |
217 | |
|
218 | |
cv = fd.getFieldConversion().javaToSql(cv); |
219 | |
} |
220 | |
result[i] = new ValueContainer(cv, fd.getJdbcType()); |
221 | |
} |
222 | |
} |
223 | |
catch(Exception e) |
224 | |
{ |
225 | |
throw new PersistenceBrokerException("Can't generate primary key values for given Identity " + oid, e); |
226 | |
} |
227 | |
return result; |
228 | |
} |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
public ValueContainer[] getKeyValues(ClassDescriptor cld, Object objectOrProxy) throws PersistenceBrokerException |
240 | |
{ |
241 | |
return getKeyValues(cld, objectOrProxy, true); |
242 | |
} |
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
public boolean representsNull(FieldDescriptor fld, Object aValue) |
256 | |
{ |
257 | |
if(aValue == null) return true; |
258 | |
|
259 | |
boolean result = false; |
260 | |
if(((aValue instanceof Number) && (((Number) aValue).longValue() == 0))) |
261 | |
{ |
262 | |
Class type = fld.getPersistentField().getType(); |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
if(type != null) |
270 | |
{ |
271 | |
result = type.isPrimitive(); |
272 | |
} |
273 | |
} |
274 | |
|
275 | |
|
276 | |
else if((aValue instanceof String) && (((String) aValue).length() == 0)) |
277 | |
{ |
278 | |
result = fld.isPrimaryKey(); |
279 | |
} |
280 | |
return result; |
281 | |
} |
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
public boolean hasNullPKField(ClassDescriptor cld, Object obj) |
287 | |
{ |
288 | |
FieldDescriptor[] fields = cld.getPkFields(); |
289 | |
boolean hasNull = false; |
290 | |
|
291 | |
IndirectionHandler handler = ProxyHelper.getIndirectionHandler(obj); |
292 | |
if(handler == null || handler.alreadyMaterialized()) |
293 | |
{ |
294 | |
if(handler != null) obj = handler.getRealSubject(); |
295 | |
FieldDescriptor fld; |
296 | |
for(int i = 0; i < fields.length; i++) |
297 | |
{ |
298 | |
fld = fields[i]; |
299 | |
hasNull = representsNull(fld, fld.getPersistentField().get(obj)); |
300 | |
if(hasNull) break; |
301 | |
} |
302 | |
} |
303 | |
return hasNull; |
304 | |
} |
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
private Object setAutoIncrementValue(FieldDescriptor fd, Object obj) |
320 | |
{ |
321 | |
PersistentField f = fd.getPersistentField(); |
322 | |
try |
323 | |
{ |
324 | |
|
325 | |
Object result = m_broker.serviceSequenceManager().getUniqueValue(fd); |
326 | |
|
327 | |
f.set(obj, result); |
328 | |
return result; |
329 | |
} |
330 | |
catch(MetadataException e) |
331 | |
{ |
332 | |
throw new PersistenceBrokerException( |
333 | |
"Error while trying to autoincrement field " + f.getDeclaringClass() + "#" + f.getName(), |
334 | |
e); |
335 | |
} |
336 | |
catch(SequenceManagerException e) |
337 | |
{ |
338 | |
throw new PersistenceBrokerException("Could not get key value", e); |
339 | |
} |
340 | |
} |
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
public ValueContainer[] getValuesForObject(FieldDescriptor[] fields, Object obj, boolean convertToSql, boolean assignAutoincrement) throws PersistenceBrokerException |
350 | |
{ |
351 | |
ValueContainer[] result = new ValueContainer[fields.length]; |
352 | |
|
353 | |
for(int i = 0; i < fields.length; i++) |
354 | |
{ |
355 | |
FieldDescriptor fd = fields[i]; |
356 | |
Object cv = fd.getPersistentField().get(obj); |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
if(assignAutoincrement && fd.isAutoIncrement() && representsNull(fd, cv)) |
365 | |
{ |
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
cv = setAutoIncrementValue(fd, obj); |
374 | |
} |
375 | |
if(convertToSql) |
376 | |
{ |
377 | |
|
378 | |
cv = fd.getFieldConversion().javaToSql(cv); |
379 | |
} |
380 | |
|
381 | |
result[i] = new ValueContainer(cv, fd.getJdbcType()); |
382 | |
} |
383 | |
return result; |
384 | |
} |
385 | |
|
386 | |
public ValueContainer[] getValuesForObject(FieldDescriptor[] fields, Object obj, boolean convertToSql) throws PersistenceBrokerException |
387 | |
{ |
388 | |
return getValuesForObject(fields, obj, convertToSql, false); |
389 | |
} |
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
public ValueContainer[] getNonKeyRwValues(ClassDescriptor cld, Object obj) throws PersistenceBrokerException |
403 | |
{ |
404 | |
return getValuesForObject(cld.getNonPkRwFields(), obj, true); |
405 | |
} |
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
public ValueContainer[] getAllRwValues(ClassDescriptor cld, Object obj) throws PersistenceBrokerException |
419 | |
{ |
420 | |
return getValuesForObject(cld.getAllRwFields(), obj, true); |
421 | |
} |
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
public Object[] extractValueArray(ValueContainer[] containers) |
429 | |
{ |
430 | |
Object[] result = new Object[containers.length]; |
431 | |
for(int i = 0; i < containers.length; i++) |
432 | |
{ |
433 | |
result[i] = containers[i].getValue(); |
434 | |
} |
435 | |
return result; |
436 | |
} |
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
public boolean assertValidPksForStore(FieldDescriptor[] fieldDescriptors, Object[] pkValues) |
448 | |
{ |
449 | |
int fieldDescriptorSize = fieldDescriptors.length; |
450 | |
for(int i = 0; i < fieldDescriptorSize; i++) |
451 | |
{ |
452 | |
FieldDescriptor fld = fieldDescriptors[i]; |
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
if(!(fld.isAutoIncrement() |
459 | |
|| fld.isLocking() |
460 | |
|| !representsNull(fld, pkValues[i]))) |
461 | |
{ |
462 | |
return false; |
463 | |
} |
464 | |
} |
465 | |
return true; |
466 | |
} |
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
public boolean assertValidPkForDelete(ClassDescriptor cld, Object obj) |
476 | |
{ |
477 | |
if(!ProxyHelper.isProxy(obj)) |
478 | |
{ |
479 | |
FieldDescriptor fieldDescriptors[] = cld.getPkFields(); |
480 | |
int fieldDescriptorSize = fieldDescriptors.length; |
481 | |
for(int i = 0; i < fieldDescriptorSize; i++) |
482 | |
{ |
483 | |
FieldDescriptor fd = fieldDescriptors[i]; |
484 | |
Object pkValue = fd.getPersistentField().get(obj); |
485 | |
if (representsNull(fd, pkValue)) |
486 | |
{ |
487 | |
return false; |
488 | |
} |
489 | |
} |
490 | |
} |
491 | |
return true; |
492 | |
} |
493 | |
|
494 | |
|
495 | |
|
496 | |
|
497 | |
|
498 | |
|
499 | |
public Query getCountQuery(Query aQuery) |
500 | |
{ |
501 | |
if(aQuery instanceof QueryBySQL) |
502 | |
{ |
503 | |
return getQueryBySqlCount((QueryBySQL) aQuery); |
504 | |
} |
505 | |
else if(aQuery instanceof ReportQueryByCriteria) |
506 | |
{ |
507 | |
return getReportQueryByCriteriaCount((ReportQueryByCriteria) aQuery); |
508 | |
} |
509 | |
else |
510 | |
{ |
511 | |
return getQueryByCriteriaCount((QueryByCriteria) aQuery); |
512 | |
} |
513 | |
} |
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
private Query getQueryBySqlCount(QueryBySQL aQuery) |
522 | |
{ |
523 | |
String countSql = aQuery.getSql(); |
524 | |
|
525 | |
int fromPos = countSql.toUpperCase().indexOf(" FROM "); |
526 | |
if(fromPos >= 0) |
527 | |
{ |
528 | |
countSql = "select count(*)" + countSql.substring(fromPos); |
529 | |
} |
530 | |
|
531 | |
int orderPos = countSql.toUpperCase().indexOf(" ORDER BY "); |
532 | |
if(orderPos >= 0) |
533 | |
{ |
534 | |
countSql = countSql.substring(0, orderPos); |
535 | |
} |
536 | |
|
537 | |
return new QueryBySQL(aQuery.getSearchClass(), countSql); |
538 | |
} |
539 | |
|
540 | |
|
541 | |
|
542 | |
|
543 | |
private Query getQueryByCriteriaCount(QueryByCriteria aQuery) |
544 | |
{ |
545 | |
Class searchClass = aQuery.getSearchClass(); |
546 | |
ReportQueryByCriteria countQuery = null; |
547 | |
Criteria countCrit = null; |
548 | |
String[] columns = new String[1]; |
549 | |
|
550 | |
|
551 | |
if (aQuery.getCriteria() != null) |
552 | |
{ |
553 | |
countCrit = aQuery.getCriteria().copy(false, false, false); |
554 | |
} |
555 | |
|
556 | |
if (aQuery.isDistinct()) |
557 | |
{ |
558 | |
|
559 | |
|
560 | |
|
561 | |
|
562 | |
|
563 | |
|
564 | |
|
565 | |
|
566 | |
|
567 | |
|
568 | |
|
569 | |
|
570 | |
|
571 | |
|
572 | |
FieldDescriptor[] pkFields = m_broker.getClassDescriptor(searchClass).getPkFields(); |
573 | |
String[] keyColumns = new String[pkFields.length]; |
574 | |
|
575 | |
if (pkFields.length > 1) |
576 | |
{ |
577 | |
|
578 | |
|
579 | |
for (int idx = 0; idx < pkFields.length; idx++) |
580 | |
{ |
581 | |
keyColumns[idx] = pkFields[idx].getColumnName(); |
582 | |
} |
583 | |
} |
584 | |
else |
585 | |
{ |
586 | |
for (int idx = 0; idx < pkFields.length; idx++) |
587 | |
{ |
588 | |
keyColumns[idx] = pkFields[idx].getAttributeName(); |
589 | |
} |
590 | |
} |
591 | |
|
592 | |
|
593 | |
|
594 | |
|
595 | |
|
596 | |
|
597 | |
|
598 | |
|
599 | |
|
600 | |
|
601 | |
|
602 | |
columns[0] = "count(distinct " + getPlatform().concatenate(keyColumns) + ")"; |
603 | |
} |
604 | |
else |
605 | |
{ |
606 | |
columns[0] = "count(*)"; |
607 | |
} |
608 | |
|
609 | |
|
610 | |
if (aQuery instanceof MtoNQuery) |
611 | |
{ |
612 | |
MtoNQuery mnQuery = (MtoNQuery)aQuery; |
613 | |
ReportQueryByMtoNCriteria mnReportQuery = new ReportQueryByMtoNCriteria(searchClass, columns, countCrit); |
614 | |
|
615 | |
mnReportQuery.setIndirectionTable(mnQuery.getIndirectionTable()); |
616 | |
countQuery = mnReportQuery; |
617 | |
} |
618 | |
else |
619 | |
{ |
620 | |
countQuery = new ReportQueryByCriteria(searchClass, columns, countCrit); |
621 | |
} |
622 | |
|
623 | |
|
624 | |
for (Iterator outerJoinPath = aQuery.getOuterJoinPaths().iterator(); outerJoinPath.hasNext();) |
625 | |
{ |
626 | |
String path = (String) outerJoinPath.next(); |
627 | |
|
628 | |
if (aQuery.isPathOuterJoin(path)) |
629 | |
{ |
630 | |
countQuery.setPathOuterJoin(path); |
631 | |
} |
632 | |
} |
633 | |
|
634 | |
|
635 | |
List orderBy = aQuery.getOrderBy(); |
636 | |
|
637 | |
if ((orderBy != null) && !orderBy.isEmpty()) |
638 | |
{ |
639 | |
String[] joinAttributes = new String[orderBy.size()]; |
640 | |
|
641 | |
for (int idx = 0; idx < orderBy.size(); idx++) |
642 | |
{ |
643 | |
joinAttributes[idx] = ((FieldHelper)orderBy.get(idx)).name; |
644 | |
} |
645 | |
countQuery.setJoinAttributes(joinAttributes); |
646 | |
} |
647 | |
|
648 | |
|
649 | |
|
650 | |
|
651 | |
|
652 | |
|
653 | |
|
654 | |
|
655 | |
|
656 | |
|
657 | |
|
658 | |
return countQuery; |
659 | |
} |
660 | |
|
661 | |
|
662 | |
|
663 | |
|
664 | |
private Query getReportQueryByCriteriaCount(ReportQueryByCriteria aQuery) |
665 | |
{ |
666 | |
ReportQueryByCriteria countQuery = (ReportQueryByCriteria) getQueryByCriteriaCount(aQuery); |
667 | |
|
668 | |
|
669 | |
countQuery.setJoinAttributes(aQuery.getAttributes()); |
670 | |
|
671 | |
|
672 | |
Iterator iter = aQuery.getGroupBy().iterator(); |
673 | |
while(iter.hasNext()) |
674 | |
{ |
675 | |
countQuery.addGroupBy((FieldHelper) iter.next()); |
676 | |
} |
677 | |
|
678 | |
return countQuery; |
679 | |
} |
680 | |
|
681 | |
|
682 | |
|
683 | |
|
684 | |
|
685 | |
|
686 | |
private Platform getPlatform() |
687 | |
{ |
688 | |
return m_broker.serviceSqlGenerator().getPlatform(); |
689 | |
} |
690 | |
|
691 | |
|
692 | |
|
693 | |
|
694 | |
|
695 | |
|
696 | |
private Map sqlSelectMap = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.HARD); |
697 | |
|
698 | |
|
699 | |
|
700 | |
|
701 | |
|
702 | |
|
703 | |
|
704 | |
|
705 | |
|
706 | |
|
707 | |
|
708 | |
|
709 | |
|
710 | |
|
711 | |
public boolean doesExist(ClassDescriptor cld, Identity oid, Object obj) |
712 | |
{ |
713 | |
boolean result = false; |
714 | |
String sql = (String) sqlSelectMap.get(cld); |
715 | |
if(sql == null) |
716 | |
{ |
717 | |
sql = new SqlExistStatement(cld, LoggerFactory.getDefaultLogger()).getStatement(); |
718 | |
sqlSelectMap.put(cld, sql); |
719 | |
} |
720 | |
ValueContainer[] pkValues; |
721 | |
if(oid == null) |
722 | |
{ |
723 | |
pkValues = getKeyValues(cld, obj, true); |
724 | |
} |
725 | |
else |
726 | |
{ |
727 | |
pkValues = getKeyValues(cld, oid); |
728 | |
} |
729 | |
StatementManagerIF sm = m_broker.serviceStatementManager(); |
730 | |
PreparedStatement stmt = null; |
731 | |
ResultSet rs = null; |
732 | |
try |
733 | |
{ |
734 | |
stmt = sm.getPreparedStatement(cld, sql, false, 1, false); |
735 | |
sm.bindValues(stmt, pkValues, 1); |
736 | |
rs = stmt.executeQuery(); |
737 | |
result = rs.next(); |
738 | |
} |
739 | |
catch(SQLException e) |
740 | |
{ |
741 | |
throw ExceptionHelper.generateException("[BrokerHelper#doesExist] Can't check if specified" + |
742 | |
" object is already persisted", e, sql, cld, pkValues, null, obj); |
743 | |
} |
744 | |
finally |
745 | |
{ |
746 | |
sm.closeResources(stmt, rs); |
747 | |
} |
748 | |
|
749 | |
return result; |
750 | |
} |
751 | |
|
752 | |
|
753 | |
|
754 | |
|
755 | |
|
756 | |
|
757 | |
|
758 | |
public void link(Object obj, boolean insert) |
759 | |
{ |
760 | |
linkOrUnlink(true, obj, insert); |
761 | |
} |
762 | |
|
763 | |
|
764 | |
|
765 | |
|
766 | |
|
767 | |
|
768 | |
public void unlink(Object obj) |
769 | |
{ |
770 | |
linkOrUnlink(false, obj, false); |
771 | |
} |
772 | |
|
773 | |
private void linkOrUnlink(boolean doLink, Object obj, boolean insert) |
774 | |
{ |
775 | |
ClassDescriptor cld = m_broker.getDescriptorRepository().getDescriptorFor(obj.getClass()); |
776 | |
|
777 | |
if (cld.getObjectReferenceDescriptors().size() > 0) |
778 | |
{ |
779 | |
|
780 | |
Iterator descriptors = cld.getObjectReferenceDescriptors().iterator(); |
781 | |
while (descriptors.hasNext()) |
782 | |
{ |
783 | |
ObjectReferenceDescriptor ord = (ObjectReferenceDescriptor) descriptors.next(); |
784 | |
linkOrUnlinkOneToOne(doLink, obj, ord, insert); |
785 | |
} |
786 | |
} |
787 | |
if (cld.getCollectionDescriptors().size() > 0) |
788 | |
{ |
789 | |
|
790 | |
Iterator descriptors = cld.getCollectionDescriptors().iterator(); |
791 | |
while (descriptors.hasNext()) |
792 | |
{ |
793 | |
CollectionDescriptor cod = (CollectionDescriptor) descriptors.next(); |
794 | |
linkOrUnlinkXToMany(doLink, obj, cod, insert); |
795 | |
} |
796 | |
} |
797 | |
} |
798 | |
|
799 | |
|
800 | |
|
801 | |
|
802 | |
|
803 | |
|
804 | |
|
805 | |
|
806 | |
|
807 | |
|
808 | |
|
809 | |
|
810 | |
public void link(Object obj, ObjectReferenceDescriptor ord, boolean insert) |
811 | |
{ |
812 | |
linkOrUnlink(true, obj, ord, insert); |
813 | |
} |
814 | |
|
815 | |
|
816 | |
|
817 | |
|
818 | |
|
819 | |
|
820 | |
|
821 | |
|
822 | |
|
823 | |
|
824 | |
|
825 | |
|
826 | |
|
827 | |
public boolean link(Object obj, String attributeName, boolean insert) |
828 | |
{ |
829 | |
return linkOrUnlink(true, obj, attributeName, insert); |
830 | |
} |
831 | |
|
832 | |
|
833 | |
|
834 | |
|
835 | |
|
836 | |
|
837 | |
|
838 | |
|
839 | |
|
840 | |
|
841 | |
|
842 | |
|
843 | |
|
844 | |
|
845 | |
public boolean link(Object obj, String attributeName, Object reference, boolean insert) |
846 | |
{ |
847 | |
ClassDescriptor cld = m_broker.getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(obj)); |
848 | |
ObjectReferenceDescriptor ord; |
849 | |
boolean match = false; |
850 | |
|
851 | |
ord = cld.getObjectReferenceDescriptorByName(attributeName); |
852 | |
if (ord != null) |
853 | |
{ |
854 | |
linkOrUnlinkOneToOne(true, obj, ord, insert); |
855 | |
match = true; |
856 | |
} |
857 | |
else |
858 | |
{ |
859 | |
CollectionDescriptor cod = cld.getCollectionDescriptorByName(attributeName); |
860 | |
if (cod != null) |
861 | |
{ |
862 | |
linkOrUnlinkXToMany(true, obj, cod, insert); |
863 | |
match = true; |
864 | |
} |
865 | |
} |
866 | |
return match; |
867 | |
} |
868 | |
|
869 | |
|
870 | |
|
871 | |
|
872 | |
|
873 | |
|
874 | |
|
875 | |
|
876 | |
public boolean unlink(Object source, String attributeName, Object target) |
877 | |
{ |
878 | |
return linkOrUnlink(false, source, attributeName, false); |
879 | |
} |
880 | |
|
881 | |
|
882 | |
|
883 | |
|
884 | |
|
885 | |
|
886 | |
|
887 | |
public boolean unlink(Object source, String attributeName) |
888 | |
{ |
889 | |
return linkOrUnlink(false, source, attributeName, false); |
890 | |
} |
891 | |
|
892 | |
|
893 | |
|
894 | |
|
895 | |
|
896 | |
|
897 | |
|
898 | |
|
899 | |
|
900 | |
public void unlink(Object obj, ObjectReferenceDescriptor ord, boolean insert) |
901 | |
{ |
902 | |
linkOrUnlink(false, obj, ord, insert); |
903 | |
} |
904 | |
|
905 | |
private boolean linkOrUnlink(boolean doLink, Object obj, String attributeName, boolean insert) |
906 | |
{ |
907 | |
boolean match = false; |
908 | |
ClassDescriptor cld = m_broker.getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(obj)); |
909 | |
ObjectReferenceDescriptor ord; |
910 | |
|
911 | |
|
912 | |
ord = cld.getObjectReferenceDescriptorByName(attributeName); |
913 | |
if (ord != null) |
914 | |
{ |
915 | |
linkOrUnlinkOneToOne(doLink, obj, ord, insert); |
916 | |
match = true; |
917 | |
} |
918 | |
else |
919 | |
{ |
920 | |
CollectionDescriptor cod = cld.getCollectionDescriptorByName(attributeName); |
921 | |
if (cod != null) |
922 | |
{ |
923 | |
linkOrUnlinkXToMany(doLink, obj, cod, insert); |
924 | |
match = true; |
925 | |
} |
926 | |
} |
927 | |
|
928 | |
return match; |
929 | |
} |
930 | |
|
931 | |
private void linkOrUnlink(boolean doLink, Object obj, ObjectReferenceDescriptor ord, boolean insert) |
932 | |
{ |
933 | |
if (ord instanceof CollectionDescriptor) |
934 | |
{ |
935 | |
linkOrUnlinkXToMany(doLink, obj, (CollectionDescriptor) ord, insert); |
936 | |
} |
937 | |
else |
938 | |
{ |
939 | |
linkOrUnlinkOneToOne(doLink, obj, ord, insert); |
940 | |
} |
941 | |
} |
942 | |
|
943 | |
private void linkOrUnlinkXToMany(boolean doLink, Object obj, CollectionDescriptor cod, boolean insert) |
944 | |
{ |
945 | |
if (doLink) |
946 | |
{ |
947 | |
if (cod.isMtoNRelation()) |
948 | |
{ |
949 | |
m_broker.linkMtoN(obj, cod, insert); |
950 | |
} |
951 | |
else |
952 | |
{ |
953 | |
m_broker.linkOneToMany(obj, cod, insert); |
954 | |
} |
955 | |
} |
956 | |
else |
957 | |
{ |
958 | |
m_broker.unlinkXtoN(obj, cod); |
959 | |
} |
960 | |
} |
961 | |
|
962 | |
private void linkOrUnlinkOneToOne(boolean doLink, Object obj, ObjectReferenceDescriptor ord, boolean insert) |
963 | |
{ |
964 | |
|
965 | |
|
966 | |
|
967 | |
|
968 | |
|
969 | |
|
970 | |
ClassDescriptor cld = ord.getClassDescriptor(); |
971 | |
if(cld.isInterface()) |
972 | |
{ |
973 | |
cld = m_broker.getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(obj)); |
974 | |
} |
975 | |
|
976 | |
if (doLink) |
977 | |
{ |
978 | |
m_broker.linkOneToOne(obj, cld, ord, insert); |
979 | |
} |
980 | |
else |
981 | |
{ |
982 | |
m_broker.unlinkFK(obj, cld, ord); |
983 | |
|
984 | |
ord.getPersistentField().set(obj, null); |
985 | |
} |
986 | |
} |
987 | |
|
988 | |
|
989 | |
|
990 | |
|
991 | |
|
992 | |
|
993 | |
|
994 | |
|
995 | |
public void unlink(Object source, CollectionDescriptor cds, List referencesToUnlink) |
996 | |
{ |
997 | |
for(int i = 0; i < referencesToUnlink.size(); i++) |
998 | |
{ |
999 | |
unlink(source, cds, referencesToUnlink.get(i)); |
1000 | |
} |
1001 | |
} |
1002 | |
|
1003 | |
|
1004 | |
|
1005 | |
|
1006 | |
|
1007 | |
|
1008 | |
|
1009 | |
|
1010 | |
public void unlink(Object source, CollectionDescriptor cds, Object referenceToUnlink) |
1011 | |
{ |
1012 | |
if(cds.isMtoNRelation()) |
1013 | |
{ |
1014 | |
m_broker.deleteMtoNImplementor(new MtoNImplementor(cds, source, referenceToUnlink)); |
1015 | |
} |
1016 | |
else |
1017 | |
{ |
1018 | |
ClassDescriptor cld = m_broker.getClassDescriptor(referenceToUnlink.getClass()); |
1019 | |
m_broker.unlinkFK(referenceToUnlink, cld, cds); |
1020 | |
} |
1021 | |
} |
1022 | |
|
1023 | |
|
1024 | |
|
1025 | |
|
1026 | |
|
1027 | |
|
1028 | |
|
1029 | |
|
1030 | |
public void link(Object source, CollectionDescriptor cds, List referencesToLink) |
1031 | |
{ |
1032 | |
for(int i = 0; i < referencesToLink.size(); i++) |
1033 | |
{ |
1034 | |
link(source, cds, referencesToLink.get(i)); |
1035 | |
} |
1036 | |
} |
1037 | |
|
1038 | |
|
1039 | |
|
1040 | |
|
1041 | |
|
1042 | |
|
1043 | |
|
1044 | |
|
1045 | |
public void link(Object source, CollectionDescriptor cds, Object referenceToLink) |
1046 | |
{ |
1047 | |
if(cds.isMtoNRelation()) |
1048 | |
{ |
1049 | |
m_broker.addMtoNImplementor(new MtoNImplementor(cds, source, referenceToLink)); |
1050 | |
} |
1051 | |
else |
1052 | |
{ |
1053 | |
ClassDescriptor cld = m_broker.getClassDescriptor(referenceToLink.getClass()); |
1054 | |
m_broker.link(referenceToLink, cld, cds, source, false); |
1055 | |
} |
1056 | |
} |
1057 | |
|
1058 | |
|
1059 | |
|
1060 | |
|
1061 | |
|
1062 | |
|
1063 | |
|
1064 | |
|
1065 | |
|
1066 | |
public static Iterator getCollectionIterator(Object collectionOrArray) |
1067 | |
{ |
1068 | |
Iterator colIterator; |
1069 | |
if (collectionOrArray instanceof ManageableCollection) |
1070 | |
{ |
1071 | |
colIterator = ((ManageableCollection) collectionOrArray).ojbIterator(); |
1072 | |
} |
1073 | |
else if (collectionOrArray instanceof Collection) |
1074 | |
{ |
1075 | |
colIterator = ((Collection) collectionOrArray).iterator(); |
1076 | |
} |
1077 | |
else if (collectionOrArray.getClass().isArray()) |
1078 | |
{ |
1079 | |
colIterator = new ArrayIterator(collectionOrArray); |
1080 | |
} |
1081 | |
else |
1082 | |
{ |
1083 | |
throw new OJBRuntimeException( "Given object collection of type '" |
1084 | |
+ (collectionOrArray != null ? collectionOrArray.getClass().toString() : "null") |
1085 | |
+ "' can not be managed by OJB. Use Array, Collection or ManageableCollection instead!"); |
1086 | |
} |
1087 | |
return colIterator; |
1088 | |
} |
1089 | |
|
1090 | |
|
1091 | |
|
1092 | |
|
1093 | |
|
1094 | |
|
1095 | |
|
1096 | |
|
1097 | |
|
1098 | |
public static Object[] getCollectionArray(Object collectionOrArray) |
1099 | |
{ |
1100 | |
Object[] result; |
1101 | |
if (collectionOrArray instanceof Collection) |
1102 | |
{ |
1103 | |
result = ((Collection) collectionOrArray).toArray(); |
1104 | |
} |
1105 | |
else if (collectionOrArray instanceof ManageableCollection) |
1106 | |
{ |
1107 | |
Collection newCol = new ArrayList(); |
1108 | |
CollectionUtils.addAll(newCol, ((ManageableCollection) collectionOrArray).ojbIterator()); |
1109 | |
result = newCol.toArray(); |
1110 | |
} |
1111 | |
else if (collectionOrArray.getClass().isArray()) |
1112 | |
{ |
1113 | |
result = (Object[]) collectionOrArray; |
1114 | |
} |
1115 | |
else |
1116 | |
{ |
1117 | |
throw new OJBRuntimeException( "Given object collection of type '" |
1118 | |
+ (collectionOrArray != null ? collectionOrArray.getClass().toString() : "null") |
1119 | |
+ "' can not be managed by OJB. Use Array, Collection or ManageableCollection instead!"); |
1120 | |
} |
1121 | |
return result; |
1122 | |
} |
1123 | |
|
1124 | |
|
1125 | |
|
1126 | |
|
1127 | |
|
1128 | |
|
1129 | |
|
1130 | |
public static boolean hasAnonymousKeyReference(ClassDescriptor cld, ObjectReferenceDescriptor rds) |
1131 | |
{ |
1132 | |
boolean result = false; |
1133 | |
FieldDescriptor[] fkFields = rds.getForeignKeyFieldDescriptors(cld); |
1134 | |
for(int i = 0; i < fkFields.length; i++) |
1135 | |
{ |
1136 | |
FieldDescriptor fkField = fkFields[i]; |
1137 | |
if(fkField.isAnonymous()) |
1138 | |
{ |
1139 | |
result = true; |
1140 | |
break; |
1141 | |
} |
1142 | |
} |
1143 | |
return result; |
1144 | |
} |
1145 | |
|
1146 | |
|
1147 | |
|
1148 | |
|
1149 | |
|
1150 | |
|
1151 | |
|
1152 | |
|
1153 | |
|
1154 | |
|
1155 | |
|
1156 | |
|
1157 | |
|
1158 | |
|
1159 | |
|
1160 | |
|
1161 | |
|
1162 | |
|
1163 | |
|
1164 | |
|
1165 | |
|
1166 | |
|
1167 | |
|
1168 | |
|
1169 | |
|
1170 | |
|
1171 | |
|
1172 | |
|
1173 | |
|
1174 | |
|
1175 | |
|
1176 | |
|
1177 | |
|
1178 | |
|
1179 | |
|
1180 | |
|
1181 | |
|
1182 | |
|
1183 | |
|
1184 | |
|
1185 | |
|
1186 | |
|
1187 | |
|
1188 | |
|
1189 | |
|
1190 | |
|
1191 | |
|
1192 | |
|
1193 | |
|
1194 | |
|
1195 | |
|
1196 | |
|
1197 | |
|
1198 | |
|
1199 | |
|
1200 | |
|
1201 | |
|
1202 | |
|
1203 | |
|
1204 | |
|
1205 | |
} |