Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2005-2013 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 package org.kuali.rice.ken.service.impl;
 //import org.kuali.rice.core.jpa.criteria.Criteria;
NotificationService implementation - this is the default out-of-the-box implementation of the service.

Kuali Rice Team (
 public class NotificationServiceImpl implements NotificationService {
 	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
Constructs a NotificationServiceImpl class instance.

 	public NotificationServiceImpl(GenericDao businessObjectDaoNotificationMessageContentService messageContentService,
 			NotificationAuthorizationService notificationAuthorizationServiceNotificationRecipientService notificationRecipientService
 			NotificationWorkflowDocumentService notificationWorkflowDocumentService
 			NotificationMessageDeliveryService notificationMessageDeliveryService,
 			NotificationDao notDao) {
 		this. = businessObjectDao;
 		this. = messageContentService;
 		this. = notificationAuthorizationService;
 		this. = notificationRecipientService;
 		this. = notificationWorkflowDocumentService;
 		this. = notificationMessageDeliveryService;
 		this. = notDao;

This is the default implementation that uses the businessObjectDao.

 		HashMap<StringLongprimaryKeys = new HashMap<StringLong>();

This method is responsible for parsing out the notification message which is sent in as a String of XML. It calls the appropriate services to validate the message content, converts it to a BO, and then passes it to another service where its content and meta-data is validated and if successful, it is saved.

 	public NotificationResponseBo sendNotification(String notificationMessageAsXmlthrows IOExceptionXmlException {
 		// try to parse out the XML with the message content service
		NotificationBo notification = .parseNotificationRequestMessage(notificationMessageAsXml);
		// now call out to the meat of the notification sending - this will validate users, groups, producers, and save
		return sendNotification(notification);

		// make sure that the producer is able to send notifications on behalf of the channel
		boolean producerAuthorizedForChannel = .isProducerAuthorizedToSendNotificationForChannel(notification.getProducer(), notification.getChannel());
		if(!producerAuthorizedForChannel) {
			.error("Producer " + notification.getProducer() + " is not authorized to send messages to channel " + notification.getChannel());
			return response;
		// make sure that the recipients are valid
		for(int i = 0; i < notification.getRecipients().size(); i++) {
			NotificationRecipientBo recipient = notification.getRecipient(i);
			boolean validRecipient = .isRecipientValid(recipient.getRecipientId(), recipient.getRecipientType());
			if(!validRecipient) {
						recipient.getRecipientId() + ", recipientType=" + recipient.getRecipientType());
				return response;
		// set the creationDateTime attribute to the current timestamp if it's currently null
		if (notification.getCreationDateTime() == null) {
		// set the sendDateTime attribute to the current timestamp if it's currently null
		if(notification.getSendDateTime() == null) {
			notification.setSendDateTimeValue(new Timestamp(System.currentTimeMillis()));
		// if the autoremove time is before the send date time, reject the notification
		if (notification.getAutoRemoveDateTime() != null) {
			if (notification.getAutoRemoveDateTimeValue().before(notification.getSendDateTimeValue()))  {
				return response;
		// make sure the delivery types are valid
			return response;
		// now try to persist the object
		try {
catch(Exception e) {
			return response;
		// everything looks good!
		return response;

This is the default implementation that uses the businessObjectDao and its findMatching method.

	public Collection getNotificationsForRecipientByType(String contentTypeNameString recipientId) {
		HashMap<StringStringqueryCriteria = new HashMap<StringString>();
		return .findMatching(NotificationBo.classqueryCriteria);

	public void dismissNotificationMessageDelivery(Long idString userString cause) {
		// TODO: implement pessimistic locking on the message delivery

		// get the notification that generated this particular message delivery
		NotificationBo notification = nmd.getNotification();
		// get all of the other deliveries of this notification for the user
		final String targetStatus;
		// if the cause was our internal "autoremove" cause, then we need to indicate
		// the message was autoremoved instead of normally dismissed
else {
		// TODO: implement pessimistic locking on all these message deliveries
		// now, do dispatch in reverse...dismiss each message delivery via the appropriate deliverer
		for (NotificationMessageDelivery messageDeliveryuserDeliveries) {
			// don't attempt to dismiss undelivered message deliveries
				.info("Skipping dismissal of non-delivered message delivery #" + messageDelivery.getId());
else if (targetStatus.equals(messageDelivery.getMessageDeliveryStatus())) {
				.info("Skipping dismissal of already removed message delivery #" + messageDelivery.getId());
else {
				.debug("Dismissing message delivery #" + messageDelivery.getId() + " " + messageDelivery.getVersionNumber());//.getLockVerNbr());
				// we have our message deliverer, so tell it to dismiss the message
				//try {
				//} catch (NotificationMessageDismissalException nmde) {
				//LOG.error("Error dismissing message " + messageDelivery, nmde);
				//throw new RuntimeException(nmde);
			// by definition we have succeeded at this point if no exception was thrown by the messageDeliverer
			// so update the status of the delivery message instance to indicate its dismissal
			// if the message delivery was not actually delivered in the first place, we still need to mark it as
			// removed here so delivery is not attempted again
			// TODO: locking
			// mark as unlocked
			.debug("Saving message delivery #" + messageDelivery.getId() + " " + messageDelivery.getVersionNumber());
			.debug("Message delivery '" + messageDelivery.getId() + "' for notification '" + messageDelivery.getNotification().getId() + "' was successfully dismissed.");

This method is responsible for atomically finding all untaken, unresolved notifications that are ready to be sent, marking them as taken and returning them to the caller for processing. NOTE: it is important that this method execute in a SEPARATE dedicated transaction; either the caller should NOT be wrapped by Spring declarative transaction and this service should be wrapped (which is the case), or the caller should arrange to invoke this from within a newly created transaction).

a list of available notifications that have been marked as taken by the caller
	//switch to JPA criteria
		// get all unprocessed notifications with sendDateTime <= current
//		Criteria criteria = new Criteria();
//		criteria.addEqualTo(NotificationConstants.BO_PROPERTY_NAMES.PROCESSING_FLAG, NotificationConstants.PROCESSING_FLAGS.UNRESOLVED);
//		criteria.addLessOrEqualThan(NotificationConstants.BO_PROPERTY_NAMES.SEND_DATE_TIME, new Timestamp(System.currentTimeMillis()));
//		criteria.addIsNull(NotificationConstants.BO_PROPERTY_NAMES.LOCKED_DATE);
		//criteria = Util.makeSelectForUpdate(criteria);
		//		Criteria criteria = new Criteria(Notification.class.getName());
		//		criteria.eq(NotificationConstants.BO_PROPERTY_NAMES.PROCESSING_FLAG, NotificationConstants.PROCESSING_FLAGS.UNRESOLVED);
		//		criteria.lte(NotificationConstants.BO_PROPERTY_NAMES.SEND_DATE_TIME, new Timestamp(System.currentTimeMillis()));
		//		criteria.isNull(NotificationConstants.BO_PROPERTY_NAMES.LOCKED_DATE);
		//Collection<Notification> available_notifications = businessObjectDao.findMatching(Notification.class, criteria, true, RiceConstants.NO_WAIT);
		//LOG.debug("Available notifications: " + available_notifications.size());
		// mark as "taken"
		if (available_notifications != null) {
			for (NotificationBo notificationavailable_notifications) {
				.info("notification: " + notification);
				notification.setLockedDateValue(new Timestamp(System.currentTimeMillis()));
		return available_notifications;

Unlocks specified notification

notification the notification object to unlock
	//switch to JPA criteria
	public void unlockNotification(NotificationBo notification) {
//		Map<String, Long> criteria = new HashMap<String, Long>();
//		criteria.put(NotificationConstants.BO_PROPERTY_NAMES.ID, notification.getId());
//		Criteria criteria = new Criteria();
//		criteria.addEqualTo(NotificationConstants.BO_PROPERTY_NAMES.ID, notification.getId());
		//criteria = Util.makeSelectForUpdate(criteria);
		//		Criteria criteria = new Criteria(Notification.class.getName());
		//		criteria.eq(NotificationConstants.BO_PROPERTY_NAMES.ID, notification.getId());
		//Collection<Notification> notifications = businessObjectDao.findMatching(Notification.class, criteria, true, RiceConstants.NO_WAIT);
		if (notifications == null || notifications.size() == 0) {
			throw new RuntimeException("Notification #" + notification.getId() + " not found to unlock");
		NotificationBo n = notifications.iterator().next();
New to GrepCode? Check out our FAQ X