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 package org.melati.poem.odmg;
45
46 import java.util.Iterator;
47 import java.util.Collection;
48 import java.util.Enumeration;
49
50 import org.melati.poem.Persistent;
51 import org.melati.poem.Table;
52
53 /**
54 * Wrapper class to present a Poem Table as a Collection.
55 */
56
57 class PoemTableAsDCollection implements org.odmg.DCollection {
58
59 private Table _wrappedTable = null;
60
61 PoemTableAsDCollection(Table aTable) {
62 _wrappedTable = aTable;
63 }
64
65 public boolean isEmpty() { return size() == 0; }
66 public int size() { return _wrappedTable.count(""); }
67
68 public boolean add(Object obj) {
69 _wrappedTable.create((Persistent)obj);
70 return true;
71 }
72
73 public boolean removeAll(Collection coll) {
74 Iterator iter = coll.iterator();
75 while (iter.hasNext()) {
76 if (!remove(iter.next()))
77 return false;
78 }
79 return true;
80 }
81
82 /**
83 * removes the specified object from the collection.
84 *
85 * WARNING - this removes and commits it immediately!
86 * WARNING2 - it also deletes entries from tables that reference this object
87 * - this means you CANNOT have circular references
88 */
89 public boolean remove(Object obj) {
90 Persistent p = (Persistent)obj;
91
92 Enumeration refs = _wrappedTable.getDatabase().referencesTo(p);
93 while (refs.hasMoreElements()) {
94 Persistent q = (Persistent)refs.nextElement();
95 q.deleteAndCommit();
96 }
97
98 p.deleteAndCommit();
99 return true;
100 }
101
102 public Iterator iterator() {
103 return new EnumerationIterator(_wrappedTable.selection());
104 }
105
106 /**
107 * returns all objects that match the query.
108 * NOTE: The query string is split into the where-clause and
109 * the order-by-clause and passed to poem.
110 */
111 public Iterator select(String queryString) {
112 String lowerCaseQueryString = queryString.toLowerCase();
113 int whereStart = lowerCaseQueryString.indexOf("where ");
114 if (whereStart<0)
115 whereStart = 0;
116 else
117 whereStart += 6;
118
119 int orderByStart = lowerCaseQueryString.indexOf("order by ");
120 int whereEnd = 0;
121 if (orderByStart<0)
122 whereEnd = queryString.length();
123 else if (orderByStart==0) {
124 whereStart = -1;
125 orderByStart += 9;
126 }
127 else {
128 whereEnd = orderByStart;
129 orderByStart += 9;
130 }
131
132 String whereClause = "";
133 String orderByClause = "";
134 if (whereStart>=0)
135 whereClause = queryString.substring(whereStart,whereEnd);
136 if (orderByStart>=0)
137 orderByClause = queryString.substring(orderByStart,queryString.length());
138
139
140
141
142
143
144
145
146
147 return new EnumerationIterator(
148 _wrappedTable.selection(whereClause,orderByClause,true));
149 }
150
151 public Object selectElement(String queryString) {
152 Iterator iter = select(queryString);
153 if (iter.hasNext())
154 return iter.next();
155 return null;
156 }
157
158
159 public org.odmg.DCollection query(String queryString) {
160 throw new org.odmg.NotImplementedException();
161 }
162 public Object[] toArray(Object[] obj) {
163 throw new org.odmg.NotImplementedException();
164 }
165 public Object[] toArray() {
166 throw new org.odmg.NotImplementedException();
167 }
168 public boolean existsElement(String obj) {
169 throw new org.odmg.NotImplementedException();
170 }
171 public boolean contains(Object obj) {
172 throw new org.odmg.NotImplementedException();
173 }
174 public boolean addAll(Collection coll) {
175 throw new org.odmg.NotImplementedException();
176 }
177 public boolean containsAll(Collection coll) {
178 throw new org.odmg.NotImplementedException();
179 }
180 public boolean retainAll(Collection coll) {
181 throw new org.odmg.NotImplementedException();
182 }
183 public void clear() {
184 throw new org.odmg.NotImplementedException();
185 }
186
187 /** utility class for converting enumerations into iterators */
188 private class EnumerationIterator implements Iterator {
189 private Enumeration _enum = null;
190 EnumerationIterator(Enumeration en) {
191 _enum = en;
192 }
193
194 public boolean hasNext() { return _enum.hasMoreElements(); }
195 public Object next() { return _enum.nextElement(); }
196 public void remove() { throw new org.odmg.NotImplementedException(); }
197 }
198
199
200 }