Skip to content

Commit

Permalink
adding implementation of default search item panels for debug page
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Dec 2, 2020
1 parent 3fffff8 commit 3652aef
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 198 deletions.
Expand Up @@ -94,7 +94,7 @@ protected String getSearchByNameParameterValue() {

protected Search createSearch() {
return SearchFactory.createSearch(type.getClassDefinition(), isCollectionViewPanelForCompiledView() ? getCollectionNameParameterValue().toString() : null,
getFixedSearchItems(), null, getPageBase(), true);
getFixedSearchItems(), null, getPageBase(), null, true, true);
}

protected List<ItemPath> getFixedSearchItems() {
Expand Down
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2010-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.web.component.search;

import javax.xml.namespace.QName;

import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.Referencable;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.DisplayableValue;

import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author skublik
*/
public class ObjectClassSearchItem extends PropertySearchItem {

private static final long serialVersionUID = 1L;
private String lastResourceOid;

public ObjectClassSearchItem(Search search, SearchItemDefinition definition) {
super(search, definition);
}

@Override
public List<DisplayableValue> getAllowedValues(PageBase pageBase) {
List<DisplayableValue> list = new ArrayList<>();
for (PropertySearchItem property : getSearch().getPropertyItems()) {
if (ShadowType.F_RESOURCE_REF.equivalent(property.getPath())
&& property.getValue() != null && property.getValue().getValue() != null) {
Referencable ref = (Referencable) property.getValue().getValue();
Task task = pageBase.createSimpleTask("load resource");
if (StringUtils.isNotBlank(ref.getOid())) {
PrismObject<ResourceType> resource = WebModelServiceUtils.loadObject(ref, pageBase,
task, task.getResult());
if (resource != null) {
lastResourceOid = resource.getOid();
List<QName> objectClasses = WebComponentUtil.loadResourceObjectClassValues(resource.asObjectable(), pageBase);
for (QName objectClass : objectClasses) {
list.add(new SearchValue(objectClass, pageBase.createStringResource(objectClass.getLocalPart()).getString()));
}
}
break;
}
}

}
return list;
}

@Override
public Type getType() {
return Type.ENUM;
}

@Override
public DisplayableValue getValue() {
for (PropertySearchItem property : getSearch().getPropertyItems()) {
if (ShadowType.F_RESOURCE_REF.equivalent(property.getPath())
&& property.getValue() != null && property.getValue().getValue() != null) {
Referencable ref = (Referencable) property.getValue().getValue();
if (StringUtils.isNotBlank(ref.getOid())
&& ref.getOid().equals(lastResourceOid)) {
return super.getValue();
}
break;
}

}
return new SearchValue<>();
}

@Override
protected String getTitle(PageBase pageBase) {
for (PropertySearchItem property : getSearch().getPropertyItems()) {
if (ShadowType.F_RESOURCE_REF.equivalent(property.getPath())
&& property.getValue() != null && property.getValue().getValue() != null) {
Referencable ref = (Referencable) property.getValue().getValue();
if (StringUtils.isNotBlank(ref.getOid())) {
return super.getTitle(pageBase);
}
}
}
return pageBase.createStringResource("ObjectClassSearchItem.notFoundResourceItemSearchPanel").getString();
}
}
Expand Up @@ -60,7 +60,7 @@ public void setValue(DisplayableValue<T> value) {
this.value = value;
}

public List<DisplayableValue<T>> getAllowedValues() {
public List<DisplayableValue<T>> getAllowedValues(PageBase pageBase) {
List<DisplayableValue<T>> list = new ArrayList<>();
if (!(getDefinition().getDef() instanceof PrismPropertyDefinition)) {
return list;
Expand Down
Expand Up @@ -52,7 +52,7 @@ protected List<QName> getSupportedTargetList() {
@Override
protected void confirmPerformed(AjaxRequestTarget target) {
target.add(ReferenceValueSearchPanel.this);
referenceValueUpdated(ReferenceValueSearchPanel.this.getModelObject());
referenceValueUpdated(ReferenceValueSearchPanel.this.getModelObject(), target);
}
};
value.setRenderBodyOnly(true);
Expand All @@ -69,6 +69,6 @@ protected String load() {
};
}

protected void referenceValueUpdated(ObjectReferenceType ort) {
protected void referenceValueUpdated(ObjectReferenceType ort, AjaxRequestTarget target) {
}
}
Expand Up @@ -162,6 +162,8 @@ public SearchItem addItem(ItemDefinition def) {
PropertySearchItem item;
if (QNameUtil.match(itemToRemove.getDef().getTypeName(), DOMUtil.XSD_DATETIME)) {
item = new DateSearchItem(this, itemToRemove);
} else if (ShadowType.F_OBJECT_CLASS.equivalent(itemToRemove.getPath())) {
item = new ObjectClassSearchItem(this, itemToRemove);
} else {
item = new PropertySearchItem(this, itemToRemove);
}
Expand Down Expand Up @@ -366,6 +368,16 @@ private ObjectFilter createFilterForSearchValue(PropertySearchItem item, Display
String text = (String) searchValue.getValue();
return ctx.queryFor(ObjectType.class)
.item(path, propDef).contains(text).matchingCaseIgnore().buildFilter();
} else if (DOMUtil.XSD_QNAME.equals(propDef.getTypeName())) {
Object value = searchValue.getValue();
QName qName;
if (value instanceof QName) {
qName = (QName) value;
} else {
qName = new QName((String) value);
}
return ctx.queryFor(ObjectType.class)
.item(path, propDef).eq(qName).buildFilter();
} else if (DOMUtil.XSD_DATETIME.equals(propDef.getTypeName())) {
if (((DateSearchItem) item).getFromDate() != null && ((DateSearchItem) item).getToDate() != null) {
return ctx.queryFor(ObjectType.class)
Expand Down
Expand Up @@ -9,7 +9,6 @@
import java.lang.reflect.Modifier;
import java.util.*;

import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordType;

import com.evolveum.prism.xml.ns._public.types_3.PolyStringTranslationType;
Expand Down Expand Up @@ -102,7 +101,8 @@ public class SearchFactory {
// ItemPath.create(ReportType.F_NAME)
// ));
SEARCHABLE_OBJECTS.put(ShadowType.class, Arrays.asList(
// ItemPath.create(ShadowType.F_OBJECT_CLASS),
ItemPath.create(ShadowType.F_OBJECT_CLASS),
ItemPath.create(ShadowType.F_RESOURCE_REF),
ItemPath.create(ShadowType.F_DEAD),
ItemPath.create(ShadowType.F_INTENT),
ItemPath.create(ShadowType.F_EXISTS),
Expand Down Expand Up @@ -183,7 +183,7 @@ public static <C extends Containerable> Search createContainerSearch(Class<C> ty
PrismContainerDefinition<C> containerDef = modelServiceLocator.getPrismContext().getSchemaRegistry().findContainerDefinitionByCompileTimeClass(type);
List<SearchItemDefinition> availableDefs = defaultAvailableDefs;
if (CollectionUtils.isEmpty(defaultAvailableDefs)) {
availableDefs = getAvailableDefinitions(containerDef, true);
availableDefs = getAvailableDefinitions(containerDef, null, true);
}

Search search = new Search(type, availableDefs);
Expand Down Expand Up @@ -212,43 +212,29 @@ public static <T extends ObjectType> Search createSearch(Class<T> type, ModelSer
public static <T extends ObjectType> Search createSearch(
Class<T> type, ResourceShadowDiscriminator discriminator,
ModelServiceLocator modelServiceLocator, boolean useDefsFromSuperclass) {
return createSearch(type, null, null, discriminator, modelServiceLocator, useDefsFromSuperclass);
return createSearch(type, null, null, discriminator, modelServiceLocator, null, useDefsFromSuperclass, true);
}

public static <T extends ObjectType> Search createSearch(
Class<T> type, String collectionViewName, List<ItemPath> fixedSearchItems, ResourceShadowDiscriminator discriminator,
ModelServiceLocator modelServiceLocator, boolean useDefsFromSuperclass) {
ModelServiceLocator modelServiceLocator, List<ItemPath> availableItemPath, boolean useDefsFromSuperclass, boolean useObjectCollection) {

PrismObjectDefinition objectDef = findObjectDefinition(type, discriminator, modelServiceLocator);
List<SearchItemDefinition> availableDefs = getAvailableDefinitions(objectDef, useDefsFromSuperclass);
List<SearchItemDefinition> availableDefs = getAvailableDefinitions(objectDef, availableItemPath, useDefsFromSuperclass);
boolean isFullTextSearchEnabled = isFullTextSearchEnabled(modelServiceLocator, type);

Search search = new Search(type, availableDefs, isFullTextSearchEnabled,
getDefaultSearchType(modelServiceLocator, type, collectionViewName));

SchemaRegistry registry = modelServiceLocator.getPrismContext().getSchemaRegistry();

PrismObjectDefinition objDef = registry.findObjectDefinitionByCompileTimeClass(type);
SearchItemsType searchItemsConfig = getConfiguredSearchItems(modelServiceLocator, type, collectionViewName);
List<SearchItemDefinition> configuredSearchItemDefs = getConfiguredSearchItemDefinitions(objectDef, useDefsFromSuperclass, searchItemsConfig);
if (!CollectionUtils.isEmpty(configuredSearchItemDefs)) {
configuredSearchItemDefs.forEach(searchItemDef -> {
search.addItemToAllDefinitions(searchItemDef);
if (searchItemDef.isShowAsDefault()) {
SearchItem item = null;
if (searchItemDef.getPath() != null) {
ItemDefinition def = objDef.findItemDefinition(searchItemDef.getPath());
item = search.addItem(def);
((PropertySearchItem) item).setDisplayName(searchItemDef.getDisplayName());
} else if (searchItemDef.getPredefinedFilter() != null) {
item = search.addItem(searchItemDef.getPredefinedFilter());
}
if (item != null) {
item.setFixed(true);
item.setDefinition(searchItemDef);
}
}
});

List<SearchItemDefinition> configuredSearchItemDefs = null;
if (useObjectCollection) {
configuredSearchItemDefs = getConfiguredSearchItemDefinitions(availableDefs, modelServiceLocator, type, collectionViewName);
}
if (useObjectCollection && !CollectionUtils.isEmpty(configuredSearchItemDefs)) {
processSearchItemDefFromCompiledView(configuredSearchItemDefs, search, objDef);
} else {
if (CollectionUtils.isEmpty(fixedSearchItems)) {
fixedSearchItems = new ArrayList<>();
Expand All @@ -266,6 +252,26 @@ public static <T extends ObjectType> Search createSearch(
return search;
}

public static void processSearchItemDefFromCompiledView(List<SearchItemDefinition> configuredSearchItemDefs, Search search, PrismObjectDefinition objDef) {
configuredSearchItemDefs.forEach(searchItemDef -> {
search.addItemToAllDefinitions(searchItemDef);
if (searchItemDef.isShowAsDefault()) {
SearchItem item = null;
if (searchItemDef.getPath() != null) {
ItemDefinition def = objDef.findItemDefinition(searchItemDef.getPath());
item = search.addItem(def);
((PropertySearchItem) item).setDisplayName(searchItemDef.getDisplayName());
} else if (searchItemDef.getPredefinedFilter() != null) {
item = search.addItem(searchItemDef.getPredefinedFilter());
}
if (item != null) {
item.setFixed(true);
item.setDefinition(searchItemDef);
}
}
});
}

public static <T extends ObjectType> PrismObjectDefinition findObjectDefinition(
Class<T> type, ResourceShadowDiscriminator discriminator,
ModelServiceLocator modelServiceLocator) {
Expand All @@ -292,9 +298,13 @@ public static <T extends ObjectType> PrismObjectDefinition findObjectDefinition(
}
}

private static <C extends Containerable> List<SearchItemDefinition> getConfiguredSearchItemDefinitions(PrismContainerDefinition<C> objectDef,
boolean useDefsFromSuperclass, SearchItemsType configuredSearchItems) {
List<SearchItemDefinition> availableDefinitions = getAvailableDefinitions(objectDef, useDefsFromSuperclass);
public static <T extends ObjectType> List<SearchItemDefinition> getConfiguredSearchItemDefinitions(List<SearchItemDefinition> availableDefinitions,
ModelServiceLocator modelServiceLocator, Class<T> type, String collectionViewName) {
SearchBoxConfigurationType searchConfig = getSearchBoxConfiguration(modelServiceLocator, type, collectionViewName);
if (searchConfig == null) {
return null;
}
SearchItemsType configuredSearchItems = searchConfig.getSearchItems();
if (configuredSearchItems == null || CollectionUtils.isEmpty(configuredSearchItems.getSearchItem())) {
return null;
}
Expand All @@ -319,8 +329,7 @@ private static <C extends Containerable> List<SearchItemDefinition> getConfigure
}

public static <C extends Containerable> List<SearchItemDefinition> getAvailableDefinitions(
PrismContainerDefinition<C> objectDef, boolean useDefsFromSuperclass) {
// Map<ItemPath, ItemDefinition> map = new HashMap<>();
PrismContainerDefinition<C> objectDef, List<ItemPath> availableItemPath, boolean useDefsFromSuperclass) {
List<SearchItemDefinition> definitions = new ArrayList<>();

if (objectDef == null) {
Expand All @@ -331,7 +340,7 @@ public static <C extends Containerable> List<SearchItemDefinition> getAvailableD

Class<C> typeClass = objectDef.getCompileTimeClass();
while (typeClass != null && !com.evolveum.prism.xml.ns._public.types_3.ObjectType.class.equals(typeClass)) {
List<ItemPath> paths = SEARCHABLE_OBJECTS.get(typeClass);
List<ItemPath> paths = CollectionUtils.isEmpty(availableItemPath) ? SEARCHABLE_OBJECTS.get(typeClass) : availableItemPath;
if (paths != null) {
for (ItemPath path : paths) {
ItemDefinition def = objectDef.findItemDefinition(path);
Expand Down Expand Up @@ -370,14 +379,6 @@ private static <T extends ObjectType> SearchBoxModeType getDefaultSearchType(Mod
return searchConfig.getDefaultMode();
}

private static <T extends ObjectType> SearchItemsType getConfiguredSearchItems(ModelServiceLocator modelServiceLocator, Class<T> type, String collectionViewName) {
SearchBoxConfigurationType searchConfig = getSearchBoxConfiguration(modelServiceLocator, type, collectionViewName);
if (searchConfig == null) {
return null;
}
return searchConfig.getSearchItems();
}

private static <T extends ObjectType> boolean isAllowToConfigureSearchItems(ModelServiceLocator modelServiceLocator, Class<T> type, String collectionViewName) {
SearchBoxConfigurationType searchConfig = getSearchBoxConfiguration(modelServiceLocator, type, collectionViewName);
if (searchConfig == null || searchConfig.isAllowToConfigureSearchItems() == null) {
Expand Down
Expand Up @@ -14,6 +14,8 @@

import com.evolveum.midpoint.web.util.InfoTooltipBehavior;

import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;

import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
Expand Down Expand Up @@ -79,15 +81,20 @@ protected void initSearchItemField(WebMarkupContainer searchItemContainer) {
case REFERENCE:
searchItemField = new ReferenceValueSearchPanel(ID_SEARCH_ITEM_FIELD,
new PropertyModel<>(getModel(), "value.value"),
(PrismReferenceDefinition) item.getDefinition());
(PrismReferenceDefinition) item.getDefinition().getDef()){
@Override
protected void referenceValueUpdated(ObjectReferenceType ort, AjaxRequestTarget target) {
searchPerformed(target);
}
};
break;
case BOOLEAN:
choices = (IModel) createBooleanChoices();
case ENUM:
if (choices == null) {
choices = new ListModel<>(item.getAllowedValues());
choices = new ListModel<>(item.getAllowedValues(getPageBase()));
}
searchItemField = new DropDownChoicePanel<>(ID_SEARCH_ITEM_FIELD, new PropertyModel<>(getModel(), "value.value"),
searchItemField = new DropDownChoicePanel<>(ID_SEARCH_ITEM_FIELD, new PropertyModel<>(getModel(), "value"),
choices, new IChoiceRenderer<DisplayableValue>() {
private static final long serialVersionUID = 1L;

Expand Down
Expand Up @@ -653,14 +653,16 @@ public boolean isVisible() {
return false;
}

ItemPath propertyPath = property.getPath();
for (SearchItem searchItem : search.getItems()) {
if (searchItem instanceof FilterSearchItem) {
return searchItem.getDefinition().equals(property);
}
if (propertyPath != null && QNameUtil.match(propertyPath.lastName(), ((PropertySearchItem) searchItem).getPath().lastName())) {
if (searchItem.getDefinition().equals(property)) {
return false;
}
if (searchItem instanceof PropertySearchItem) {
ItemPath propertyPath = property.getPath();
if (propertyPath != null && QNameUtil.match(propertyPath.lastName(), ((PropertySearchItem) searchItem).getPath().lastName())) {
return false;
}
}
}

MoreDialogDto dto = moreDialogModel.getObject();
Expand Down

0 comments on commit 3652aef

Please sign in to comment.