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 2006 by Michael Berger
019: */
020:
021: package net.sf.borg.model.db.remote.server;
022:
023: import net.sf.borg.common.J13Helper;
024: import net.sf.borg.common.XTree;
025: import net.sf.borg.model.AddressModel;
026: import net.sf.borg.model.AppointmentModel;
027: import net.sf.borg.model.BorgOption;
028: import net.sf.borg.model.MemoModel;
029: import net.sf.borg.model.TaskModel;
030: import net.sf.borg.model.beans.Address;
031: import net.sf.borg.model.beans.Appointment;
032: import net.sf.borg.model.beans.KeyedBean;
033: import net.sf.borg.model.beans.Memo;
034: import net.sf.borg.model.beans.Task;
035: import net.sf.borg.model.db.AppointmentDB;
036: import net.sf.borg.model.db.BeanDB;
037: import net.sf.borg.model.db.MemoDB;
038: import net.sf.borg.model.db.MultiUserDB;
039: import net.sf.borg.model.db.remote.IRemoteProxy;
040: import net.sf.borg.model.db.remote.XmlObjectHelper;
041:
042: /**
043: * The server-side component of the BORG remote invocation framework. This is
044: * designed to be run out of a running BORG instance - not a web server The
045: * databases already open by the application will be used
046: */
047: public class SingleInstanceHandler {
048:
049: public static String execute(String msg) {
050: Object result = null;
051:
052: // System.out.println("[INPUT] "+msg);
053: String strXml = J13Helper.replace(msg, "%NEWLINE%", "\n");
054: try {
055: XTree xmlParms = XTree.readFromBuffer(strXml);
056: IRemoteProxy.Parms parms = (IRemoteProxy.Parms) XmlObjectHelper
057: .fromXml(xmlParms);
058:
059: // Figure out what we need to do
060: // String uid = parms.getUser();
061: String cmd = parms.getCommand();
062: if (parms.getMyClass() == Memo.class) {
063: MemoDB db = MemoModel.getReference().getDB();
064: if (cmd.equals("getNames")) {
065: result = db.getNames();
066: } else if (cmd.equals("readAllMemos")) {
067: result = db.readAll();
068: } else if (cmd.equals("readMemo")) {
069: result = db.readMemo((String) parms.getArgs());
070: } else if (cmd.equals("getMemoByPalmId")) {
071: result = db.getMemoByPalmId(((Integer) parms
072: .getArgs()).intValue());
073: } else if (cmd.equals("addMemo")
074: || cmd.equals("updateMemo")) {
075: IRemoteProxy.ComposedObject agg = (IRemoteProxy.ComposedObject) parms
076: .getArgs();
077: Memo m = (Memo) agg.getO1();
078:
079: if (cmd.equals("addMemo")) {
080: db.addMemo(m);
081: } else {
082: db.updateMemo(m);
083: }
084: } else if (cmd.equals("deleteMemo")) {
085: db.delete((String) parms.getArgs());
086: }
087:
088: } else {
089: BeanDB beanDB = getBeanDB(parms);
090:
091: if (cmd.equals("readAll")) {
092: result = beanDB.readAll();
093: } else if (cmd.equals("readObj")) {
094: int key = ((Integer) parms.getArgs()).intValue();
095: result = beanDB.readObj(key);
096: } else if (cmd.equals("delete")) {
097: int key = ((Integer) parms.getArgs()).intValue();
098: beanDB.delete(key);
099: } else if (cmd.equals("getOption")) {
100: String key = (String) parms.getArgs();
101: result = beanDB.getOption(key);
102: } else if (cmd.equals("getOptions")) {
103: result = beanDB.getOptions();
104: } else if (cmd.equals("getTodoKeys")) {
105: result = ((AppointmentDB) beanDB).getTodoKeys();
106: } else if (cmd.equals("getRepeatKeys")) {
107: result = ((AppointmentDB) beanDB).getRepeatKeys();
108: } else if (cmd.equals("nextkey")) {
109: result = new Integer(beanDB.nextkey());
110: } else if (cmd.equals("isDirty")) {
111: result = new Boolean(beanDB.isDirty());
112: } else if (cmd.equals("setOption")) {
113: BorgOption option = (BorgOption) parms.getArgs();
114: beanDB.setOption(option);
115: } else if (cmd.equals("addObj")
116: || cmd.equals("updateObj")) {
117: IRemoteProxy.ComposedObject agg = (IRemoteProxy.ComposedObject) parms
118: .getArgs();
119: KeyedBean bean = (KeyedBean) agg.getO1();
120: boolean crypt = ((Boolean) agg.getO2())
121: .booleanValue();
122:
123: if (cmd.equals("addObj"))
124: beanDB.addObj(bean, crypt);
125: else
126: beanDB.updateObj(bean, crypt);
127:
128: } else if (cmd.equals("getAllUsers")
129: && beanDB instanceof MultiUserDB) {
130: result = ((MultiUserDB) beanDB).getAllUsers();
131: } else
132: throw new UnsupportedOperationException(cmd);
133: }
134: } catch (Exception e) {
135: e.printStackTrace();
136:
137: }
138:
139: String resultString = XmlObjectHelper.toXml(result).toString();
140: // System.out.println("[OUTPUT] "+resultString);
141: resultString = J13Helper.replace(resultString, "\n",
142: "%NEWLINE%");
143: return resultString;
144: }
145:
146: private static BeanDB getBeanDB(IRemoteProxy.Parms parms)
147: throws Exception {
148:
149: if (parms.getMyClass() == Address.class) {
150: return AddressModel.getReference().getDB();
151: } else if (parms.getMyClass() == Appointment.class) {
152: return AppointmentModel.getReference().getDB();
153: } else if (parms.getMyClass() == Task.class) {
154: return TaskModel.getReference().getDB();
155: } else
156: throw new Exception("Invalid Class: "
157: + parms.getClassString());
158: }
159:
160: }
|