@SuppressWarnings("unchecked")
@Override
protected void handleRequest(HttpServletRequest request,
HttpServletResponse response) {
Command command = (Command) request.getAttribute("command");
MetadataManager manager = new MetadataManager();
ArrayList<String> errors = new ArrayList<String>();
HashMap<String, String> fieldMessages = new HashMap<String, String>();
boolean goToMetadataList = false;
String cancel;
// parse the "other" data from the Command object into metadata ID
// and sub-command
int metadataId = -2;
String subCommand = "";
if ((command.getOther() != null) && (command.getOther().size() > 0)) {
metadataId = Integer.parseInt((String) command.getOther().get(0));
if (command.getOther().size() > 1) {
subCommand = (String) command.getOther().get(1);
}
} else if (request.getParameter("metadataId") != null) {
metadataId = Integer.parseInt(request.getParameter("metadataId"));
}
// 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