Skip to content

Commit

Permalink
MID-6274 popup to select task execution mode before simulation starts
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Mar 2, 2023
1 parent fc53188 commit 49f9fc3
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.evolveum.midpoint.gui.api.util;

import java.util.Locale;

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

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.authentication.api.util.AuthUtil;
import com.evolveum.midpoint.common.LocalizationService;
import com.evolveum.midpoint.model.api.authentication.GuiProfiledPrincipal;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.util.LocalizableMessage;
import com.evolveum.midpoint.web.security.MidPointApplication;
import com.evolveum.midpoint.web.security.MidPointAuthWebSession;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LocalizableMessageType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableRowType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.Locale;

public class LocalizationUtil {

Expand Down Expand Up @@ -43,7 +41,7 @@ public static String translate(String key) {
}

public static String translate(String key, Object[] params) {
return translate(key, params, null);
return translate(key, params, key);
}

public static String translate(String key, Object[] params, String defaultMsg) {
Expand Down Expand Up @@ -118,4 +116,17 @@ public static String translatePolyString(PolyString poly, Locale locale) {

return poly.getOrig();
}

public static String translateEnum(Enum<?> e) {
return translateEnum(e, null);
}

public static String translateEnum(Enum<?> e, String nullKey) {
if (e == null) {
return nullKey != null ? translate(nullKey) : null;
}

String key = WebComponentUtil.createEnumResourceKey(e);
return translate(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5355,20 +5355,37 @@ public static <C extends Containerable> GuiObjectListViewType getPrincipalUserOb
return objectListView;
}

public static <T> DropDownChoicePanel createDropDownChoices(String id, IModel<DisplayableValue<T>> model, IModel<List<DisplayableValue<T>>> choices,
boolean allowNull) {
return createDropDownChoices(id, model, choices, allowNull, null);
}

/**
* page base parameter not needed, please use {@link WebComponentUtil#createDropDownChoices(String, IModel, IModel, boolean)}
*/
@Deprecated
public static <T> DropDownChoicePanel createDropDownChoices(String id, IModel<DisplayableValue<T>> model, IModel<List<DisplayableValue<T>>> choices,
boolean allowNull, PageBase pageBase) {
return new DropDownChoicePanel(id, model, choices, new IChoiceRenderer<DisplayableValue>() {
private static final long serialVersionUID = 1L;

/**
* TODO This impl doesn't look good, label should take preference, that's why it's there for...
*/
@Override
public Object getDisplayValue(DisplayableValue val) {
if (val.getValue() instanceof Enum) {
return pageBase.createStringResource((Enum<?>) val.getValue()).getString();
Object value = val.getValue();
String label = val.getLabel();

if (value instanceof Enum) {
return com.evolveum.midpoint.gui.api.util.LocalizationUtil.translateEnum((Enum<?>) value);
}

if (val.getLabel() == null) {
return pageBase.createStringResource(String.valueOf(val.getValue())).getString();
return com.evolveum.midpoint.gui.api.util.LocalizationUtil.translate(String.valueOf(value));
}
return pageBase.createStringResource(val.getLabel()).getString();

return com.evolveum.midpoint.gui.api.util.LocalizationUtil.translate(label);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
~ Copyright (c) 2010-2018 Evolveum
~
~ This work is dual-licensed under the Apache License 2.0
~ and European Union Public License. See LICENSE file for details.
-->

<wicket:panel xmlns:wicket="http://wicket.apache.org">
<div class="d-flex flex-column align-items-center gap-4">
<p class="mb-0"><wicket:message key="ChooseTaskExecutionModePopup.text"/></p>

<div wicket:id="dropdown"/>
</div>

<wicket:fragment wicket:id="buttons">
<div class="d-flex flex-wrap gap-2 w-100">
<a class="btn btn-outline-primary" wicket:id="cancel">
<i class="fa fa-xmark mr-1"/>
<span><wicket:message key="ChooseTaskExecutionModePopup.button.cancel"/></span>
</a>
<a class="btn btn-success" wicket:id="select">
<span><wicket:message key="ChooseTaskExecutionModePopup.button.select"/></span>
<i class="fa fa-arrow-right ml-1"/>
</a>
</div>
</wicket:fragment>
</wicket:panel>
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.web.page.admin.shadows;

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.prism.impl.DisplayableValueImpl;
import com.evolveum.midpoint.schema.TaskExecutionMode;
import com.evolveum.midpoint.util.DisplayableValue;
import com.evolveum.midpoint.web.component.AjaxButton;
import com.evolveum.midpoint.web.component.dialog.Popupable;
import com.evolveum.midpoint.web.component.dialog.SimplePopupable;
import com.evolveum.midpoint.web.component.input.DropDownChoicePanel;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.model.IModel;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

public class ChooseTaskExecutionModePopup extends SimplePopupable<DisplayableValue<TaskExecutionMode>> {

private static final String ID_BUTTONS = "buttons";
private static final String ID_DROPDOWN = "dropdown";
private static final String ID_CANCEL_BUTTON = "cancel";
private static final String ID_SELECT_BUTTON = "select";

private Fragment footer;

public ChooseTaskExecutionModePopup(String id) {
super(id, null, 400, 200, PageBase.createStringResourceStatic("ChooseTaskExecutionModePopup.title"));

initLayout();
}

@Override
public IModel<DisplayableValue<TaskExecutionMode>> createModel() {
return new LoadableModel<>(false) {
@Override
protected DisplayableValue<TaskExecutionMode> load() {
return createValue(TaskExecutionMode.SIMULATED_SHADOWS_PRODUCTION);
}
};
}

@Override
public Component getContent() {
return this;
}

@Override
public @NotNull Component getFooter() {
return footer;
}

private DisplayableValue createValue(TaskExecutionMode mode) {
return new DisplayableValueImpl(mode, "TaskExecutionMode." + mode);
}

private void initLayout() {
IModel<List<DisplayableValue<TaskExecutionMode>>> choices = () -> Arrays.asList(
createValue(TaskExecutionMode.SIMULATED_SHADOWS_DEVELOPMENT),
createValue(TaskExecutionMode.SIMULATED_SHADOWS_PRODUCTION));

DropDownChoicePanel<TaskExecutionMode> dropdown = WebComponentUtil.createDropDownChoices(ID_DROPDOWN, getModel(), choices, false);
add(dropdown);

footer = initFooter();
}

private Fragment initFooter() {
Fragment footer = new Fragment(Popupable.ID_FOOTER, ID_BUTTONS, this);

AjaxButton cancelButton = new AjaxButton(ID_CANCEL_BUTTON) {

@Override
public void onClick(AjaxRequestTarget target) {
onCancelPerformed(target);
}
};
footer.add(cancelButton);

AjaxButton selectButton = new AjaxButton(ID_SELECT_BUTTON) {

@Override
public void onClick(AjaxRequestTarget target) {
onSelectPerformed(target, ChooseTaskExecutionModePopup.this.getModelObject().getValue());
}
};
footer.add(selectButton);

return footer;
}

protected void onCancelPerformed(AjaxRequestTarget target) {
getPageBase().hideMainPopup(target);
}

protected void onSelectPerformed(AjaxRequestTarget target, TaskExecutionMode mode) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import com.evolveum.midpoint.gui.api.GuiStyleConstants;
import com.evolveum.midpoint.gui.api.component.MainObjectListPanel;
import com.evolveum.midpoint.gui.api.component.ObjectBrowserPanel;
Expand Down Expand Up @@ -308,7 +307,7 @@ private List<IColumn<SelectableBean<ShadowType>, String>> initColumns() {

@Override
public void populateItem(Item<ICellPopulator<SelectableBean<ShadowType>>> cellItem,
String componentId, IModel<SelectableBean<ShadowType>> rowModel) {
String componentId, IModel<SelectableBean<ShadowType>> rowModel) {

SelectableBean<ShadowType> dto = rowModel.getObject();
RepeatingView repeater = new RepeatingView(componentId);
Expand Down Expand Up @@ -349,7 +348,7 @@ public FocusType getObject() {

@Override
public void onClick(AjaxRequestTarget target, IModel<SelectableBean<ShadowType>> rowModel,
ObjectType targetObjectType) {
ObjectType targetObjectType) {
ownerDetailsPerformed((FocusType) targetObjectType);
}
};
Expand All @@ -362,7 +361,7 @@ public void onClick(AjaxRequestTarget target, IModel<SelectableBean<ShadowType>>

@Override
public void populateItem(Item<ICellPopulator<SelectableBean<ShadowType>>> cellItem,
String componentId, IModel<SelectableBean<ShadowType>> rowModel) {
String componentId, IModel<SelectableBean<ShadowType>> rowModel) {
cellItem.add(new PendingOperationPanel(componentId,
new PropertyModel<>(rowModel, SelectableBeanImpl.F_VALUE + "." + ShadowType.F_PENDING_OPERATION.getLocalPart())));
}
Expand Down Expand Up @@ -433,12 +432,23 @@ private void ownerDetailsPerformed(FocusType owner) {

protected void importPreviewResourceObject(ShadowType selected, AjaxRequestTarget target) {
PageBase page = getPageBase();
page.showMainPopup(new ChooseTaskExecutionModePopup(getPageBase().getMainPopupBodyId()) {

@Override
protected void onSelectPerformed(AjaxRequestTarget target, TaskExecutionMode mode) {
importPreviewResourceObjectConfirmed(mode, selected, target);
}
}, target);
}

protected void importPreviewResourceObjectConfirmed(TaskExecutionMode mode, ShadowType selected, AjaxRequestTarget target) {
PageBase page = getPageBase();

Task task = page.createSimpleTask(OPERATION_IMPORT_PREVIEW_OBJECT);
OperationResult opResult = task.getResult();
try {
String resultOid = page.getModelInteractionService().executeWithSimulationResult(
TaskExecutionMode.SIMULATED_SHADOWS_PRODUCTION,
mode,
null,
task,
opResult,
Expand Down Expand Up @@ -497,7 +507,7 @@ protected void importResourceObject(ShadowType selected, AjaxRequestTarget targe
}

protected void updateResourceObjectStatusPerformed(IModel<SelectableBean<ShadowType>> selected, AjaxRequestTarget target,
boolean enabled) {
boolean enabled) {
List<SelectableBean<ShadowType>> selectedShadow = getSelectedShadowsList(selected);

OperationResult result = new OperationResult(OPERATION_UPDATE_STATUS);
Expand All @@ -522,8 +532,8 @@ protected void updateResourceObjectStatusPerformed(IModel<SelectableBean<ShadowT
getPageBase().getModelService().executeChanges(
MiscUtil.createCollection(deleteDelta), null, task, result);
} catch (ObjectAlreadyExistsException | ObjectNotFoundException | SchemaException
| ExpressionEvaluationException | CommunicationException | ConfigurationException
| PolicyViolationException | SecurityViolationException e) {
| ExpressionEvaluationException | CommunicationException | ConfigurationException
| PolicyViolationException | SecurityViolationException e) {
result.recordPartialError(
createStringResource(
"ResourceContentPanel.message.updateResourceObjectStatusPerformed.partialError", status, shadow)
Expand Down Expand Up @@ -564,7 +574,7 @@ public void yesPerformed(AjaxRequestTarget target) {
}

private void deleteAccountConfirmedPerformed(AjaxRequestTarget target, OperationResult result,
List<SelectableBean<ShadowType>> selected) {
List<SelectableBean<ShadowType>> selected) {
Task task = getPageBase().createSimpleTask(OPERATION_DELETE_OBJECT);

for (SelectableBean<ShadowType> shadow : selected) {
Expand Down Expand Up @@ -606,7 +616,7 @@ private IModel<String> createDeleteConfirmString(List<SelectableBean<ShadowType>
}

private void changeOwner(IModel<SelectableBean<ShadowType>> selected, AjaxRequestTarget target, FocusType ownerToChange,
boolean remove) {
boolean remove) {

getPageBase().hideMainPopup(target);

Expand Down Expand Up @@ -672,8 +682,8 @@ private void markProtectedShadow(IModel<SelectableBean<ShadowType>> model, AjaxR
getPageBase().getModelService().executeChanges(
MiscUtil.createCollection(delta), null, task, result);
} catch (ObjectAlreadyExistsException | ObjectNotFoundException | SchemaException
| ExpressionEvaluationException | CommunicationException | ConfigurationException
| PolicyViolationException | SecurityViolationException e) {
| ExpressionEvaluationException | CommunicationException | ConfigurationException
| PolicyViolationException | SecurityViolationException e) {
result.recordPartialError(
createStringResource(
"ResourceContentPanel.message.markShadowProtectedPerformed.partialError", shadow)
Expand Down Expand Up @@ -704,7 +714,7 @@ private boolean isSatisfyConstraints(List selected) {
}

private void changeOwnerInternal(String ownerOid, Class<? extends FocusType> ownerType, Collection<? extends ItemDelta> modifications,
AjaxRequestTarget target) {
AjaxRequestTarget target) {
OperationResult result = new OperationResult(OPERATION_CHANGE_OWNER);
Task task = getPageBase().createSimpleTask(OPERATION_CHANGE_OWNER);
ObjectDelta<? extends ObjectType> objectDelta =
Expand Down

0 comments on commit 49f9fc3

Please sign in to comment.