| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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.Notification; |
| 21 | |
import org.kuali.rice.ken.bo.NotificationMessageDelivery; |
| 22 | |
import org.kuali.rice.ken.bo.NotificationRecipient; |
| 23 | |
import org.kuali.rice.ken.bo.NotificationRecipientList; |
| 24 | |
import org.kuali.rice.ken.bo.UserChannelSubscription; |
| 25 | |
import org.kuali.rice.ken.deliverer.impl.KEWActionListMessageDeliverer; |
| 26 | |
import org.kuali.rice.ken.exception.NotificationMessageDeliveryException; |
| 27 | |
import org.kuali.rice.ken.service.NotificationMessageDeliveryResolverService; |
| 28 | |
import org.kuali.rice.ken.service.NotificationRecipientService; |
| 29 | |
import org.kuali.rice.ken.service.NotificationService; |
| 30 | |
import org.kuali.rice.ken.service.ProcessingResult; |
| 31 | |
import org.kuali.rice.ken.service.UserPreferenceService; |
| 32 | |
import org.kuali.rice.ken.util.NotificationConstants; |
| 33 | |
import org.kuali.rice.kim.util.KimConstants.KimGroupMemberTypes; |
| 34 | |
import org.springframework.transaction.PlatformTransactionManager; |
| 35 | |
|
| 36 | |
import java.sql.Timestamp; |
| 37 | |
import java.util.ArrayList; |
| 38 | |
import java.util.Collection; |
| 39 | |
import java.util.HashSet; |
| 40 | |
import java.util.Iterator; |
| 41 | |
import java.util.List; |
| 42 | |
import java.util.concurrent.ExecutorService; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 0 | public class NotificationMessageDeliveryResolverServiceImpl extends ConcurrentJob<Notification> implements NotificationMessageDeliveryResolverService { |
| 51 | 0 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger |
| 52 | |
.getLogger(NotificationMessageDeliveryResolverServiceImpl.class); |
| 53 | |
|
| 54 | |
private NotificationRecipientService notificationRecipientService; |
| 55 | |
private GenericDao businessObjectDao; |
| 56 | |
private UserPreferenceService userPreferenceService; |
| 57 | |
private NotificationService notificationService; |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public NotificationMessageDeliveryResolverServiceImpl(NotificationService notificationService, NotificationRecipientService notificationRecipientService, |
| 68 | |
GenericDao businessObjectDao, PlatformTransactionManager txManager, ExecutorService executor, |
| 69 | |
UserPreferenceService userPreferenceService) { |
| 70 | 0 | super(txManager, executor); |
| 71 | 0 | this.notificationService = notificationService; |
| 72 | 0 | this.notificationRecipientService = notificationRecipientService; |
| 73 | 0 | this.businessObjectDao = businessObjectDao; |
| 74 | 0 | this.userPreferenceService = userPreferenceService; |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
@Override |
| 82 | |
protected Collection<Notification> takeAvailableWorkItems() { |
| 83 | 0 | Collection<Notification> nots = notificationService.takeNotificationsForResolution(); |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | 0 | return nots; |
| 90 | |
} |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
private HashSet<String> buildCompleteRecipientList(Notification notification) { |
| 100 | 0 | HashSet<String> completeRecipientList = new HashSet<String>(notification.getRecipients().size()); |
| 101 | |
|
| 102 | |
|
| 103 | 0 | for (int i = 0; i < notification.getRecipients().size(); i++) { |
| 104 | 0 | NotificationRecipient recipient = notification.getRecipient(i); |
| 105 | 0 | if (KimGroupMemberTypes.GROUP_MEMBER_TYPE.equals(recipient.getRecipientType())) { |
| 106 | |
|
| 107 | 0 | String[] groupMembers = notificationRecipientService.getGroupMembers(recipient.getRecipientId()); |
| 108 | 0 | for(int j = 0; j < groupMembers.length; j++) { |
| 109 | 0 | completeRecipientList.add(groupMembers[j]); |
| 110 | |
} |
| 111 | 0 | } else { |
| 112 | 0 | completeRecipientList.add(recipient.getRecipientId()); |
| 113 | |
} |
| 114 | |
} |
| 115 | |
|
| 116 | |
|
| 117 | 0 | Iterator<NotificationRecipientList> i = notification.getChannel().getRecipientLists().iterator(); |
| 118 | 0 | while (i.hasNext()) { |
| 119 | 0 | NotificationRecipientList listRecipient = i.next(); |
| 120 | 0 | if (KimGroupMemberTypes.GROUP_MEMBER_TYPE.equals(listRecipient.getRecipientType())) { |
| 121 | |
|
| 122 | 0 | String[] groupMembers = notificationRecipientService.getGroupMembers(listRecipient.getRecipientId()); |
| 123 | 0 | for (int j = 0; j < groupMembers.length; j++) { |
| 124 | 0 | completeRecipientList.add(groupMembers[j]); |
| 125 | |
} |
| 126 | 0 | } else { |
| 127 | 0 | completeRecipientList.add(listRecipient.getRecipientId()); |
| 128 | |
} |
| 129 | 0 | } |
| 130 | |
|
| 131 | |
|
| 132 | 0 | List<UserChannelSubscription> subscriptions = notification.getChannel().getSubscriptions(); |
| 133 | 0 | for (UserChannelSubscription subscription: subscriptions) { |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | 0 | completeRecipientList.add(subscription.getUserId()); |
| 138 | |
} |
| 139 | |
|
| 140 | 0 | return completeRecipientList; |
| 141 | |
} |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
@Override |
| 152 | |
protected Collection<Object> processWorkItems(Collection<Notification> notifications) { |
| 153 | 0 | List<Object> successes = new ArrayList<Object>(); |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | 0 | for (Notification notification: notifications) { |
| 158 | |
|
| 159 | 0 | HashSet<String> uniqueRecipients = buildCompleteRecipientList(notification); |
| 160 | |
|
| 161 | |
|
| 162 | 0 | Iterator<String> j = uniqueRecipients.iterator(); |
| 163 | 0 | while(j.hasNext()) { |
| 164 | 0 | String userRecipientId = j.next(); |
| 165 | |
|
| 166 | 0 | NotificationMessageDelivery defaultMessageDelivery = new NotificationMessageDelivery(); |
| 167 | 0 | defaultMessageDelivery.setMessageDeliveryStatus(NotificationConstants.MESSAGE_DELIVERY_STATUS.UNDELIVERED); |
| 168 | 0 | defaultMessageDelivery.setNotification(notification); |
| 169 | 0 | defaultMessageDelivery.setUserRecipientId(userRecipientId); |
| 170 | |
|
| 171 | |
|
| 172 | 0 | businessObjectDao.save(defaultMessageDelivery); |
| 173 | |
|
| 174 | |
try { |
| 175 | 0 | new KEWActionListMessageDeliverer().deliverMessage(defaultMessageDelivery); |
| 176 | 0 | } catch (NotificationMessageDeliveryException e) { |
| 177 | 0 | throw new RuntimeException(e); |
| 178 | 0 | } |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | 0 | defaultMessageDelivery.setMessageDeliveryStatus(NotificationConstants.MESSAGE_DELIVERY_STATUS.DELIVERED); |
| 183 | 0 | businessObjectDao.save(defaultMessageDelivery); |
| 184 | |
|
| 185 | 0 | successes.add(defaultMessageDelivery); |
| 186 | |
|
| 187 | |
|
| 188 | 0 | notification.setProcessingFlag(NotificationConstants.PROCESSING_FLAGS.RESOLVED); |
| 189 | |
|
| 190 | 0 | notification.setLockedDate(null); |
| 191 | 0 | businessObjectDao.save(notification); |
| 192 | 0 | } |
| 193 | |
|
| 194 | 0 | } |
| 195 | |
|
| 196 | 0 | return successes; |
| 197 | |
} |
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
@Override |
| 203 | |
protected void unlockWorkItem(Notification notification) { |
| 204 | 0 | LOG.debug("Unlocking notification: " + notification.getId() + " " + notification.getTitle()); |
| 205 | 0 | notificationService.unlockNotification(notification); |
| 206 | 0 | } |
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
public ProcessingResult resolveNotificationMessageDeliveries() { |
| 214 | 0 | LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] STARTING RESOLUTION OF NOTIFICATION MESSAGE DELIVERIES"); |
| 215 | |
|
| 216 | 0 | ProcessingResult result = run(); |
| 217 | |
|
| 218 | 0 | LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] FINISHED RESOLUTION OF NOTIFICATION MESSAGE DELIVERIES - " + |
| 219 | |
"Message Delivery End Points Resolved = " + result.getSuccesses().size()); |
| 220 | |
|
| 221 | 0 | return result; |
| 222 | |
} |
| 223 | |
} |