View Javadoc

1   /*
2    * $Source: /usr/cvsroot/melati/melati-example-odmg/src/main/java/org/melati/poem/odmg/PoemTableAsDCollection.java,v $
3    * $Revision: 1.14 $
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 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      // delete all references first
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; // no where clause
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     System.err.println("[poem-odmg]query string="+queryString);
141     System.err.println("[poem-odmg]where start="+whereStart);
142     System.err.println("[poem-odmg]where end="+whereEnd);
143     System.err.println("[poem-odmg]order by start="+orderByStart);
144     System.err.println("[poem-odmg]where clause="+whereClause);
145     System.err.println("[poem-odmg]order by clause="+orderByClause);
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 // the following have not been implemented - yet...
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 }