Coverage Report - org.kuali.rice.ken.service.impl.NotificationMessageDeliveryServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
NotificationMessageDeliveryServiceImpl
0%
0/40
0%
0/14
2.25
 
 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.core.util.RiceConstants;
 21  
 import org.kuali.rice.ken.bo.Notification;
 22  
 import org.kuali.rice.ken.bo.NotificationMessageDelivery;
 23  
 import org.kuali.rice.ken.dao.NotificationMessegeDeliveryDao;
 24  
 import org.kuali.rice.ken.service.NotificationMessageDeliveryService;
 25  
 import org.kuali.rice.ken.util.NotificationConstants;
 26  
 
 27  
 import java.sql.Timestamp;
 28  
 import java.util.Collection;
 29  
 import java.util.HashMap;
 30  
 import java.util.Map;
 31  
 
 32  
 //import org.kuali.rice.core.jpa.criteria.Criteria;
 33  
 
 34  
 /**
 35  
  * NotificationService implementation - this is the default out-of-the-box implementation of the service that uses the 
 36  
  * businessObjectDao to get at the data via our OOTB DBMS.
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  */
 39  
 public class NotificationMessageDeliveryServiceImpl implements NotificationMessageDeliveryService {
 40  0
     private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
 41  
         .getLogger(NotificationMessageDeliveryServiceImpl.class);
 42  
     
 43  
     private GenericDao businessObjectDao;
 44  
     private NotificationMessegeDeliveryDao ntdDao;
 45  
     
 46  
     /**
 47  
      * Constructs a NotificationServiceImpl class instance.
 48  
      * @param businessObjectDao
 49  
      */
 50  0
     public NotificationMessageDeliveryServiceImpl(GenericDao businessObjectDao, NotificationMessegeDeliveryDao ntdDao) {
 51  0
         this.businessObjectDao = businessObjectDao;
 52  0
         this.ntdDao = ntdDao;
 53  0
     }
 54  
 
 55  
     /**
 56  
      * This is the default implementation that uses the businessObjectDao.
 57  
      * @param id
 58  
      * @return NotificationMessageDelivery
 59  
      */
 60  
     public NotificationMessageDelivery getNotificationMessageDelivery(Long id) {
 61  0
         HashMap<String, Long> primaryKeys = new HashMap<String, Long>();
 62  0
         primaryKeys.put(NotificationConstants.BO_PROPERTY_NAMES.ID, id);
 63  
         
 64  0
         return (NotificationMessageDelivery) businessObjectDao.findByPrimaryKey(NotificationMessageDelivery.class, primaryKeys);
 65  
     }
 66  
 
 67  
     /**
 68  
      * @see org.kuali.rice.ken.service.NotificationMessageDeliveryService#getNotificationMessageDeliveryByDelivererId(java.lang.String)
 69  
      */
 70  
     //switch to JPA criteria
 71  
     public NotificationMessageDelivery getNotificationMessageDeliveryByDelivererId(String id) {
 72  0
             Map<String, Object> c = new HashMap<String, Object>();
 73  0
             c.put(NotificationConstants.BO_PROPERTY_NAMES.DELIVERY_SYSTEM_ID, id);        
 74  0
         Collection<NotificationMessageDelivery> results = businessObjectDao.findMatching(NotificationMessageDelivery.class, c);
 75  
         
 76  0
         if (results == null || results.size() == 0) return null;
 77  0
         if (results.size() > 1) {
 78  0
             throw new RuntimeException("More than one message delivery found with the following delivery system id: " + id);
 79  
         }
 80  0
         return results.iterator().next();
 81  
     }
 82  
 
 83  
     /**
 84  
      * @see org.kuali.rice.ken.service.NotificationMessageDeliveryService#getNotificationMessageDeliveries()
 85  
      */
 86  
     public Collection<NotificationMessageDelivery> getNotificationMessageDeliveries() {
 87  0
         return businessObjectDao.findAll(NotificationMessageDelivery.class);
 88  
     }
 89  
     
 90  
     /**
 91  
      * @see org.kuali.rice.ken.service.NotificationMessageDeliveryService#getNotificationMessageDeliveries(java.lang.Long, java.lang.String)
 92  
      */
 93  
     //switch to JPA criteria
 94  
     public Collection<NotificationMessageDelivery> getNotificationMessageDeliveries(Notification notification, String userRecipientId) {
 95  
 
 96  0
             Map<String, Object> c = new HashMap<String, Object>();
 97  0
             c.put(NotificationConstants.BO_PROPERTY_NAMES.NOTIFICATION, notification.getId());
 98  0
             c.put(NotificationConstants.BO_PROPERTY_NAMES.USER_RECIPIENT_ID, userRecipientId);
 99  
             
 100  0
         return businessObjectDao.findMatching(NotificationMessageDelivery.class, c);
 101  
     }
 102  
 
 103  
     /**
 104  
      * This method is responsible for atomically finding all untaken, undelivered messagedeliveries, marking them as taken
 105  
      * and returning them to the caller for processing.
 106  
      * NOTE: it is important that this method execute in a SEPARATE dedicated transaction; either the caller should
 107  
      * NOT be wrapped by Spring declarative transaction and this service should be wrapped (which is the case), or
 108  
      * the caller should arrange to invoke this from within a newly created transaction).
 109  
 
 110  
      * @return a list of available message deliveries that have been marked as taken by the caller
 111  
      */
 112  
     //switch to JPA criteria
 113  
     public Collection<NotificationMessageDelivery> takeMessageDeliveriesForDispatch() {
 114  
         // DO WITHIN TRANSACTION: get all untaken messagedeliveries, and mark as "taken" so no other thread/job takes them
 115  
         // need to think about durability of work list
 116  
 
 117  
         // get all undelivered message deliveries
 118  0
         Collection<NotificationMessageDelivery> messageDeliveries = ntdDao.getUndeliveredMessageDelivers(businessObjectDao);
 119  
 
 120  
         
 121  0
         LOG.debug("Retrieved " + messageDeliveries.size() + " available message deliveries: " + System.currentTimeMillis());
 122  
 
 123  
         // mark messageDeliveries as taken
 124  0
         for (NotificationMessageDelivery delivery: messageDeliveries) {
 125  0
             delivery.setLockedDate(new Timestamp(System.currentTimeMillis()));
 126  0
             businessObjectDao.save(delivery);
 127  
         }
 128  0
         return messageDeliveries;
 129  
     }
 130  
     
 131  
     /**
 132  
      * This method is responsible for atomically finding all untaken message deliveries that are ready to be autoremoved,
 133  
      * marking them as taken and returning them to the caller for processing.
 134  
      * NOTE: it is important that this method execute in a SEPARATE dedicated transaction; either the caller should
 135  
      * NOT be wrapped by Spring declarative transaction and this service should be wrapped (which is the case), or
 136  
      * the caller should arrange to invoke this from within a newly created transaction).
 137  
      * @return a list of notifications to be autoremoved that have been marked as taken by the caller
 138  
      */
 139  
     public Collection<NotificationMessageDelivery> takeMessageDeliveriesForAutoRemoval() {
 140  
         // get all UNDELIVERED/DELIVERED notification notification message delivery records with associated notifications that have and autoRemovalDateTime <= current
 141  0
             Collection<NotificationMessageDelivery> messageDeliveries = ntdDao.getMessageDeliveriesForAutoRemoval(new Timestamp(System.currentTimeMillis()), businessObjectDao);
 142  
             
 143  0
         for (NotificationMessageDelivery d: messageDeliveries) {
 144  0
             d.setLockedDate(new Timestamp(System.currentTimeMillis()));
 145  0
             businessObjectDao.save(d);
 146  
         }
 147  
         
 148  0
         return messageDeliveries;
 149  
     
 150  
     }
 151  
 
 152  
     /**
 153  
      * Unlocks the specified messageDelivery object
 154  
      * @param messageDelivery the message delivery to unlock
 155  
      */
 156  
     public void unlockMessageDelivery(NotificationMessageDelivery messageDelivery) {
 157  0
             Map<String, Long> c = new HashMap<String, Long>();
 158  0
             c.put(NotificationConstants.BO_PROPERTY_NAMES.DELIVERY_SYSTEM_ID, messageDelivery.getId());        
 159  
 
 160  0
         Collection<NotificationMessageDelivery> deliveries = businessObjectDao.findMatching(NotificationMessageDelivery.class, c, true, RiceConstants.NO_WAIT);
 161  0
         if (deliveries == null || deliveries.size() == 0) {
 162  0
             throw new RuntimeException("NotificationMessageDelivery #" + messageDelivery.getId() + " not found to unlock");
 163  
         }
 164  
 
 165  0
         NotificationMessageDelivery d = deliveries.iterator().next();
 166  0
         d.setLockedDate(null);
 167  
 
 168  0
         businessObjectDao.save(d);
 169  0
     }
 170  
 }