Package com.evasion.plugin.travel

Source Code of com.evasion.plugin.travel.BookTravelService

/*
* 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.ejb.remote.GeolocEJBRemote;
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.entity.geolocation.Location;
import com.evasion.exception.EvasionException;
import com.evasion.module.common.ICommonModule;
import com.evasion.module.travel.ITravelModule;
import com.evasion.module.travel.ITravelStep;
import com.evasion.plugin.travel.dao.BookTravelDAO;
import com.evasion.plugin.travel.dao.RoadMapDAO;
import java.util.*;
import javax.annotation.PostConstruct;
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", "ROLE_VOYAGEUR"})
@Remote(value = ITravelModule.class)
public class BookTravelService extends AbstractEJBModule implements ITravelModule {

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

    @EJB
    private ICommonModule eventEJB;

    @EJB
    private GeolocEJBRemote geolocEJB;

    @PersistenceContext(unitName = "EvasionPU", type = PersistenceContextType.EXTENDED)
    private transient 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 }.
     */
    @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());
        for (int i = 0; i < itinerary.getBreakPoints().size(); i++) {
            Location loc = itinerary.getBreakPoints().get(i);

            geolocEJB.getLocationByid(loc.getId());
            itinerary.replaceBreakPoint(i, geolocEJB.getLocationByid(loc.getId()));
        }
        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<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
    @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<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);
    }

    @Override
    public Map<Long, String> findBooktravelForUser(String username) throws EvasionException {
        List<BookTravel> books = bookTravelDAO.findBookTravelByAuthor(username);
        Map<Long, String> result = new HashMap<Long, String>(books.size());

        for (BookTravel bookTravel : books) {
            result.put(bookTravel.getId(), bookTravel.getNom());
        }
        return result;
    }

    @Override
    public Map<Long, String> lastBookTravel(int nbr) {
        List<BookTravel> books = bookTravelDAO.findBookTravelOrderbyDateDepart(nbr);
        Map<Long, String> result = new HashMap<Long, String>(books.size());
        for (BookTravel bookTravel : books) {
            result.put(bookTravel.getId(), bookTravel.getNom());
        }
        return result;
    }

    @Override
    public List<ITravelStep> getBookTravelStep(Long idBookTravel) {
        List<RoadMap> roadMaps = roadMapDAO.selectRoadMapByDescendingExecutionDate(idBookTravel, 999);
        List<ITravelStep> result = new ArrayList<ITravelStep>();
        for (RoadMap roadMap : roadMaps) {
            List<Location> breakPoints = roadMap.getItinerary().getBreakPoints();
            Location start;
            Location end;

            if (breakPoints.size() > 0) {
                start = breakPoints.get(0);
            } else {
                start = null;
            }


            if (breakPoints.size() > 1) {
                end = breakPoints.get(breakPoints.size() - 1);
            } else {
                end = null;
            }

            result.add(new TravelSetBean(roadMap.getId(),
                    roadMap.getContribution().getTitle(), start, end));
        }

        return result;
    }
}
TOP

Related Classes of com.evasion.plugin.travel.BookTravelService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.