Package com.evasion.plugin.common

Source Code of com.evasion.plugin.common.ParametreManager

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.evasion.plugin.common;

import com.evasion.plugin.common.dao.ParametreDaoImpl;
import com.evasion.ejb.local.ParametreManagerLocal;
import com.evasion.ejb.remote.ParametreManagerRemote;
import com.evasion.entity.Parametre;
import javax.annotation.PostConstruct;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author sebastien.glon
*/
@Stateless
@Local(value = ParametreManagerLocal.class)
@Remote(value = ParametreManagerRemote.class)
public class ParametreManager implements ParametreManagerLocal, ParametreManagerRemote {

    /** LOGGER */
    private static final Logger LOGGER = LoggerFactory.getLogger(
            ParametreManager.class);
    @PersistenceContext(unitName = "EvasionPU")
    private EntityManager em;
    private final ParametreDaoImpl dao;

    protected ParametreManager(EntityManager em) {
        dao = new ParametreDaoImpl();
        dao.setEntityManager(em);
    }

    public ParametreManager() {
        dao = new ParametreDaoImpl();
    }

    @SuppressWarnings("PMD.UnusedPrivateMethod")
    @edu.umd.cs.findbugs.annotations.SuppressWarnings("UPM_UNCALLED_PRIVATE_METHOD")
    @PostConstruct
    private void init() {
        dao.setEntityManager(em);
    }

    @Override
    public Boolean saveParametre(String property, String valeur) {
        validSaveParam(property, valeur);
        return saveParam(property, valeur, Boolean.FALSE);
    }

    /**
     * {@inheritDoc }.
     */
    @Override
    public Boolean saveParametre(final String property,
            final String valeur, final Boolean force) {
        validSaveParam(property, valeur);
        return saveParam(property, valeur, force);
    }

    private void validSaveParam(String property, String valeur) {
        if (property == null || property.isEmpty()
                || valeur == null || valeur.isEmpty()) {
            throw new IllegalArgumentException("Les parametres property "
                    + "et valeur ne peuvent etre vide ou null");
        }
    }

    /**
     *
     * @param property
     * @param valeur
     * @param force
     * @return
     */
    private Boolean saveParam(final String property,
            final String valeur, final Boolean force) {
        Boolean result = Boolean.FALSE;
        LOGGER.debug("Sauvegarde du parametre : {} = {}", property, valeur);
        Parametre param = dao.findByPropertyName(property);
        Boolean exist = param != null;
        if (!exist || force) {
            if (exist) {
                param.setValeur(valeur);
                dao.merge(param);
            } else {
                param = new Parametre(property, valeur);
                dao.persist(param);
            }
            dao.flush();
            result = Boolean.TRUE;
        }
        return result;
    }

    @Override
    public void deleteParametre(String property) {
        if (property == null || property.isEmpty()) {
            throw new IllegalArgumentException("La property ne "
                    + "peut etre vide ou null");
        }
        Parametre param = dao.findByPropertyName(property);
        if (param != null) {
            dao.remove(param);
        }
    }

    @Override
    public String getProperty(String property) {
        if (property == null || property.isEmpty()) {
            throw new IllegalArgumentException("La property ne "
                    + "peut etre vide ou null");
        }
        Parametre result = dao.findByPropertyName(property);
        if (result == null) {
            return null;
        } else {
            return result.getValeur();
        }
    }
}
TOP

Related Classes of com.evasion.plugin.common.ParametreManager

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.