Always conditionally handling notification and audit events in a Quartz job


Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/d310af83
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/d310af83
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/d310af83

Branch: refs/heads/master
Commit: d310af8337e3fbc410d70e21f62e6bd40ffd753e
Parents: c626205
Author: Francesco Chicchiriccò <ilgro...@apache.org>
Authored: Tue Jun 13 11:14:02 2017 +0200
Committer: Francesco Chicchiriccò <ilgro...@apache.org>
Committed: Tue Jun 13 13:23:14 2017 +0200

----------------------------------------------------------------------
 .../pushpull/AbstractPushResultHandler.java     | 55 +++++++++++++++-----
 .../pushpull/RealmPushResultHandlerImpl.java    | 41 ++++++++++-----
 2 files changed, 68 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/d310af83/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/AbstractPushResultHandler.java
----------------------------------------------------------------------
diff --git 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/AbstractPushResultHandler.java
 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/AbstractPushResultHandler.java
index 542009b..d84fa25 100644
--- 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/AbstractPushResultHandler.java
+++ 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/AbstractPushResultHandler.java
@@ -46,9 +46,11 @@ import 
org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject;
 import org.apache.syncope.core.persistence.api.entity.group.Group;
 import org.apache.syncope.core.persistence.api.entity.resource.MappingItem;
 import org.apache.syncope.core.persistence.api.entity.resource.Provision;
+import org.apache.syncope.core.provisioning.api.AuditManager;
 import org.apache.syncope.core.provisioning.api.MappingManager;
 import org.apache.syncope.core.provisioning.api.TimeoutException;
 import org.apache.syncope.core.provisioning.api.event.AfterHandlingEvent;
+import 
org.apache.syncope.core.provisioning.api.notification.NotificationManager;
 import 
org.apache.syncope.core.provisioning.api.pushpull.IgnoreProvisionException;
 import 
org.apache.syncope.core.provisioning.api.pushpull.SyncopePushResultHandler;
 import org.apache.syncope.core.provisioning.api.utils.EntityUtils;
@@ -66,6 +68,18 @@ import 
org.springframework.transaction.annotation.Transactional;
 public abstract class AbstractPushResultHandler extends 
