/*
* GNTConduit.java
*
* Created on November 15, 2003, 10:27 PM
*/
package gnt;
import palm.conduit.*;
import java.util.*;
import java.io.*;
import javax.sql.*;
/**
*
* @author Mike Kirschman
*/
public class GNTConduit implements Conduit{
SyncProperties props = null;
String pcGNTFilePath, backupFilePath;
Vector pcTextRecords, pcGraphicsRecords, archiveRecords, pcCategories;
int textDB, graphicsDB, count, i;
Class recordClass;
GNTWriter writer;
RecordManager textRecordMgr, graphicsRecordMgr;
// CategoryManager categoryMgr;
/** Creates a new instance of GNTConduit */
public GNTConduit(){
}
public int configure(ConfigureConduitInfo configureConduitInfo) {
return 0;
}
public String name() {
return "GNoteT Conduit";
}
/**
*
The "entry point" into the control of the Conduit. This method gets
* called during the HotSync.
* The steps executed by this method are as follows:
* - Initialize variables to handle the records.
* - Determine a path and name for the desktop data file.
* Determine what synchronization type is being used by HotSync Manager.
* - This can be a slow sync, a fast sync, copying the handheld’s records
* to the desktop, copying the desktop’s records to the handheld, or
* others. A slow sync looks at all records and also examines the
* backup file on the desktop. A fast sync looks only at modified or
* “dirty” records. HotSync Manager will automatically determine which
* is needed.
*
* - Open the handheld database that you will be synchronizing with.
* - Read the data from the desktop.
* - Synchronize the records.
* - Synchronize the categories.
* - Write out the archive files, if there are any.
* - Write out the main desktop data file and copy that into the backup file.
* - Close the database.
* - Finalize the conduit and exit.
* @see palm.conduit.Conduit#open(palm.conduit.SyncProperties)
*/
public void open(SyncProperties syncProperties) {
// Grab the SyncProperties passed in
this.props = syncProperties;
// Initialize MemoWriter for read/write to desktop
writer = new GNTWriter();
// Generate a file path for desktop data
pcGNTFilePath = new String(props.pathName + props.localName);
// Create vectors for records and categories
pcTextRecords = new Vector();
pcGraphicsRecords = new Vector();
archiveRecords = new Vector();
// pcCategories = new Vector();
// Generate name for backup File
String frontPart;
int index = props.localName.lastIndexOf(".");
if(index != -1) {
frontPart = props.pathName + props.localName.substring(0, index);
} else {
frontPart = pcGNTFilePath;
}
backupFilePath = frontPart + ".bak";
try{
// Get the class to be used for RecordManager
recordClass = Class.forName("gnt.GNTRecord");
GNTRecord.resetRecordCount();
// Open handheld DBs
textDB = SyncManager.openDB("GNTTextDB", 0, SyncManager.OPEN_READ |
SyncManager.OPEN_WRITE |
SyncManager.OPEN_EXCLUSIVE);
// graphicsExist = textDB
// graphicsDB = SyncManager.openDB("GNTGrphDB", 0, SyncManager.OPEN_READ |
// SyncManager.OPEN_WRITE |
// SyncManager.OPEN_EXCLUSIVE);
// Create instance of RecordManager for sync logic
textRecordMgr = new RecordManager(props, textDB, recordClass);
// graphicsRecordMgr = new RecordManager(props, graphicsDB, recordClass);
// // Create instance of CategoryManager to sync
// categoryMgr = new CategoryManager(props, textDB);
// Read all records from desktop file
pcTextRecords = writer.readRecords(pcGNTFilePath);
// pcGraphicsRecords = writer.readRecords(pcGNTFilePath);
// // Read all categories from desktop file
// pcCategories = writer.readCategories(pcGNTFilePath);
// Tell the log we are starting
Log.startSync();
if(pcTextRecords.size() == 0) {
pcTextRecords = textRecordMgr.copyHHRecords();
// pcGraphicsRecords = graphicsRecordMgr.copyHHRecords();
} else {
switch(props.syncType) {
case SyncProperties.SYNC_FAST :
textRecordMgr.fastSyncData(pcTextRecords, archiveRecords);
// graphicsRecordMgr.fastSyncData(pcGraphicsRecords, archiveRecords);
break;
case SyncProperties.SYNC_SLOW :
// read the backup file, slow sync the recs
Vector buRecs = writer.readRecords(backupFilePath);
pcTextRecords = textRecordMgr.slowSyncData(pcTextRecords, archiveRecords, buRecs);
// pcGraphicsRecords = graphicsRecordMgr.slowSyncData(pcTextRecords, archiveRecords, buRecs);
break;
case SyncProperties.SYNC_HH_TO_PC :
// Copy the HH records to the desktop
pcTextRecords = textRecordMgr.copyHHRecords();
// pcGraphicsRecords = graphicsRecordMgr.copyHHRecords();
break;
case SyncProperties.SYNC_PC_TO_HH :
// remove all the records on HH db and copy desktop recs to HH
pcTextRecords = textRecordMgr.copyPCRecords(pcTextRecords);
// pcGraphicsRecords = graphicsRecordMgr.copyPCRecords(pcGraphicsRecords);
break;
case SyncProperties.SYNC_DO_NOTHING :
// Do nothing here.
Log.endSync();
return;
}
}
// Use the category manager to sync categories
// pcCategories = categoryMgr.synchronize(pcCategories, pcTextRecords);
//since we're ignoring the categories, make a zero length substitute
pcCategories = new Vector(0);
// Write backup files
writer.writeData(pcGNTFilePath, pcTextRecords, pcCategories);
// writer.writeData(pcGNTFilePath, pcGraphicsRecords, pcCategories);
writer.writeData(backupFilePath, pcTextRecords, pcCategories);
// Archive the items for archiving
doArchives();
// Close handheld's database
SyncManager.closeDB(textDB);
// SyncManager.closeDB(graphicsDB);
// Signal end of sync
Log.endSync();
return;
} catch(Throwable t) {
t.printStackTrace();
Log.abortSync();
}
}
void doArchives(){
try{
if(archiveRecords.size() > 0) {
writer.writeArchiveRecords(props.pathName, archiveRecords, pcCategories);
}
} catch(Throwable t) {
t.printStackTrace();
}
}
}