Skip to content

Commit

Permalink
MID-9538 cleanup improvements and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Mar 18, 2024
1 parent 3c3595c commit 168eea9
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 53 deletions.
@@ -1,6 +1,7 @@
package com.evolveum.midpoint.gui.impl.duplication;

import com.evolveum.midpoint.common.cleanup.CleanupActionProcessor;
import com.evolveum.midpoint.common.cleanup.Source;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.prism.wrapper.PrismContainerValueWrapper;
import com.evolveum.midpoint.gui.api.prism.wrapper.PrismContainerWrapper;
Expand Down Expand Up @@ -128,7 +129,7 @@ public boolean isHeaderMenuItem() {
public static <O extends ObjectType> PrismObject<O> duplicateObjectDefault(PrismObject<O> object) {
PrismObject<O> duplicate = object.cloneComplex(CloneStrategy.REUSE);
CleanupActionProcessor cleanupProcessor = new CleanupActionProcessor();
cleanupProcessor.process(duplicate);
cleanupProcessor.process(duplicate, Source.EMPTY);
duplicate.setOid(null);
return duplicate;
}
Expand All @@ -143,7 +144,7 @@ public static <C extends Containerable> PrismContainerValue<C> duplicateContaine
.iterator().next();
duplicate.setParent(container.getParent());
CleanupActionProcessor cleanupProcessor = new CleanupActionProcessor();
cleanupProcessor.process(duplicate);
cleanupProcessor.process(duplicate, Source.EMPTY);
return duplicate;
}

Expand Down
Expand Up @@ -10,6 +10,8 @@
import java.util.*;
import javax.xml.namespace.QName;

