goToMetadataList = true;
} else
// all other commands (user is adding or updating an element, or
// retrieving from history)
if (metadataId != -2) {
AuditLogManager alm = null;
MetadataElement elem = null;
try {
if (metadataId == -1) {
// -1 is for a blank form request from the list, asking
// to add a new element
elem = new MetadataElement();
try {
elem.setIndexMultiplier(1);
} catch (ValidationException e) {
// won't happen
}
elem.setMetadataId(-1);
elem.setProjectId(((Project) ((HashMap) getServletContext()
.getAttribute("projectMap")).get(command
.getProject())).getProjectId());
if (subCommand.equals("kf")) {
elem.setIsKeyField(true);
}
} else if (subCommand.equals("history")) {
alm = new AuditLogManager();
String[] vals = command.getOther().get(2).split("\\|");
try {
AuditLog record = alm.getRecord(Integer
.parseInt(vals[0]), ContentType.valueOf(Integer
.parseInt(vals[1])), metadataId, Timestamp
.valueOf(vals[2]));
String data = record.getDataExport();
elem = new MetadataElement();
elem.fromString(data);
request.setAttribute("isDirty", "t");
} catch (NullPointerException e) {
errors.add("Invalid history request format.");
} catch (NumberFormatException e) {
errors.add("Invalid history request format.");
} catch (NoSuchAuditLogRecordException e) {
errors
.add("The history record requested couldn't be located.");
} catch (DataTypeMismatchException e) {
Logger.error("Data type mismatch occurred pulling "
+ command.getOther().get(0)
+ " from the audit log", e);
errors
.add("A system error was encountered (DataTypeMismatch). "
+ "Please contact a system administrator.");
}
if (elem == null) {
elem = manager.getMetadataElement(metadataId);
}
} else {
elem = manager.getMetadataElement(metadataId);
}
} catch (NumberFormatException e) {
request.setAttribute("errorMessage",
"Invalid metadata ID requested.");
} catch (NoSuchMetadataException e) {
request.setAttribute("errorMessage",
"The requested metadata element could not be found.");
}
if (request.getParameter("projectId") != null) {
// this is an add/update from the form
String oldContent = elem.toString();
populateVBOFromRequest(elem, request, fieldMessages, true);
if (oldContent.equals(elem.toString())) {
errors.add("No changes detected.");
}
if ((fieldMessages.size() == 0) && (errors.size() == 0)) {
try {
elem
.setContentType(ContentType.valueOf(
command.getModifier().toUpperCase())
.getValue());
if (elem.getMetadataId() == -1) {
manager.addMetadata(((User) request.getSession()
.getAttribute("user")), elem);
} else {
manager.updateMetadata(elem, oldContent,
((User) request.getSession().getAttribute(
"user")));
}
request
.setAttribute(
"successMessage",
"Your metadata element ("
+ elem
.getFriendlyElementQualifierName()
+ ") was successfully updated.");
goToMetadataList = true;
} catch (NoSuchMetadataException e) {
errors
.add("Couldn't find the metadata element for updating, or an ID for adding.");
}
}
if (fieldMessages.size() != 0) {
String plural = "";
if (fieldMessages.size() > 1) {
plural = "s";
}
errors.add("Form field" + plural
+ " failed validation (see below).");
}
}
if ((elem != null) && (!goToMetadataList)) {
request.setAttribute("metadata", elem);
// put the project's history in the request scope (the project
// itself is in the application scope)
if (elem.getMetadataId() != -1) {
if (alm == null) {
alm = new AuditLogManager();
}
ArrayList<AuditLog> historyRecords = alm.getUpdateRecords(
((HashMap<String, Project>) getServletContext()
.getAttribute("projectMap")).get(
command.getProject()).getProjectId(),
ContentType.METADATA, elem.getMetadataId());
if ((historyRecords != null) && (historyRecords.size() > 0)) {