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.AbstractEJBModule;
import com.evasion.entity.booktravel.BookTravel;
import com.evasion.entity.booktravel.Itinerary;
import com.evasion.entity.booktravel.RoadMap;
import com.evasion.entity.content.Comment;
import com.evasion.entity.content.Contribution;
import com.evasion.exception.EvasionException;
import com.evasion.module.common.ICommonModule;
import com.evasion.module.travel.ITravelModule;
import com.evasion.plugin.travel.dao.BookTravelDAO;
import com.evasion.plugin.travel.dao.RoadMapDAO;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.ejb.*;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author sebastien
*/
@Stateful(name = "bookTravelManager")
@DeclareRoles({"ROLE_USER"})
@Remote(value = ITravelModule.class)
public class BookTravelService extends AbstractEJBModule implements ITravelModule {
/**
* LOGGER.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(
BookTravelService.class);
@EJB
ICommonModule eventEJB;
@PersistenceContext(unitName = "EvasionPU", type = PersistenceContextType.EXTENDED)
private transient 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 }.
*/
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
@Override
public BookTravel findBooktravelWithoutRoadMap(final Long id)
throws EvasionException {
LOGGER.debug("Recherche du BookTravel id: {}", id);
BookTravel result = bookTravelDAO.findById(id);
return result;
}
/**
* {@inheritDoc }.
*/
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Override
public BookTravel createOrUpdateBookTravel(final BookTravel booktravel)
throws EvasionException {
LOGGER.debug("Création du BookTravel nom: {}", booktravel.getNom());
if ("ANONYMOUS".equals(this.getPrinciaplUserName())) {
booktravel.setUsername("admin");
} else {
booktravel.setUsername(this.getPrinciaplUserName());
}
bookTravelDAO.merge(booktravel);
if (booktravel.getId()
!= null) {
eventEJB.addEvent(Constante.PLUGIN_NAME, BookTravel.class.getSimpleName(),
booktravel.getId().toString(), "CREATE_BOOKTRAVEL");
}
return booktravel;
}
/**
* {@inheritDoc }.
*/
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Override
public void createRoadMap(final Long idBookTravel,
final Contribution contribution, final Itinerary itinerary)
throws EvasionException {
final BookTravel bookTravel = bookTravelDAO.findById(idBookTravel);
contribution.setUser(this.getPrinciaplUserName());
final RoadMap roadMap = new RoadMap(contribution, itinerary, bookTravel);
roadMapDAO.persist(roadMap);
eventEJB.addEvent(Constante.PLUGIN_NAME, RoadMap.class.getSimpleName(),
roadMap.getId().toString(), "CREATE_ROADMAP");
}
@Override
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
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
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public RoadMap findRoadMapById(Long id) throws EvasionException {
if (id == null) {
throw new UnsupportedOperationException("Properties id"
+ " can not be null");
}
RoadMap result = roadMapDAO.findById(id);
if (result != null) {
result.getContribution().getCommentaires().size(); // force la remontée en lazy;
}
return result;
}
@RolesAllowed("ROLE_USER")
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public boolean createCommentOnRoadMap(final RoadMap roadMap,
final Comment commentaireNew) throws EvasionException {
if (roadMap == null || commentaireNew == null) {
throw new IllegalArgumentException("roadMap and commentaireNew can not be null;");
}
commentaireNew.setUserName(this.getPrinciaplUserName());
if (commentaireNew.getUserName() == 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");
}
return result;
}
/**
* {@inheritDoc }
*/
@Override
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public List 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);
}
@Override
public Map findBooktravelForUser(String username) throws EvasionException {
List books = bookTravelDAO.findBookTravelByAuthor(username);
Map result = new HashMap(books.size());
for (BookTravel bookTravel : books) {
result.put(bookTravel.getId(), bookTravel.getNom());
}
return result;
}
@Override
public Map lastBookTravel(int nbr) {
List books = bookTravelDAO.findBookTravelOrderbyDateDepart(nbr);
Map result = new HashMap(books.size());
for (BookTravel bookTravel : books) {
result.put(bookTravel.getId(), bookTravel.getNom());
}
return result;
}
}