String errorString = null;
IPerson person = user.getPerson();
String email = person.getEmail();
JSONObject projectTemplatesObject;
JSONArray existingTemplates;
try{
IStorage projectTemplatesDirectory = getProjectTemplatesDirectory();
projectTemplatesObject = _getProjectTemplatesIndex();
existingTemplates = projectTemplatesObject.getJSONArray("templates");
// Verify that all of the templatesToDelete exist
// and are owned by this user
int count = templatesToDelete.length();
for(int i=0 ; i< count; i++){
JSONObject templateToDelete = templatesToDelete.getJSONObject(i);
int found = findTemplateInArray(existingTemplates, templateToDelete, email);
if(found == -1){
return "at least one template to delete not found";
}
}
// Delete the overridden template(s), both in the index file
// and on disk
JSONArray newTemplates = new JSONArray();
int existingCount = existingTemplates.length();
for(int j=0; j<existingCount; j++){
JSONObject existingTemplate = existingTemplates.getJSONObject(j);
String existingName = existingTemplate.getString("name");
String existingEmail = existingTemplate.getString("authorEmail");
Boolean found = false;
int deleteCount = templatesToDelete.length();
for(int i=0 ; i< deleteCount; i++){
JSONObject templateToDelete = templatesToDelete.getJSONObject(i);
String deleteName = templateToDelete.getString("name");
if(existingName.equals(deleteName) && existingEmail.equals(email)){
found = true;
break;
}
}
if(found){
String existingFolderName = makeProjectTemplateFolderName(existingName, existingEmail);
IStorage existingFolder = projectTemplatesDirectory.newInstance(projectTemplatesDirectory, existingFolderName);
deleteDirectory(existingFolder);
}else{
newTemplates.put(existingTemplate);
}
}
updateTemplates(newTemplates);
} catch (JSONException e) {
String desc = "deleteProjectTemplates - json exception";