01: package org.drools.repository;
02:
03: import java.util.Iterator;
04: import java.util.NoSuchElementException;
05:
06: import javax.jcr.Node;
07: import javax.jcr.NodeIterator;
08: import javax.jcr.RepositoryException;
09:
10: /**
11: * This wraps a node iterator, and provides PackageItems when requested. This
12: * supports lazy loading if needed.
13: */
14: public class PackageIterator implements Iterator {
15:
16: private final NodeIterator packageNodeIterator;
17: private final RulesRepository repository;
18: private boolean searchArchived = false;
19: private Node current = null;
20: private Node next = null;
21:
22: public PackageIterator(RulesRepository repository,
23: NodeIterator packageNodes) {
24: this .packageNodeIterator = packageNodes;
25: this .repository = repository;
26: }
27:
28: public boolean hasNext() {
29: boolean hasnext = false;
30: if (this .next == null) {
31: while (this .packageNodeIterator.hasNext()) {
32: Node element = (Node) this .packageNodeIterator.next();
33: try {
34: if (searchArchived
35: || !element.getProperty("drools:archive")
36: .getBoolean()) {
37: hasnext = true;
38: this .next = element;
39: break;
40: }
41: } catch (RepositoryException e) {
42: e.printStackTrace();
43: }
44: }
45: } else {
46: hasnext = true;
47: }
48: return hasnext;
49: }
50:
51: public Object next() {
52: if (this .next == null) {
53: this .hasNext();
54: }
55:
56: this .current = this .next;
57: this .next = null;
58:
59: if (this .current == null) {
60: throw new NoSuchElementException(
61: "No more elements to return");
62: }
63:
64: return new PackageItem(this .repository, (Node) this .current);
65:
66: }
67:
68: public void setArchivedIterator(boolean search) {
69: this .searchArchived = search;
70: }
71:
72: public boolean isSetArchivedSearch() {
73: return this .searchArchived;
74: }
75:
76: public void remove() {
77: throw new UnsupportedOperationException(
78: "You can not remove items this way.");
79: }
80:
81: }
|