Commit df71d50b authored by Canh Ngo's avatar Canh Ngo

HSTCONFIGEDIT-188: fixed by loading and saving editor-bean object upon changes...

HSTCONFIGEDIT-188: fixed by loading and saving editor-bean object upon changes in plugin's JCR node model
parent 4a4ddb64
...@@ -28,7 +28,6 @@ import org.apache.wicket.markup.html.form.Form; ...@@ -28,7 +28,6 @@ import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel; import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel; import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel; import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.StringResourceModel; import org.apache.wicket.model.StringResourceModel;
import org.hippoecm.frontend.PluginRequestTarget; import org.hippoecm.frontend.PluginRequestTarget;
import org.hippoecm.frontend.dialog.IDialogService; import org.hippoecm.frontend.dialog.IDialogService;
...@@ -53,7 +52,8 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug ...@@ -53,7 +52,8 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug
private final FeedbackPanel feedback; private final FeedbackPanel feedback;
protected final Form<K> form; protected final Form<K> form;
private final LoadableDetachableModel<K> beanModel;
private K editorBeanObject;
protected AjaxSubmitLink saveLink; protected AjaxSubmitLink saveLink;
...@@ -62,29 +62,9 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug ...@@ -62,29 +62,9 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug
public BasicEditorPlugin(IPluginContext context, IPluginConfig config) { public BasicEditorPlugin(IPluginContext context, IPluginConfig config) {
super(context, config); super(context, config);
beanModel = new LoadableDetachableModel<K>() { editorBeanObject = getEditorBeanObject();
@Override
protected K load() {
IModel model = getModel();
if (model instanceof JcrNodeModel) {
return dao.load((JcrNodeModel) model);
} else {
log.error("Cannot load bean object model due to the model of '{}' is not the JcrNodeModel object",
BasicEditorPlugin.this.getClass().getName());
return null;
}
}
@Override IModel<K> formModel = new CompoundPropertyModel<>(editorBeanObject);
protected void onDetach() {
final K object = getObject();
if (object != null) {
object.detach();
}
}
};
IModel<K> formModel = new CompoundPropertyModel<>(beanModel);
add(form = new Form<>("editor", formModel)); add(form = new Form<>("editor", formModel));
add(saveLink = new AjaxSubmitLink("save", form) { add(saveLink = new AjaxSubmitLink("save", form) {
...@@ -182,6 +162,17 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug ...@@ -182,6 +162,17 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug
add(downLink); add(downLink);
} }
private K getEditorBeanObject() {
IModel model = getModel();
if (model instanceof JcrNodeModel) {
return dao.load((JcrNodeModel) model);
} else {
log.error("Cannot load bean object due to the model of '{}' is not the JcrNodeModel object",
BasicEditorPlugin.this.getClass().getName());
return null;
}
}
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
...@@ -199,6 +190,8 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug ...@@ -199,6 +190,8 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug
@Override @Override
protected void onModelChanged() { protected void onModelChanged() {
updateFormModel();
IModel model = getModel(); IModel model = getModel();
if (model instanceof JcrNodeModel) { if (model instanceof JcrNodeModel) {
nodeSort.setModel((JcrNodeModel) model); nodeSort.setModel((JcrNodeModel) model);
...@@ -206,6 +199,15 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug ...@@ -206,6 +199,15 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug
super.onModelChanged(); super.onModelChanged();
} }
private void updateFormModel() {
if (editorBeanObject != null) {
editorBeanObject.detach();
}
editorBeanObject = getEditorBeanObject();
form.setModelObject(editorBeanObject);
}
@Override @Override
public void render(PluginRequestTarget target) { public void render(PluginRequestTarget target) {
if (target != null && feedback.anyMessage()) { if (target != null && feedback.anyMessage()) {
...@@ -250,15 +252,17 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug ...@@ -250,15 +252,17 @@ public abstract class BasicEditorPlugin<K extends EditorBean> extends EditorPlug
} }
public K getBean() { public K getBean() {
return beanModel.getObject(); return editorBeanObject;
} }
@Override @Override
public void detachModels() { public void detachModels() {
nodeSort.detach(); nodeSort.detach();
if (beanModel != null) {
beanModel.detach(); if (editorBeanObject != null) {
editorBeanObject.detach();
} }
super.detachModels(); 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