Skip to content

Commit

Permalink
MID-7976 better relation panel handling, in cooperation with cart ass…
Browse files Browse the repository at this point in the history
…ignment editor (relation listed there as well)
  • Loading branch information
1azyman committed Aug 2, 2022
1 parent b3361e8 commit 4c44159
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;


/**
* @author lskublik
*/
Expand Down Expand Up @@ -142,17 +141,20 @@ protected IModel<String> getSubTextModel() {
}

public boolean onNextPerformed(AjaxRequestTarget target) {
getWizard().next();
target.add(getWizard().getPanel());
WizardModel model = getWizard();
if (model.hasNext()) {
model.next();
target.add(model.getPanel());
}

return false;
}

public boolean onBackPerformed(AjaxRequestTarget target) {
int index = getWizard().getActiveStepIndex();
if (index > 0) {
getWizard().previous();
target.add(getWizard().getPanel());
WizardModel model = getWizard();
if (model.hasPrevious()) {
model.previous();
target.add(model.getPanel());
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public void init(Page page) {
String stepId = getStepIdFromParams(page);
if (stepId != null) {
setActiveStepById(stepId);
} else {
for (int i = 0; i < steps.size(); i++) {
WizardStep step = steps.get(i);

if (BooleanUtils.isTrue(step.isStepVisible().getObject())) {
activeStepIndex = i;
break;
}
}
}

fireActiveStepChanged(getActiveStep());
Expand Down Expand Up @@ -113,17 +122,13 @@ public void setActiveStepById(String id) {
WizardStep step = steps.get(i);

if (Objects.equals(id, step.getStepId()) && BooleanUtils.isTrue(step.isStepVisible().getObject())) {
setActiveStepIndex(i);
activeStepIndex = i;
break;
}
}
}

public int getActiveStepIndex() {
return activeStepIndex;
}

public int getActiveStepVisibleIndex() {
int index = 0;
for (int i = 0; i < activeStepIndex; i++) {
if (BooleanUtils.isTrue(steps.get(i).isStepVisible().getObject())) {
Expand All @@ -133,27 +138,70 @@ public int getActiveStepVisibleIndex() {
return index;
}

private void setActiveStepIndex(int activeStepIndex) {
if (activeStepIndex < 0) {
return;
public boolean hasNext() {
return findNextStep() != null;
}

private WizardStep findNextStep() {
for (int i = activeStepIndex + 1; i < steps.size(); i++) {
if (i >= steps.size()) {
return null;
}

if (BooleanUtils.isTrue(steps.get(i).isStepVisible().getObject())) {
return steps.get(i);
}
}
if (activeStepIndex >= steps.size()) {

return null;
}

public void next() {
int index = activeStepIndex;

WizardStep next = findNextStep();
if (next == null) {
return;
}

this.activeStepIndex = activeStepIndex;
activeStepIndex = steps.indexOf(next);

if (index != activeStepIndex) {
fireActiveStepChanged(getActiveStep());
}
}

public boolean hasPrevious() {
return findPreviousStep() != null;
}

public void next() {
setActiveStepIndex(activeStepIndex + 1);
private WizardStep findPreviousStep() {
for (int i = activeStepIndex - 1; i >= 0; i--) {
if (i < 0) {
return null;
}

fireActiveStepChanged(getActiveStep());
if (BooleanUtils.isTrue(steps.get(i).isStepVisible().getObject())) {
return steps.get(i);
}
}

return null;
}

public void previous() {
setActiveStepIndex(activeStepIndex - 1);
int index = activeStepIndex;

fireActiveStepChanged(getActiveStep());
WizardStep previous = findPreviousStep();
if (previous == null) {
return;
}

activeStepIndex = steps.indexOf(previous);

if (index != activeStepIndex) {
fireActiveStepChanged(getActiveStep());
}
}

public WizardStep getNextPanel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void initLayout() {
protected void populateItem(ListItem<IModel<String>> listItem) {
WizardHeaderStepPanel step = new WizardHeaderStepPanel(ID_STEP, listItem.getIndex(), listItem.getModelObject());
// todo fix, if steps are invisible index might shift?
step.add(AttributeAppender.append("class", () -> wizardModel.getActiveStepVisibleIndex() == listItem.getIndex() ? "active" : null));
step.add(AttributeAppender.append("class", () -> wizardModel.getActiveStepIndex() == listItem.getIndex() ? "active" : null));
listItem.add(step);

WebMarkupContainer line = new WebMarkupContainer(ID_LINE);
Expand All @@ -144,7 +144,7 @@ protected void onBackPerformed(AjaxRequestTarget target) {
return;
}

if (wizardModel.getActiveStepIndex() == 0) {
if (!wizardModel.hasPrevious()) {
getPageBase().redirectBack();
} else {
wizardModel.previous();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,6 @@ private void initLayout() {
WizardPanel wizard = new WizardPanel(ID_WIZARD, new WizardModel(createSteps()));
wizard.setOutputMarkupId(true);
mainForm.add(wizard);

// List<ListGroupMenuItem> list = new ArrayList<>();
// ListGroupMenuItem allRoles = new ListGroupMenuItem("fa fa-fw fa-border-all", "All roles");
// allRoles.setActive(true);
// list.add(allRoles);
//
// ListGroupMenuItem rolesOfTeamMate = new ListGroupMenuItem("fa fa-fw fa-users", "Roles of team mate");
// rolesOfTeamMate.setBadge("12");
// list.add(rolesOfTeamMate);
//
// ListGroupMenuItem menu2 = new ListGroupMenuItem("fa fa-fw fa-users", "Roles of team mate");
// list.add(menu2);
//
// ListGroupMenuItem o1 = new ListGroupMenuItem(null, "Option 1");
// menu2.getItems().add(o1);
// ListGroupMenuItem o2 = new ListGroupMenuItem(null, "Option 2");
// menu2.getItems().add(o2);
// ListGroupMenuItem o3 = new ListGroupMenuItem(null, "Option 3");
// menu2.getItems().add(o3);
// ListGroupMenuItem o31 = new ListGroupMenuItem(null, "Option 31");
// o3.getItems().add(o31);
//
// ListGroupMenuPanel sample = new ListGroupMenuPanel("sample", Model.ofList(list));
// add(sample);
}

private List<WizardStep> createSteps() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public void onClick(AjaxRequestTarget target) {
private void editItemPerformed(AjaxRequestTarget target, IModel<ShoppingCartItem> model) {
PageBase page = getPageBase();

ShoppingCartEditPanel panel = new ShoppingCartEditPanel(model) {
ShoppingCartEditPanel panel = new ShoppingCartEditPanel(model, getModel()) {

@Override
protected void savePerformed(AjaxRequestTarget target, IModel<ShoppingCartItem> model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,6 @@ public void init(WizardModel wizard) {
if (canSkipStep()) {
// no user input needed, we'll populate model with data
submitData();

wizard.next();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import java.util.List;
import javax.xml.namespace.QName;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.list.ListItem;
Expand All @@ -24,13 +22,8 @@
import com.evolveum.midpoint.gui.api.model.LoadableModel;
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.gui.impl.component.tile.Tile;
import com.evolveum.midpoint.gui.impl.component.tile.TilePanel;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.web.component.util.EnableBehaviour;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
Expand All @@ -44,9 +37,6 @@ public class RelationPanel extends BasicWizardStepPanel<RequestAccess> implement

public static final String STEP_ID = "relation";

private static final String DOT_CLASS = RelationPanel.class.getName() + ".";
private static final String OPERATION_LOAD_ASSIGNABLE_RELATIONS_LIST = DOT_CLASS + "loadAssignableRelationsList";

private static final String DEFAULT_RELATION_ICON = "fa-solid fa-user";

private static final String ID_LIST_CONTAINER = "listContainer";
Expand Down Expand Up @@ -95,8 +85,6 @@ public void init(WizardModel wizard) {
if (canSkipStep()) {
// no user input needed, we'll populate model with data
submitData();

wizard.next();
}
}

Expand All @@ -110,26 +98,22 @@ private void initModels() {

@Override
protected List<Tile<QName>> load() {
QName currentRelation = getModelObject().getRelation();
RequestAccess ra = getModelObject();

QName currentRelation = ra.getRelation();
if (currentRelation == null) {
currentRelation = getDefaultRelation();
currentRelation = ra.getDefaultRelation();
getModelObject().setRelation(currentRelation);
}

List<Tile<QName>> tiles = new ArrayList<>();

RelationSelectionType config = getRelationConfiguration();
List<QName> list = getAvailableRelationsList();
for (QName name : list) {
List<QName> availableRelations = ra.getAvailableRelations(page);
for (QName name : availableRelations) {
Tile<QName> tile = createTileForRelation(name);
tile.setSelected(name.equals(currentRelation));
tile.setValue(name);

if (BooleanUtils.isFalse(config.isAllowOtherRelations()) && !tile.isSelected()) {
// skip non default tiles as other relations are not allowed
continue;
}

tiles.add(tile);
}

Expand All @@ -138,16 +122,6 @@ protected List<Tile<QName>> load() {
};
}

private QName getDefaultRelation() {
RelationSelectionType config = getRelationConfiguration();
QName defaultRelation = null;
if (config != null) {
defaultRelation = config.getDefaultRelation();
}

return defaultRelation != null ? defaultRelation : SchemaConstants.ORG_DEFAULT;
}

@Override
public IModel<String> getTitle() {
return createStringResource("RelationPanel.title");
Expand Down Expand Up @@ -223,35 +197,6 @@ private Tile<QName> createTileForRelation(QName name) {
return tile;
}

private List<QName> getAvailableRelationsList() {
List<ObjectReferenceType> personsOfInterest = getModelObject().getPersonOfInterest();
if (personsOfInterest.isEmpty()) {
return new ArrayList<>();
}

ObjectReferenceType ref = personsOfInterest.get(0);

FocusType focus;
try {
PrismObject<UserType> prismFocus = WebModelServiceUtils.loadObject(ref, page);
focus = prismFocus.asObjectable();
} catch (Exception ex) {
page.error(getString("RelationPanel.loadRelationsError", ref.getTargetName(), ref.getOid()));
return new ArrayList<>();
}

Task task = page.createSimpleTask(OPERATION_LOAD_ASSIGNABLE_RELATIONS_LIST);
OperationResult result = task.getResult();
List<QName> assignableRelationsList = WebComponentUtil.getAssignableRelationsList(
focus.asPrismObject(), RoleType.class, WebComponentUtil.AssignmentOrder.ASSIGNMENT, result, task, page);

if (CollectionUtils.isEmpty(assignableRelationsList)) {
return WebComponentUtil.getCategoryRelationChoices(AreaCategoryType.SELF_SERVICE, page);
}

return assignableRelationsList;
}

@Override
public VisibleEnableBehaviour getNextBehaviour() {
return new EnableBehaviour(() -> relations.getObject().stream().filter(t -> t.isSelected()).count() > 0);
Expand Down

0 comments on commit 4c44159

Please sign in to comment.