Skip to content

Commit

Permalink
adding fix for basic expression field
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Feb 28, 2020
1 parent f4f7496 commit 3b33660
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 107 deletions.
Expand Up @@ -11,14 +11,11 @@
import com.evolveum.midpoint.gui.impl.prism.PrismPropertyWrapper;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.web.component.AceEditor;
import com.evolveum.midpoint.web.page.admin.configuration.component.EmptyOnBlurAjaxFormUpdatingBehaviour;
import com.evolveum.midpoint.web.page.admin.configuration.component.EmptyOnChangeAjaxFormUpdatingBehavior;
import com.evolveum.midpoint.web.page.admin.reports.component.AceEditorPanel;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
import org.apache.wicket.markup.html.panel.Panel;
import org.springframework.stereotype.Component;

Expand All @@ -35,7 +32,7 @@ public void register() {

@Override
protected Panel getPanel(PrismPropertyPanelContext<ExpressionType> panelCtx) {
AceEditorPanel conditionPanel = new AceEditorPanel(panelCtx.getComponentId(), null, new ConditionExpressionModel(panelCtx.getRealValueModel(), panelCtx.getPageBase()), 200){
AceEditorPanel conditionPanel = new AceEditorPanel(panelCtx.getComponentId(), null, new ExpressionModel(panelCtx.getRealValueModel(), panelCtx.getPageBase()), 200){
@Override
protected boolean isResizeToMaxHeight() {
return false;
Expand Down
@@ -1,99 +1,96 @@
/*
* Copyright (c) 2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.gui.impl.factory;

import com.evolveum.midpoint.web.util.ExpressionUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType;

import com.evolveum.prism.xml.ns._public.types_3.RawType;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.model.IModel;

import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.prism.xml.ns._public.query_3.SearchFilterType;

import javax.xml.bind.JAXBElement;
import java.util.List;

public class ConditionExpressionModel implements IModel<String> {

private static final Trace LOGGER = TraceManager.getTrace(ConditionExpressionModel.class);

private static final long serialVersionUID = 1L;

private IModel<ExpressionType> baseModel;
private PageBase pageBase;

public ConditionExpressionModel(IModel<ExpressionType> valueWrapper, PageBase pageBase) {
this.baseModel = valueWrapper;
this.pageBase = pageBase;
}

@Override
public void detach() {
// TODO Auto-generated method stub

}

@Override
public String getObject() {
try {
ExpressionType value = baseModel.getObject();
if (value == null) {
return null;
}

List<JAXBElement<?>> evaluatros = value.getExpressionEvaluator();
//should be one
if (CollectionUtils.isEmpty(evaluatros)) {
return null;
}
if (evaluatros.size() > 1) {
LOGGER.warn("More than one evaluator found. getting first of them");
}

JAXBElement<?> evaluator = evaluatros.get(0);
if (evaluator == null) {
return null;
}

return ExpressionUtil.serialize(evaluator, pageBase.getPrismContext());

} catch (SchemaException e) {
// TODO handle!!!!
LoggingUtils.logUnexpectedException(LOGGER, "Cannot serialize filter", e);
// getSession().error("Cannot serialize filter");
}
return null;
}

@Override
public void setObject(String object) {
if (StringUtils.isBlank(object)) {
baseModel.setObject(null);
return;
}

try {
ExpressionType condition = new ExpressionType();
ExpressionUtil.parseExpressionEvaluators(object, condition, pageBase.getPrismContext());
// ExpressionType condition = pageBase.getPrismContext().parserFor(object).parseRealValue();
baseModel.setObject(condition);
} catch (Exception e) {
// TODO handle!!!!
LoggingUtils.logUnexpectedException(LOGGER, "Cannot parse filter", e);
// getSession().error("Cannot parse filter");
}

}
}
/*
* Copyright (c) 2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.gui.impl.factory;

import com.evolveum.midpoint.web.util.ExpressionUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.model.IModel;

import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

import javax.xml.bind.JAXBElement;
import java.util.List;

public class ExpressionModel implements IModel<String> {

private static final Trace LOGGER = TraceManager.getTrace(ExpressionModel.class);

private static final long serialVersionUID = 1L;

private IModel<ExpressionType> baseModel;
private PageBase pageBase;

public ExpressionModel(IModel<ExpressionType> valueWrapper, PageBase pageBase) {
this.baseModel = valueWrapper;
this.pageBase = pageBase;
}

@Override
public void detach() {
// TODO Auto-generated method stub

}

@Override
public String getObject() {
try {
ExpressionType value = baseModel.getObject();
if (value == null) {
return null;
}

List<JAXBElement<?>> evaluatros = value.getExpressionEvaluator();
//should be one
if (CollectionUtils.isEmpty(evaluatros)) {
return null;
}
if (evaluatros.size() > 1) {
LOGGER.warn("More than one evaluator found. getting first of them");
}

JAXBElement<?> evaluator = evaluatros.get(0);
if (evaluator == null) {
return null;
}

return ExpressionUtil.serialize(evaluator, pageBase.getPrismContext());

} catch (SchemaException e) {
// TODO handle!!!!
LoggingUtils.logUnexpectedException(LOGGER, "Cannot serialize filter", e);
// getSession().error("Cannot serialize filter");
}
return null;
}

@Override
public void setObject(String object) {
if (StringUtils.isBlank(object)) {
baseModel.setObject(null);
return;
}

try {
ExpressionType condition = new ExpressionType();
ExpressionUtil.parseExpressionEvaluators(object, condition, pageBase.getPrismContext());
// ExpressionType condition = pageBase.getPrismContext().parserFor(object).parseRealValue();
baseModel.setObject(condition);
} catch (Exception e) {
// TODO handle!!!!
LoggingUtils.logUnexpectedException(LOGGER, "Cannot parse filter", e);
// getSession().error("Cannot parse filter");
}

}
}
Expand Up @@ -8,9 +8,9 @@
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<wicket:panel>
<div class="col-md-2 col-xs-12 prism-property-label " wicket:id="header"/>
<div class="col-lg-2 col-md-4 col-xs-12 prism-property-label " wicket:id="header"/>
<div class="row">
<div class="col-md-12 col-xs-12 prism-property-value" wicket:id="values">
<div class="col-lg-10 col-md-8 col-xs-12 prism-property-value" wicket:id="values">
<div wicket:id="expressionPanel"/>
</div>
</div>
Expand Down
Expand Up @@ -8,6 +8,7 @@

import com.evolveum.midpoint.gui.api.factory.GuiComponentFactory;
import com.evolveum.midpoint.gui.api.model.ReadOnlyValueModel;
import com.evolveum.midpoint.gui.impl.factory.ExpressionModel;
import com.evolveum.midpoint.gui.impl.factory.ItemRealValueModel;
import com.evolveum.midpoint.gui.impl.factory.WrapperContext;
import com.evolveum.midpoint.gui.impl.prism.*;
Expand All @@ -20,6 +21,9 @@
import com.evolveum.midpoint.web.component.input.TextPanel;
import com.evolveum.midpoint.web.component.prism.ValueStatus;
import com.evolveum.midpoint.web.component.util.VisibleBehaviour;
import com.evolveum.midpoint.web.page.admin.configuration.component.EmptyOnBlurAjaxFormUpdatingBehaviour;
import com.evolveum.midpoint.web.page.admin.configuration.component.EmptyOnChangeAjaxFormUpdatingBehavior;
import com.evolveum.midpoint.web.page.admin.reports.component.AceEditorPanel;
import com.evolveum.midpoint.web.util.ExpressionUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType;
import org.apache.commons.collections.CollectionUtils;
Expand Down Expand Up @@ -99,7 +103,15 @@ protected Component createValuePanel(ListItem<PrismPropertyValueWrapper<Expressi
expressionPanel.add(new VisibleBehaviour(() -> isExpanded));
item.add(expressionPanel);
} else {
expressionPanel = new TextPanel<ExpressionType>(ID_EXPRESSION_PANEL, Model.of(getModelObject().getItem().getRealValue()));
ItemRealValueModel<ExpressionType> realValueModel = new ItemRealValueModel<ExpressionType>(item.getModel());
expressionPanel = new AceEditorPanel(ID_EXPRESSION_PANEL, null, new ExpressionModel(realValueModel, getPageBase()), 200){
@Override
protected boolean isResizeToMaxHeight() {
return false;
}
};
((AceEditorPanel)expressionPanel).getEditor().add(new EmptyOnBlurAjaxFormUpdatingBehaviour());
((AceEditorPanel)expressionPanel).getEditor().add(new EmptyOnChangeAjaxFormUpdatingBehavior());
expressionPanel.add(new VisibleBehaviour(() -> isExpanded));
item.add(expressionPanel);
}
Expand Down
Expand Up @@ -272,7 +272,7 @@ public static void parseExpressionEvaluators(String xml, ExpressionType expressi
expressionObject.getExpressionEvaluator().clear();
if (StringUtils.isNotBlank(xml)) {
xml = WebXmlUtil.wrapInElement("expression", xml, true);
LOGGER.info("Expression to serialize: {}", xml);
LOGGER.trace("Expression to serialize: {}", xml);
JAXBElement<?> newElement = context.parserFor(xml).xml().parseRealValueToJaxbElement();
expressionObject.getExpressionEvaluator().addAll(((ExpressionType) (newElement.getValue())).getExpressionEvaluator());
}
Expand Down

0 comments on commit 3b33660

Please sign in to comment.