Skip to content

Commit

Permalink
MID-8518:fix for filter parsing panel for attributes of resource
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Mar 22, 2023
1 parent 2228849 commit 2fb7036
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2023 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.panel;

import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.impl.page.admin.resource.PageResource;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.schema.processor.ResourceObjectDefinition;
import com.evolveum.midpoint.schema.util.ShadowUtil;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import com.evolveum.prism.xml.ns._public.query_3.SearchFilterType;

import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import javax.xml.namespace.QName;

public class ResourceAttributeSearchFilterTypeForQueryModel extends SearchFilterTypeForQueryModel<ShadowType> {

private static final long serialVersionUID = 1L;

private final IModel<QName> objectClass;

public ResourceAttributeSearchFilterTypeForQueryModel(
IModel<SearchFilterType> valueWrapper, PageBase pageBase, boolean useParsing, IModel<QName> objectClass) {
super(valueWrapper, pageBase, Model.of(ShadowType.class), useParsing);
this.objectClass = objectClass;
}

protected void parseQuery(String object) throws SchemaException, ConfigurationException {
PrismObjectDefinition<ShadowType> def = PrismContext.get().getSchemaRegistry().findObjectDefinitionByCompileTimeClass(ShadowType.class);
ResourceObjectDefinition objectClassDef =
((PageResource) getPageBase()).getObjectDetailsModels().getRefinedSchema().findDefinitionForObjectClass(objectClass.getObject());
PrismObjectDefinition<ShadowType> newDef = ShadowUtil.applyObjectDefinition(def, objectClassDef);

ObjectFilter objectFilter = getPageBase().getPrismContext().createQueryParser().parseFilter(newDef, object);
SearchFilterType filter = getPageBase().getQueryConverter().createSearchFilterType(objectFilter);
filter.setText(object);
getBaseModel().setObject(filter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2023 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.panel;

import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.gui.api.prism.wrapper.ItemWrapper;
import com.evolveum.midpoint.gui.api.prism.wrapper.PrismPropertyWrapper;
import com.evolveum.midpoint.gui.api.prism.wrapper.PrismValueWrapper;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.page.admin.reports.component.SearchFilterConfigurationPanel;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.query_3.SearchFilterType;

import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.springframework.stereotype.Component;

import javax.xml.namespace.QName;
import java.io.Serializable;

@Component
public class ResourceAttributesSearchFilterPanelFactory extends SearchFilterPanelFactory implements Serializable {

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

@Override
public <IW extends ItemWrapper<?, ?>> boolean match(IW wrapper) {
return super.match(wrapper)
&& ItemPath.create(
ResourceType.F_SCHEMA_HANDLING,
SchemaHandlingType.F_OBJECT_TYPE,
ResourceObjectTypeDefinitionType.F_DELINEATION,
ResourceObjectTypeDelineationType.F_FILTER)
.equivalent(wrapper.getPath().namedSegmentsOnly());
}

@Override
protected Panel getPanel(PrismPropertyPanelContext<SearchFilterType> panelCtx) {
return new SearchFilterConfigurationPanel(
panelCtx.getComponentId(), panelCtx.getItemWrapperModel(), panelCtx.getRealValueModel(), null) {
@Override
protected IModel<String> createQueryModel(IModel model, LoadableModel filterTypeModel, boolean useParsing) {
ItemRealValueModel<QName> objectClassModel = new ItemRealValueModel<>((IModel<? extends PrismValueWrapper<QName>>) () -> {
try {
PrismPropertyWrapper<QName> objectClass =
getItemModelObject().getParent().findProperty(ResourceObjectTypeDelineationType.F_OBJECT_CLASS);
return objectClass.getValue();

} catch (SchemaException e) {
LOGGER.error("Couldn't find object class property.");
return null;
}
});
return new ResourceAttributeSearchFilterTypeForQueryModel(model, getPageBase(), useParsing, objectClassModel);
}
};
}

@Override
public Integer getOrder() {
return Integer.MAX_VALUE - 10;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ protected Panel getPanel(PrismPropertyPanelContext<SearchFilterType> panelCtx) {
PrismContainerValueWrapper<?> containerWrapper = searchFilterItemWrapper.getParent();
if (containerWrapper != null && containerWrapper.getRealValue() instanceof ObjectCollectionType) {
return new SearchFilterConfigurationPanel(
panelCtx.getComponentId(), panelCtx.getRealValueModel(), containerWrapper);
panelCtx.getComponentId(), panelCtx.getItemWrapperModel(), panelCtx.getRealValueModel(), containerWrapper);
}
return new SearchFilterConfigurationPanel(
panelCtx.getComponentId(), panelCtx.getRealValueModel(), null);
panelCtx.getComponentId(), panelCtx.getItemWrapperModel(), panelCtx.getRealValueModel(), null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
package com.evolveum.midpoint.gui.impl.factory.panel;

import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.SchemaException;

import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.ThreadContext;
import org.apache.wicket.model.IModel;
Expand Down Expand Up @@ -68,13 +71,17 @@ public void setObject(String object) {
return;
}
try {
ObjectFilter objectFilter = getPageBase().getPrismContext().createQueryParser().parseFilter(filterTypeModel.getObject(), object);
SearchFilterType filter = getPageBase().getQueryConverter().createSearchFilterType(objectFilter);
filter.setText(object);
getBaseModel().setObject(filter);
parseQuery(object);
} catch (Exception e) {
LoggingUtils.logUnexpectedException(LOGGER, "Cannot parse filter", e);
ThreadContext.getSession().error("Cannot parse filter: " + e.getMessage() + ". For more details, please, see midpoint log");
}
}

protected void parseQuery(String object) throws SchemaException, ConfigurationException {
ObjectFilter objectFilter = getPageBase().getPrismContext().createQueryParser().parseFilter(filterTypeModel.getObject(), object);
SearchFilterType filter = getPageBase().getQueryConverter().createSearchFilterType(objectFilter);
filter.setText(object);
getBaseModel().setObject(filter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

import javax.xml.namespace.QName;

import com.evolveum.midpoint.gui.api.page.PageBase;

import com.evolveum.midpoint.gui.api.prism.wrapper.PrismPropertyWrapper;

import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
Expand Down Expand Up @@ -62,8 +66,12 @@ private enum FieldType {

private FieldType fieldType;

public SearchFilterConfigurationPanel(String id, IModel<SearchFilterType> model, PrismContainerValueWrapper<ObjectCollectionType> containerWrapper) {
private final IModel<PrismPropertyWrapper<SearchFilterType>> itemModel;

public SearchFilterConfigurationPanel(String id, IModel<PrismPropertyWrapper<SearchFilterType>> itemModel,
IModel<SearchFilterType> model, PrismContainerValueWrapper<ObjectCollectionType> containerWrapper) {
super(id, model);
this.itemModel = itemModel;
this.containerWrapper = containerWrapper;
// todo why resolving model in constructor?
if (model.getObject() == null || StringUtils.isNotBlank(model.getObject().getText())) {
Expand Down Expand Up @@ -106,7 +114,7 @@ private void initLayout() {
aceEditorField.add(new VisibleBehaviour(() -> FieldType.XML.equals(fieldType)));
container.add(aceEditorField);

TextPanel textPanel = new TextPanel(ID_TEXT_FIELD, new SearchFilterTypeForQueryModel<>(getModel(), getPageBase(), filterTypeModel, containerWrapper != null));
TextPanel textPanel = new TextPanel(ID_TEXT_FIELD, createQueryModel(getModel(), filterTypeModel, containerWrapper != null));
textPanel.add(new VisibleBehaviour(() -> FieldType.QUERY.equals(fieldType)));
container.add(textPanel);

Expand Down Expand Up @@ -147,6 +155,10 @@ public void onClick(AjaxRequestTarget target) {
add(fieldTypeButton);
}

protected IModel<String> createQueryModel(IModel<SearchFilterType> model, LoadableModel<Class<O>> filterTypeModel, boolean useParsing) {
return new SearchFilterTypeForQueryModel<>(model, getPageBase(), filterTypeModel, useParsing);
}

private void searchConfigurationPerformed(AjaxRequestTarget target) {
filterTypeModel.reset();
SearchPropertiesConfigPanel<O> configPanel = new SearchPropertiesConfigPanel<>(getPageBase().getMainPopupBodyId(),
Expand All @@ -171,4 +183,8 @@ protected void filterConfiguredPerformed(ObjectFilter configuredFilter, AjaxRequ
};
getPageBase().showMainPopup(configPanel, target);
}

public PrismPropertyWrapper<SearchFilterType> getItemModelObject() {
return itemModel.getObject();
}
}

0 comments on commit 2fb7036

Please sign in to comment.