/*
* 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.local.UserAuthServiceLocal;
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.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
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")
@DeclareRoles({"ROLE_USER"})
@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;
@EJB
UserAuthServiceLocal userEjb;
@PersistenceContext(unitName = "EvasionPU")
private EntityManager em;
@Resource
SessionContext sessionContext;
/**
* 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);
contribution.setUser(userEjb.findUserByUserName(sessionContext.getCallerPrincipal().getName()));
roadMapDAO.persist(roadMap);
eventEJB.addEvent(Constante.PLUGIN_NAME, RoadMap.class.getSimpleName(),
roadMap.getId().toString(), "CREATE_ROADMAP", roadMap.getContribution().getUser());
}
@Override
public List<RoadMap> 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);
}
@RolesAllowed("ROLE_USER")
@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;");
}
commentaireNew.setUser(userEjb.findUserByUserName(sessionContext.getCallerPrincipal().getName()));
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;
}
/**
* {@inheritDoc }
*/
@Override
public List<RoadMap> getArchiveRoadMapBetweenDate(Long idBookTravel, Date dateDebut, Date dateFin) {
final long yearInMilisecond = 86400 * 365;
if (dateDebut.before(dateFin) && dateDebut.compareTo(dateFin) > yearInMilisecond) {
throw new IllegalArgumentException("Start date can not be upper than end Date and end date - start date > 1 year");
}
return roadMapDAO.getRoadMapUnderDate(idBookTravel, dateDebut, dateFin);
}
}