Coverage Report - org.kuali.rice.ken.service.impl.NotificationMessageDeliveryAutoRemovalServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
NotificationMessageDeliveryAutoRemovalServiceImpl
0%
0/32
0%
0/2
1.429
 
 1  
 /*
 2  
  * Copyright 2006-2011 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  
 
 17  
 package org.kuali.rice.ken.service.impl;
 18  
 
 19  
 import org.kuali.rice.core.framework.persistence.dao.GenericDao;
 20  
 import org.kuali.rice.ken.bo.NotificationMessageDelivery;
 21  
 import org.kuali.rice.ken.deliverer.NotificationMessageDeliverer;
 22  
 import org.kuali.rice.ken.deliverer.impl.KEWActionListMessageDeliverer;
 23  
 import org.kuali.rice.ken.exception.NotificationAutoRemoveException;
 24  
 import org.kuali.rice.ken.service.NotificationMessageDeliveryAutoRemovalService;
 25  
 import org.kuali.rice.ken.service.NotificationMessageDeliveryService;
 26  
 import org.kuali.rice.ken.service.ProcessingResult;
 27  
 import org.kuali.rice.ken.util.NotificationConstants;
 28  
 import org.springframework.transaction.PlatformTransactionManager;
 29  
 
 30  
 import java.sql.Timestamp;
 31  
 import java.util.ArrayList;
 32  
 import java.util.Collection;
 33  
 import java.util.List;
 34  
 import java.util.concurrent.ExecutorService;
 35  
 
 36  
 /**
 37  
  * Auto removes expired message deliveries.
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  */
 40  0
 public class NotificationMessageDeliveryAutoRemovalServiceImpl extends ConcurrentJob<NotificationMessageDelivery> implements NotificationMessageDeliveryAutoRemovalService {
 41  
     private GenericDao businessObjectDao;
 42  
     private NotificationMessageDeliveryService messageDeliveryService;
 43  
 
 44  
     /**
 45  
      * Constructs a NotificationMessageDeliveryDispatchServiceImpl instance.
 46  
      * @param businessObjectDao
 47  
      * @param txManager
 48  
      * @param executor
 49  
      * @param messageDeliveryRegistryService
 50  
      */
 51  
     public NotificationMessageDeliveryAutoRemovalServiceImpl(GenericDao businessObjectDao, PlatformTransactionManager txManager,
 52  
             ExecutorService executor, NotificationMessageDeliveryService messageDeliveryService) {
 53  0
         super(txManager, executor);
 54  0
         this.messageDeliveryService = messageDeliveryService;
 55  0
         this.businessObjectDao = businessObjectDao;
 56  0
     }
 57  
 
 58  
     /**
 59  
      * @see org.kuali.rice.ken.service.impl.ConcurrentJob#takeAvailableWorkItems()
 60  
      */
 61  
     @Override
 62  
     protected Collection<NotificationMessageDelivery> takeAvailableWorkItems() {
 63  0
         return messageDeliveryService.takeMessageDeliveriesForAutoRemoval();
 64  
     }
 65  
 
 66  
     /**
 67  
      * @see org.kuali.rice.ken.service.impl.ConcurrentJob#processWorkItem(java.lang.Object)
 68  
      */
 69  
     @Override
 70  
     protected Collection<String> processWorkItems(Collection<NotificationMessageDelivery> messageDeliveries) {
 71  0
         NotificationMessageDelivery firstMessageDelivery = messageDeliveries.iterator().next();
 72  
 
 73  0
         KEWActionListMessageDeliverer deliverer = new KEWActionListMessageDeliverer();
 74  0
         Collection<String> successes = new ArrayList<String>();
 75  0
         for (NotificationMessageDelivery delivery: messageDeliveries) {
 76  0
             successes.addAll(autoRemove(deliverer, delivery));
 77  
         }
 78  0
         return successes;
 79  
     }
 80  
 
 81  
     /**
 82  
      * Auto-removes a single message delivery
 83  
      * @param messageDeliverer the message deliverer
 84  
      * @param messageDelivery the message delivery to auto-remove
 85  
      * @return collection of strings indicating successful auto-removals
 86  
      */
 87  
     protected Collection<String> autoRemove(NotificationMessageDeliverer messageDeliverer, NotificationMessageDelivery messageDelivery) {
 88  0
         List<String> successes = new ArrayList<String>(1);
 89  
 
 90  
         // we have our message deliverer, so tell it to auto remove the message
 91  
         try {
 92  0
             messageDeliverer.autoRemoveMessageDelivery(messageDelivery);
 93  0
             LOG.debug("Auto-removal of message delivery '" + messageDelivery.getId() + "' for notification '" + messageDelivery.getNotification().getId() + "' was successful.");
 94  0
             successes.add("Auto-removal of message delivery '" + messageDelivery.getId() + "' for notification '" + messageDelivery.getNotification().getId() + "' was successful.");
 95  0
         } catch (NotificationAutoRemoveException nmde) {
 96  0
             LOG.error("Error auto-removing message " + messageDelivery);
 97  0
             throw new RuntimeException(nmde);
 98  0
         }
 99  
         
 100  
         // unlock item
 101  
         // now update the status of the delivery message instance to AUTO_REMOVED and persist
 102  0
         markAutoRemoved(messageDelivery);
 103  
 
 104  0
         return successes;
 105  
     }
 106  
 
 107  
     /**
 108  
      * Marks a MessageDelivery as having been auto-removed, and unlocks it
 109  
      * @param messageDelivery the messageDelivery instance to mark
 110  
      */
 111  
     protected void markAutoRemoved(NotificationMessageDelivery messageDelivery) {
 112  0
         messageDelivery.setMessageDeliveryStatus(NotificationConstants.MESSAGE_DELIVERY_STATUS.AUTO_REMOVED);
 113  
         // mark as unlocked
 114  0
         messageDelivery.setLockedDate(null);
 115  0
         businessObjectDao.save(messageDelivery);
 116  0
     }
 117  
 
 118  
     /**
 119  
      * @see org.kuali.rice.ken.service.impl.ConcurrentJob#unlockWorkItem(java.lang.Object)
 120  
      */
 121  
     @Override
 122  
     protected void unlockWorkItem(NotificationMessageDelivery delivery) {
 123  0
         messageDeliveryService.unlockMessageDelivery(delivery);
 124  0
     }
 125  
 
 126  
     /**
 127  
      * This implementation looks up all UNDELIVERED/DELIVERED message deliveries with an autoRemoveDateTime <= current date time and then iterates 
 128  
      * over each to call the appropriate functions to do the "auto-removal" by "canceling" each associated notification 
 129  
      * workflow document.
 130  
      * @see org.kuali.rice.ken.service.NotificationMessageDeliveryDispatchService#processAutoRemovalOfDeliveredNotificationMessageDeliveries()
 131  
      */
 132  
     public ProcessingResult processAutoRemovalOfDeliveredNotificationMessageDeliveries() {
 133  0
         LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] STARTING NOTIFICATION AUTO-REMOVAL PROCESSING");
 134  
 
 135  0
         ProcessingResult result = run();
 136  
         
 137  0
         LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] FINISHED NOTIFICATION AUTO-REMOVAL PROCESSING - Successes = " + result.getSuccesses().size() + ", Failures = " + result.getFailures().size());
 138  
 
 139  0
         return result;
 140  
     }
 141  
 }