Commit c3259172 authored by Canh Ngo's avatar Canh Ngo

HSTCONFIGEDIT-169: fixed NPE when deleting a page in hst configuration editor:

Use the bean model to lazily load object from the plugin model and update when plugin model changes
parent 59f208d9
......@@ -49,11 +49,11 @@ import org.slf4j.LoggerFactory;
public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlugin<K> implements BeanProvider<K> {
static final Logger log = LoggerFactory.getLogger(BasicEditorPlugin.class);
private static final Logger log = LoggerFactory.getLogger(BasicEditorPlugin.class);
private final FeedbackPanel feedback;
protected final Form<K> form;
private K bean;
private final LoadableDetachableModel<K> beanModel;
protected AjaxSubmitLink saveLink;
......@@ -62,21 +62,27 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug
public BasicEditorPlugin(IPluginContext context, IPluginConfig config) {
super(context, config);
bean = dao.load((JcrNodeModel) getModel());
IModel<K> formModel = new CompoundPropertyModel<>(new LoadableDetachableModel<K>(bean) {
beanModel = new LoadableDetachableModel<K>() {
@Override
protected K load() {
if (getModel() instanceof JcrNodeModel) {
JcrNodeModel newModel = (JcrNodeModel) getModel();
JcrNodeModel oldModel = bean.getModel();
if (!newModel.equals(oldModel)) {
bean = dao.load(newModel);
}
IModel model = getModel();
if (model instanceof JcrNodeModel) {
return dao.load((JcrNodeModel) model);
}
return bean;
return null;
}
});
@Override
protected void onDetach() {
super.onDetach();
final K object = getObject();
if (object != null) {
object.detach();
}
}
};
IModel<K> formModel = new CompoundPropertyModel<>(beanModel);
add(form = new Form<>("editor", formModel));
add(saveLink = new AjaxSubmitLink("save", form) {
......@@ -99,7 +105,7 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug
IDialogService dialogService = getDialogService();
try {
String name = bean.getModel().getNode().getName();
String name = getBean().getModel().getNode().getName();
String title = new StringResourceModel("dialog.remove.confirm.title", BasicEditorPlugin.this, null)
.getString();
String message = new StringResourceModel("dialog.remove.confirm.message", BasicEditorPlugin.this,
......@@ -207,16 +213,16 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug
}
protected void doSave() {
if (dao.save(bean)) {
setDefaultModel(bean.getModel());
if (dao.save(getBean())) {
setDefaultModel(getBean().getModel());
info(getString("save.succes"));
}
}
protected void doRemove() {
try {
final String name = bean.getModel().getNode().getName();
Node next = bean.getModel().getParentModel().getNode();
final String name = getBean().getModel().getNode().getName();
Node next = getBean().getModel().getParentModel().getNode();
for (NodeIterator it = next.getNodes(); it.hasNext();) {
Node node = it.nextNode();
......@@ -230,24 +236,24 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug
}
}
if (dao.delete(bean)) {
if (dao.delete(getBean())) {
setDefaultModel(new JcrNodeModel(next));
info(new StringResourceModel("node.removed", this, null, new Object[] { name }).getString());
}
} catch (RepositoryException e) {
log.error("Failed to remove node, model = " + bean.getModel());
log.error("Failed to remove node, model = " + getBean().getModel());
}
}
public K getBean() {
return bean;
return beanModel.getObject();
}
@Override
public void detachModels() {
nodeSort.detach();
if(bean != null) {
bean.detach();
if(beanModel != null) {
beanModel.detach();
}
super.detachModels();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment