From a1215783e5f6e9e45d79970c045be61b2d5607d0 Mon Sep 17 00:00:00 2001 From: kate Date: Thu, 12 Apr 2018 15:03:32 +0200 Subject: [PATCH] code refactoring --- .../admin/configuration/PageDebugView.java | 131 ++++++++---------- 1 file changed, 61 insertions(+), 70 deletions(-) diff --git a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/page/admin/configuration/PageDebugView.java b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/page/admin/configuration/PageDebugView.java index 41ac3e4e16e..bb03e263769 100644 --- a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/page/admin/configuration/PageDebugView.java +++ b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/page/admin/configuration/PageDebugView.java @@ -16,6 +16,7 @@ package com.evolveum.midpoint.web.page.admin.configuration; +import com.evolveum.midpoint.gui.api.model.LoadableModel; import com.evolveum.midpoint.gui.api.util.WebComponentUtil; import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils; import com.evolveum.midpoint.model.api.ModelExecuteOptions; @@ -50,6 +51,7 @@ import org.apache.wicket.model.AbstractReadOnlyModel; import org.apache.wicket.model.IModel; import org.apache.wicket.model.Model; +import org.apache.wicket.model.PropertyModel; import org.apache.wicket.util.string.StringValue; import javax.xml.namespace.QName; @@ -83,7 +85,7 @@ public class PageDebugView extends PageAdminConfiguration { final Form mainForm = new com.evolveum.midpoint.web.component.form.Form("mainForm"); private String dataLanguage; private boolean isInitialized = false; - private ObjectViewDto objectViewDto = new ObjectViewDto(); + private IModel objectViewDtoModel; public PageDebugView() { @@ -104,7 +106,7 @@ protected IModel createPageTitleModel() { @Override public String getObject() { - if (objectViewDto == null) { + if (objectViewDtoModel == null || objectViewDtoModel.getObject() == null) { return ""; } // ObjectViewDto object; @@ -118,62 +120,69 @@ public String getObject() { // if (object == null) { // return ""; // } else { - return createStringResource("PageDebugView.title", objectViewDto.getName()).getString(); + return createStringResource("PageDebugView.title", objectViewDtoModel.getObject().getName()).getString(); // } } }; } private void initObjectViewObject() { - StringValue objectOid = getPageParameters().get(PARAM_OBJECT_ID); - if (objectOid == null || StringUtils.isEmpty(objectOid.toString())) { - getSession().error(getString("pageDebugView.message.oidNotDefined")); - throw new RestartResponseException(PageDebugList.class); - } + objectViewDtoModel = new LoadableModel(false) { + @Override + protected ObjectViewDto load() { + ObjectViewDto objectViewDto = new ObjectViewDto(); + StringValue objectOid = getPageParameters().get(PARAM_OBJECT_ID); + if (objectOid == null || StringUtils.isEmpty(objectOid.toString())) { + getSession().error(getString("pageDebugView.message.oidNotDefined")); + throw new RestartResponseException(PageDebugList.class); + } - Task task = createSimpleTask(OPERATION_LOAD_OBJECT); - OperationResult result = task.getResult(); //todo is this result != null ? - try { - MidPointApplication application = PageDebugView.this.getMidpointApplication(); - - GetOperationOptions rootOptions = GetOperationOptions.createRaw(); - - rootOptions.setResolveNames(true); - rootOptions.setTolerateRawData(true); - Collection> options = SelectorOptions.createCollection(rootOptions); - // FIXME: ObjectType.class will not work well here. We need more specific type. - //todo on page debug list create page params, put there oid and class for object type and send that to this page....read it here - Class type = ObjectType.class; - StringValue objectType = getPageParameters().get(PARAM_OBJECT_TYPE); - if (objectType != null && StringUtils.isNotBlank(objectType.toString())) { - type = getPrismContext().getSchemaRegistry().determineCompileTimeClass(new QName(SchemaConstantsGenerated.NS_COMMON, objectType.toString())); - } + Task task = createSimpleTask(OPERATION_LOAD_OBJECT); + OperationResult result = task.getResult(); //todo is this result != null ? + try { + MidPointApplication application = PageDebugView.this.getMidpointApplication(); - // TODO make this configurable (or at least do not show campaign cases in production) - WebModelServiceUtils.addIncludeOptionsForExportOrView(options, type); - PrismObject object = getModelService().getObject(type, objectOid.toString(), options, task, result); + GetOperationOptions rootOptions = GetOperationOptions.createRaw(); - PrismContext context = application.getPrismContext(); + rootOptions.setResolveNames(true); + rootOptions.setTolerateRawData(true); + Collection> options = SelectorOptions.createCollection(rootOptions); + // FIXME: ObjectType.class will not work well here. We need more specific type. + //todo on page debug list create page params, put there oid and class for object type and send that to this page....read it here + Class type = ObjectType.class; + StringValue objectType = getPageParameters().get(PARAM_OBJECT_TYPE); + if (objectType != null && StringUtils.isNotBlank(objectType.toString())) { + type = getPrismContext().getSchemaRegistry().determineCompileTimeClass(new QName(SchemaConstantsGenerated.NS_COMMON, objectType.toString())); + } - String lex = context.serializerFor(dataLanguage).serialize(object); - objectViewDto = new ObjectViewDto(object.getOid(), WebComponentUtil.getName(object), object, lex); + // TODO make this configurable (or at least do not show campaign cases in production) + WebModelServiceUtils.addIncludeOptionsForExportOrView(options, type); + PrismObject object = getModelService().getObject(type, objectOid.toString(), options, task, result); - result.recomputeStatus(); - } catch (Exception ex) { - result.recordFatalError("Couldn't load object.", ex); - } + PrismContext context = application.getPrismContext(); - if (objectViewDto == null) { - showResult(result); - throw new RestartResponseException(PageDebugList.class); - } + String lex = context.serializerFor(dataLanguage).serialize(object); + objectViewDto = new ObjectViewDto(object.getOid(), WebComponentUtil.getName(object), object, lex); - showResult(result, false); - - if (!WebComponentUtil.isSuccessOrHandledErrorOrWarning(result)) { - showResult(result, false); - throw new RestartResponseException(PageDebugList.class); - } + result.recomputeStatus(); + } catch (Exception ex) { + result.recordFatalError("Couldn't load object.", ex); + } + + if (objectViewDto == null) { + showResult(result); + throw new RestartResponseException(PageDebugList.class); + } + + showResult(result, false); + + if (!WebComponentUtil.isSuccessOrHandledErrorOrWarning(result)) { + showResult(result, false); + throw new RestartResponseException(PageDebugList.class); + } + return objectViewDto; + } + }; } private void initLayout() { @@ -222,7 +231,7 @@ protected void onUpdate(AjaxRequestTarget target) { } }); - plainTextarea = new TextArea<>(ID_PLAIN_TEXTAREA, getObjectViewXmlModel()); + plainTextarea = new TextArea<>(ID_PLAIN_TEXTAREA, new PropertyModel<>(objectViewDtoModel, ObjectViewDto.F_XML)); plainTextarea.setVisible(false); mainForm.add(plainTextarea); @@ -235,7 +244,7 @@ protected void onUpdate(AjaxRequestTarget target) { } private void addOrReplaceEditor(){ - editor = new AceEditor("aceEditor", getObjectViewXmlModel()); + editor = new AceEditor("aceEditor", new PropertyModel<>(objectViewDtoModel, ObjectViewDto.F_XML)); editor.setModeForDataLanguage(dataLanguage); editor.add(new AjaxFormComponentUpdatingBehavior("blur") { private static final long serialVersionUID = 1L; @@ -255,14 +264,14 @@ private void initViewButton(Form mainForm) { @Override protected void onLanguageSwitched(AjaxRequestTarget target, int updatedIndex, String updatedLanguage, String objectString) { - objectViewDto.setXml(objectString); + objectViewDtoModel.getObject().setXml(objectString); dataLanguage = updatedLanguage; addOrReplaceEditor(); target.add(mainForm); } @Override protected String getObjectStringRepresentation() { - return objectViewDto.getXml(); + return objectViewDtoModel.getObject().getXml(); } @Override protected boolean isValidateSchema() { @@ -315,7 +324,7 @@ private boolean isReport(PrismObject object){ } public void savePerformed(AjaxRequestTarget target) { - if (StringUtils.isEmpty(objectViewDto.getXml())) { + if (StringUtils.isEmpty(objectViewDtoModel.getObject().getXml())) { error(getString("pageDebugView.message.cantSaveEmpty")); target.add(getFeedbackPanel()); return; @@ -325,7 +334,7 @@ public void savePerformed(AjaxRequestTarget target) { OperationResult result = task.getResult(); try { - PrismObject oldObject = objectViewDto.getObject(); + PrismObject oldObject = objectViewDtoModel.getObject().getObject(); oldObject.revive(getPrismContext()); Holder objectHolder = new Holder<>(null); @@ -380,25 +389,7 @@ public void savePerformed(AjaxRequestTarget target) { } private void validateObject(OperationResult result, Holder objectHolder) { - parseObject(objectViewDto.getXml(), objectHolder, dataLanguage, validateSchema.getObject(), false, Objectable.class, result); + parseObject(objectViewDtoModel.getObject().getXml(), objectHolder, dataLanguage, validateSchema.getObject(), false, Objectable.class, result); } - private IModel getObjectViewXmlModel(){ - return new IModel() { - @Override - public String getObject() { - return objectViewDto != null ? objectViewDto.getXml() : ""; - } - - @Override - public void setObject(String s) { - objectViewDto.setXml(s); - } - - @Override - public void detach() { - - } - }; - } }