001: /*
002: This file is part of BORG.
003:
004: BORG is free software; you can redistribute it and/or modify
005: it under the terms of the GNU General public final synchronized License as published by
006: the Free Software Foundation; either version 2 of the License, or
007: (at your option) any later version.
008:
009: BORG is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General public final synchronized License for more details.
013:
014: You should have received a copy of the GNU General public final synchronized License
015: along with BORG; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: Copyright 2003 by Mike Berger
019: */
020:
021: package net.sf.borg.model.db.remote;
022:
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028:
029: import net.sf.borg.model.beans.Appointment;
030: import net.sf.borg.model.beans.KeyedBean;
031: import net.sf.borg.model.db.AppointmentDB;
032: import net.sf.borg.model.db.BeanDB;
033: import net.sf.borg.model.db.MultiUserDB;
034:
035: public class ApptCachingBeanDB extends CachingBeanDB implements
036: AppointmentDB, MultiUserDB {
037: public ApptCachingBeanDB(BeanDB delegate) {
038: super (delegate);
039: }
040:
041: // BeanDB overrides
042: public final synchronized void addObj(KeyedBean bean, boolean crypt)
043: throws Exception {
044: super .addObj(bean, crypt);
045: rebuildKeys();
046: }
047:
048: public final synchronized void updateObj(KeyedBean bean,
049: boolean crypt) throws Exception {
050: super .updateObj(bean, crypt);
051: rebuildKeys();
052: }
053:
054: public final synchronized void delete(int key) throws Exception {
055: super .delete(key);
056: rebuildKeys();
057: }
058:
059: // AppointmentKeyFilter overrides
060: public Collection getTodoKeys() throws Exception {
061: refresh();
062: return todoKeys;
063: }
064:
065: public Collection getRepeatKeys() throws Exception {
066: refresh();
067: return repeatKeys;
068: }
069:
070: // protected //
071: protected boolean refresh() throws Exception {
072: if (inRefresh)
073: return false;
074:
075: try {
076: inRefresh = true;
077:
078: if (!super .refresh())
079: return false;
080:
081: rebuildKeys();
082:
083: return true;
084: } finally {
085: inRefresh = false;
086: }
087: }
088:
089: // private //
090: private boolean inRefresh;
091: private List todoKeys = new ArrayList();
092: private List repeatKeys = new ArrayList();
093:
094: private void rebuildKeys() throws Exception {
095: // Refresh our cached ToDo and Repeat keys.
096: todoKeys.clear();
097: repeatKeys.clear();
098: Iterator itr = getObjectMap().entrySet().iterator();
099: while (itr.hasNext()) {
100: Map.Entry entry = (Map.Entry) itr.next();
101: Appointment appt = (Appointment) entry.getValue();
102: if (appt.getTodo())
103: todoKeys.add(entry.getKey());
104: if (appt.getRepeatFlag())
105: repeatKeys.add(entry.getKey());
106: }
107: }
108:
109: public Collection getAllUsers() throws Exception {
110: if (delegate instanceof MultiUserDB) {
111: return (((MultiUserDB) delegate).getAllUsers());
112: }
113: return null;
114: }
115: }
|