PublisherServiceSpring.java 3.21 KB
package org.legrog.web.publisher;


import org.legrog.entities.Publisher;
import org.legrog.entities.PublisherRepository;
import org.legrog.entities.PublisherVersion;
import org.legrog.entities.PublisherVersionRepository;
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 PublisherServiceSpring implements PublisherService {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Inject
    PublisherRepository publisherRepository;

    @Inject
    PublisherVersionRepository publisherVersionRepository;

    @Inject
    SharedService sharedService;

    public void addPublisherWithVersion(Publisher publisher, PublisherVersion publisherVersion) {
        Set<PublisherVersion> publisherVersions = new HashSet<PublisherVersion>();
        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);
    }

    public void 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);
    }

    public void validatePublisherVersion(Publisher publisher, Integer publisherVersionId) {
        publisher.setValidatedVersion(this.getPublisherVersion(publisherVersionId));
        publisher.setValidator(sharedService.getCurrentUser());
        publisher.setValidationDateTime(new Timestamp(new Date().getTime()));
        this.savePublisher(publisher);
    }

    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 Publisher getPublisher(Integer publisherId) {
        return publisherRepository.findOne(publisherId);
    }

}