View Javadoc
1   /*
2    * $Source$
3    * $Revision$
4    *
5    * Copyright (C) 2000 Chris Kimpton
6    * 
7    * Part of Melati (http://melati.org), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati (http://melati.org),
30   * if you would like to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     Chris Kimpton (kimtoc@techie.com)
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<P extends Persistent> implements org.odmg.DCollection {
58  
59    private Table<P> _wrappedTable = null;
60  
61    PoemTableAsDCollection(Table<P> aTable) {
62      _wrappedTable = aTable;
63    }
64  
65    public boolean isEmpty() { return size() == 0; }
66    public int size() { return _wrappedTable.count(); }
67  
68    @SuppressWarnings("unchecked")
69    public boolean add(Object obj) { 
70      _wrappedTable.create((P)obj);
71      return true;
72    }
73  
74    public boolean removeAll(@SuppressWarnings("rawtypes") Collection coll) {  
75      Iterator<?> iter = coll.iterator();
76      while (iter.hasNext()) {
77         if (!remove(iter.next()))
78           return false;
79      }
80      return true;
81    }
82  
83   /** 
84    * Removes the specified object from the collection.
85    *
86    * WARNING - this removes and commits it immediately!
87    * WARNING2 - it also deletes entries from tables that reference this object 
88    * - this means you CANNOT have circular references
89    */
90    public boolean remove(Object obj) {  
91      @SuppressWarnings("unchecked")
92      P p = (P)obj;
93      // delete all references first
94      Enumeration<Persistent> refs = _wrappedTable.getDatabase().referencesTo(p);
95      while (refs.hasMoreElements()) {
96        Persistent q = (Persistent)refs.nextElement();
97        q.deleteAndCommit();
98      }
99  
100     p.deleteAndCommit();
101     return true;
102   }
103 
104   public Iterator<P> iterator() { 
105     return new EnumerationIterator<P>(_wrappedTable.selection());
106   }
107 
108  /** 
109   * returns all objects that match the query.
110   * NOTE: The query string is split into the where-clause and 
111   * the order-by-clause and passed to poem.
112   */
113   public Iterator<P> select(String queryString) {
114     String lowerCaseQueryString = queryString.toLowerCase();
115     int whereStart = lowerCaseQueryString.indexOf("where ");
116     if (whereStart<0) 
117       whereStart = 0;
118     else
119       whereStart += 6;
120 
121     int orderByStart = lowerCaseQueryString.indexOf("order by ");
122     int whereEnd = 0;
123     if (orderByStart<0)
124       whereEnd = queryString.length();
125     else if (orderByStart==0) {
126       whereStart = -1; // no where clause
127       orderByStart += 9;
128     }
129     else {
130       whereEnd = orderByStart;
131       orderByStart += 9;
132     }
133     
134     String whereClause = "";
135     String orderByClause = "";
136     if (whereStart>=0)
137       whereClause = queryString.substring(whereStart,whereEnd);
138     if (orderByStart>=0)
139       orderByClause = queryString.substring(orderByStart,queryString.length());
140 
141 /*
142     System.err.println("[poem-odmg]query string="+queryString);
143     System.err.println("[poem-odmg]where start="+whereStart);
144     System.err.println("[poem-odmg]where end="+whereEnd);
145     System.err.println("[poem-odmg]order by start="+orderByStart);
146     System.err.println("[poem-odmg]where clause="+whereClause);
147     System.err.println("[poem-odmg]order by clause="+orderByClause);
148 */
149     return new EnumerationIterator<P>(
150         _wrappedTable.selection(whereClause,orderByClause,true));
151   }
152 
153   public Object selectElement(String queryString) {
154     Iterator<P> iter = select(queryString);
155     if (iter.hasNext())
156       return iter.next();
157     return null;
158   }
159 
160 // the following have not been implemented - yet...
161   public org.odmg.DCollection query(String queryString) { 
162     throw new org.odmg.NotImplementedException(); 
163   }
164   public Object[] toArray(Object[] obj) { 
165     throw new org.odmg.NotImplementedException(); 
166   }
167   public Object[] toArray() { 
168     throw new org.odmg.NotImplementedException(); 
169   }
170   public boolean existsElement(String obj) { 
171     throw new org.odmg.NotImplementedException(); 
172   }
173   public boolean contains(Object obj) { 
174     throw new org.odmg.NotImplementedException(); 
175   }
176   public boolean addAll(@SuppressWarnings("rawtypes") Collection coll) { 
177     throw new org.odmg.NotImplementedException(); 
178   }
179   public boolean containsAll(@SuppressWarnings("rawtypes") Collection coll) { 
180     throw new org.odmg.NotImplementedException(); 
181   }
182 
183   public boolean retainAll(@SuppressWarnings("rawtypes") Collection coll) { 
184     throw new org.odmg.NotImplementedException(); 
185   }
186   public void clear() { 
187     throw new org.odmg.NotImplementedException(); 
188   }
189 
190 /** Utility class for converting enumerations into iterators 
191  * @param <T>*/
192 private class EnumerationIterator<T> implements Iterator<T> {
193   private Enumeration<T> _enum = null;
194   EnumerationIterator(Enumeration<T> en) {
195     _enum = en;
196   }
197 
198   public boolean hasNext() { return _enum.hasMoreElements(); }
199   public T next() { return _enum.nextElement(); }
200   public void remove() { throw new org.odmg.NotImplementedException(); }
201 }
202 
203 
204 }