Skip to content

Commit

Permalink
fix of search popup for reference object (MID-7138)
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Aug 25, 2021
1 parent dc60374 commit ba67145
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import java.util.Locale;

import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;

import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -52,7 +53,6 @@ public ObjectReferenceType convertToObject(String value, Locale locale) throws C
ObjectQuery query = pageBase.getPrismContext().queryFor(type)
.item(ObjectType.F_NAME)
.eq(value)
// .matchingOrig()
.matchingNorm()
.build();
List<PrismObject<ObjectType>> objectsList = WebModelServiceUtils.searchObjects(
Expand All @@ -74,7 +74,7 @@ public ObjectReferenceType convertToObject(String value, Locale locale) throws C
} else {
ref.setRelation(((ObjectReferenceType) baseComponent.getModelObject()).getRelation());
}
ref.setType(((ObjectReferenceType) baseComponent.getModelObject()).getType());
ref.setType(WebComponentUtil.classToQName(PrismContext.get(), type));
}
}
return ref;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4891,7 +4891,7 @@ public static List<DisplayableValue<?>> getAllowedValues(SearchFilterParameterTy
Object value = null;
try {

value = ExpressionUtil.evaluateExpression(new VariablesMap(), null,
value = ExpressionUtil.evaluateExpressionNative(null, new VariablesMap(), null,
expression, MiscSchemaUtil.getExpressionProfile(),
pageBase.getExpressionFactory(), "evaluate expression for allowed values", task, task.getResult());
} catch (Exception e) {
Expand All @@ -4903,19 +4903,22 @@ public static List<DisplayableValue<?>> getAllowedValues(SearchFilterParameterTy
value = ((PrismPropertyValue) value).getRealValue();
}

if (!(value instanceof List)) {
LOGGER.error("Exception return unexpected type, expected List<DisplayableValue>, but was " + (value == null ? null : value.getClass()));
if (!(value instanceof Set)) {
LOGGER.error("Exception return unexpected type, expected Set<PPV<DisplayableValue>>, but was " + (value == null ? null : value.getClass()));
pageBase.error(pageBase.createStringResource("FilterSearchItem.message.error.wrongType", expression).getString());
return allowedValues;
}

if (!((List<?>) value).isEmpty()) {
if (!(((List<?>) value).get(0) instanceof DisplayableValue)) {
LOGGER.error("Exception return unexpected type, expected List<DisplayableValue>, but was " + (value == null ? null : value.getClass()));
if (!((Set<?>) value).isEmpty()) {
if (!(((Set<?>) value).iterator().next() instanceof PrismPropertyValue)
|| !(((PrismPropertyValue)(((Set<?>) value).iterator().next())).getValue() instanceof DisplayableValue)) {
LOGGER.error("Exception return unexpected type, expected Set<PPV<DisplayableValue>>, but was " + (value == null ? null : value.getClass()));
pageBase.error(pageBase.createStringResource("FilterSearchItem.message.error.wrongType", expression).getString());
return allowedValues;
}
return (List<DisplayableValue<?>>) value;

return (List<DisplayableValue<?>>) ((Set<PrismPropertyValue<?>>) value).stream()
.map(PrismPropertyValue::getValue).collect(Collectors.toList());
}
return allowedValues;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.List;

import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;

import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.IAutoCompleteRenderer;
Expand All @@ -28,12 +27,9 @@
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AbstractRoleType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

import javax.xml.namespace.QName;

/**
* @author honchar
*/
Expand All @@ -52,7 +48,9 @@ public ReferenceAutocomplete(String id, final IModel<ObjectReferenceType> model,
@Override
public Iterator<ObjectReferenceType> getIterator(String input) {
FormComponent<ObjectReferenceType> inputField = getBaseFormComponent();
String realInput = StringUtils.isEmpty(input) ? inputField.getRawInput() : input;
String realInput = StringUtils.isEmpty(input) ?
(!inputField.hasRawInput() ? inputField.getValue() : inputField.getRawInput())
: input;
if (StringUtils.isEmpty(realInput)) {
return Collections.emptyIterator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import javax.xml.namespace.QName;

import com.evolveum.midpoint.gui.api.component.ObjectBrowserPanel;
import com.evolveum.midpoint.gui.api.component.PopupObjectListPanel;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.web.component.AjaxButton;
import com.evolveum.midpoint.web.component.form.MidpointForm;
import com.evolveum.midpoint.web.component.form.ValueChoosePanel;
import com.evolveum.midpoint.web.component.input.DropDownChoicePanel;

import com.evolveum.midpoint.web.component.message.FeedbackAlerts;
import com.evolveum.midpoint.web.component.util.EnableBehaviour;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.ajax.AjaxChannel;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.feedback.ComponentFeedbackMessageFilter;
import org.apache.wicket.markup.html.WebMarkupContainer;
Expand Down Expand Up @@ -79,7 +77,7 @@ public void setObject(String object) {
TextField<String> oidField = new TextField<>(ID_OID, oidModel);
oidField.add(new EmptyOnBlurAjaxFormUpdatingBehaviour());
oidField.setOutputMarkupId(true);
oidField.add(new EnableBehaviour(() -> isItemPanelEnabled()));
oidField.add(new EnableBehaviour(this::isItemPanelEnabled));
midpointForm.add(oidField);

ReferenceAutocomplete nameField = new ReferenceAutocomplete(ID_NAME, Model.of(getModelObject()),
Expand All @@ -92,6 +90,14 @@ public void setObject(String object) {
protected boolean isAllowedNotFoundObjectRef() {
return ReferenceValueSearchPopupPanel.this.isAllowedNotFoundObjectRef();
}

@Override
protected <O extends ObjectType> Class<O> getReferenceTargetObjectType() {
if (getModelObject() != null && getModelObject().getType() != null) {
return (Class<O>) WebComponentUtil.qnameToClass(PrismContext.get(), getModelObject().getType());
}
return super.getReferenceTargetObjectType();
}
};

feedback.setFilter(new ComponentFeedbackMessageFilter(nameField));
Expand All @@ -101,21 +107,14 @@ protected boolean isAllowedNotFoundObjectRef() {

@Override
protected void onUpdate(AjaxRequestTarget target) {
target.add(midpointForm.get(ID_FEEDBACK));

ObjectReferenceType ort = nameField.getBaseFormComponent().getModelObject();
if (ort == null) {
return;
}
ReferenceValueSearchPopupPanel.this.getModel().setObject(ort);
target.add(midpointForm.get(ID_OID));
updateModel(nameField.getBaseFormComponent().getModelObject(), midpointForm, target);
}
});
nameField.setOutputMarkupId(true);
nameField.add(new EnableBehaviour(() -> isItemPanelEnabled()));
nameField.add(new EnableBehaviour(this::isItemPanelEnabled));
midpointForm.add(nameField);

DropDownChoicePanel<QName> type = new DropDownChoicePanel<QName>(ID_TYPE, new PropertyModel<>(getModel(), "type"),
DropDownChoicePanel<QName> type = new DropDownChoicePanel<>(ID_TYPE, new PropertyModel<>(getModel(), "type"),
Model.ofList(getSupportedTargetList()), new QNameObjectTypeChoiceRenderer(), true);
type.setOutputMarkupId(true);
type.add(new VisibleEnableBehaviour() {
Expand All @@ -128,13 +127,25 @@ public boolean isEnabled() {
}
});
type.getBaseFormComponent().add(new EmptyOnBlurAjaxFormUpdatingBehaviour());
type.getBaseFormComponent().add(new AjaxFormComponentUpdatingBehavior("change") {
private static final long serialVersionUID = 1L;

@Override
protected void onUpdate(AjaxRequestTarget target) {
ObjectReferenceType ref = nameField.getAutoCompleteConverter(ObjectReferenceType.class, null)
.convertToObject(nameField.getBaseFormComponent().getValue(), WebComponentUtil.getCurrentLocale());
updateModel(ref, midpointForm, target);
target.add(oidField);
target.add(ReferenceValueSearchPopupPanel.this);
}
});
midpointForm.add(type);

WebMarkupContainer relationContainer = new WebMarkupContainer(ID_RELATION_CONTAINER);
midpointForm.add(relationContainer);
relationContainer.add(new VisibleBehaviour(() -> getAllowedRelations().size() > 0));
List<QName> allowedRelations = new ArrayList<>(getAllowedRelations());
DropDownChoicePanel<QName> relation = new DropDownChoicePanel<QName>(ID_RELATION,
DropDownChoicePanel<QName> relation = new DropDownChoicePanel<>(ID_RELATION,
new PropertyModel<>(getModel(), "relation"),
Model.ofList(allowedRelations), new QNameObjectTypeChoiceRenderer(), true);
relation.setOutputMarkupId(true);
Expand All @@ -161,7 +172,7 @@ public void onClick(AjaxRequestTarget target) {
if (CollectionUtils.isEmpty(supportedTypes)) {
supportedTypes = WebComponentUtil.createObjectTypeList();
}
ObjectBrowserPanel<O> objectBrowserPanel = new ObjectBrowserPanel<O>(
ObjectBrowserPanel<O> objectBrowserPanel = new ObjectBrowserPanel<>(
getPageBase().getMainPopupBodyId(), null, supportedTypes, false, getPageBase(),
null) {
private static final long serialVersionUID = 1L;
Expand All @@ -185,10 +196,20 @@ protected void onSelectPerformed(AjaxRequestTarget target, O object) {
getPageBase().showMainPopup(objectBrowserPanel, target);
}
};
selectObject.add(new VisibleBehaviour(() -> getPageBase().getMainPopup() instanceof WebMarkupContainer));
selectObject.add(new VisibleBehaviour(() -> getPageBase().getMainPopup() != null));
midpointForm.add(selectObject);
}

private void updateModel(ObjectReferenceType ref, MidpointForm<?> midpointForm, AjaxRequestTarget target) {
target.add(getPageBase().getFeedbackPanel());

if (ref == null) {
return;
}
ReferenceValueSearchPopupPanel.this.getModel().setObject(ref);
target.add(midpointForm.get(ID_OID));
}

protected List<QName> getAllowedRelations() {
return WebComponentUtil.getAllRelations(getPageBase());
}
Expand Down

0 comments on commit ba67145

Please sign in to comment.