All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.evasion.plugin.travel.BookTravelService Maven / Gradle / Ivy

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evasion.plugin.travel;

import com.evasion.ejb.local.BookTravelManagerLocal;
import com.evasion.ejb.local.EventHandlerLocal;
import com.evasion.ejb.remote.BookTravelManagerRemote;
import com.evasion.entity.booktravel.BookTravel;
import com.evasion.entity.booktravel.Itinerary;
import com.evasion.entity.booktravel.RoadMap;
import com.evasion.entity.booktravel.exception.BookTravelServiceException;
import com.evasion.entity.content.Comment;
import com.evasion.entity.content.Contribution;
import com.evasion.plugin.travel.dao.BookTravelDAO;
import com.evasion.plugin.travel.dao.RoadMapDAO;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author sebastien
 */
@Stateless(name = "bookTravelManager")
@Local(value = BookTravelManagerLocal.class)
@Remote(value = BookTravelManagerRemote.class)
public class BookTravelService implements BookTravelManagerLocal, BookTravelManagerRemote {

    /**
     * LOGGER.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(
            BookTravelService.class);

    @EJB
    EventHandlerLocal eventEJB;

    @PersistenceContext(unitName = "EvasionPU")
    private EntityManager em;

    /**
     * Couche d'acces aux donnees du carnet de voyage.
     */
    private final BookTravelDAO bookTravelDAO;

    /**
     * Couche d'acces aux donnees des feuilles de route.
     */
    private RoadMapDAO roadMapDAO;

    /**
     * Constructeur par defaut.
     * @param em Entity Manager.
     */
    protected BookTravelService(final EntityManager em) {
        bookTravelDAO = new BookTravelDAO();
        bookTravelDAO.setEntityManager(em);

        roadMapDAO = new RoadMapDAO();
        roadMapDAO.setEntityManager(em);
    }

    public BookTravelService() {
        bookTravelDAO = new BookTravelDAO();
        roadMapDAO = new RoadMapDAO();
    }

    @SuppressWarnings("PMD.UnusedPrivateMethod")
    @edu.umd.cs.findbugs.annotations.SuppressWarnings("UPM_UNCALLED_PRIVATE_METHOD")
    @PostConstruct
    private void init() {
        bookTravelDAO.setEntityManager(em);
        roadMapDAO.setEntityManager(em);
    }

    /**
     * {@inheritDoc }.
     */
    @Override
    public BookTravel findBooktravelWithoutRoadMap(final Long id)
            throws BookTravelServiceException {
        LOGGER.debug("Recherche du BookTravel id: {}", id);
        BookTravel result = bookTravelDAO.findById(id);
        return result;
    }

    /**
     * {@inheritDoc }.
     */
    @Override
    public BookTravel createBookTravel(final BookTravel booktravel)
            throws BookTravelServiceException {
        LOGGER.debug("Création du BookTravel nom: {}", booktravel.getNom());
        BookTravel book = bookTravelDAO.merge(booktravel);
        eventEJB.addEvent(Constante.PLUGIN_NAME, BookTravel.class.getSimpleName(),
                    book.getId().toString(), "CREATE_BOOKTRAVEL", book.getAuteur());
        return book;
    }

    /**
     * {@inheritDoc }.
     */
    @Override
    public void createRoadMap(final Long idBookTravel,
            final Contribution contribution, final Itinerary itinerary)
            throws BookTravelServiceException {
        final BookTravel bookTravel = bookTravelDAO.findById(idBookTravel);
        final RoadMap roadMap = new RoadMap(contribution, itinerary, bookTravel);

        roadMapDAO.persist(roadMap);
        eventEJB.addEvent(Constante.PLUGIN_NAME, RoadMap.class.getSimpleName(),
                roadMap.getId().toString(), "CREATE_ROADMAP", roadMap.getContribution().getUser());
    }

    @Override
    public List findNewestRoadMap(final Long idBookTravel,
            final int size) {
        if (idBookTravel == null) {
            throw new UnsupportedOperationException("Properties idBookTravel"
                    + " can not be null");
        }
        return roadMapDAO.selectRoadMapByDescendingExecutionDate(idBookTravel,
                size);
    }

    @Override
    public RoadMap findRoadMapById(Long id) throws BookTravelServiceException {
        if (id == null) {
            throw new UnsupportedOperationException("Properties id"
                    + " can not be null");
        }
        return roadMapDAO.findById(id);
    }

    @Override
    public boolean createCommentOnRoadMap(final RoadMap roadMap,
            final Comment commentaireNew) {
        if (roadMap == null || commentaireNew == null) {
            throw new IllegalArgumentException("roadMap and commentaireNew can not be null;");
        }
        if (commentaireNew.getUser() == null) {
            throw new IllegalArgumentException("Comment can have a user; it can't be null");
        }
        if (StringUtils.isBlank(commentaireNew.getText())) {
            throw new IllegalArgumentException("Comment text can be blank or null");
        }
        final RoadMap roadMapBDD = this.roadMapDAO.findById(roadMap.getId());
        final boolean result;

        if (roadMapBDD.getContribution() == null) {
            result = false;
        } else {

            result = roadMapBDD.getContribution().addCommentaire(commentaireNew);
            this.roadMapDAO.merge(roadMapBDD);
            eventEJB.addEvent(Constante.PLUGIN_NAME, RoadMap.class.getSimpleName(),
                    roadMap.getId().toString(), "ADD_COMMENT_ON_ROADMAP", commentaireNew.getUser());
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy