1 | |
package org.apache.ojb.odmg.collections; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import java.util.AbstractList; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
import java.util.ListIterator; |
23 | |
|
24 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
25 | |
import org.apache.ojb.broker.ManageableCollection; |
26 | |
import org.apache.ojb.broker.OJBRuntimeException; |
27 | |
import org.apache.ojb.broker.PBKey; |
28 | |
import org.apache.ojb.broker.PersistenceBroker; |
29 | |
import org.apache.ojb.broker.PersistenceBrokerAware; |
30 | |
import org.apache.ojb.broker.PersistenceBrokerException; |
31 | |
import org.apache.ojb.broker.core.ValueContainer; |
32 | |
import org.apache.ojb.broker.metadata.ClassDescriptor; |
33 | |
import org.apache.ojb.broker.metadata.FieldDescriptor; |
34 | |
import org.apache.ojb.broker.query.Criteria; |
35 | |
import org.apache.ojb.broker.query.Query; |
36 | |
import org.apache.ojb.broker.query.QueryByCriteria; |
37 | |
import org.apache.ojb.broker.util.logging.Logger; |
38 | |
import org.apache.ojb.broker.util.logging.LoggerFactory; |
39 | |
import org.apache.ojb.odmg.PBCapsule; |
40 | |
import org.apache.ojb.odmg.TransactionImpl; |
41 | |
import org.apache.ojb.odmg.TxManagerFactory; |
42 | |
import org.apache.ojb.odmg.RuntimeObject; |
43 | |
import org.apache.ojb.odmg.oql.OQLQueryImpl; |
44 | |
import org.odmg.DArray; |
45 | |
import org.odmg.DCollection; |
46 | |
import org.odmg.DList; |
47 | |
import org.odmg.ODMGRuntimeException; |
48 | |
import org.odmg.OQLQuery; |
49 | |
import org.odmg.QueryInvalidException; |
50 | |
import org.odmg.Transaction; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public class DListImpl extends AbstractList implements DList, DArray, |
60 | |
ManageableCollection, PersistenceBrokerAware |
61 | |
{ |
62 | |
private static final long serialVersionUID = -9219943066614026526L; |
63 | |
|
64 | |
private transient Logger log; |
65 | |
|
66 | |
private Integer id; |
67 | |
private List elements; |
68 | |
|
69 | |
private PBKey pbKey; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public DListImpl() |
76 | |
{ |
77 | |
super(); |
78 | |
elements = new ArrayList(); |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
getPBKey(); |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public DListImpl(PBKey pbKey) |
91 | |
{ |
92 | |
this(); |
93 | |
this.pbKey = pbKey; |
94 | |
} |
95 | |
|
96 | |
protected Logger getLog() |
97 | |
{ |
98 | |
if (log == null) |
99 | |
{ |
100 | |
log = LoggerFactory.getLogger(DListImpl.class); |
101 | |
} |
102 | |
return log; |
103 | |
} |
104 | |
|
105 | |
private DListEntry prepareEntry(Object obj) |
106 | |
{ |
107 | |
return new DListEntry(this, obj); |
108 | |
} |
109 | |
|
110 | |
protected TransactionImpl getTransaction() |
111 | |
{ |
112 | |
return TxManagerFactory.instance().getTransaction(); |
113 | |
} |
114 | |
|
115 | |
protected boolean checkForOpenTransaction(TransactionImpl tx) |
116 | |
{ |
117 | |
boolean result = false; |
118 | |
if(tx != null && tx.isOpen()) |
119 | |
{ |
120 | |
result = true; |
121 | |
} |
122 | |
return result; |
123 | |
} |
124 | |
|
125 | |
public PBKey getPBKey() |
126 | |
{ |
127 | |
if(pbKey == null) |
128 | |
{ |
129 | |
TransactionImpl tx = getTransaction(); |
130 | |
if(tx != null && tx.isOpen()) |
131 | |
{ |
132 | |
pbKey = tx.getBroker().getPBKey(); |
133 | |
} |
134 | |
} |
135 | |
return pbKey; |
136 | |
} |
137 | |
|
138 | |
public void setPBKey(PBKey pbKey) |
139 | |
{ |
140 | |
this.pbKey = pbKey; |
141 | |
} |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
public void add(int index, Object element) |
162 | |
{ |
163 | |
DListEntry entry = prepareEntry(element); |
164 | |
elements.add(index, entry); |
165 | |
|
166 | |
TransactionImpl tx = getTransaction(); |
167 | |
if (checkForOpenTransaction(tx)) |
168 | |
{ |
169 | |
RuntimeObject rt = new RuntimeObject(this, tx); |
170 | |
List regList = tx.getRegistrationList(); |
171 | |
tx.lockAndRegister(rt, Transaction.WRITE, false, regList); |
172 | |
|
173 | |
rt = new RuntimeObject(element, tx); |
174 | |
tx.lockAndRegister(rt, Transaction.READ, regList); |
175 | |
|
176 | |
rt = new RuntimeObject(entry, tx, true); |
177 | |
tx.lockAndRegister(rt, Transaction.WRITE, false, regList); |
178 | |
} |
179 | |
|
180 | |
|
181 | |
int offset = 0; |
182 | |
try |
183 | |
{ |
184 | |
offset = ((DListEntry) elements.get(index - 1)).getPosition(); |
185 | |
} |
186 | |
catch (Exception ignored) |
187 | |
{ |
188 | |
} |
189 | |
for (int i = offset; i < elements.size(); i++) |
190 | |
{ |
191 | |
entry = (DListEntry) elements.get(i); |
192 | |
entry.setPosition(i); |
193 | |
} |
194 | |
} |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public Object remove(int index) |
214 | |
{ |
215 | |
DListEntry entry = (DListEntry) elements.get(index); |
216 | |
|
217 | |
TransactionImpl tx = getTransaction(); |
218 | |
if (checkForOpenTransaction(tx)) |
219 | |
{ |
220 | |
tx.deletePersistent(new RuntimeObject(entry, tx)); |
221 | |
} |
222 | |
elements.remove(index); |
223 | |
|
224 | |
int offset = 0; |
225 | |
try |
226 | |
{ |
227 | |
offset = ((DListEntry) elements.get(index)).getPosition(); |
228 | |
} |
229 | |
catch (Exception ignored) |
230 | |
{ |
231 | |
} |
232 | |
for (int i = offset; i < elements.size(); i++) |
233 | |
{ |
234 | |
entry = (DListEntry) elements.get(i); |
235 | |
entry.setPosition(i); |
236 | |
} |
237 | |
|
238 | |
return entry.getRealSubject(); |
239 | |
} |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
public DList concat(DList otherList) |
251 | |
{ |
252 | |
DListImpl result = new DListImpl(pbKey); |
253 | |
result.addAll(this); |
254 | |
result.addAll(otherList); |
255 | |
return result; |
256 | |
} |
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
public boolean existsElement(String predicate) throws org.odmg.QueryInvalidException |
267 | |
{ |
268 | |
DList results = (DList) this.query(predicate); |
269 | |
if (results == null || results.size() == 0) |
270 | |
return false; |
271 | |
else |
272 | |
return true; |
273 | |
} |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
public Object get(int index) |
285 | |
{ |
286 | |
DListEntry entry = (DListEntry) elements.get(index); |
287 | |
return entry.getRealSubject(); |
288 | |
} |
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
public List getElements() |
296 | |
{ |
297 | |
return elements; |
298 | |
} |
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
public Integer getId() |
305 | |
{ |
306 | |
return id; |
307 | |
} |
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
public Iterator iterator() |
318 | |
{ |
319 | |
return new DListIterator(this); |
320 | |
} |
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
public ListIterator listIterator() |
330 | |
{ |
331 | |
return new DListIterator(this); |
332 | |
} |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
public ListIterator listIterator(int index) |
350 | |
{ |
351 | |
return new DListIterator(this, index); |
352 | |
} |
353 | |
|
354 | |
private Criteria getPkCriteriaForAllElements(PersistenceBroker brokerForClass) |
355 | |
{ |
356 | |
try |
357 | |
{ |
358 | |
Criteria crit = null; |
359 | |
for (int i = 0; i < elements.size(); i++) |
360 | |
{ |
361 | |
DListEntry entry = (DListEntry) elements.get(i); |
362 | |
Object obj = entry.getRealSubject(); |
363 | |
ClassDescriptor cld = brokerForClass.getClassDescriptor(obj.getClass()); |
364 | |
|
365 | |
FieldDescriptor[] pkFields = cld.getPkFields(); |
366 | |
ValueContainer[] pkValues = brokerForClass.serviceBrokerHelper().getKeyValues(cld, obj); |
367 | |
|
368 | |
Criteria criteria = new Criteria(); |
369 | |
for (int j = 0; j < pkFields.length; j++) |
370 | |
{ |
371 | |
FieldDescriptor fld = pkFields[j]; |
372 | |
criteria.addEqualTo(fld.getPersistentField().getName(), pkValues[j].getValue()); |
373 | |
} |
374 | |
|
375 | |
if (crit == null) |
376 | |
crit = criteria; |
377 | |
else |
378 | |
crit.addOrCriteria(criteria); |
379 | |
} |
380 | |
return crit; |
381 | |
} |
382 | |
catch (PersistenceBrokerException e) |
383 | |
{ |
384 | |
return null; |
385 | |
} |
386 | |
} |
387 | |
|
388 | |
private Class getElementsExtentClass(PersistenceBroker brokerForClass) throws PersistenceBrokerException |
389 | |
{ |
390 | |
|
391 | |
DListEntry entry = (DListEntry) elements.get(0); |
392 | |
Class elementsClass = entry.getRealSubject().getClass(); |
393 | |
Class extentClass = brokerForClass.getTopLevelClass(elementsClass); |
394 | |
return extentClass; |
395 | |
} |
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
public DCollection query(String predicate) throws QueryInvalidException |
405 | |
{ |
406 | |
|
407 | |
String oql = "select all from java.lang.Object where " + predicate; |
408 | |
TransactionImpl tx = getTransaction(); |
409 | |
if (tx == null) throw new QueryInvalidException("Need running transaction to do query"); |
410 | |
|
411 | |
OQLQuery predicateQuery = tx.getImplementation().newOQLQuery(); |
412 | |
predicateQuery.create(oql); |
413 | |
Query pQ = ((OQLQueryImpl) predicateQuery).getQuery(); |
414 | |
Criteria pCrit = pQ.getCriteria(); |
415 | |
|
416 | |
PBCapsule handle = new PBCapsule(pbKey, tx); |
417 | |
DList result; |
418 | |
try |
419 | |
{ |
420 | |
PersistenceBroker broker = handle.getBroker(); |
421 | |
Criteria allElementsCriteria = this.getPkCriteriaForAllElements(broker); |
422 | |
|
423 | |
allElementsCriteria.addAndCriteria(pCrit); |
424 | |
|
425 | |
Class clazz = null; |
426 | |
try |
427 | |
{ |
428 | |
clazz = this.getElementsExtentClass(broker); |
429 | |
} |
430 | |
catch (PersistenceBrokerException e) |
431 | |
{ |
432 | |
getLog().error(e); |
433 | |
throw new ODMGRuntimeException(e.getMessage()); |
434 | |
} |
435 | |
Query q = new QueryByCriteria(clazz, allElementsCriteria); |
436 | |
if (getLog().isDebugEnabled()) getLog().debug(q.toString()); |
437 | |
|
438 | |
result = null; |
439 | |
try |
440 | |
{ |
441 | |
result = (DList) broker.getCollectionByQuery(DListImpl.class, q); |
442 | |
} |
443 | |
catch (PersistenceBrokerException e) |
444 | |
{ |
445 | |
getLog().error("Query failed", e); |
446 | |
throw new OJBRuntimeException(e); |
447 | |
} |
448 | |
} |
449 | |
finally |
450 | |
{ |
451 | |
|
452 | |
if (handle != null) handle.destroy(); |
453 | |
} |
454 | |
|
455 | |
|
456 | |
return result; |
457 | |
|
458 | |
} |
459 | |
|
460 | |
public int hashCode() |
461 | |
{ |
462 | |
int hashCode = 1; |
463 | |
Iterator it = elements.iterator(); |
464 | |
while (it.hasNext()) |
465 | |
{ |
466 | |
Object obj = it.next(); |
467 | |
hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode()); |
468 | |
} |
469 | |
return hashCode; |
470 | |
} |
471 | |
|
472 | |
public String toString() |
473 | |
{ |
474 | |
ToStringBuilder buf = new ToStringBuilder(this); |
475 | |
buf.append("id", id); |
476 | |
buf.append("pbKey", pbKey); |
477 | |
buf.append("[containing elements: "); |
478 | |
Iterator it = elements.iterator(); |
479 | |
while (it.hasNext()) |
480 | |
{ |
481 | |
Object obj = it.next(); |
482 | |
buf.append(obj != null ? obj.toString() : null); |
483 | |
} |
484 | |
buf.append("]"); |
485 | |
return buf.toString(); |
486 | |
} |
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
|
493 | |
|
494 | |
|
495 | |
public Iterator select(String predicate) throws org.odmg.QueryInvalidException |
496 | |
{ |
497 | |
return this.query(predicate).iterator(); |
498 | |
} |
499 | |
|
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
|
508 | |
public Object selectElement(String predicate) throws org.odmg.QueryInvalidException |
509 | |
{ |
510 | |
return ((DList) this.query(predicate)).get(0); |
511 | |
} |
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
|
520 | |
public int size() |
521 | |
{ |
522 | |
return elements.size(); |
523 | |
} |
524 | |
|
525 | |
|
526 | |
|
527 | |
|
528 | |
|
529 | |
|
530 | |
public void ojbAdd(Object anObject) |
531 | |
{ |
532 | |
DListEntry entry = prepareEntry(anObject); |
533 | |
entry.setPosition(elements.size()); |
534 | |
elements.add(entry); |
535 | |
} |
536 | |
|
537 | |
|
538 | |
|
539 | |
|
540 | |
|
541 | |
public void ojbAddAll(ManageableCollection otherCollection) |
542 | |
{ |
543 | |
|
544 | |
|
545 | |
Iterator it = otherCollection.ojbIterator(); |
546 | |
while (it.hasNext()) |
547 | |
{ |
548 | |
ojbAdd(it.next()); |
549 | |
} |
550 | |
} |
551 | |
|
552 | |
public void afterStore(PersistenceBroker broker) throws PersistenceBrokerException |
553 | |
{ |
554 | |
} |
555 | |
|
556 | |
|
557 | |
|
558 | |
|
559 | |
|
560 | |
public Iterator ojbIterator() |
561 | |
{ |
562 | |
return this.iterator(); |
563 | |
} |
564 | |
|
565 | |
|
566 | |
|
567 | |
|
568 | |
|
569 | |
public void resize(int newSize) |
570 | |
{ |
571 | |
} |
572 | |
|
573 | |
|
574 | |
|
575 | |
|
576 | |
|
577 | |
public void setElements(List elements) |
578 | |
{ |
579 | |
this.elements = elements; |
580 | |
} |
581 | |
|
582 | |
|
583 | |
|
584 | |
|
585 | |
|
586 | |
public void setId(Integer id) |
587 | |
{ |
588 | |
this.id = id; |
589 | |
} |
590 | |
|
591 | |
|
592 | |
|
593 | |
|
594 | |
|
595 | |
|
596 | |
|
597 | |
|
598 | |
|
599 | |
|
600 | |
|
601 | |
public void beforeInsert(PersistenceBroker broker) throws PersistenceBrokerException |
602 | |
{ |
603 | |
|
604 | |
|
605 | |
|
606 | |
|
607 | |
|
608 | |
|
609 | |
|
610 | |
} |
611 | |
|
612 | |
|
613 | |
|
614 | |
|
615 | |
public void beforeUpdate(PersistenceBroker broker) throws PersistenceBrokerException |
616 | |
{ |
617 | |
} |
618 | |
|
619 | |
|
620 | |
|
621 | |
|
622 | |
public void beforeDelete(PersistenceBroker broker) throws PersistenceBrokerException |
623 | |
{ |
624 | |
} |
625 | |
|
626 | |
|
627 | |
|
628 | |
|
629 | |
public void afterUpdate(PersistenceBroker broker) throws PersistenceBrokerException |
630 | |
{ |
631 | |
} |
632 | |
|
633 | |
|
634 | |
|
635 | |
|
636 | |
public void afterInsert(PersistenceBroker broker) throws PersistenceBrokerException |
637 | |
{ |
638 | |
} |
639 | |
|
640 | |
|
641 | |
|
642 | |
|
643 | |
public void afterDelete(PersistenceBroker broker) throws PersistenceBrokerException |
644 | |
{ |
645 | |
} |
646 | |
|
647 | |
|
648 | |
|
649 | |
|
650 | |
public void afterLookup(PersistenceBroker broker) throws PersistenceBrokerException |
651 | |
{ |
652 | |
} |
653 | |
} |