return;
} else if (metaFile.exists()) {
// now the meta file is there but the direct file is deleted, so we will
// ask whether he want to get the up. Default behaviour is not to take the update from
// the server. This is because the working copy is up-to-date.
if (callback == null || callback.getConfirmation(new Message(
MessageCode.KEEP_DELETED_FILE,
new String[]{filePath}),
SynchronizationConstants.DELETE_CONFIRMATION_CONTEXT)) {
return;
}
}
if (!isUpdating) {
try {
// Create file if it does not exist
if (isCollection) {
boolean ignore = file.mkdir(); // ignores the return value purposely
} else {
boolean ignore = file.createNewFile(); // ignores the return value purposely
}
} catch (IOException e) {
throw new SynchronizationException(MessageCode.FILE_CREATION_FAILED, e,
new String[]{"file name: " + filePath});
}
}
if (!isCollection) {
try {
boolean writeToFile = true;
if (file.exists()) {
byte[] currentContentBytes = Utils.getBytesFromFile(file);
if (currentContentBytes != null && contentBytes != null) {
String currentContentMd5 = Utils.getMD5(currentContentBytes);
String writingContentMd5 = Utils.getMD5(contentBytes);
if (writingContentMd5 != null &&
writingContentMd5.equals(currentContentMd5)) {
writeToFile = false;
}
}
}
if (writeToFile) {
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(contentBytes);
fileOutputStream.flush();
fileOutputStream.close();
}
} catch (IOException e) {
throw new SynchronizationException(MessageCode.PROBLEM_IN_CREATING_CONTENT,
e,
new String[]{"file name: " + filePath});
}
} else {
// creating the meta directory
String metaDirectoryName =
filePath + File.separator + SynchronizationConstants.META_DIRECTORY;
File metaDirectory = new File(metaDirectoryName);
if (!metaDirectory.exists() && !metaDirectory.mkdir()) {
throw new SynchronizationException(MessageCode.ERROR_CREATING_META_FILE,
new String[]{"file: " + metaDirectoryName});
}
}
boolean iterateChildren = true;
if (!xmlReader.hasNext() || !(xmlReader.isStartElement() &&
xmlReader.getLocalName().equals("children"))) {
// finished the recursion
// consuming the stream until the resource end element found
while (xmlReader.hasNext() && !(xmlReader.isEndElement() &&
xmlReader.getLocalName().equals("resource"))) {
xmlReader.next();
}
iterateChildren = false;
}
if (iterateChildren) {
do {
xmlReader.next();
if (xmlReader.isEndElement() && xmlReader.getLocalName().equals("children")) {
// this means empty children, just quit from here
// before that we have to set the cursor to the end of the current resource
if (xmlReader.hasNext()) {
do {
xmlReader.next();
} while (xmlReader.hasNext() && !(xmlReader.isEndElement() &&
xmlReader.getLocalName().equals("resource")));
}
iterateChildren = false;
break;
}
} while (!xmlReader.isStartElement() && xmlReader.hasNext());
}
Map<String, Boolean> childNames = new HashMap<String, Boolean>();
if (iterateChildren) {
while (xmlReader.hasNext() && xmlReader.isStartElement() &&
xmlReader.getLocalName().equals("resource")) {
// prepare the children absolute path
String childName = xmlReader.getAttributeValue(null, "name");
String fileResourceName = Utils.encodeResourceName(childName);
String childFilePath = filePath + File.separator + fileResourceName;
String childPath = (path.equals("/") ? "" : path) + "/" + childName;
updateRecursively(xmlReader, childFilePath, childPath, callback);
childNames.put(fileResourceName, true);
while ((!xmlReader.isStartElement() && xmlReader.hasNext()) &&
!(xmlReader.isEndElement() &&
xmlReader.getLocalName().equals("children"))) {
xmlReader.next();
}
if (xmlReader.isEndElement() && xmlReader.getLocalName().equals("children")) {
// we are in the end of the children tag.
break;
}
}
// consuming the stream until the resource end element found
while (xmlReader.hasNext() && !(xmlReader.isEndElement() &&
xmlReader.getLocalName().equals("resource"))) {
xmlReader.next();
}
// now we are checking which files have been deleted at the server end.
String[] childFileNames = file.list();
if (childFileNames != null) {
for (String childFileName : childFileNames) {
if (childFileName.equals(SynchronizationConstants.META_DIRECTORY)) {
continue;
}
if (childNames.get(childFileName) != null && childNames.get(childFileName)) {
// this files stays on the server as well, so nothing to worry
continue;
}
// hm, we have a situation that stuff exist local, but not at the server
// first need to check whether they are newly added.
// we can do that by checking the existence of meta directory
String childFilePath = file + File.separator + childFileName;
File childFile = new File(file, childFileName);
boolean shouldDelete = false;
File childMetaFile;
if (childFile.isDirectory()) {
// the meta directory should exist in .meta
String metaDirName =
filePath + File.separator + childFileName + File.separator +
SynchronizationConstants.META_DIRECTORY;
childMetaFile = new File(metaDirName);
if (childMetaFile.exists()) {
// looks like it's bean earlier checkout from registry, mean it is now deleted
shouldDelete = true;
}
} else {
String metaFileName =
filePath + File.separator +
SynchronizationConstants.META_DIRECTORY + File.separator +
SynchronizationConstants.META_FILE_PREFIX + childFileName +
SynchronizationConstants.META_FILE_EXTENSION;
childMetaFile = new File(metaFileName);
if (childMetaFile.exists()) {
// looks like it's bean earlier checkout from registry, mean it is now deleted
shouldDelete = true;
}
}
if (shouldDelete && !isSilentUpdate) {
boolean isDeleted = Utils.confirmDelete(childFile, childMetaFile, callback);
if (isDeleted) {
if (callback != null && !isSilentUpdate) {
callback.displayMessage(new Message(MessageCode.DELETED,
new String[]{refinedPathToPrint(childFilePath)}));
}
deletedCount++;
} else {
if (callback != null && !isSilentUpdate) {
callback.displayMessage(new Message(MessageCode.NOT_DELETED,
new String[]{refinedPathToPrint(childFilePath)}));
}
notDeletedCount++;
}
}
}
}
}
if (file.isDirectory() && collectionIsNotUpdated) {
return;
}
// creating the meta file
String metaFileName;
if (isCollection) {
metaFileName = filePath + File.separator + SynchronizationConstants.META_DIRECTORY +
File.separator +
SynchronizationConstants.META_FILE_PREFIX +
SynchronizationConstants.META_FILE_EXTENSION;
} else {
String parentDirName = file.getParent();
metaFileName =
parentDirName + File.separator + SynchronizationConstants.META_DIRECTORY +
File.separator + SynchronizationConstants.META_FILE_PREFIX +
Utils.encodeResourceName(name) +
SynchronizationConstants.META_FILE_EXTENSION;
}
Utils.createMetaFile(metaFileName, root);
// printing out the information of the file
if (isConflicting) {
if (callback != null && !isSilentUpdate) {
callback.displayMessage(new Message(MessageCode.CONFLICTED,
new String[]{refinedPathToPrint(filePath)}));
}
conflictedCount++;
} else if (isUpdating) {
if (callback != null && !isSilentUpdate) {
callback.displayMessage(new Message(MessageCode.UPDATED,
new String[]{refinedPathToPrint(filePath)}));
}
updatedCount++;
} else {
if (callback != null && !isSilentUpdate) {
callback.displayMessage(new Message(MessageCode.ADDED,
new String[]{refinedPathToPrint(filePath)}));
}
addedCount++;
}
}