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