Skip to content

Commit

Permalink
MID-8607: Added Remove Marks to Resource -> Accounts view
Browse files Browse the repository at this point in the history
  • Loading branch information
tonydamage committed Mar 27, 2023
1 parent b84608a commit f3299b9
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public void setObject(ObjectTypes object) {
this.multiselect = multiselect;
}

@Override
protected void onInitialize(){
super.onInitialize();
initLayout();
Expand Down Expand Up @@ -163,8 +164,7 @@ protected void onUpdate(AjaxRequestTarget target) {
ObjectListPanel<O> listPanel = createObjectListPanel(objType, multiselect);
add(listPanel);

AjaxButton addButton = new AjaxButton(ID_BUTTON_ADD,
createStringResource("userBrowserDialog.button.addButton")) {
AjaxButton addButton = new AjaxButton(ID_BUTTON_ADD, getAddButtonTitle()) {

private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -288,4 +288,8 @@ public Component getContent() {
public StringResourceModel getTitle() {
return parentPage.createStringResource("ObjectBrowserPanel.chooseObject");
}

protected StringResourceModel getAddButtonTitle() {
return createStringResource("userBrowserDialog.button.addButton");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,55 @@ public org.apache.wicket.model.StringResourceModel getTitle() {
}
});

items.add(new InlineMenuItem(createStringResource("pageContentAccounts.menu.mark.remove"), true) {
private static final long serialVersionUID = 1L;

@Override
public InlineMenuItemAction initAction() {
return new ColumnMenuAction<SelectableBean<ShadowType>>() {
private static final long serialVersionUID = 1L;

@Override
public void onSubmit(AjaxRequestTarget target) {

var selected = getSelectedShadowsList(getRowModel());
var selectedMarks = collectExistingMarks(selected);

ObjectFilter marksFilter = PrismContext.get().queryFor(MarkType.class)
.item(MarkType.F_ASSIGNMENT, AssignmentType.F_TARGET_REF)
.ref(SystemObjectsType.ARCHETYPE_OBJECT_MARK.value())
.and()
.id(selectedMarks)
.buildFilter();

ObjectBrowserPanel<MarkType> browser = new ObjectBrowserPanel<>(
getPageBase().getMainPopupBodyId(), MarkType.class,
Collections.singletonList(MarkType.COMPLEX_TYPE), true, getPageBase(), marksFilter) {

protected void addPerformed(AjaxRequestTarget target, QName type, List<MarkType> selected) {
LOGGER.warn("Selected marks: {}", selected);

List<String> markOids = Lists.transform(selected, MarkType::getOid);
removeShadowMarks(getRowModel(), markOids, target);
super.addPerformed(target, type, selected);
}

public org.apache.wicket.model.StringResourceModel getTitle() {
return createStringResource("pageContentAccounts.menu.mark.select.remove");
}

protected org.apache.wicket.model.StringResourceModel getAddButtonTitle() {
return createStringResource("pageContentAccounts.menu.mark.remove");
};
};

getPageBase().showMainPopup(browser, target);
}

};
}
});

return items;
}

Expand Down Expand Up @@ -807,7 +856,7 @@ private <F extends FocusType> void setNewShadowOwner(F ownerToChange, Selectable
changeOwnerInternal(ownerToChange.getOid(), ownerToChange.getClass(), Collections.singletonList(delta), target);
}

private void markShadows(IModel<SelectableBean<ShadowType>> rowModel, List<String> markOids,
private void removeShadowMarks(IModel<SelectableBean<ShadowType>> rowModel, List<String> markOids,
AjaxRequestTarget target) {
OperationResult result = new OperationResult(OPERATION_MARK_SHADOW);
Task task = getPageBase().createSimpleTask(OPERATION_MARK_SHADOW);
Expand All @@ -820,19 +869,70 @@ private void markShadows(IModel<SelectableBean<ShadowType>> rowModel, List<Strin
return;
}

for (SelectableBean<ShadowType> shadow : selected) {
List<PolicyStatementType> statements = new ArrayList<>();
// We recreate statements (can not reuse them between multiple objects - we can create new or clone
// but for each delta we need separate statement
for (String oid : markOids) {
statements.add(new PolicyStatementType().markRef(oid, MarkType.COMPLEX_TYPE)
.type(PolicyStatementTypeType.APPLY));
for (SelectableBean<ShadowType> shadow : selected) {
List<PolicyStatementType> statements = new ArrayList<>();
// We recreate statements (can not reuse them between multiple objects - we can create new or clone
// but for each delta we need separate statement

for (var statement : shadow.getValue().getPolicyStatement()) {
if (!PolicyStatementTypeType.APPLY.equals(statement.getType())) {
continue;
}
try {
var delta = getPageBase().getPrismContext().deltaFactory().object()
.createModificationAddContainer(ShadowType.class,
shadow.getValue().getOid(), ShadowType.F_POLICY_STATEMENT,
statements.toArray(new PolicyStatementType[0]));
if (markOids.contains(statement.getMarkRef().getOid())) {
statements.add(statement.clone());
}
}
try {
var delta = getPageBase().getPrismContext().deltaFactory().object()
.createModificationDeleteContainer(ShadowType.class,
shadow.getValue().getOid(), ShadowType.F_POLICY_STATEMENT,
statements.toArray(new PolicyStatementType[0]));
getPageBase().getModelService().executeChanges(MiscUtil.createCollection(delta), null, task, result);
} catch (ObjectAlreadyExistsException | ObjectNotFoundException | SchemaException
| ExpressionEvaluationException | CommunicationException | ConfigurationException
| PolicyViolationException | SecurityViolationException e) {
result.recordPartialError(
createStringResource(
"ResourceContentPanel.message.markShadowPerformed.partialError", shadow)
.getString(),
e);
LOGGER.error("Could not mark shadow {} with marks {}", shadow, markOids, e);
}
}

result.computeStatusIfUnknown();
getPageBase().showResult(result);
refreshTable(target);
target.add(getPageBase().getFeedbackPanel());
}


private void markShadows(IModel<SelectableBean<ShadowType>> rowModel, List<String> markOids,
AjaxRequestTarget target) {
OperationResult result = new OperationResult(OPERATION_MARK_SHADOW);
Task task = getPageBase().createSimpleTask(OPERATION_MARK_SHADOW);

var selected = getSelectedShadowsList(rowModel);
if (selected == null || selected.isEmpty()) {
result.recordWarning(createStringResource("ResourceContentPanel.message.markShadowPerformed.warning").getString());
getPageBase().showResult(result);
target.add(getPageBase().getFeedbackPanel());
return;
}

for (SelectableBean<ShadowType> shadow : selected) {
List<PolicyStatementType> statements = new ArrayList<>();
// We recreate statements (can not reuse them between multiple objects - we can create new or clone
// but for each delta we need separate statement
for (String oid : markOids) {
statements.add(new PolicyStatementType().markRef(oid, MarkType.COMPLEX_TYPE)
.type(PolicyStatementTypeType.APPLY));
}
try {
var delta = getPageBase().getPrismContext().deltaFactory().object()
.createModificationAddContainer(ShadowType.class,
shadow.getValue().getOid(), ShadowType.F_POLICY_STATEMENT,
statements.toArray(new PolicyStatementType[0]));
getPageBase().getModelService().executeChanges(MiscUtil.createCollection(delta), null, task, result);
} catch (ObjectAlreadyExistsException | ObjectNotFoundException | SchemaException
| ExpressionEvaluationException | CommunicationException | ConfigurationException
Expand Down Expand Up @@ -906,6 +1006,19 @@ private PrismObjectDefinition<FocusType> getFocusDefinition() {
.findObjectDefinitionByCompileTimeClass(FocusType.class);
}


private String[] collectExistingMarks(List<SelectableBean<ShadowType>> selected) {
Set<String> marks = new HashSet<>();
for (SelectableBean<ShadowType> shadow : selected) {
for (var statement : shadow.getValue().getPolicyStatement()) {
if (PolicyStatementTypeType.APPLY.equals(statement.getType())) {
marks.add(statement.getMarkRef().getOid());
}
}
}
return marks.toArray(new String[] {});
}

protected ModelExecuteOptions createModelExecuteOptions() {
return null;
}
Expand Down

0 comments on commit f3299b9

Please sign in to comment.