PublisherServiceDefault.java 6.31 KB
package org.legrog.web.publisher;


import org.legrog.entities.*;
import org.legrog.web.xyz.SharedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.sql.Timestamp;
import java.util.*;

/**
 * Implémentation de l'interface PublisherService
 */
@Stateless
public class PublisherServiceDefault implements PublisherService {
    Logger logger = LoggerFactory.getLogger(getClass());

    PublisherRepository publisherRepository;
    PublisherVersionRepository publisherVersionRepository;
    PublisherActionRepository publisherActionRepository;
    PublisherSearchRepository publisherSearchRepository;
    SharedService sharedService;

    /**
     * Le service s'appuie concrètement sur un ensemble de dépôts et sur le service auxiliaire SharedService.
     *
     * @param publisherRepository
     * @param publisherVersionRepository
     * @param publisherActionRepository
     * @param publisherSearchRepository
     * @param sharedService
     */
    @Inject
    public PublisherServiceDefault(PublisherRepository publisherRepository,
                                   PublisherVersionRepository publisherVersionRepository,
                                   PublisherActionRepository publisherActionRepository,
                                   PublisherSearchRepository publisherSearchRepository,
                                   SharedService sharedService) {
        this.publisherRepository = publisherRepository;
        this.publisherVersionRepository = publisherVersionRepository;
        this.publisherActionRepository = publisherActionRepository;
        this.publisherSearchRepository = publisherSearchRepository;
        this.sharedService = sharedService;
    }

    PublisherServiceDefault() {
        //no args constructor to make it proxyable
    }

    @Override
    public PublisherVersion addNewPublisher(PublisherVersion publisherVersion) {
        Publisher publisher = new Publisher();
        Set<PublisherVersion> publisherVersions = new HashSet<>();
        publisherVersions.add(publisherVersion);
        publisher.setVersions(publisherVersions);
        publisherVersion.setPublisher(publisher);
        publisherVersion.setPublisherVersionAuthor(sharedService.getCurrentUser());
        publisherVersion.setPublisherVersionDatetime(new Timestamp(new Date().getTime()));
        this.savePublisherVersion(publisherVersion);
        this.savePublisher(publisher);
        return publisherVersion;
    }

    @Override
    public PublisherVersion addVersionToPublisher(Publisher publisher, PublisherVersion publisherVersion) {
        logger.trace("debut addVersionToPublisher, publisherVersion = {}", publisherVersion);
        Set<PublisherVersion> publisherVersionSet = publisher.getVersions();
        publisherVersionSet.add(publisherVersion);
        publisher.setVersions(publisherVersionSet);
        publisherVersion.setPublisher(publisher);
        publisherVersion.setPublisherVersionAuthor(sharedService.getCurrentUser());
        publisherVersion.setPublisherVersionDatetime(new Timestamp(new Date().getTime()));
        this.savePublisherVersion(publisherVersion);
        this.savePublisher(publisher);
        logger.trace("fin addVersionToPublisher, publisherVersion = {}", publisherVersion);
        return publisherVersion;
    }

    @Override
    public PublisherAction validatePublisherVersion(PublisherVersion publisherVersion) {
        Publisher publisher = publisherVersion.getPublisher();
        publisher.setValidatedVersion(publisherVersion);
        PublisherAction publisherAction = new PublisherAction();
        publisherAction.setPublisherActionAuthor(sharedService.getCurrentUser());
        publisherAction.setPublisherActionDatetime(new Timestamp(new Date().getTime()));
        publisherAction.setActionType(ActionType.VALIDATE);
        publisherAction.setPublisherVersion(publisherVersion);
        publisherAction.setPublisher(publisher);
        this.savePublisher(publisher);
        this.index(publisher);
        publisherActionRepository.save(publisherAction);
        return publisherAction;
    }

    protected void index(Publisher publisher) {
        IndexedPublisher indexedPublisher = new IndexedPublisher(publisher);
        try {
            publisherSearchRepository.save(indexedPublisher);
        } catch (IndexingException e) {
            logger.error("IndexingException {}", e);
            // TODO Ajouter l'information de manière plus accessible que dans les logs
        }
    }

    protected void savePublisherVersion(PublisherVersion publisherVersion) {
        publisherVersionRepository.save(publisherVersion);
    }

    protected void savePublisher(Publisher publisher) {
        publisherRepository.save(publisher);
    }

    // get
    @Override
    public List<PublisherVersion> getAllPublisherVersions() {
        return publisherVersionRepository.findAll();
    }

    @Override
    public PublisherVersion getPublisherVersion(Integer publisherVersionId) {
        return publisherVersionRepository.findOne(publisherVersionId);
    }

    @Override
    public PublisherAction getLastValidate(Publisher publisher) {
        return publisherActionRepository.findFirstByActionTypeAndPublisherOrderByPublisherActionDatetimeDesc(ActionType.VALIDATE, publisher);
    }

    @Override
    public List<PublisherAction> getAllPublisherActions() {
        return publisherActionRepository.findAll();
    }

    @Override
    public List<PublisherAction> getAllPublisherVersionActions(PublisherVersion publisherVersion) {
        return publisherActionRepository.findByPublisherVersion(publisherVersion);
    }

    @Override
    public List<PublisherVersion> search(@NotNull String string) throws SearchingException {
        return convertIndexedPublishersIntoPublisherVersions(publisherSearchRepository.search(string));
    }

    @Override
    public List<PublisherVersion> convertIndexedPublishersIntoPublisherVersions(List<IndexedPublisher> indexedPublishers) {
            List<Integer> integers = new ArrayList<>(indexedPublishers.size());
            indexedPublishers.forEach(indexedPublisher -> integers.add(indexedPublisher.getPublisherId()));

        if (!integers.isEmpty()) {
            return publisherVersionRepository.findByPublisherVersionIdIn(integers);
        }

        return new ArrayList<>();
    }

}