AbstractSyncopeResultHandler<PushTask, PushActions>
         implements SyncopePushResultHandler {
 
+    /**
+     * Notification Manager.
+     */
+    @Autowired
+    protected NotificationManager notificationManager;
+
+    /**
+     * Audit Manager.
+     */
+    @Autowired
+    protected AuditManager auditManager;
+
     @Autowired
     protected MappingManager mappingManager;
 
@@ -196,7 +210,6 @@ public abstract class AbstractPushResultHandler extends 
AbstractSyncopeResultHan
 
         Object output = null;
         Result resultStatus = null;
-        String operation = null;
 
         // Try to read remote object BEFORE any actual operation
         Provision provision = 
profile.getTask().getResource().getProvision(any.getType());
@@ -214,9 +227,22 @@ public abstract class AbstractPushResultHandler extends 
AbstractSyncopeResultHan
             }
             result.setStatus(ProvisioningReport.Status.SUCCESS);
         } else {
+            String operation = beforeObj == null
+                    ? 
UnmatchingRule.toEventName(profile.getTask().getUnmatchingRule())
+                    : 
MatchingRule.toEventName(profile.getTask().getMatchingRule());
+
+            boolean notificationsAvailable = 
notificationManager.notificationsAvailable(
+                    AuditElements.EventCategoryType.PUSH,
+                    any.getType().getKind().name().toLowerCase(),
+                    profile.getTask().getResource().getKey(),
+                    operation);
+            boolean auditRequested = auditManager.auditRequested(
+                    AuditElements.EventCategoryType.PUSH,
+                    any.getType().getKind().name().toLowerCase(),
+                    profile.getTask().getResource().getKey(),
+                    operation);
             try {
                 if (beforeObj == null) {
-                    operation = 
UnmatchingRule.toEventName(profile.getTask().getUnmatchingRule());
                     
result.setOperation(getResourceOperation(profile.getTask().getUnmatchingRule()));
 
                     switch (profile.getTask().getUnmatchingRule()) {
@@ -266,7 +292,6 @@ public abstract class AbstractPushResultHandler extends 
AbstractSyncopeResultHan
                         // do nothing
                     }
                 } else {
-                    operation = 
MatchingRule.toEventName(profile.getTask().getMatchingRule());
                     
result.setOperation(getResourceOperation(profile.getTask().getMatchingRule()));
 
                     switch (profile.getTask().getMatchingRule()) {
@@ -365,17 +390,19 @@ public abstract class AbstractPushResultHandler extends 
AbstractSyncopeResultHan
 
                 throw new JobExecutionException(e);
             } finally {
-                Map<String, Object> jobMap = new HashMap<>();
-                jobMap.put(AfterHandlingEvent.JOBMAP_KEY, new 
AfterHandlingEvent(
-                        AuditElements.EventCategoryType.PUSH,
-                        any.getType().getKind().name().toLowerCase(),
-                        profile.getTask().getResource().getKey(),
-                        operation,
-                        resultStatus,
-                        beforeObj,
-                        output,
-                        any));
-                AfterHandlingJob.schedule(scheduler, jobMap);
+                if (notificationsAvailable || auditRequested) {
+                    Map<String, Object> jobMap = new HashMap<>();
+                    jobMap.put(AfterHandlingEvent.JOBMAP_KEY, new 
AfterHandlingEvent(
+                            AuditElements.EventCategoryType.PUSH,
+                            any.getType().getKind().name().toLowerCase(),
+                            profile.getTask().getResource().getKey(),
+                            operation,
+                            resultStatus,
+                            beforeObj,
+                            output,
+                            any));
+                    AfterHandlingJob.schedule(scheduler, jobMap);
+                }
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/syncope/blob/d310af83/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/RealmPushResultHandlerImpl.java
----------------------------------------------------------------------
diff --git 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/RealmPushResultHandlerImpl.java
 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/RealmPushResultHandlerImpl.java
index 313a527..50e68f0 100644
--- 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/RealmPushResultHandlerImpl.java
+++ 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/RealmPushResultHandlerImpl.java
@@ -152,7 +152,6 @@ public class RealmPushResultHandlerImpl
 
         Object output = null;
         Result resultStatus = null;
-        String operation = null;
 
         // Try to read remote object BEFORE any actual operation
         ConnectorObject beforeObj = getRemoteObject(
@@ -168,9 +167,22 @@ public class RealmPushResultHandlerImpl
             }
             result.setStatus(ProvisioningReport.Status.SUCCESS);
         } else {
+            String operation = beforeObj == null
+                    ? 
UnmatchingRule.toEventName(profile.getTask().getUnmatchingRule())
+                    : 
MatchingRule.toEventName(profile.getTask().getMatchingRule());
+
+            boolean notificationsAvailable = 
notificationManager.notificationsAvailable(
+                    AuditElements.EventCategoryType.PUSH,
+                    REALM_TYPE.toLowerCase(),
+                    profile.getTask().getResource().getKey(),
+                    operation);
+            boolean auditRequested = auditManager.auditRequested(
+                    AuditElements.EventCategoryType.PUSH,
+                    REALM_TYPE.toLowerCase(),
+                    profile.getTask().getResource().getKey(),
+                    operation);
             try {
                 if (beforeObj == null) {
-                    operation = 
UnmatchingRule.toEventName(profile.getTask().getUnmatchingRule());
                     
result.setOperation(getResourceOperation(profile.getTask().getUnmatchingRule()));
 
                     switch (profile.getTask().getUnmatchingRule()) {
@@ -220,7 +232,6 @@ public class RealmPushResultHandlerImpl
                         // do nothing
                     }
                 } else {
-                    operation = 
MatchingRule.toEventName(profile.getTask().getMatchingRule());
                     
result.setOperation(getResourceOperation(profile.getTask().getMatchingRule()));
 
                     switch (profile.getTask().getMatchingRule()) {
@@ -322,17 +333,19 @@ public class RealmPushResultHandlerImpl
 
                 throw new JobExecutionException(e);
             } finally {
-                Map<String, Object> jobMap = new HashMap<>();
-                jobMap.put(AfterHandlingEvent.JOBMAP_KEY, new 
AfterHandlingEvent(
-                        AuditElements.EventCategoryType.PUSH,
-                        REALM_TYPE.toLowerCase(),
-                        profile.getTask().getResource().getKey(),
-                        operation,
-                        resultStatus,
-                        beforeObj,
-                        output,
-                        realm));
-                AfterHandlingJob.schedule(scheduler, jobMap);
+                if (notificationsAvailable || auditRequested) {
+                    Map<String, Object> jobMap = new HashMap<>();
+                    jobMap.put(AfterHandlingEvent.JOBMAP_KEY, new 
AfterHandlingEvent(
+                            AuditElements.EventCategoryType.PUSH,
+                            REALM_TYPE.toLowerCase(),
+                            profile.getTask().getResource().getKey(),
+                            operation,
+                            resultStatus,
+                            beforeObj,
+                            output,
+                            realm));
+                    AfterHandlingJob.schedule(scheduler, jobMap);
+                }
             }
         }
     }

Reply via email to