001: package net.sf.borg.apptconduit;
002:
003: import java.io.FileInputStream;
004: import java.io.IOException;
005: import java.util.Properties;
006:
007: import net.sf.borg.common.Errmsg;
008: import net.sf.borg.common.J13Helper;
009: import net.sf.borg.common.SocketClient;
010: import net.sf.borg.model.AppointmentModel;
011: import net.sf.borg.model.db.remote.IRemoteProxy;
012: import net.sf.borg.model.db.remote.IRemoteProxyProvider;
013: import net.sf.borg.model.db.remote.RemoteProxyHome;
014: import net.sf.borg.model.db.remote.socket.SocketProxy;
015: import palm.conduit.Conduit;
016: import palm.conduit.ConfigureConduitInfo;
017: import palm.conduit.Log;
018: import palm.conduit.SyncManager;
019: import palm.conduit.SyncProperties;
020:
021: //"Portions copyright (c) 1996-2002 PalmSource, Inc. or its affiliates. All rights reserved."
022: public class ApptCond implements Conduit {
023:
024: /**
025: * Name of the conduit to be displayed on the dialog
026: */
027: static final String NAME = "BORG Appt Conduit";
028:
029: static private int port;
030:
031: static public void log(String s) {
032: Log.out(s);
033: String s2 = J13Helper.replace(s, "\n", "%NL%");
034: try {
035: SocketClient.sendMsg("localhost", port, "lock:" + s2);
036: } catch (IOException e) {
037:
038: }
039: }
040:
041: public void open(SyncProperties props) {
042:
043: ApptRecordManager recordMgr;
044: TodoRecordManager trecordMgr;
045: AppointmentModel apptModel;
046: //TaskModel taskModel;
047:
048: Errmsg.console(true);
049:
050: // Tell the log we are starting
051: Log.startSync();
052:
053: try {
054: if (props.syncType == SyncProperties.SYNC_DO_NOTHING) {
055:
056: Log.out("OK ApptCond Do Nothing");
057: Log.endSync();
058: } else {
059: // read the pc records on the PC
060: String loc = props.localName;
061: String dbdir = props.pathName;
062: String user = props.userName;
063: Log.out("dbdir=" + dbdir);
064: Log.out("user=" + user);
065: Log.out("localName=" + loc);
066:
067: if (loc.indexOf(':') != -1) {
068: dbdir = loc;
069: }
070:
071: // check for properties file
072: String propfile = dbdir + "/db.properties";
073: try {
074: FileInputStream is = new FileInputStream(propfile);
075: Properties dbprops = new Properties();
076: dbprops.load(is);
077: dbdir = dbprops.getProperty("dburl");
078: user = dbprops.getProperty("user");
079: port = Integer
080: .parseInt(dbprops.getProperty("port"));
081: } catch (Exception e) {
082: Log.out("Properties exception: " + e.toString());
083: }
084:
085: // If we're doing remote stuff, use SocketProxy
086: RemoteProxyHome.getInstance().setProxyProvider(
087: new IRemoteProxyProvider() {
088: public final IRemoteProxy createProxy(
089: String url) {
090: // No synchronization needed - we're
091: // single-threaded.
092: if (proxy == null)
093: proxy = new SocketProxy(url, port);
094: return proxy;
095: }
096:
097: public final Credentials getCredentials() {
098: return new Credentials("$default",
099: "$default");
100: }
101:
102: // private //
103: private IRemoteProxy proxy = null;
104: });
105:
106: // shutdown the app - unless we are using a remote socket
107: // interface
108: if (!dbdir.startsWith("remote:")) {
109: try {
110: SocketClient.sendMsg("localhost", port,
111: "shutdown");
112: } catch (Exception e) {
113: }
114: } else {
115: try {
116: SocketClient
117: .sendMsg("localhost", port,
118: "lock:Appointment HotSync In Progress...Please wait");
119: } catch (Exception e) {
120: }
121: }
122:
123: log("dbdir=" + dbdir);
124: log("user=" + user);
125: apptModel = AppointmentModel.create();
126: apptModel.open_db(dbdir, user, false);
127: /*
128: taskModel = TaskModel.create();
129: try{
130: taskModel.open_db(dbdir, user, false, false);
131: }
132: catch( Exception e)
133: {
134: //ignore exception for subtasks no supported
135: }*/
136: // have to get todo data into BORG, then get appt data, then
137: // sync back
138: // appt data and finally overwrite Todo data.
139: if (props.syncType != SyncProperties.SYNC_DO_NOTHING) {
140: log("Sync Todos from Palm to Borg");
141: int tododb = SyncManager.openDB("ToDoDB", 0,
142: SyncManager.OPEN_READ
143: | SyncManager.OPEN_WRITE
144: | SyncManager.OPEN_EXCLUSIVE);
145: trecordMgr = new net.sf.borg.apptconduit.TodoRecordManager(
146: props, tododb);
147: trecordMgr.SyncData();
148: SyncManager.closeDB(tododb);
149: }
150:
151: int apptdb = SyncManager.openDB("DatebookDB", 0,
152: SyncManager.OPEN_READ | SyncManager.OPEN_WRITE
153: | SyncManager.OPEN_EXCLUSIVE);
154: recordMgr = new net.sf.borg.apptconduit.ApptRecordManager(
155: props, apptdb);
156: int numrecs = SyncManager.getDBRecordCount(apptdb);
157:
158: // send ALL records to HH if wipe option set by user OR is HH db
159: // is empty
160: if (props.syncType == SyncProperties.SYNC_PC_TO_HH
161: || numrecs == 0) {
162: log("Wipe Palm/Sync Appts");
163: recordMgr.WipeData();
164: } else if (props.syncType == SyncProperties.SYNC_HH_TO_PC) {
165: recordMgr.quickSyncAndWipe();
166: } else {
167: log("Sync Appts");
168: recordMgr.SyncData();
169: }
170:
171: SyncManager.closeDB(apptdb);
172:
173: if (props.syncType != SyncProperties.SYNC_DO_NOTHING) {
174: int tododb = SyncManager.openDB("ToDoDB", 0,
175: SyncManager.OPEN_READ
176: | SyncManager.OPEN_WRITE
177: | SyncManager.OPEN_EXCLUSIVE);
178: trecordMgr = new net.sf.borg.apptconduit.TodoRecordManager(
179: props, tododb);
180: log("Wipe Todos and load Palm from BORG");
181: trecordMgr.WipeData();
182: SyncManager.closeDB(tododb);
183: }
184:
185: // Close DB
186: apptModel.close_db();
187: //taskModel.close_db();
188: // Single Log we are successful
189: log("OK ApptCond Conduit");
190: Log.endSync();
191: if (dbdir.startsWith("remote:")) {
192: try {
193: SocketClient.sendMsg("localhost", port, "sync");
194: SocketClient.sendMsg("localhost", port,
195: "lock:Appointment HotSync Completed");
196: SocketClient.sendMsg("localhost", port,
197: "unlock");
198: } catch (Exception e) {
199: }
200: }
201: }
202:
203: } catch (Throwable t) {
204:
205: // If there was an error, dump the stack trace
206: // and tell the log the conduit failed
207:
208: t.printStackTrace();
209: Log.abortSync();
210: }
211: }
212:
213: /**
214: * Returns a String representation of the conduit name.
215: */
216: public String name() {
217: return NAME;
218: }
219:
220: public int configure(ConfigureConduitInfo info) {
221: ConduitConfigure config = new ConduitConfigure(info, NAME);
222: config.createDialog();
223:
224: if (config.dataChanged) {
225: int propsValue = SyncProperties.SYNC_DO_NOTHING; // default
226: propsValue = config.saveState;
227:
228: if (config.setDefault) {
229: info.syncPermanent = propsValue;
230: info.syncPref = ConfigureConduitInfo.PREF_PERMANENT;
231: }
232: info.syncNew = propsValue;
233: }
234:
235: return 0;
236: }
237: }
|