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);
}
CollectionClient col = localContentClient.getCollection("/system/SysConfig");
col.setDocument("sync" + src_host + ".xml", doc);
}
}
catch ( Exception e ) {
error = true;
log.add("Exception: " + e.getMessage());