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: package net.sf.borg.model;
021:
022: import java.io.Writer;
023: import java.text.SimpleDateFormat;
024: import java.util.Collection;
025: import java.util.Date;
026: import java.util.Iterator;
027:
028: import net.sf.borg.common.Errmsg;
029: import net.sf.borg.common.PrefName;
030: import net.sf.borg.common.Prefs;
031: import net.sf.borg.common.Resource;
032: import net.sf.borg.common.Warning;
033: import net.sf.borg.common.XTree;
034: import net.sf.borg.model.beans.Memo;
035: import net.sf.borg.model.beans.MemoXMLAdapter;
036: import net.sf.borg.model.db.BeanDataFactoryFactory;
037: import net.sf.borg.model.db.IBeanDataFactory;
038: import net.sf.borg.model.db.MemoDB;
039:
040: public class MemoModel extends Model {
041:
042: private static SimpleDateFormat normalDateFormat_ = new SimpleDateFormat(
043: "MM/dd/yyyy hh:mm aa");
044:
045: private MemoDB db_; // the database
046:
047: static private MemoModel self_ = null;
048:
049: public static MemoModel getReference() {
050: return (self_);
051: }
052:
053: public static MemoModel create() {
054: self_ = new MemoModel();
055: return (self_);
056: }
057:
058: public void remove() {
059: removeListeners();
060: try {
061: if (db_ != null)
062: db_.close();
063: } catch (Exception e) {
064: Errmsg.errmsg(e);
065: return;
066: }
067: db_ = null;
068: }
069:
070: public MemoDB getDB() {
071: return db_;
072: }
073:
074: public boolean hasMemos() {
075: if (db_ != null)
076: return true;
077: return false;
078: }
079:
080: public Collection getMemos() throws Exception {
081: Collection memos = db_.readAll();
082: Iterator it = memos.iterator();
083: while (it.hasNext()) {
084: Memo memo = (Memo) it.next();
085: if (memo.getDeleted())
086: it.remove();
087: parseOutDates(memo);
088: }
089: return memos;
090: }
091:
092: public Collection getNames() throws Exception {
093:
094: return db_.getNames();
095: }
096:
097: public Collection getDeletedMemos() throws Exception {
098: Collection memos = db_.readAll();
099: Iterator it = memos.iterator();
100: while (it.hasNext()) {
101: Memo memo = (Memo) it.next();
102: if (!memo.getDeleted())
103: it.remove();
104: parseOutDates(memo);
105: }
106: return memos;
107: }
108:
109: public void open_db(String url, String username, boolean shared)
110: throws Exception {
111:
112: StringBuffer tmp = new StringBuffer(url);
113: IBeanDataFactory factory = BeanDataFactoryFactory.getInstance()
114: .getFactory(tmp, shared);
115: url = tmp.toString();
116: // let the factory tweak dbdir
117:
118: db_ = factory.createMemoDB(url, username);
119: if (db_ == null)
120: throw new Warning(Resource
121: .getPlainResourceString("MemosNotSupported"));
122: }
123:
124: public void delete(String name, boolean refresh) throws Exception {
125:
126: try {
127: Memo m = getMemo(name);
128: if (m == null)
129: return;
130: String sync = Prefs.getPref(PrefName.PALM_SYNC);
131: if (sync.equals("true")) {
132: m.setDeleted(true);
133: db_.updateMemo(m);
134: } else {
135: db_.delete(m.getMemoName());
136: }
137: } catch (Exception e) {
138: Errmsg.errmsg(e);
139: }
140:
141: if (refresh)
142: refresh();
143: }
144:
145: public void forceDelete(Memo memo) throws Exception {
146:
147: try {
148:
149: db_.delete(memo.getMemoName());
150: } catch (Exception e) {
151: Errmsg.errmsg(e);
152: }
153:
154: refresh();
155: }
156:
157: public void saveMemo(Memo memo) throws Exception {
158: saveMemo(memo, false);
159: }
160:
161: public void saveMemo(Memo memo, boolean sync) throws Exception {
162:
163: // determine dates
164: Date now = new Date();
165: if (memo.getCreated() == null)
166: memo.setCreated(now);
167: memo.setUpdated(now);
168:
169: addDateString(memo);
170: String name = memo.getMemoName();
171: Memo old = db_.readMemo(name);
172: if (old == null) {
173: if (!sync) {
174: memo.setNew(true);
175: memo.setDeleted(false);
176: memo.setModified(false);
177: }
178:
179: db_.addMemo(memo);
180:
181: } else {
182:
183: if (!sync) {
184: memo.setModified(true);
185: memo.setDeleted(false);
186: }
187: db_.updateMemo(memo);
188:
189: }
190:
191: // inform views of data change
192: refresh();
193: }
194:
195: static private void addDateString(Memo m) {
196: if (m.getCreated() == null || m.getUpdated() == null)
197: return;
198: if (m.getMemoText() == null)
199: m.setMemoText("");
200: m.setMemoText("TS;" + normalDateFormat_.format(m.getCreated())
201: + ";" + normalDateFormat_.format(m.getUpdated()) + ";"
202: + m.getMemoText());
203:
204: }
205:
206: static private void parseOutDates(Memo m) {
207:
208: // separate timestamps if needed TS:created;updated;
209: String text = m.getMemoText();
210: if (text == null)
211: return;
212: if (text != null && text.startsWith("TS;")) {
213: int idx1 = 2;
214: int idx2 = text.indexOf(';', idx1 + 1);
215: int idx3 = text.indexOf(';', idx2 + 1);
216: if (idx2 != -1 && idx3 != -1) {
217: try {
218: Date create = normalDateFormat_.parse(text
219: .substring(idx1 + 1, idx2));
220: Date update = normalDateFormat_.parse(text
221: .substring(idx2 + 1, idx3));
222: if (create != null)
223: m.setCreated(create);
224: if (update != null)
225: m.setUpdated(update);
226: m.setMemoText(text.substring(idx3 + 1));
227: } catch (Exception e) {
228:
229: }
230: }
231: }
232: }
233:
234: public Memo getMemo(String name) throws Exception {
235: Memo m = db_.readMemo(name);
236:
237: if (m == null)
238: return null;
239: if (m.getDeleted() == true)
240: return null;
241: parseOutDates(m);
242:
243: return m;
244: }
245:
246: public void export(Writer fw) throws Exception {
247:
248: // FileWriter fw = new FileWriter(fname);
249: fw.write("<MEMOS>\n");
250: MemoXMLAdapter ta = new MemoXMLAdapter();
251:
252: // export Memoes
253:
254: Collection memos = getMemos();
255: Iterator ti = memos.iterator();
256: while (ti.hasNext()) {
257: Memo memo = (Memo) ti.next();
258:
259: XTree xt = ta.toXml(memo);
260: fw.write(xt.toString());
261: }
262:
263: fw.write("</MEMOS>");
264:
265: }
266:
267: public void importXml(XTree xt) throws Exception {
268:
269: MemoXMLAdapter aa = new MemoXMLAdapter();
270:
271: for (int i = 1;; i++) {
272: XTree ch = xt.child(i);
273: if (ch == null)
274: break;
275:
276: if (!ch.name().equals("Memo"))
277: continue;
278: Memo memo = (Memo) aa.fromXml(ch);
279: memo.setKey(-1);
280: saveMemo(memo);
281: }
282:
283: refresh();
284: }
285:
286: public void refresh() {
287: refreshListeners();
288: }
289:
290: public void sync() {
291: refresh();
292: }
293:
294: public void close_db() throws Exception {
295: db_.close();
296: }
297:
298: public Memo getMemoByPalmId(int id) throws Exception {
299: Memo m = db_.getMemoByPalmId(id);
300: if (m != null)
301: parseOutDates(m);
302: return m;
303: }
304: }
|