@SuppressWarnings("serial")
public OrganizeGlobalsPage() {
TreeModel treeModel = createTreeModel();
final LinkTree tree = new LinkTree("tree", treeModel)
{
@Override
protected void onNodeLinkClicked(Object node, BaseTree tree, AjaxRequestTarget target) {
DefaultMutableTreeNode mnode = (DefaultMutableTreeNode) node;
if (!mnode.isLeaf()) {
return;
}
Global global = (Global) mnode.getUserObject();
globalName = global.getName();
className = global.getClassname();
target.add(nameField);
target.add(classNameField);
}
};
tree.getTreeState().expandAll();
add(tree);
Form<Object> form = new Form<Object>("editForm", new CompoundPropertyModel<Object>(this));
submitButton = new AjaxButton("submitButton", form) {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
if (globalName == null || globalName.equals("") || className == null || className.equals("")) {
String message = new StringResourceModel("emptyError", this, null).getString();
error(message);
target.add(feedbackPanel);
return;
}
try {
ruleManager.addGlobal(className, globalName);
String message = new StringResourceModel("insertedGlobal", this, null).getString();
info(globalName + " " + message);
LOGGER.info("successfully inserted global " + globalName);
} catch (RuleBaseException e) {
LOGGER.debug("error while inserting global " + globalName, e);
String temp = ruleManager.getGlobalType(globalName);
try {
// it comes here if the global already exists
ruleManager.removeGlobal(globalName);
ruleManager.addGlobal(className, globalName);
String message = new StringResourceModel("updatedGlobal", this, null).getString();
info(globalName + " " + message);
} catch (RuleBaseException ex) {
LOGGER.debug("error while updating global " + globalName, e);
// it restores the old value if the new value for global is invalid
ruleManager.removeGlobal(globalName);
ruleManager.addGlobal(temp, globalName);
String message = new StringResourceModel("savingError", this, null).getString();
error(globalName + " " + message + "\n" + ex.getLocalizedMessage());
}
}
tree.setModelObject(createTreeModel());
globalName = "";
className = "";
target.add(nameField);
target.add(classNameField);
target.add(tree);
target.add(feedbackPanel);
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
LOGGER.warn("Error during handling submitButton submit");
}
};
submitButton.setOutputMarkupId(true);
form.add(submitButton);
deleteButton = new AjaxButton("deleteButton", form) {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
String temp = ruleManager.getGlobalType(globalName);
try {
ruleManager.removeGlobal(globalName);
String message = new StringResourceModel("deletedGlobal", this, null).getString();
info(globalName + " " + message);
LOGGER.info("successfully deleted global " + globalName);
} catch (RuleBaseException e) {
LOGGER.debug("error while deleting global " + globalName, e);
if (e.getMessage().startsWith("Rule Compilation error")) {
ruleManager.addGlobal(temp, globalName);
String message = new StringResourceModel("deletingError", this, null).getString();
error(globalName + " " + message + "\n" + e.getLocalizedMessage());
} else {
String message = new StringResourceModel("notExistingError", this, null).getString();
error(globalName + " " + message);
}
target.add(feedbackPanel);
return;
}
tree.setModelObject(createTreeModel());
globalName = "";
className = "";
target.add(feedbackPanel);