// the delete action's being performed from the metadata list
if (subCommand.equals("del")) {
try {
// we really disable, though we talk about deleting to the user
MetadataElement element = manager
.getMetadataElement(metadataId);
if (element.isKeyField()) {
errors.add("Key fields can not be deleted.");
} else {
manager.deleteMetadata(element);
request.setAttribute("successMessage", "Metadata element ("
+ metadataId + ") successfully deleted.");
}
} catch (NumberFormatException e) {
request.setAttribute("errorMessage",
"The metadata ID requested (" + metadataId
+ ") couldn't be located to delete.");
} catch (NoSuchMetadataException e) {
request.setAttribute("errorMessage",
"The metadata ID requested (" + metadataId
+ ") couldn't be located to delete: "
+ e.getMessage());
}
goToMetadataList = true;
} else
// ... and this one is for re-ordering from the metadata list
if ((subCommand.equals("down")) || (subCommand.equals("up"))) {
try {
// "up" in the GUI means "down" in the ordering, so the
// true/false here may look a little goofy
manager.moveMetadata(manager.getMetadataElement(metadataId),
subCommand.equals("down") ? true : false);
request.setAttribute("successMessage", "Metadata element "
+ metadataId + " successfully moved.");
} catch (NumberFormatException e) {
request.setAttribute("errorMessage",
"The metadata ID requested (" + metadataId
+ ") couldn't be located to move.");
} catch (NoSuchMetadataException e) {
request.setAttribute("errorMessage",
"The metadata ID requested (" + metadataId
+ ") couldn't be located to move: "
+ e.getMessage());
}
goToMetadataList = true;
} else
// cancel the changes
if (((cancel = request.getParameter("cancelChanges")) != null)
&& (cancel.equals("yes"))) {
request.setAttribute("successMessages",
"Your changes have been cancelled.");
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)) {
request.setAttribute("history", historyRecords);
}
}
} else if (elem == null) {