import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.prism.path.ItemPath;
Expand All @@ -24,7 +26,7 @@
*/
public class CleanupActionProcessor {

private CleanupListener listener;
private CleanupHandler handler;

private boolean removeAskActionItemsByDefault = true;

Expand All @@ -42,8 +44,8 @@ public void setRemoveContainerIds(boolean removeContainerIds) {
this.removeContainerIds = removeContainerIds;
}

public void setListener(CleanupListener listener) {
this.listener = listener;
public void setHandler(CleanupHandler handler) {
this.handler = handler;
}

public void setPaths(List<CleanupPath> paths) {
Expand Down Expand Up @@ -80,18 +82,18 @@ public void setIgnoreNamespaces(boolean ignoreNamespaces) {
/**
* Processes object (modifies it) and removes unwanted items.
*/
public CleanupResult process(PrismObject<?> object) {
public CleanupResult process(@NotNull PrismObject<?> object, @NotNull Source source) {
CleanupResult result = new CleanupResult();

processItemRecursively(object, ItemPath.EMPTY_PATH, new HashMap<>(), object, result);
processItemRecursively(object, ItemPath.EMPTY_PATH, new HashMap<>(), object, source, result);

return result;
}

/**
* Processes container value (modifies it) and removes unwanted items.
*/
public CleanupResult process(PrismContainerValue<?> containerValue) {
public CleanupResult process(@NotNull PrismContainerValue<?> containerValue, @NotNull Source source) {
CleanupResult result = new CleanupResult();

if (containerValue.isEmpty()) {
Expand All @@ -104,7 +106,7 @@ public CleanupResult process(PrismContainerValue<?> containerValue) {

Collection<Item<?, ?>> items = containerValue.getItems();
for (Item<?, ?> i : items) {
if (processItemRecursively(i, i.getElementName(), customItemActions, null, result)) {
if (processItemRecursively(i, i.getElementName(), customItemActions, null, source, result)) {
toBeRemoved.add(i);
}
}
Expand All @@ -115,9 +117,9 @@ public CleanupResult process(PrismContainerValue<?> containerValue) {

private boolean processItemRecursively(
Item<?, ?> item, ItemPath currentPath, Map<Item<?, ?>, CleanupPathAction> customItemActions, PrismObject<?> object,
CleanupResult result) {
Source source, CleanupResult result) {

boolean remove = processItem(item, currentPath, customItemActions, object, result);
boolean remove = processItem(item, currentPath, customItemActions, object, source, result);
if (remove) {
return true;
}
Expand All @@ -127,17 +129,15 @@ private boolean processItemRecursively(

if (item instanceof PrismProperty<?> property) {
if (ProtectedStringType.COMPLEX_TYPE.equals(def.getTypeName())) {
fireProtectedStringCleanup(result, object, currentPath, (PrismProperty<ProtectedStringType>) property);
fireProtectedStringCleanup(
createEvent(object, currentPath, (PrismProperty<ProtectedStringType>) property, source, result));
}
} else if (item instanceof PrismReference) {
fireReferenceCleanup(result, object, currentPath, (PrismReference) item);
fireReferenceCleanup(
createEvent(object, currentPath, (PrismReference) item, source, result));
}
}

if (item instanceof PrismReference) {
fireReferenceCleanup(result, object, currentPath, (PrismReference) item);
}

if (item instanceof PrismContainer<?> pc) {
boolean emptyBefore = pc.isEmpty();

Expand All @@ -150,7 +150,8 @@ private boolean processItemRecursively(

Collection<Item<?, ?>> items = value.getItems();
for (Item<?, ?> i : items) {
if (processItemRecursively(i, currentPath.append(i.getElementName()), customItemActions, object, result)) {
if (processItemRecursively(
i, currentPath.append(i.getElementName()), customItemActions, object, source, result)) {
toBeRemoved.add(i);
}
}
Expand All @@ -169,7 +170,7 @@ private boolean processItemRecursively(
*/
private boolean processItem(
Item<?, ?> item, ItemPath currentPath, Map<Item<?, ?>, CleanupPathAction> customItemActions, PrismObject<?> object,
CleanupResult result) {
Source source, CleanupResult result) {

final ItemDefinition<?> def = item.getDefinition();
if (def != null) {
Expand All @@ -180,7 +181,7 @@ private boolean processItem(
if (customAction != null) {
return switch (customAction) {
case REMOVE -> true;
case ASK -> fireConfirmOptionalCleanup(result, object, currentPath, item);
case ASK -> fireConfirmOptionalCleanup(createEvent(object, currentPath, item, source, result));
default -> false;
};
}
Expand All @@ -194,7 +195,7 @@ private boolean processItem(
}

if (def.isOptionalCleanup()) {
return fireConfirmOptionalCleanup(result, object, currentPath, item);
return fireConfirmOptionalCleanup(createEvent(object, currentPath, item, source, result));
}

return false;
Expand Down Expand Up @@ -268,27 +269,31 @@ private void findItems(PrismContainerValue<?> parent, ItemPath named, List<Item<
}
}

private void fireProtectedStringCleanup(CleanupResult result, PrismObject<?> object, ItemPath path, PrismProperty<ProtectedStringType> string) {
if (listener == null) {
private <T> CleanupEvent<T> createEvent(PrismObject<?> object, ItemPath path, T item, Source source, CleanupResult result) {
return new CleanupEvent<>(object, path, item, source, result);
}

private void fireProtectedStringCleanup(CleanupEvent<PrismProperty<ProtectedStringType>> event) {
if (handler == null) {
return;
}

listener.onProtectedStringCleanup(new CleanupEvent<>(result, object, path, string));
handler.onProtectedStringCleanup(event);
}

private void fireReferenceCleanup(CleanupResult result, PrismObject<?> object, ItemPath path, PrismReference reference) {
if (listener == null) {
private void fireReferenceCleanup(CleanupEvent<PrismReference> event) {
if (handler == null) {
return;
}

listener.onReferenceCleanup(new CleanupEvent<>(result, object, path, reference));
handler.onReferenceCleanup(event);
}

private boolean fireConfirmOptionalCleanup(CleanupResult result, PrismObject<?> object, ItemPath path, Item<?, ?> item) {
if (listener == null) {
private boolean fireConfirmOptionalCleanup(CleanupEvent<Item<?, ?>> event) {
if (handler == null) {
return removeAskActionItemsByDefault;
}

return listener.onConfirmOptionalCleanup(new CleanupEvent<>(result, object, path, item));
return handler.onConfirmOptionalCleanup(event);
}
}
Expand Up @@ -13,9 +13,9 @@
import com.evolveum.midpoint.prism.path.ItemPath;

public record CleanupEvent<I>(
@NotNull CleanupResult result,
@NotNull PrismObject<?> object,
@NotNull ItemPath path,
@NotNull I value) {

@NotNull I value,
@NotNull Source source,
@NotNull CleanupResult result) {
}
Expand Up @@ -16,7 +16,7 @@
* Listener that can be used to react on cleanup events created for items that are marked
* with action {@link CleanupPathAction#ASK}.
*/
public interface CleanupListener {
public interface CleanupHandler {

/**
* Method that allows consumers to react on cleanup event marked with action {@link CleanupPathAction#ASK}.
Expand Down
Expand Up @@ -25,8 +25,8 @@ public enum CleanupPathAction {
/**
* Consumer is asked what to do.
*
* @see CleanupListener
* @see CleanupActionProcessor#setListener(CleanupListener)
* @see CleanupHandler
* @see CleanupActionProcessor#setHandler(CleanupHandler)
*/
ASK;
}

0 comments on commit 168eea9

Please sign in to comment.