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 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 License for more details.
013:
014: You should have received a copy of the GNU General Public 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.file;
022:
023: import java.io.File;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.Iterator;
027:
028: import net.sf.borg.common.Errmsg;
029: import net.sf.borg.common.Resource;
030: import net.sf.borg.common.XTree;
031: import net.sf.borg.model.db.AppointmentDB;
032: import net.sf.borg.model.db.file.mdb.MDB;
033: import net.sf.borg.model.db.file.mdb.SMDB;
034: import net.sf.borg.model.db.file.mdb.Schema;
035:
036: class ApptFileDB extends FileDBCreator implements AppointmentDB {
037: ApptFileDB() {
038: }
039:
040: // AppointmentKeyFilter overrides
041: public final Collection getTodoKeys() throws Exception {
042: return getKeys(F_TODO);
043: }
044:
045: public final Collection getRepeatKeys() throws Exception {
046: return getKeys(F_RPT);
047: }
048:
049: // FileDBCreator overrides
050: final void init(String file, boolean shared) throws Exception {
051: file = file + "/borg.jdb";
052:
053: // here is the schema for an appt with the field names and data types
054: // SMDB will use this to manage each DB Appointment.
055: // the boolean appt flags are shown near the top of the class
056: Schema sch = new Schema();
057: java.net.URL schurl = getClass().getResource(
058: "/resource/borg_schema.xml");
059: XTree sch_xml = XTree.readFromURL(schurl);
060: sch.setFromXML(sch_xml);
061:
062: // create the database if it does not exist
063: boolean newdb = false;
064: File fp = new File(file);
065: if (!fp.exists()) {
066: Errmsg.notice(Resource
067: .getResourceString("Creating_DB_file:_")
068: + file);
069: SMDB.create("Borg Database", file, 60, sch);
070: newdb = true;
071: }
072:
073: AppointmentAdapter adapter = new AppointmentAdapter();
074:
075: db_ = new FileBeanDB(file, MDB.READ_WRITE, adapter, shared);
076:
077: FileBeanDB fdb = (FileBeanDB) db_;
078: if (newdb) {
079: fdb.setNormalize(true);
080: }
081: Schema oldsch = fdb.getSchema();
082: try {
083: oldsch.getType("REM");
084: } catch (Exception e) {
085: // DB does not have new category field - update the schema
086: fdb.setSchema(sch);
087: }
088: }
089:
090: // private //
091: private static final int F_RPT = 0x01;
092:
093: private static final int F_TODO = 0x02;
094:
095: private Collection getKeys(int mask) throws Exception {
096: ArrayList l = new ArrayList();
097: FileBeanDB fdb = (FileBeanDB) db_;
098: Collection keycol = fdb.getKeys();
099: Iterator keyiter = keycol.iterator();
100:
101: while (keyiter.hasNext()) {
102: Integer ki = (Integer) keyiter.next();
103: int key = ki.intValue();
104: int flags = fdb.getFlags(key);
105: if ((flags & mask) == 0)
106: continue;
107: l.add(ki);
108: }
109:
110: return l;
111: }
112: }
|