PublisherVersionView.java 10.8 KB
package org.legrog.web.publisher;

import org.legrog.entities.PublisherVersion;
import org.legrog.web.xyz.SharedService;
import org.legrog.entities.Country;
import org.legrog.entities.Publisher;
import org.legrog.entities.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;

/*
    Vue de publisherVersion.xhtml
    Permet la création, l'affichage et la modification d'une version d'éditeur.
    La création d'une version crée l'éditeur et sa version initiale.
    Le modification crée une nouvelle version liée à l'éditeur.
 */
@Named
@ViewScoped
public class PublisherVersionView implements Serializable {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Inject
    PublisherService publisherService;
    @Inject
    SharedService sharedService;

    private boolean editMode;
    private boolean newPublisher;

    private int publisherVersionId;
    private String publisherName;
    private String publisherStreetAddress;
    private String publisherPostalCode;
    private String publisherPostOfficeBoxNumber;
    private String publisherAddressRegion;
    private String publisherAddressLocality;
    private Country publisherAddressCountry;
    private String publisherTelephone;
    private String publisherEmail;
    private String publisherURL;
    private boolean publisherActive;
    private String publisherHistory;
    private User publisherVerisionAuthor;
    private Timestamp publisherVersionDatetime;
    private Publisher publisher;

    private List<Country> availableCountries;

    @PostConstruct
    public void init() {
        availableCountries = sharedService.getAllCountries();
    }

    public void edit() {
        editMode = true;
    }

    public void cancel() {
        if (!newPublisher) {
            loadData();
            editMode = false;
        } else {
            publisherName = null;
            publisherStreetAddress = null;
            publisherPostalCode = null;
            publisherPostOfficeBoxNumber = null;
            publisherAddressRegion = null;
            publisherAddressLocality = null;
            publisherAddressCountry = null;
            publisherTelephone = null;
            publisherEmail = null;
            publisherURL = null;
            publisherActive = false;
            publisherHistory = null;
            publisher = null;

            editMode = true;
        }
    }

    public void save() {
        logger.debug("save()");
        if (newPublisher) {
            logger.debug("->add");
            add();
        } else {
            logger.debug("->update");
            update();
        }
    }

    private void setValues(PublisherVersion publisherVersion) {
        publisherVersion.setPublisherActive(publisherActive);
        publisherVersion.setPublisherAddressCountry(publisherAddressCountry);
        publisherVersion.setPublisherAddressLocality(publisherAddressLocality);
        publisherVersion.setPublisherAddressRegion(publisherAddressRegion);
        publisherVersion.setPublisherEmail(publisherEmail);
        publisherVersion.setPublisherHistory(publisherHistory);
        publisherVersion.setPublisherName(publisherName);
        publisherVersion.setPublisherPostalCode(publisherPostalCode);
        publisherVersion.setPublisherPostOfficeBoxNumber(publisherPostOfficeBoxNumber);
        publisherVersion.setPublisherStreetAddress(publisherStreetAddress);
        publisherVersion.setPublisherTelephone(publisherTelephone);
        publisherVersion.setPublisherURL(publisherURL);
    }

    private void getImmutableValues() {
        PublisherVersion publisherVersion;

        publisherVersion = publisherService.getPublisherVersion(publisherVersionId);
        publisherVerisionAuthor = publisherVersion.getPublisherVersionAuthor();
        publisherVersionDatetime = publisherVersion.getPublisherVersionDatetime();
    }

    public void add() {
        Publisher publisher = new Publisher();
        PublisherVersion publisherVersion = new PublisherVersion();
        setValues(publisherVersion);

        publisherService.addPublisherWithVersion(publisher, publisherVersion);

        newPublisher = false;
        publisherVersionId = publisherVersion.getPublisherVersionId();
        this.publisher = publisher;

        getImmutableValues();

        editMode = false;
    }

    public void update() {
        logger.trace("debut update, attributs locaux, publisherActive = {} publisherAddressCountry = {} publisherAddressLocality = {} publisherAddressRegion = {} publisherEmail = {} publisherHistory = {} publisherName = {} publisherPostalCode = {} publisherPostOfficeBoxNumber = {} publisherStreetAddress = {} publisherTelephone {} publisherURL {}",
                publisherActive, publisherAddressCountry, publisherAddressLocality, publisherAddressRegion,
                publisherEmail, publisherHistory, publisherName, publisherPostalCode, publisherPostOfficeBoxNumber,
                publisherStreetAddress, publisherTelephone, publisherURL);

        PublisherVersion publisherVersion = new PublisherVersion();
        setValues(publisherVersion);

        logger.trace("milieu update, avant enregistrement, publisherVersion = {}", publisherVersion);
        publisherService.addVersionToPublisher(publisher, publisherVersion);

        publisherVersionId = publisherVersion.getPublisherVersionId();

        getImmutableValues();

        editMode = false;
        logger.trace("fin update, après récupération, publisherVersion = {}", publisherVersion);
    }

