| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.ken.service.impl; |
| 17 | |
|
| 18 | |
import java.io.IOException; |
| 19 | |
import java.sql.Timestamp; |
| 20 | |
import java.util.Collection; |
| 21 | |
import java.util.HashMap; |
| 22 | |
|
| 23 | |
import org.apache.ojb.broker.query.Criteria; |
| 24 | |
import org.kuali.rice.core.dao.GenericDao; |
| 25 | |
import org.kuali.rice.core.util.RiceConstants; |
| 26 | |
import org.kuali.rice.ken.bo.Notification; |
| 27 | |
import org.kuali.rice.ken.bo.NotificationMessageDelivery; |
| 28 | |
import org.kuali.rice.ken.bo.NotificationRecipient; |
| 29 | |
import org.kuali.rice.ken.bo.NotificationResponse; |
| 30 | |
import org.kuali.rice.ken.deliverer.impl.KEWActionListMessageDeliverer; |
| 31 | |
import org.kuali.rice.ken.exception.InvalidXMLException; |
| 32 | |
import org.kuali.rice.ken.service.NotificationAuthorizationService; |
| 33 | |
import org.kuali.rice.ken.service.NotificationMessageContentService; |
| 34 | |
import org.kuali.rice.ken.service.NotificationMessageDeliveryService; |
| 35 | |
import org.kuali.rice.ken.service.NotificationRecipientService; |
| 36 | |
import org.kuali.rice.ken.service.NotificationService; |
| 37 | |
import org.kuali.rice.ken.service.NotificationWorkflowDocumentService; |
| 38 | |
import org.kuali.rice.ken.util.NotificationConstants; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public class NotificationServiceImpl implements NotificationService { |
| 45 | 0 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger |
| 46 | |
.getLogger(NotificationServiceImpl.class); |
| 47 | |
|
| 48 | |
private GenericDao businessObjectDao; |
| 49 | |
private NotificationMessageContentService messageContentService; |
| 50 | |
private NotificationAuthorizationService notificationAuthorizationService; |
| 51 | |
private NotificationRecipientService notificationRecipientService; |
| 52 | |
private NotificationWorkflowDocumentService notificationWorkflowDocumentService; |
| 53 | |
private NotificationMessageDeliveryService notificationMessageDeliveryService; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public NotificationServiceImpl(GenericDao businessObjectDao, NotificationMessageContentService messageContentService, |
| 65 | |
NotificationAuthorizationService notificationAuthorizationService, NotificationRecipientService notificationRecipientService, |
| 66 | |
NotificationWorkflowDocumentService notificationWorkflowDocumentService, |
| 67 | 0 | NotificationMessageDeliveryService notificationMessageDeliveryService) { |
| 68 | 0 | this.businessObjectDao = businessObjectDao; |
| 69 | 0 | this.messageContentService = messageContentService; |
| 70 | 0 | this.notificationAuthorizationService = notificationAuthorizationService; |
| 71 | 0 | this.notificationRecipientService = notificationRecipientService; |
| 72 | 0 | this.notificationWorkflowDocumentService = notificationWorkflowDocumentService; |
| 73 | 0 | this.notificationMessageDeliveryService = notificationMessageDeliveryService; |
| 74 | 0 | } |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public Notification getNotification(Long id) { |
| 81 | 0 | HashMap<String, Long> primaryKeys = new HashMap<String, Long>(); |
| 82 | 0 | primaryKeys.put(NotificationConstants.BO_PROPERTY_NAMES.ID, id); |
| 83 | |
|
| 84 | 0 | return (Notification) businessObjectDao.findByPrimaryKey(Notification.class, primaryKeys); |
| 85 | |
} |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
public NotificationResponse sendNotification(String notificationMessageAsXml) throws IOException, InvalidXMLException { |
| 95 | |
|
| 96 | 0 | Notification notification = messageContentService.parseNotificationRequestMessage(notificationMessageAsXml); |
| 97 | |
|
| 98 | |
|
| 99 | 0 | return sendNotification(notification); |
| 100 | |
} |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public NotificationResponse sendNotification(Notification notification) { |
| 106 | 0 | NotificationResponse response = new NotificationResponse(); |
| 107 | |
|
| 108 | |
|
| 109 | 0 | boolean producerAuthorizedForChannel = notificationAuthorizationService.isProducerAuthorizedToSendNotificationForChannel(notification.getProducer(), notification.getChannel()); |
| 110 | 0 | if(!producerAuthorizedForChannel) { |
| 111 | 0 | LOG.error("Producer " + notification.getProducer() + " is not authorized to send messages to channel " + notification.getChannel()); |
| 112 | 0 | response.setStatus(NotificationConstants.RESPONSE_STATUSES.FAILURE); |
| 113 | 0 | response.setMessage(NotificationConstants.RESPONSE_MESSAGES.PRODUCER_NOT_AUTHORIZED_FOR_CHANNEL); |
| 114 | 0 | return response; |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | 0 | for(int i = 0; i < notification.getRecipients().size(); i++) { |
| 119 | 0 | NotificationRecipient recipient = notification.getRecipient(i); |
| 120 | 0 | boolean validRecipient = notificationRecipientService.isRecipientValid(recipient.getRecipientId(), recipient.getRecipientType()); |
| 121 | 0 | if(!validRecipient) { |
| 122 | 0 | response.setStatus(NotificationConstants.RESPONSE_STATUSES.FAILURE); |
| 123 | 0 | response.setMessage(NotificationConstants.RESPONSE_MESSAGES.INVALID_RECIPIENT + " - recipientId=" + |
| 124 | |
recipient.getRecipientId() + ", recipientType=" + recipient.getRecipientType()); |
| 125 | 0 | return response; |
| 126 | |
} |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | 0 | if (notification.getCreationDateTime() == null) { |
| 131 | 0 | notification.setCreationDateTime(new Timestamp(System.currentTimeMillis())); |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | 0 | if(notification.getSendDateTime() == null) { |
| 136 | 0 | notification.setSendDateTime(new Timestamp(System.currentTimeMillis())); |
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | 0 | if (notification.getAutoRemoveDateTime() != null) { |
| 141 | 0 | if (notification.getAutoRemoveDateTime().before(notification.getSendDateTime())) { |
| 142 | 0 | response.setStatus(NotificationConstants.RESPONSE_STATUSES.FAILURE); |
| 143 | 0 | response.setMessage(NotificationConstants.RESPONSE_MESSAGES.INVALID_REMOVE_DATE); |
| 144 | 0 | return response; |
| 145 | |
} |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | 0 | if(!notification.getDeliveryType().equalsIgnoreCase(NotificationConstants.DELIVERY_TYPES.ACK) && |
| 150 | |
!notification.getDeliveryType().equalsIgnoreCase(NotificationConstants.DELIVERY_TYPES.FYI)) { |
| 151 | 0 | response.setStatus(NotificationConstants.RESPONSE_STATUSES.FAILURE); |
| 152 | 0 | response.setMessage(NotificationConstants.RESPONSE_MESSAGES.INVALID_DELIVERY_TYPE + " - deliveryType=" + |
| 153 | |
notification.getDeliveryType()); |
| 154 | 0 | return response; |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | |
try { |
| 159 | 0 | businessObjectDao.save(notification); |
| 160 | 0 | } catch(Exception e) { |
| 161 | 0 | response.setStatus(NotificationConstants.RESPONSE_STATUSES.FAILURE); |
| 162 | 0 | response.setMessage(NotificationConstants.RESPONSE_MESSAGES.ERROR_SAVING_NOTIFICATION); |
| 163 | 0 | return response; |
| 164 | 0 | } |
| 165 | |
|
| 166 | |
|
| 167 | 0 | response.setMessage(NotificationConstants.RESPONSE_MESSAGES.SUCCESSFULLY_RECEIVED); |
| 168 | 0 | response.setNotificationId(notification.getId()); |
| 169 | 0 | return response; |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
public Collection getNotificationsForRecipientByType(String contentTypeName, String recipientId) { |
| 177 | 0 | HashMap<String, String> queryCriteria = new HashMap<String, String>(); |
| 178 | 0 | queryCriteria.put(NotificationConstants.BO_PROPERTY_NAMES.CONTENT_TYPE_NAME, contentTypeName); |
| 179 | 0 | queryCriteria.put(NotificationConstants.BO_PROPERTY_NAMES.RECIPIENTS_RECIPIENT_ID, recipientId); |
| 180 | |
|
| 181 | 0 | return businessObjectDao.findMatching(Notification.class, queryCriteria); |
| 182 | |
} |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
public void dismissNotificationMessageDelivery(Long id, String user, String cause) { |
| 188 | |
|
| 189 | 0 | NotificationMessageDelivery nmd = notificationMessageDeliveryService.getNotificationMessageDelivery(id); |
| 190 | 0 | dismissNotificationMessageDelivery(nmd, user, cause); |
| 191 | 0 | } |
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
public void dismissNotificationMessageDelivery(NotificationMessageDelivery nmd, String user, String cause) { |
| 197 | |
|
| 198 | 0 | Notification notification = nmd.getNotification(); |
| 199 | |
|
| 200 | |
|
| 201 | 0 | Collection<NotificationMessageDelivery> userDeliveries = notificationMessageDeliveryService.getNotificationMessageDeliveries(notification, nmd.getUserRecipientId()); |
| 202 | |
|
| 203 | |
final String targetStatus; |
| 204 | |
|
| 205 | |
|
| 206 | 0 | if (NotificationConstants.AUTO_REMOVE_CAUSE.equals(cause)) { |
| 207 | 0 | targetStatus = NotificationConstants.MESSAGE_DELIVERY_STATUS.AUTO_REMOVED; |
| 208 | |
} else { |
| 209 | 0 | targetStatus = NotificationConstants.MESSAGE_DELIVERY_STATUS.REMOVED; |
| 210 | |
} |
| 211 | |
|
| 212 | 0 | KEWActionListMessageDeliverer deliverer = new KEWActionListMessageDeliverer(); |
| 213 | |
|
| 214 | |
|
| 215 | 0 | for (NotificationMessageDelivery messageDelivery: userDeliveries) { |
| 216 | |
|
| 217 | |
|
| 218 | 0 | if (!NotificationConstants.MESSAGE_DELIVERY_STATUS.DELIVERED.equals(messageDelivery.getMessageDeliveryStatus())) { |
| 219 | 0 | LOG.info("Skipping dismissal of non-delivered message delivery #" + messageDelivery.getId()); |
| 220 | 0 | } else if (targetStatus.equals(messageDelivery.getMessageDeliveryStatus())) { |
| 221 | 0 | LOG.info("Skipping dismissal of already removed message delivery #" + messageDelivery.getId()); |
| 222 | |
} else { |
| 223 | 0 | LOG.debug("Dismissing message delivery #" + messageDelivery.getId() + " " + messageDelivery.getLockVerNbr()); |
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | 0 | deliverer.dismissMessageDelivery(messageDelivery, user, cause); |
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
} |
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | 0 | messageDelivery.setMessageDeliveryStatus(targetStatus); |
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | 0 | LOG.debug("Saving message delivery #" + messageDelivery.getId() + " " + messageDelivery.getLockVerNbr()); |
| 243 | 0 | businessObjectDao.save(messageDelivery); |
| 244 | |
|
| 245 | 0 | LOG.debug("Message delivery '" + messageDelivery.getId() + "' for notification '" + messageDelivery.getNotification().getId() + "' was successfully dismissed."); |
| 246 | |
} |
| 247 | 0 | } |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
public Collection<Notification> takeNotificationsForResolution() { |
| 258 | |
|
| 259 | 0 | Criteria criteria = new Criteria(); |
| 260 | 0 | criteria.addEqualTo(NotificationConstants.BO_PROPERTY_NAMES.PROCESSING_FLAG, NotificationConstants.PROCESSING_FLAGS.UNRESOLVED); |
| 261 | 0 | criteria.addLessOrEqualThan(NotificationConstants.BO_PROPERTY_NAMES.SEND_DATE_TIME, new Timestamp(System.currentTimeMillis())); |
| 262 | 0 | criteria.addIsNull(NotificationConstants.BO_PROPERTY_NAMES.LOCKED_DATE); |
| 263 | |
|
| 264 | |
|
| 265 | 0 | Collection<Notification> available_notifications = businessObjectDao.findMatching(Notification.class, criteria, true, RiceConstants.NO_WAIT); |
| 266 | |
|
| 267 | 0 | LOG.debug("Available notifications: " + available_notifications.size()); |
| 268 | |
|
| 269 | |
|
| 270 | 0 | for (Notification notification: available_notifications) { |
| 271 | 0 | LOG.info("notification: " + notification); |
| 272 | 0 | notification.setLockedDate(new Timestamp(System.currentTimeMillis())); |
| 273 | 0 | businessObjectDao.save(notification); |
| 274 | |
} |
| 275 | |
|
| 276 | 0 | return available_notifications; |
| 277 | |
} |
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
public void unlockNotification(Notification notification) { |
| 284 | 0 | Criteria criteria = new Criteria(); |
| 285 | 0 | criteria.addEqualTo(NotificationConstants.BO_PROPERTY_NAMES.ID, notification.getId()); |
| 286 | |
|
| 287 | |
|
| 288 | 0 | Collection<Notification> notifications = businessObjectDao.findMatching(Notification.class, criteria, true, RiceConstants.NO_WAIT); |
| 289 | 0 | if (notifications == null || notifications.size() == 0) { |
| 290 | 0 | throw new RuntimeException("Notification #" + notification.getId() + " not found to unlock"); |
| 291 | |
} |
| 292 | |
|
| 293 | 0 | Notification n = notifications.iterator().next(); |
| 294 | 0 | n.setLockedDate(null); |
| 295 | |
|
| 296 | 0 | businessObjectDao.save(n); |
| 297 | 0 | } |
| 298 | |
} |