PublisherServiceDefault.java 5.04 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 java.sql.Timestamp;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/*
    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;

    @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;
    }

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

    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;
    }

    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;
    }

    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);
        IndexedPublisher indexedPublisher = new IndexedPublisher(publisher);
        try {
            publisherSearchRepository.save(indexedPublisher);
        } catch (IndexingException e) {
            logger.error("IndexingException {}", e);
        }
        publisherActionRepository.save(publisherAction);
        return publisherAction;
    }

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

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

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

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

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

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

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

}