Skip to content

Commit

Permalink
adding sorting for properties and values of multivalue item (MID-6690)
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Feb 24, 2021
1 parent a55bda7 commit 3f7502f
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 34 deletions.
Expand Up @@ -8,17 +8,22 @@
package com.evolveum.midpoint.web.component.prism.show;

import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.model.api.visualizer.Name;
import com.evolveum.midpoint.model.api.visualizer.Scene;
import com.evolveum.midpoint.model.api.visualizer.SceneDeltaItem;
import com.evolveum.midpoint.model.api.visualizer.SceneItem;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.delta.ChangeType;
import com.evolveum.midpoint.web.security.factory.channel.AbstractChannelFactory;

import org.apache.wicket.Component;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
Expand All @@ -31,9 +36,11 @@ public class SceneDto implements Serializable {
public static final java.lang.String F_DESCRIPTION = "description";
public static final java.lang.String F_ITEMS = "items";
public static final java.lang.String F_PARTIAL_SCENES = "partialScenes";
public static final java.lang.String F_SORTED = "sorted";

@NotNull private final Scene scene;
private boolean minimized;
private boolean sorted = false;

private String boxClassOverride;

Expand Down Expand Up @@ -71,6 +78,19 @@ public List<SceneDto> getPartialScenes() {
}

public List<SceneItemDto> getItems() {
if (isSorted()) {
List<SceneItemDto> itemsClone = new ArrayList<>();
itemsClone.addAll(items);
Collator collator = WebComponentUtil.getCollator();
Comparator<? super SceneItemDto> comparator =
(s1, s2) -> {
String name1 = PageBase.createStringResourceStatic(null, s1.getName()).getString();
String name2 = PageBase.createStringResourceStatic(null, s2.getName()).getString();
return collator.compare(name1, name2);
};
itemsClone.sort(comparator);
return itemsClone;
}
return items;
}

Expand Down Expand Up @@ -137,6 +157,14 @@ public void setBoxClassOverride(String boxClassOverride) {
this.boxClassOverride = boxClassOverride;
}

public boolean isSorted() {
return sorted;
}

public void setSorted(boolean sorted) {
this.sorted = sorted;
}

// minimized is NOT included in equality check - because the SceneDto's are compared in order to determine
// whether they should be redrawn (i.e. their content is important, not the presentation)

Expand Down
Expand Up @@ -7,12 +7,16 @@

package com.evolveum.midpoint.web.component.prism.show;

import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.model.api.visualizer.*;
import org.apache.commons.lang3.Validate;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

Expand All @@ -26,7 +30,7 @@ public class SceneItemDto implements Serializable {

@NotNull private final SceneItem sceneItem;
@NotNull private final SceneDto sceneDto;
@NotNull private final List<SceneItemLineDto> lines;
@NotNull private List<SceneItemLineDto> lines;

public SceneItemDto(@NotNull SceneDto sceneDto, @NotNull SceneItem sceneItem) {
Validate.notNull(sceneDto);
Expand Down Expand Up @@ -54,6 +58,7 @@ public String getNewValue() {
public List<SceneItemLineDto> computeLines() {
List<SceneItemLineDto> rv = new ArrayList<>();
int index = 0;
Collator collator = WebComponentUtil.getCollator();
if (!isDelta()) {
for (SceneItemValue itemValue : sceneItem.getNewValues()) {
rv.add(new SceneItemLineDto(this, null, itemValue, index++, false));
Expand All @@ -63,14 +68,51 @@ public List<SceneItemLineDto> computeLines() {
for (SceneItemValue itemValue : deltaItem.getUnchangedValues()) {
rv.add(new SceneItemLineDto(this, null, itemValue, index++, false));
}
Iterator<? extends SceneItemValue> deletedValuesIter = deltaItem.getDeletedValues().iterator();
Iterator<? extends SceneItemValue> addedValuesIter = deltaItem.getAddedValues().iterator();
List<? extends SceneItemValue> deletedValues = deltaItem.getDeletedValues();
List<? extends SceneItemValue> addedValues = deltaItem.getAddedValues();
Comparator<? super SceneItemValue> comparator =
(s1, s2) -> {
String value1 = s1 == null ? null : s1.getText();
String value2 = s2 == null ? null : s2.getText();
if (value1 == null && value2 == null) {
return 0;
} else if (value1 == null) {
return -1;
} else if (value2 == null) {
return 1;
}
return collator.compare(value1, value2);
};
deletedValues.sort(comparator);
addedValues.sort(comparator);

Iterator<? extends SceneItemValue> deletedValuesIter = deletedValues.iterator();
Iterator<? extends SceneItemValue> addedValuesIter = addedValues.iterator();
while (deletedValuesIter.hasNext() || addedValuesIter.hasNext()) {
SceneItemValue deletedValue = deletedValuesIter.hasNext() ? deletedValuesIter.next() : null;
SceneItemValue addedValue = addedValuesIter.hasNext() ? addedValuesIter.next() : null;
rv.add(new SceneItemLineDto(this, deletedValue, addedValue, index++, true));
}
}
Comparator<? super SceneItemLineDto> comparator =
(s1, s2) -> {
if (s1.isDelta()){
return 1;
} else if (s2.isDelta()){
return -1;
}
String value1 = s1.getNewValue() == null ? null : s1.getNewValue().getText();
String value2 = s2.getNewValue() == null ? null : s2.getNewValue().getText();
if (value1 == null && value2 == null) {
return 0;
} else if (value1 == null) {
return -1;
} else if (value2 == null) {
return 1;
}
return collator.compare(value1, value2);
};
rv.sort(comparator);
return rv;
}

Expand Down
Expand Up @@ -53,7 +53,7 @@ private void initLayout() {
nameCell.add(new AttributeModifier("rowspan",
new PropertyModel<Integer>(getModel(), SceneItemLineDto.F_NUMBER_OF_LINES)));

Label label = new Label("name",createStringResource("${name}", getModel()));
Label label = new Label(ID_NAME, createStringResource("${name}", getModel()));
nameCell.add(label);
nameCell.add(new VisibleEnableBehaviour() {
@Override
Expand Down
Expand Up @@ -7,15 +7,18 @@

package com.evolveum.midpoint.web.component.prism.show;

import com.evolveum.midpoint.gui.api.GuiStyleConstants;
import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.model.api.validator.StringLimitationResult;
import com.evolveum.midpoint.model.api.visualizer.SceneItemValue;
import com.evolveum.midpoint.prism.PrismReferenceValue;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.web.component.data.column.AjaxLinkPanel;
import com.evolveum.midpoint.web.component.data.column.ImagePanel;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.util.ObjectTypeGuiDescriptor;
import com.evolveum.midpoint.xml.ns._public.common.common_3.DisplayType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;

import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
Expand Down Expand Up @@ -64,7 +67,17 @@ public boolean isVisible() {
}
};

final ImagePanel icon = new ImagePanel(ID_ICON, new IconModel(), new TitleModel());
IModel<DisplayType> displayModel = (IModel) () -> {
ObjectTypeGuiDescriptor guiDescriptor = getObjectTypeDescriptor();
String cssClass = ObjectTypeGuiDescriptor.ERROR_ICON;
String title = null;
if (guiDescriptor != null) {
cssClass = guiDescriptor.getBlackIcon();
title = createStringResource(guiDescriptor.getLocalizationKey()).getObject();
}
return WebComponentUtil.createDisplayType(cssClass, "", title);
};
final ImagePanel icon = new ImagePanel(ID_ICON, displayModel);
icon.add(visibleIfReference);
add(icon);

Expand Down Expand Up @@ -131,22 +144,6 @@ private ObjectTypeGuiDescriptor getObjectTypeDescriptor() {
}
}

private class IconModel implements IModel<String> {
@Override
public String getObject() {
ObjectTypeGuiDescriptor guiDescriptor = getObjectTypeDescriptor();
return guiDescriptor != null ? guiDescriptor.getBlackIcon() : ObjectTypeGuiDescriptor.ERROR_ICON;
}
}

private class TitleModel implements IModel<String> {
@Override
public String getObject() {
ObjectTypeGuiDescriptor guiDescriptor = getObjectTypeDescriptor();
return guiDescriptor != null ? createStringResource(guiDescriptor.getLocalizationKey()).getObject() : null;
}
}

private class LabelModel implements IModel<String> {
@Override
public String getObject() {
Expand Down
Expand Up @@ -24,7 +24,10 @@ <h3 class="box-title" style="margin-right: 35px;">
<div wicket:id="body" class="box-body no-padding">
<table class="table table-striped table-hover table-bordered table-word-break" wicket:id="itemsTable">
<tr>
<th style="min-width: 100px; !important"><wicket:message key="ScenePanel.item"/></th>
<th style="min-width: 100px; !important">
<wicket:message key="ScenePanel.item"/>
<button wicket:id="sortProperties" wicket:message="title:PrismObjectPanel.sortProperties" class="btn btn-box-tool"></button>
</th>
<th wicket:id="oldValueLabel"><wicket:message key="ScenePanel.oldValue"/></th>
<th wicket:id="newValueLabel"><wicket:message key="ScenePanel.newValue"/></th>
<th wicket:id="valueLabel"><wicket:message key="ScenePanel.value"/></th>
Expand Down
Expand Up @@ -7,9 +7,12 @@

package com.evolveum.midpoint.web.component.prism.show;

import com.evolveum.midpoint.gui.api.GuiStyleConstants;
import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.component.togglebutton.ToggleIconButton;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.gui.impl.prism.panel.PrismContainerValuePanel;
import com.evolveum.midpoint.model.api.visualizer.Scene;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContainerValue;
Expand Down Expand Up @@ -51,18 +54,19 @@ public class ScenePanel extends BasePanel<SceneDto> {
private static final String ID_SHOW_OPERATIONAL_ITEMS_LINK = "showOperationalItemsLink";

private static final Trace LOGGER = TraceManager.getTrace(ScenePanel.class);
public static final String ID_OPTION_BUTTONS = "optionButtons";
public static final String ID_HEADER_PANEL = "headerPanel";
public static final String ID_HEADER_DESCRIPTION = "description";
public static final String ID_HEADER_WRAPPER_DISPLAY_NAME = "wrapperDisplayName";
public static final String ID_HEADER_NAME_LABEL = "nameLabel";
public static final String ID_HEADER_NAME_LINK = "nameLink";
public static final String ID_HEADER_CHANGE_TYPE = "changeType";
public static final String ID_HEADER_OBJECT_TYPE = "objectType";
public static final String ID_BODY = "body";
public static final String ID_OLD_VALUE_LABEL = "oldValueLabel";
public static final String ID_NEW_VALUE_LABEL = "newValueLabel";
public static final String ID_VALUE_LABEL = "valueLabel";
private static final String ID_OPTION_BUTTONS = "optionButtons";
private static final String ID_HEADER_PANEL = "headerPanel";
private static final String ID_HEADER_DESCRIPTION = "description";
private static final String ID_HEADER_WRAPPER_DISPLAY_NAME = "wrapperDisplayName";
private static final String ID_HEADER_NAME_LABEL = "nameLabel";
private static final String ID_HEADER_NAME_LINK = "nameLink";
private static final String ID_HEADER_CHANGE_TYPE = "changeType";
private static final String ID_HEADER_OBJECT_TYPE = "objectType";
private static final String ID_BODY = "body";
private static final String ID_OLD_VALUE_LABEL = "oldValueLabel";
private static final String ID_NEW_VALUE_LABEL = "newValueLabel";
private static final String ID_VALUE_LABEL = "valueLabel";
private static final String ID_SORT_PROPERTIES = "sortProperties";

private boolean showOperationalItems = false;
private boolean operationalItemsVisible = false;
Expand Down Expand Up @@ -243,6 +247,27 @@ public boolean isVisible() {
return !model.getObject().getItems().isEmpty();
}
});
itemsTable.setOutputMarkupId(true);

ToggleIconButton<String> sortPropertiesButton = new ToggleIconButton<String>(ID_SORT_PROPERTIES,
GuiStyleConstants.CLASS_ICON_SORT_ALPHA_ASC, GuiStyleConstants.CLASS_ICON_SORT_AMOUNT_ASC) {

private static final long serialVersionUID = 1L;

@Override
public void onClick(AjaxRequestTarget target) {
onSortClicked(model, target);
}

@Override
public boolean isOn() {
return model.getObject().isSorted();
}
};
sortPropertiesButton.setOutputMarkupId(true);
sortPropertiesButton.setOutputMarkupPlaceholderTag(true);
itemsTable.add(sortPropertiesButton);

WebMarkupContainer oldValueLabel = new WebMarkupContainer(ID_OLD_VALUE_LABEL);
oldValueLabel.add(new VisibleEnableBehaviour() {
@Override
Expand Down Expand Up @@ -326,6 +351,12 @@ public IModel<?> getBody() {
body.add(showOperationalItemsLink);
}

private void onSortClicked(IModel<SceneDto> model, AjaxRequestTarget target) {
model.getObject().setSorted(!model.getObject().isSorted());
target.add(get(getPageBase().createComponentPath(ID_BOX, ID_BODY, ID_ITEMS_TABLE)));
target.add(get(getPageBase().createComponentPath(ID_BOX, ID_BODY, ID_ITEMS_TABLE, ID_SORT_PROPERTIES)));
}

protected boolean isExistingViewableObject() {
final Scene scene = getModelObject().getScene();
final PrismContainerValue<?> value = scene.getSourceValue();
Expand Down

0 comments on commit 3f7502f

Please sign in to comment.