au.csiro.notify.service.NotificationServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for au.csiro.notify.service.NotificationServiceImpl.java

Source

/*
   Copyright 2012 Simon Bear
    
   Licensed under the Apache 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
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   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 au.csiro.notify.service;

import java.security.Principal;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.validation.ConstraintViolationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import au.csiro.notify.dao.NotificationDao;
import au.csiro.notify.dao.beans.Notification;
import au.csiro.notify.service.exceptions.AlreadyExistsException;
import au.csiro.notify.service.exceptions.NotFoundException;
import au.csiro.notify.service.exceptions.InvalidDataException;

import com.google.common.collect.Sets;

/**
 * Business Service Implementation for the RESTful Notification Service.
 * 
 * Copyright 2012, Simon Bear All rights reserved.
 * 
 * @author Simon on 26/04/2012
 * @version $Revision$ $Date$
 */
@Service("notificationService")
public class NotificationServiceImpl implements NotificationService {

    private static final Logger logger = LoggerFactory.getLogger(NotificationServiceImpl.class);

    @Resource(name = "notificationDao")
    private NotificationDao notificationDao;

    /* (non-Javadoc)
     * @see au.csiro.notify.service.NotificationService#create(au.csiro.notify.dao.beans.Notification, java.security.Principal)
     */
    @Override
    @Transactional(readOnly = false)
    public long create(Notification notification) {
        logger.trace("entering create({})", notification);
        Notification n;

        try {
            n = notificationDao.save(notification);
            logger.trace("exiting create={}", n.getId());
            return n.getId();
        } catch (ConstraintViolationException e) {
            logger.trace("notification invalid {}, exception: {}", notification, e);
            throw new InvalidDataException("Notification failed validation: " + e.getMessage(), e);
        } catch (DataIntegrityViolationException e) {
            logger.trace("notification already exists {}, exception: {}", notification, e);
            throw new AlreadyExistsException("Notification already exists", e);
        }
        /*catch (TransientDataAccessException e)
        {
               
        }
        catch (NonTransientDataAccessException e)
        {
               
        }*/
    }

    /* (non-Javadoc)
     * @see au.csiro.notify.service.INotificationService#retrieve(long)
     */
    @Override
    @Transactional(readOnly = true)
    public Notification retrieve(long id, Principal principal) throws NotFoundException {
        logger.trace("entering retrieve({})", id);
        Notification n = notificationDao.findOneByRecipient(id, principal.getName());
        if (n == null) {
            logger.trace("exiting retrieve");
            throw new NotFoundException(Notification.class, id);
        }
        logger.trace("exiting retrieve({})", n);
        return n;
    }

    /* (non-Javadoc)
     * @see au.csiro.notify.service.INotificationService#retrieveAll(boolean)
     */
    @Override
    @Transactional(readOnly = true)
    public List<Notification> retrieveAll(boolean includeRead, Principal principal) {
        logger.trace("entering retrieveAll({}, {})", includeRead, principal);
        List<Notification> notifications;
        if (includeRead) {
            notifications = notificationDao.findByRecipientIncludeRead(principal.getName());
        } else {
            notifications = notificationDao.findByRecipient(principal.getName());
        }
        logger.trace("exiting retrieveAll={}", notifications);
        return notifications;
    }

    /* (non-Javadoc)
     * @see au.csiro.notify.service.INotificationService#markRead(long)
     */
    @Override
    @Transactional(readOnly = false)
    public void markRead(long id, Principal principal) throws NotFoundException {
        logger.trace("entering markRead({}, {})", id, principal);
        Notification n = retrieve(id, principal);

        Set<String> read = n.getRead();
        if (read == null) {
            logger.trace("creating new readers set");
            read = Sets.newHashSet();
            n.setRead(read);
        }

        String currentUserId = principal.getName();

        if (!read.contains(currentUserId)) {
            logger.trace("adding new reader to set");
            read.add(currentUserId);
        }
        n = notificationDao.save(n);
        logger.trace("exiting markRead");
    }

    /* (non-Javadoc)
     * @see au.csiro.notify.service.INotificationService#delete(long)
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(long id, Principal principal) throws NotFoundException {
        logger.trace("entering delete({}, {})", id, principal);
        Notification n = retrieve(id, principal);

        Set<String> deleted = n.getDeleted();
        if (deleted == null) {
            logger.trace("creating new deleted set");
            deleted = Sets.newHashSet();
            n.setDeleted(deleted);
        }

        String currentUserId = principal.getName();

        if (!deleted.contains(currentUserId)) {
            logger.trace("adding new deleted to set");
            deleted.add(currentUserId);
        }
        n = notificationDao.save(n);
        logger.trace("exiting deleted");
    }

}