001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2004 Nigel Westbury <westbury@users.sourceforge.net>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022:
023: package net.sf.jmoney.jdbcdatastore;
024:
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027: import java.sql.Statement;
028: import java.util.Collection;
029: import java.util.Iterator;
030:
031: import net.sf.jmoney.model2.Entry;
032: import net.sf.jmoney.model2.EntryInfo;
033:
034: /**
035: * This class is used to get the list of entries in a given account. Entries are
036: * primarily listed under the transaction in which they belong. However, we want
037: * a quick way to find all the entries in a given account.
038: * <P>
039: * This class provides the list by submitting a query to the database. A
040: * suitable index should be created in the database.
041: *
042: * @author Nigel Westbury
043: */
044: public class AccountEntriesList implements Collection<Entry> {
045: private SessionManager sessionManager;
046: private IDatabaseRowKey keyOfRequiredPropertyValue;
047: private String tableName;
048: private String columnName;
049:
050: public AccountEntriesList(SessionManager sessionManager,
051: IDatabaseRowKey keyOfRequiredPropertyValue) {
052: this .sessionManager = sessionManager;
053: this .keyOfRequiredPropertyValue = keyOfRequiredPropertyValue;
054:
055: tableName = EntryInfo.getPropertySet().getId()
056: .replace('.', '_');
057: columnName = EntryInfo.getAccountAccessor().getLocalName();
058: }
059:
060: public int size() {
061: try {
062: String sql = "SELECT COUNT(*) FROM " + tableName
063: + " WHERE \"" + columnName + "\" = "
064: + keyOfRequiredPropertyValue.getRowId();
065: System.out.println(sql);
066: ResultSet resultSet = sessionManager.getReusableStatement()
067: .executeQuery(sql);
068: resultSet.next();
069: int size = resultSet.getInt(1);
070: resultSet.close();
071: return size;
072: } catch (SQLException e) {
073: e.printStackTrace();
074: throw new RuntimeException("SQL Exception: "
075: + e.getMessage());
076: }
077: }
078:
079: public boolean isEmpty() {
080: try {
081: String sql = "SELECT * FROM " + tableName + " WHERE \""
082: + columnName + "\" = "
083: + keyOfRequiredPropertyValue.getRowId();
084: System.out.println(sql);
085: ResultSet resultSet = sessionManager.getReusableStatement()
086: .executeQuery(sql);
087: boolean hasNext = resultSet.next();
088: return !hasNext;
089: } catch (SQLException e) {
090: e.printStackTrace();
091: throw new RuntimeException("SQL Exception: "
092: + e.getMessage());
093: }
094: }
095:
096: public boolean contains(Object arg0) {
097: throw new RuntimeException("method not implemented");
098: }
099:
100: public Iterator<Entry> iterator() {
101: try {
102: // TODO: This code will not work if the index is indexing
103: // objects of a derivable property set. Table joins would
104: // be required in such a situation.
105: Statement stmt = sessionManager.getConnection()
106: .createStatement();
107: String sql = "SELECT * FROM " + tableName + " WHERE \""
108: + columnName + "\" = "
109: + keyOfRequiredPropertyValue.getRowId();
110: System.out.println(sql);
111: ResultSet resultSet = stmt.executeQuery(sql);
112: return new UncachedObjectIterator<Entry>(resultSet,
113: EntryInfo.getPropertySet(), null, sessionManager);
114: } catch (SQLException e) {
115: e.printStackTrace();
116: throw new RuntimeException("internal error");
117: }
118: }
119:
120: public Object[] toArray() {
121: throw new RuntimeException("method not implemented");
122: }
123:
124: public <T> T[] toArray(T[] arg0) {
125: throw new RuntimeException("method not implemented");
126: }
127:
128: public boolean add(Entry arg0) {
129: // The list is not cached.
130: // We read from the database every time.
131: // There is therefore nothing to do here.
132: return true;
133: }
134:
135: public boolean remove(Object arg0) {
136: // TODO Auto-generated method stub
137: return false;
138: }
139:
140: public boolean containsAll(Collection<?> arg0) {
141: throw new RuntimeException("method not implemented");
142: }
143:
144: public boolean addAll(Collection<? extends Entry> arg0) {
145: throw new RuntimeException("method not implemented");
146: }
147:
148: public boolean removeAll(Collection<?> arg0) {
149: throw new RuntimeException("method not implemented");
150: }
151:
152: public boolean retainAll(Collection<?> arg0) {
153: throw new RuntimeException("method not implemented");
154: }
155:
156: public void clear() {
157: throw new RuntimeException("method not implemented");
158: }
159: }
|