From b1b759b562377c6075c0912c7c1496e88a5a80b5 Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Mon, 2 Oct 2017 14:17:29 +0200 Subject: [PATCH] Fixed notifications for (seemingly) empty containers. --- model/model-intest/testng-integration.xml | 1 + .../impl/formatters/TextFormatter.java | 96 +++++++++---------- 2 files changed, 45 insertions(+), 52 deletions(-) diff --git a/model/model-intest/testng-integration.xml b/model/model-intest/testng-integration.xml index 8b31c226ace..649851ac5b7 100644 --- a/model/model-intest/testng-integration.xml +++ b/model/model-intest/testng-integration.xml @@ -63,6 +63,7 @@ + diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/formatters/TextFormatter.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/formatters/TextFormatter.java index 09a6f8b2826..1648fc35056 100644 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/formatters/TextFormatter.java +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/formatters/TextFormatter.java @@ -68,6 +68,7 @@ public class TextFormatter { private static final Trace LOGGER = TraceManager.getTrace(TextFormatter.class); + @SuppressWarnings("unused") public String formatObjectModificationDelta(ObjectDelta objectDelta, List hiddenPaths, boolean showOperationalAttributes) { return formatObjectModificationDelta(objectDelta, hiddenPaths, showOperationalAttributes, null, null); } @@ -166,7 +167,7 @@ private void formatItemDeltaContent(StringBuilder sb, ItemDelta itemDelta, List< private void formatItemDeltaValues(StringBuilder sb, String type, Collection values, boolean mightBeRemoved, List hiddenPaths, boolean showOperationalAttributes) { if (values != null) { for (PrismValue prismValue : values) { - sb.append(" - " + type + ": "); + sb.append(" - ").append(type).append(": "); String prefix = " "; formatPrismValue(sb, prefix, prismValue, mightBeRemoved, hiddenPaths, showOperationalAttributes); if (!(prismValue instanceof PrismContainerValue)) { // container values already end with newline @@ -254,15 +255,19 @@ private void formatContainerValue(StringBuilder sb, String prefix, PrismContaine private void formatPrismContainer(StringBuilder sb, String prefix, Item item, boolean mightBeRemoved, List hiddenPaths, boolean showOperationalAttributes) { for (PrismContainerValue subContainerValue : ((PrismContainer) item).getValues()) { - sb.append(prefix); - sb.append(" - "); - sb.append(getItemLabel(item)); - if (subContainerValue.getId() != null) { - sb.append(" #").append(subContainerValue.getId()); - } - sb.append(":\n"); String prefixSubContainer = prefix + " "; - formatContainerValue(sb, prefixSubContainer, subContainerValue, mightBeRemoved, hiddenPaths, showOperationalAttributes); + StringBuilder valueSb = new StringBuilder(); + formatContainerValue(valueSb, prefixSubContainer, subContainerValue, mightBeRemoved, hiddenPaths, showOperationalAttributes); + if (valueSb.length() > 0) { + sb.append(prefix); + sb.append(" - "); + sb.append(getItemLabel(item)); + if (subContainerValue.getId() != null) { + sb.append(" #").append(subContainerValue.getId()); + } + sb.append(":\n"); + sb.append(valueSb.toString()); + } } } @@ -274,7 +279,7 @@ private void formatPrismReference(StringBuilder sb, String prefix, Item item, bo if (item.size() > 1) { for (PrismReferenceValue referenceValue : ((PrismReference) item).getValues()) { sb.append("\n"); - sb.append(prefix + " - "); + sb.append(prefix).append(" - "); sb.append(formatReferenceValue(referenceValue, mightBeRemoved)); } } else if (item.size() == 1) { @@ -289,13 +294,13 @@ private void formatPrismProperty(StringBuilder sb, String prefix, Item item) { sb.append(getItemLabel(item)); sb.append(": "); if (item.size() > 1) { - for (PrismPropertyValue propertyValue : ((PrismProperty) item).getValues()) { + for (PrismPropertyValue propertyValue : ((PrismProperty) item).getValues()) { sb.append("\n"); - sb.append(prefix + " - "); + sb.append(prefix).append(" - "); sb.append(ValueDisplayUtil.toStringValue(propertyValue)); } } else if (item.size() == 1) { - sb.append(ValueDisplayUtil.toStringValue(((PrismProperty) item).getValue(0))); + sb.append(ValueDisplayUtil.toStringValue(((PrismProperty) item).getValue(0))); } sb.append("\n"); } @@ -354,6 +359,7 @@ private PrismObject getPrismObject(String oid, boolean mig if (!mightBeRemoved) { LoggingUtils.logException(LOGGER, "Couldn't resolve reference when displaying object name within a notification (it might be already removed)", e); } else { + // ok, accepted } } catch (SchemaException e) { LoggingUtils.logException(LOGGER, "Couldn't resolve reference when displaying object name within a notification", e); @@ -361,14 +367,6 @@ private PrismObject getPrismObject(String oid, boolean mig return null; } - private String localPartOfType(Item item) { - if (item.getDefinition() != null) { - return localPart(item.getDefinition().getTypeName()); - } else { - return null; - } - } - private String localPart(QName qname) { return qname == null ? null : qname.getLocalPart(); } @@ -441,7 +439,7 @@ private ItemPath getPathToExplain(ItemDelta itemDelta) { } private List filterAndOrderItemDeltas(ObjectDelta objectDelta, List hiddenPaths, boolean showOperationalAttributes) { - List toBeDisplayed = new ArrayList(objectDelta.getModifications().size()); + List toBeDisplayed = new ArrayList<>(objectDelta.getModifications().size()); List noDefinition = new ArrayList<>(); for (ItemDelta itemDelta: objectDelta.getModifications()) { if (itemDelta.getDefinition() != null) { @@ -457,20 +455,17 @@ private List filterAndOrderItemDeltas(ObjectDelta() { - @Override - public int compare(ItemDelta delta1, ItemDelta delta2) { - Integer order1 = delta1.getDefinition().getDisplayOrder(); - Integer order2 = delta2.getDefinition().getDisplayOrder(); - if (order1 != null && order2 != null) { - return order1 - order2; - } else if (order1 == null && order2 == null) { - return 0; - } else if (order1 == null) { - return 1; - } else { - return -1; - } + toBeDisplayed.sort((delta1, delta2) -> { + Integer order1 = delta1.getDefinition().getDisplayOrder(); + Integer order2 = delta2.getDefinition().getDisplayOrder(); + if (order1 != null && order2 != null) { + return order1 - order2; + } else if (order1 == null && order2 == null) { + return 0; + } else if (order1 == null) { + return 1; + } else { + return -1; } }); return toBeDisplayed; @@ -486,12 +481,12 @@ private List filterAndOrderItems(List items, List hiddenPa if (items == null) { return new ArrayList<>(); } - List toBeDisplayed = new ArrayList(items.size()); + List toBeDisplayed = new ArrayList<>(items.size()); List noDefinition = new ArrayList<>(); for (Item item : items) { if (item.getDefinition() != null) { boolean isHidden = NotificationFunctionsImpl.isAmongHiddenPaths(item.getPath(), hiddenPaths); - if (!isHidden && (showOperationalAttributes || !item.getDefinition().isOperational())) { + if (!isHidden && (showOperationalAttributes || !item.getDefinition().isOperational()) && !item.isEmpty()) { toBeDisplayed.add(item); } } else { @@ -502,20 +497,17 @@ private List filterAndOrderItems(List items, List hiddenPa LOGGER.error("Items {} without definition - THEY WILL NOT BE INCLUDED IN NOTIFICATION.\nAll items:\n{}", noDefinition, DebugUtil.debugDump(items)); } - Collections.sort(toBeDisplayed, new Comparator() { - @Override - public int compare(Item item1, Item item2) { - Integer order1 = item1.getDefinition().getDisplayOrder(); - Integer order2 = item2.getDefinition().getDisplayOrder(); - if (order1 != null && order2 != null) { - return order1 - order2; - } else if (order1 == null && order2 == null) { - return 0; - } else if (order1 == null) { - return 1; - } else { - return -1; - } + toBeDisplayed.sort((item1, item2) -> { + Integer order1 = item1.getDefinition().getDisplayOrder(); + Integer order2 = item2.getDefinition().getDisplayOrder(); + if (order1 != null && order2 != null) { + return order1 - order2; + } else if (order1 == null && order2 == null) { + return 0; + } else if (order1 == null) { + return 1; + } else { + return -1; } }); return toBeDisplayed;