lastSyncDate = new Date(0);
try {
localSyncClient.getMachineID();
Document doc = localContentClient.getContent("/system/SysConfig/sync" + src_host + ".xml").getDocument();
Element e = doc.getDocumentElement();
lastSyncDate = new Variant(e.getAttribute("date")).getDate();
}
catch ( Exception e ) {
// Document doesn't exist I guess
//out.println(e.getMessage());
}
try {
// Do Stuff
Manifest mf;
if ( groups != null )
mf = sourceSyncClient.listGroupChanges(lastSyncDate, (String[])groups.toArray(EmptyStrings));
else
mf = sourceSyncClient.listAllChanges(lastSyncDate);
Date startTime = mf.getDate();
String[] grps = mf.getGroupNames();
int totalDocs = 0;
for ( int i = 0; i < grps.length; i++ )
totalDocs += mf.getGroupContent(grps[i]).length;
if ( grps.length > 0 ) {
log.add("Updating " + grps.length + " files");
int docNum = 0;
for ( int i = 0; !cancel && i < grps.length; i++ ) {
currentGroup = grps[i];
String[] files = mf.getGroupContent(currentGroup);
log.add("Group '" + currentGroup + "' contains " + files.length + " updated files");
for ( int j = 0; !cancel && j < files.length; j++ ) {
currentFile = files[j];
int idx = currentFile.lastIndexOf('/');
String colName = currentFile.substring(0, idx);
String docName = currentFile.substring(idx + 1);
Content c = mf.getContent(currentFile);
CollectionClient lclCol = (CollectionClient)lclColCache.get(colName);
if ( lclCol == null ) {
lclCol = localContentClient.getCollection(colName);
lclColCache.put(colName, lclCol);
if ( lclCol.getCollectionType() == CollectionClient.TYPE_DOCUMENTS )
lclColXML.add(colName);
}
if ( c.getStatus() != Constants.STATUS_DELETED ) {
CollectionClient srcCol = (CollectionClient)srcColCache.get(colName);
if ( srcCol == null ) {
srcCol = sourceContentClient.getCollection(colName);
srcColCache.put(colName, srcCol);
if ( srcCol.getCollectionType() == CollectionClient.TYPE_DOCUMENTS )
srcColXML.add(colName);
}
File f = File.createTempFile("dbxml_", ".sync");
tempFiles.put(currentFile, f);
if ( srcColXML.contains(colName) ) {
String doc = srcCol.getDocumentAsText(docName);
FileOutputStream fos = new FileOutputStream(f);
BufferedOutputStream bos = new BufferedOutputStream(fos, 4096);
OutputStreamWriter osw = new OutputStreamWriter(bos, "UTF8");
osw.write(doc);
osw.flush();
osw.close();
}
else {
byte[] doc = srcCol.getValue(docName);
FileOutputStream fos = new FileOutputStream(f);
fos.write(doc);
fos.close();
}
}
else
delList.put(currentFile, lclCol);
percentComplete = 100 * docNum++ / totalDocs;
}
}
if ( !cancel ) {
Iterator iter = delList.entrySet().iterator();
while ( iter.hasNext() ) {
Map.Entry entry = (Map.Entry)iter.next();
String path = (String)entry.getKey();
CollectionClient col = (CollectionClient)entry.getValue();
int idx = path.lastIndexOf('/');
String docName = path.substring(idx + 1);
col.remove(docName);
}
iter = tempFiles.entrySet().iterator();
while ( iter.hasNext() ) {
Map.Entry entry = (Map.Entry)iter.next();
String path = (String)entry.getKey();
File f = (File)entry.getValue();
int idx = path.lastIndexOf('/');
String colName = path.substring(0, idx);
String docName = path.substring(idx + 1);
CollectionClient lclCol = (CollectionClient)lclColCache.get(colName);
if ( lclCol == null ) {
lclCol = localContentClient.getCollection(colName);
lclColCache.put(colName, lclCol);
if ( lclCol.getCollectionType() == CollectionClient.TYPE_DOCUMENTS )
lclColXML.add(colName);
}
if ( lclColXML.contains(colName) ) {
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis, 4096);
InputStreamReader isr = new InputStreamReader(bis, "UTF8");
char[] buf = new char[(int)f.length()];
int size = isr.read(buf);
isr.close();
lclCol.setDocumentAsText(docName, new String(buf, 0, size));
}
else {
FileInputStream fis = new FileInputStream(f);
byte[] buf = new byte[(int)f.length()];
fis.read(buf);
fis.close();
lclCol.setValue(docName, buf);
}
}
}
log.add("Synchronization complete");
}
else
log.add("No files are out of date");
if ( !cancel ) {
// Write the last sync to the Database
Document doc = DOMHelper.newDocument();
Element srvElem = doc.createElement("server");
doc.appendChild(srvElem);
srvElem.setAttribute("host", src_host);
srvElem.setAttribute("date", new Variant(startTime).getString());
for ( int i = 0; i < log.size(); i++ ) {
Element logElem = doc.createElement("log");
DOMUtils.setText(logElem, (String)log.get(i));
srvElem.appendChild(logElem);
}