View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ken.service.impl;
17  
18  import org.kuali.rice.core.framework.persistence.dao.GenericDao;
19  import org.kuali.rice.ken.bo.NotificationMessageDelivery;
20  import org.kuali.rice.ken.deliverer.NotificationMessageDeliverer;
21  import org.kuali.rice.ken.deliverer.impl.KEWActionListMessageDeliverer;
22  import org.kuali.rice.ken.exception.NotificationAutoRemoveException;
23  import org.kuali.rice.ken.service.NotificationMessageDeliveryAutoRemovalService;
24  import org.kuali.rice.ken.service.NotificationMessageDeliveryService;
25  import org.kuali.rice.ken.service.ProcessingResult;
26  import org.kuali.rice.ken.util.NotificationConstants;
27  import org.springframework.transaction.PlatformTransactionManager;
28  
29  import java.sql.Timestamp;
30  import java.util.ArrayList;
31  import java.util.Collection;
32  import java.util.List;
33  import java.util.concurrent.ExecutorService;
34  
35  /**
36   * Auto removes expired message deliveries.
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  public class NotificationMessageDeliveryAutoRemovalServiceImpl extends ConcurrentJob<NotificationMessageDelivery> implements NotificationMessageDeliveryAutoRemovalService {
40      private GenericDao businessObjectDao;
41      private NotificationMessageDeliveryService messageDeliveryService;
42  
43      /**
44       * Constructs a NotificationMessageDeliveryDispatchServiceImpl instance.
45       * @param businessObjectDao
46       * @param txManager
47       * @param executor
48       * @param messageDeliveryRegistryService
49       */
50      public NotificationMessageDeliveryAutoRemovalServiceImpl(GenericDao businessObjectDao, PlatformTransactionManager txManager,
51  	    ExecutorService executor, NotificationMessageDeliveryService messageDeliveryService) {
52          super(txManager, executor);
53          this.messageDeliveryService = messageDeliveryService;
54          this.businessObjectDao = businessObjectDao;
55      }
56  
57      /**
58       * @see org.kuali.rice.ken.service.impl.ConcurrentJob#takeAvailableWorkItems()
59       */
60      @Override
61      protected Collection<NotificationMessageDelivery> takeAvailableWorkItems() {
62          return messageDeliveryService.takeMessageDeliveriesForAutoRemoval();
63      }
64  
65      /**
66       * @see org.kuali.rice.ken.service.impl.ConcurrentJob#processWorkItem(java.lang.Object)
67       */
68      @Override
69      protected Collection<String> processWorkItems(Collection<NotificationMessageDelivery> messageDeliveries) {
70          NotificationMessageDelivery firstMessageDelivery = messageDeliveries.iterator().next();
71  
72          KEWActionListMessageDeliverer deliverer = new KEWActionListMessageDeliverer();
73          Collection<String> successes = new ArrayList<String>();
74          for (NotificationMessageDelivery delivery: messageDeliveries) {
75              successes.addAll(autoRemove(deliverer, delivery));
76          }
77          return successes;
78      }
79  
80      /**
81       * Auto-removes a single message delivery
82       * @param messageDeliverer the message deliverer
83       * @param messageDelivery the message delivery to auto-remove
84       * @return collection of strings indicating successful auto-removals
85       */
86      protected Collection<String> autoRemove(NotificationMessageDeliverer messageDeliverer, NotificationMessageDelivery messageDelivery) {
87          List<String> successes = new ArrayList<String>(1);
88  
89          // we have our message deliverer, so tell it to auto remove the message
90          try {
91              messageDeliverer.autoRemoveMessageDelivery(messageDelivery);
92              LOG.debug("Auto-removal of message delivery '" + messageDelivery.getId() + "' for notification '" + messageDelivery.getNotification().getId() + "' was successful.");
93              successes.add("Auto-removal of message delivery '" + messageDelivery.getId() + "' for notification '" + messageDelivery.getNotification().getId() + "' was successful.");
94          } catch (NotificationAutoRemoveException nmde) {
95              LOG.error("Error auto-removing message " + messageDelivery);
96              throw new RuntimeException(nmde);
97          }
98          
99          // unlock item
100         // now update the status of the delivery message instance to AUTO_REMOVED and persist
101         markAutoRemoved(messageDelivery);
102 
103         return successes;
104     }
105 
106     /**
107      * Marks a MessageDelivery as having been auto-removed, and unlocks it
108      * @param messageDelivery the messageDelivery instance to mark
109      */
110     protected void markAutoRemoved(NotificationMessageDelivery messageDelivery) {
111         messageDelivery.setMessageDeliveryStatus(NotificationConstants.MESSAGE_DELIVERY_STATUS.AUTO_REMOVED);
112         // mark as unlocked
113         messageDelivery.setLockedDateValue(null);
114         businessObjectDao.save(messageDelivery);
115     }
116 
117     /**
118      * @see org.kuali.rice.ken.service.impl.ConcurrentJob#unlockWorkItem(java.lang.Object)
119      */
120     @Override
121     protected void unlockWorkItem(NotificationMessageDelivery delivery) {
122         messageDeliveryService.unlockMessageDelivery(delivery);
123     }
124 
125     /**
126      * This implementation looks up all UNDELIVERED/DELIVERED message deliveries with an autoRemoveDateTime <= current date time and then iterates 
127      * over each to call the appropriate functions to do the "auto-removal" by "canceling" each associated notification 
128      * workflow document.
129      * @see org.kuali.rice.ken.service.NotificationMessageDeliveryDispatchService#processAutoRemovalOfDeliveredNotificationMessageDeliveries()
130      */
131     public ProcessingResult processAutoRemovalOfDeliveredNotificationMessageDeliveries() {
132         LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] STARTING NOTIFICATION AUTO-REMOVAL PROCESSING");
133 
134         ProcessingResult result = run();
135         
136         LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] FINISHED NOTIFICATION AUTO-REMOVAL PROCESSING - Successes = " + result.getSuccesses().size() + ", Failures = " + result.getFailures().size());
137 
138         return result;
139     }
140 }