From 4a99b9d7b0c345ba3e1bba227d52ebeae3f4f2e8 Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Thu, 5 Mar 2020 17:47:16 +0100 Subject: [PATCH] Make swallowToPrimaryDelta work with frozen deltas Primary deltas are now frozen in lens element contexts. But we sometimes need to modify them e.g. in scripting hooks. The swallowToPrimaryDelta was changed to allow this. Resolves MID-6097. --- .../model/impl/lens/LensElementContext.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/lens/LensElementContext.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/lens/LensElementContext.java index e5f5303b2df..6057cea6f9d 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/lens/LensElementContext.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/lens/LensElementContext.java @@ -259,11 +259,36 @@ public void addPrimaryDelta(ObjectDelta delta) throws SchemaException { } public void swallowToPrimaryDelta(ItemDelta itemDelta) throws SchemaException { + createOrModifyPrimaryDelta( + delta -> delta.swallow(itemDelta), + () -> { + ObjectDelta newPrimaryDelta = getPrismContext().deltaFactory().object().create(getObjectTypeClass(), ChangeType.MODIFY); + newPrimaryDelta.setOid(oid); + newPrimaryDelta.addModification(itemDelta); + return newPrimaryDelta; + }); + } + + @FunctionalInterface + private interface DeltaModifier { + void modify(ObjectDelta delta) throws SchemaException; + } + + @FunctionalInterface + private interface DeltaCreator { + ObjectDelta create() throws SchemaException; + } + + private void createOrModifyPrimaryDelta(DeltaModifier modifier, DeltaCreator creator) throws SchemaException { if (primaryDelta == null) { - primaryDelta = getPrismContext().deltaFactory().object().create(getObjectTypeClass(), ChangeType.MODIFY); - primaryDelta.setOid(oid); + primaryDelta = creator.create(); + } else if (!primaryDelta.isImmutable()) { + modifier.modify(primaryDelta); + } else { + primaryDelta = primaryDelta.clone(); + modifier.modify(primaryDelta); + primaryDelta.freeze(); } - primaryDelta.swallow(itemDelta); } public abstract void swallowToSecondaryDelta(ItemDelta itemDelta) throws SchemaException;