    public void loadData() {
        logger.debug("publisherVersionId = {}", this.publisherVersionId);

        if (publisherVersionId != 0) {
            
            PublisherVersion publisherVersion = publisherService.getPublisherVersion(publisherVersionId);

            logger.debug("Found a publisher version : {}", publisherVersion.toString());

            publisherName = publisherVersion.getPublisherName();
            publisherStreetAddress = publisherVersion.getPublisherStreetAddress();
            publisherPostalCode = publisherVersion.getPublisherPostalCode();
            publisherPostOfficeBoxNumber = publisherVersion.getPublisherPostOfficeBoxNumber();
            publisherAddressRegion = publisherVersion.getPublisherAddressRegion();
            publisherAddressLocality = publisherVersion.getPublisherAddressLocality();
            publisherAddressCountry = publisherVersion.getPublisherAddressCountry();
            publisherTelephone = publisherVersion.getPublisherTelephone();
            publisherEmail = publisherVersion.getPublisherEmail();
            publisherURL = publisherVersion.getPublisherURL();
            publisherActive = publisherVersion.isPublisherActive();
            publisherHistory = publisherVersion.getPublisherHistory();
            publisher = publisherVersion.getPublisher();

            getImmutableValues();

            logger.debug("Found a publisher : {}", publisher.toString());
            newPublisher = false;
        } else {
            logger.debug("No publisher.");
            editMode = true;
            newPublisher = true;
        }

    }

    // get, set et is
    public int getPublisherVersionId() {
        return publisherVersionId;
    }

    public void setPublisherVersionId(int publisherVersionId) {
        this.publisherVersionId = publisherVersionId;
    }

    public String getPublisherName() {
        return publisherName;
    }

    public void setPublisherName(String publisherName) {
        this.publisherName = publisherName;
    }

    public String getPublisherStreetAddress() {
        return publisherStreetAddress;
    }

    public void setPublisherStreetAddress(String publisherStreetAddress) {
        this.publisherStreetAddress = publisherStreetAddress;
    }

    public String getPublisherPostalCode() {
        return publisherPostalCode;
    }

    public void setPublisherPostalCode(String publisherPostalCode) {
        this.publisherPostalCode = publisherPostalCode;
    }

    public String getPublisherPostOfficeBoxNumber() {
        return publisherPostOfficeBoxNumber;
    }

    public void setPublisherPostOfficeBoxNumber(String publisherPostOfficeBoxNumber) {
        this.publisherPostOfficeBoxNumber = publisherPostOfficeBoxNumber;
    }

    public String getPublisherAddressRegion() {
        return publisherAddressRegion;
    }

    public void setPublisherAddressRegion(String publisherAddressRegion) {
        this.publisherAddressRegion = publisherAddressRegion;
    }

    public String getPublisherAddressLocality() {
        return publisherAddressLocality;
    }

    public void setPublisherAddressLocality(String publisherAddressLocality) {
        this.publisherAddressLocality = publisherAddressLocality;
    }

    public Country getPublisherAddressCountry() {
        return publisherAddressCountry;
    }

    public void setPublisherAddressCountry(Country publisherAddressCountry) {
        this.publisherAddressCountry = publisherAddressCountry;
    }

    public String getPublisherTelephone() {
        return publisherTelephone;
    }

    public void setPublisherTelephone(String publisherTelephone) {
        this.publisherTelephone = publisherTelephone;
    }

    public String getPublisherEmail() {
        return publisherEmail;
    }

    public void setPublisherEmail(String publisherEmail) {
        this.publisherEmail = publisherEmail;
    }

    public String getPublisherURL() {
        return publisherURL;
    }

    public void setPublisherURL(String publisherURL) {
        this.publisherURL = publisherURL;
    }

    public boolean isPublisherActive() {
        return publisherActive;
    }

    public void setPublisherActive(boolean publisherActive) {
        this.publisherActive = publisherActive;
    }

    public String getPublisherHistory() {
        return publisherHistory;
    }

    public void setPublisherHistory(String publisherHistory) {
        this.publisherHistory = publisherHistory;
    }

    public User getPublisherVerisionAuthor() {
        return publisherVerisionAuthor;
    }

    public void setPublisherVerisionAuthor(User publisherVerisionAuthor) {
        this.publisherVerisionAuthor = publisherVerisionAuthor;
    }

    public Timestamp getPublisherVersionDatetime() {
        return publisherVersionDatetime;
    }

    public void setPublisherVersionDatetime(Timestamp publisherVersionDatetime) {
        this.publisherVersionDatetime = publisherVersionDatetime;
    }

    public List<Country> getAvailableCountries() {
        return availableCountries;
    }

    public void setAvailableCountries(List<Country> availableCountries) {
        this.availableCountries = availableCountries;
    }

    public boolean isNewPublisher() {
        return newPublisher;
    }

    public boolean isEditMode() {
        return editMode;
    }

}