From 10d7d6b20302b7dadc2d747c7d06369478199e3b Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Fri, 13 Mar 2020 11:16:40 +0100 Subject: [PATCH] Simplify notifier class hierarchy Preparing for MID-5849 and MID-5350 resolution. --- .../notifications/api/EventHandler.java | 2 +- .../impl/EventHandlerRegistry.java | 2 +- .../AbstractFocalObjectNotifier.java | 167 ------------------ .../notifiers/SimpleFocalObjectNotifier.java | 152 +++++++++++++++- .../impl/notifiers/SimpleUserNotifier.java | 2 +- 5 files changed, 148 insertions(+), 177 deletions(-) delete mode 100644 model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/AbstractFocalObjectNotifier.java diff --git a/model/notifications-api/src/main/java/com/evolveum/midpoint/notifications/api/EventHandler.java b/model/notifications-api/src/main/java/com/evolveum/midpoint/notifications/api/EventHandler.java index c2ef6d87a66..65e730e9068 100644 --- a/model/notifications-api/src/main/java/com/evolveum/midpoint/notifications/api/EventHandler.java +++ b/model/notifications-api/src/main/java/com/evolveum/midpoint/notifications/api/EventHandler.java @@ -28,5 +28,5 @@ public interface EventHandler { /** * @return Type of configuration objects for this event handler. The handler is selected based on exact match of this type. */ - Class getEventHandlerConfigurationType(); + Class getEventHandlerConfigurationType(); } diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/EventHandlerRegistry.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/EventHandlerRegistry.java index 0c2b0f0d555..74e51dc4440 100644 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/EventHandlerRegistry.java +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/EventHandlerRegistry.java @@ -31,7 +31,7 @@ public class EventHandlerRegistry { private Map, EventHandler> handlers = new ConcurrentHashMap<>(); - public void registerEventHandler(Class configType, EventHandler handler) { + public void registerEventHandler(Class configType, EventHandler handler) { LOGGER.trace("Registering event handler {} for config type {}", handler, configType); handlers.put(configType, handler); } diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/AbstractFocalObjectNotifier.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/AbstractFocalObjectNotifier.java deleted file mode 100644 index 985218dd65a..00000000000 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/AbstractFocalObjectNotifier.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright (c) 2010-2013 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.notifications.impl.notifiers; - -import static com.evolveum.midpoint.util.MiscUtil.emptyIfNull; - -import java.util.Date; -import java.util.List; - -import com.evolveum.midpoint.xml.ns._public.common.common_3.*; - -import org.jetbrains.annotations.Nullable; -import org.springframework.stereotype.Component; - -import com.evolveum.midpoint.model.api.context.ModelContext; -import com.evolveum.midpoint.model.api.context.ModelElementContext; -import com.evolveum.midpoint.notifications.api.events.ModelEvent; -import com.evolveum.midpoint.prism.PrismObject; -import com.evolveum.midpoint.prism.delta.ObjectDelta; -import com.evolveum.midpoint.prism.delta.ObjectDeltaCollectionsUtil; -import com.evolveum.midpoint.prism.polystring.PolyString; -import com.evolveum.midpoint.schema.result.OperationResult; -import com.evolveum.midpoint.task.api.Task; -import com.evolveum.midpoint.util.exception.SchemaException; -import com.evolveum.midpoint.util.logging.Trace; -import com.evolveum.midpoint.util.logging.TraceManager; - -/** - * This is the "main" notifier that deals with modifications of focal objects i.e. AssignmentHolderType and below. - */ -@Component -public abstract class AbstractFocalObjectNotifier extends AbstractGeneralNotifier { - - private static final Trace LOGGER = TraceManager.getTrace(AbstractFocalObjectNotifier.class); - - @Override - public Class getEventType() { - return ModelEvent.class; - } - - abstract Class getFocusClass(); - - @Override - protected boolean quickCheckApplicability(ModelEvent event, SimpleFocalObjectNotifierType configuration, OperationResult result) { - if (event.getFocusContext() == null || !event.getFocusContext().isOfType(getFocusClass())) { - LOGGER.trace("{} is not applicable to non-{} related model operations, continuing in the handler chain", - getClass().getSimpleName(), getFocusClass()); - return false; - } else { - return true; - } - } - - @Override - protected boolean checkApplicability(ModelEvent event, C configuration, OperationResult result) { - List> deltas = event.getFocusDeltas(); - if (deltas.isEmpty()) { - LOGGER.trace("No deltas found, skipping the notification"); - return false; - } else if (isWatchAuxiliaryAttributes(configuration)) { - return true; - } else { - for (ObjectDelta delta : deltas) { - if (!delta.isModify() || deltaContainsOtherPathsThan(delta, functions.getAuxiliaryPaths())) { - return true; - } - } - LOGGER.trace("No deltas for non-auxiliary attributes found, skipping the notification"); - return false; - } - } - - @Override - protected String getSubject(ModelEvent event, C configuration, String transport, Task task, OperationResult result) { - - String typeName = event.getFocusTypeName(); - - if (event.isAdd()) { - return typeName + " creation notification"; - } else if (event.isModify()) { - return typeName + " modification notification"; - } else if (event.isDelete()) { - return typeName + " deletion notification"; - } else { - return "(unknown " + typeName.toLowerCase() + " operation)"; - } - } - - @Override - protected String getBody(ModelEvent modelEvent, C configuration, String transport, - Task task, OperationResult result) throws SchemaException { - - String typeName = modelEvent.getFocusTypeName(); - String typeNameLower = typeName.toLowerCase(); - - boolean techInfo = Boolean.TRUE.equals(configuration.isShowTechnicalInformation()); - - //noinspection unchecked - ModelContext modelContext = (ModelContext) modelEvent.getModelContext(); - ModelElementContext focusContext = modelContext.getFocusContext(); - PrismObject focusObject = focusContext.getObjectNew() != null ? focusContext.getObjectNew() : focusContext.getObjectOld(); - AssignmentHolderType focus = focusObject.asObjectable(); - String oid = focusContext.getOid(); - - String fullName = emptyIfNull(getFullName(focus)); - - ObjectDelta delta = ObjectDeltaCollectionsUtil.summarize(modelEvent.getFocusDeltas()); - - StringBuilder body = new StringBuilder(); - - String status = modelEvent.getStatusAsText(); - String attemptedTo = modelEvent.isSuccess() ? "" : "(attempted to be) "; - - body.append("Notification about ").append(typeNameLower).append("-related operation (status: ").append(status).append(")\n\n"); - body.append(typeName).append(": ").append(fullName).append(" (").append(focus.getName()).append(", oid ").append(oid).append(")\n"); - body.append("Notification created on: ").append(new Date()).append("\n\n"); - - final boolean watchAuxiliaryAttributes = isWatchAuxiliaryAttributes(configuration); - if (delta.isAdd()) { - body.append("The ").append(typeNameLower).append(" record was ").append(attemptedTo).append("created with the following data:\n"); - body.append(modelEvent.getContentAsFormattedList(false, watchAuxiliaryAttributes)); - } else if (delta.isModify()) { - body.append("The ").append(typeNameLower).append(" record was ").append(attemptedTo).append("modified. Modified attributes are:\n"); - body.append(modelEvent.getContentAsFormattedList(false, watchAuxiliaryAttributes)); - } else if (delta.isDelete()) { - body.append("The ").append(typeNameLower).append(" record was ").append(attemptedTo).append("removed.\n"); - } - body.append("\n"); - - if (!modelEvent.isSuccess()) { - body.append("More information about the status of the request was displayed and/or is present in log files.\n\n"); - } - - functions.addRequesterAndChannelInformation(body, modelEvent, result); - - if (techInfo) { - body.append("----------------------------------------\n"); - body.append("Technical information:\n\n"); - body.append(modelContext.debugDump(2)); - } - - return body.toString(); - } - - @Nullable - private String getFullName(AssignmentHolderType focus) { - String fullName; - if (focus instanceof UserType) { - fullName = PolyString.getOrig(((UserType) focus).getFullName()); - } else if (focus instanceof AbstractRoleType) { - fullName = PolyString.getOrig(((AbstractRoleType) focus).getDisplayName()); - } else { - fullName = ""; // TODO - } - return fullName; - } - - @Override - protected Trace getLogger() { - return LOGGER; - } -} diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleFocalObjectNotifier.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleFocalObjectNotifier.java index b5b10d23fc2..c9c57126579 100644 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleFocalObjectNotifier.java +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleFocalObjectNotifier.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Evolveum and contributors + * Copyright (c) 2010-2013 Evolveum and contributors * * This work is dual-licensed under the Apache License 2.0 * and European Union Public License. See LICENSE file for details. @@ -7,29 +7,167 @@ package com.evolveum.midpoint.notifications.impl.notifiers; -import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentHolderType; +import static com.evolveum.midpoint.util.MiscUtil.emptyIfNull; +import java.util.Date; +import java.util.List; + +import com.evolveum.midpoint.xml.ns._public.common.common_3.*; + +import org.jetbrains.annotations.Nullable; import org.springframework.stereotype.Component; +import com.evolveum.midpoint.model.api.context.ModelContext; +import com.evolveum.midpoint.model.api.context.ModelElementContext; +import com.evolveum.midpoint.notifications.api.events.ModelEvent; +import com.evolveum.midpoint.prism.PrismObject; +import com.evolveum.midpoint.prism.delta.ObjectDelta; +import com.evolveum.midpoint.prism.delta.ObjectDeltaCollectionsUtil; +import com.evolveum.midpoint.prism.polystring.PolyString; +import com.evolveum.midpoint.schema.result.OperationResult; +import com.evolveum.midpoint.task.api.Task; +import com.evolveum.midpoint.util.exception.SchemaException; import com.evolveum.midpoint.util.logging.Trace; import com.evolveum.midpoint.util.logging.TraceManager; -import com.evolveum.midpoint.xml.ns._public.common.common_3.SimpleFocalObjectNotifierType; +/** + * This is the "main" notifier that deals with modifications of focal objects i.e. AssignmentHolderType and below. + */ @Component -public class SimpleFocalObjectNotifier extends AbstractFocalObjectNotifier { +public class SimpleFocalObjectNotifier extends AbstractGeneralNotifier { private static final Trace LOGGER = TraceManager.getTrace(SimpleFocalObjectNotifier.class); @Override - public Class getEventHandlerConfigurationType() { - return SimpleFocalObjectNotifierType.class; + public Class getEventType() { + return ModelEvent.class; } @Override - Class getFocusClass() { + public Class getEventHandlerConfigurationType() { + return SimpleFocalObjectNotifierType.class; + } + + Class getFocusClass() { return AssignmentHolderType.class; } + @Override + protected boolean quickCheckApplicability(ModelEvent event, SimpleFocalObjectNotifierType configuration, OperationResult result) { + if (event.getFocusContext() == null || !event.getFocusContext().isOfType(getFocusClass())) { + LOGGER.trace("{} is not applicable to non-{} related model operations, continuing in the handler chain", + getClass().getSimpleName(), getFocusClass()); + return false; + } else { + return true; + } + } + + @Override + protected boolean checkApplicability(ModelEvent event, SimpleFocalObjectNotifierType configuration, OperationResult result) { + List> deltas = event.getFocusDeltas(); + if (deltas.isEmpty()) { + LOGGER.trace("No deltas found, skipping the notification"); + return false; + } else if (isWatchAuxiliaryAttributes(configuration)) { + return true; + } else { + for (ObjectDelta delta : deltas) { + if (!delta.isModify() || deltaContainsOtherPathsThan(delta, functions.getAuxiliaryPaths())) { + return true; + } + } + LOGGER.trace("No deltas for non-auxiliary attributes found, skipping the notification"); + return false; + } + } + + @Override + protected String getSubject(ModelEvent event, SimpleFocalObjectNotifierType configuration, String transport, + Task task, OperationResult result) { + + String typeName = event.getFocusTypeName(); + + if (event.isAdd()) { + return typeName + " creation notification"; + } else if (event.isModify()) { + return typeName + " modification notification"; + } else if (event.isDelete()) { + return typeName + " deletion notification"; + } else { + return "(unknown " + typeName.toLowerCase() + " operation)"; + } + } + + @Override + protected String getBody(ModelEvent modelEvent, SimpleFocalObjectNotifierType configuration, String transport, + Task task, OperationResult result) throws SchemaException { + + String typeName = modelEvent.getFocusTypeName(); + String typeNameLower = typeName.toLowerCase(); + + boolean techInfo = Boolean.TRUE.equals(configuration.isShowTechnicalInformation()); + + //noinspection unchecked + ModelContext modelContext = (ModelContext) modelEvent.getModelContext(); + ModelElementContext focusContext = modelContext.getFocusContext(); + PrismObject focusObject = focusContext.getObjectNew() != null ? focusContext.getObjectNew() : focusContext.getObjectOld(); + AssignmentHolderType focus = focusObject.asObjectable(); + String oid = focusContext.getOid(); + + String fullName = emptyIfNull(getFullName(focus)); + + ObjectDelta delta = ObjectDeltaCollectionsUtil.summarize(modelEvent.getFocusDeltas()); + + StringBuilder body = new StringBuilder(); + + String status = modelEvent.getStatusAsText(); + String attemptedTo = modelEvent.isSuccess() ? "" : "(attempted to be) "; + + body.append("Notification about ").append(typeNameLower).append("-related operation (status: ").append(status).append(")\n\n"); + body.append(typeName).append(": ").append(fullName).append(" (").append(focus.getName()).append(", oid ").append(oid).append(")\n"); + body.append("Notification created on: ").append(new Date()).append("\n\n"); + + final boolean watchAuxiliaryAttributes = isWatchAuxiliaryAttributes(configuration); + if (delta.isAdd()) { + body.append("The ").append(typeNameLower).append(" record was ").append(attemptedTo).append("created with the following data:\n"); + body.append(modelEvent.getContentAsFormattedList(false, watchAuxiliaryAttributes)); + } else if (delta.isModify()) { + body.append("The ").append(typeNameLower).append(" record was ").append(attemptedTo).append("modified. Modified attributes are:\n"); + body.append(modelEvent.getContentAsFormattedList(false, watchAuxiliaryAttributes)); + } else if (delta.isDelete()) { + body.append("The ").append(typeNameLower).append(" record was ").append(attemptedTo).append("removed.\n"); + } + body.append("\n"); + + if (!modelEvent.isSuccess()) { + body.append("More information about the status of the request was displayed and/or is present in log files.\n\n"); + } + + functions.addRequesterAndChannelInformation(body, modelEvent, result); + + if (techInfo) { + body.append("----------------------------------------\n"); + body.append("Technical information:\n\n"); + body.append(modelContext.debugDump(2)); + } + + return body.toString(); + } + + @Nullable + private String getFullName(AssignmentHolderType focus) { + String fullName; + if (focus instanceof UserType) { + fullName = PolyString.getOrig(((UserType) focus).getFullName()); + } else if (focus instanceof AbstractRoleType) { + fullName = PolyString.getOrig(((AbstractRoleType) focus).getDisplayName()); + } else { + fullName = ""; // TODO + } + return fullName; + } + @Override protected Trace getLogger() { return LOGGER; diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleUserNotifier.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleUserNotifier.java index 9211712fb5e..77165433e50 100644 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleUserNotifier.java +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/notifiers/SimpleUserNotifier.java @@ -15,7 +15,7 @@ import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType; @Component -public class SimpleUserNotifier extends AbstractFocalObjectNotifier { +public class SimpleUserNotifier extends SimpleFocalObjectNotifier { private static final Trace LOGGER = TraceManager.getTrace(SimpleUserNotifier.class);