nc.noumea.mairie.appock.core.services.impl.BootstrapServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for nc.noumea.mairie.appock.core.services.impl.BootstrapServiceImpl.java

Source

package nc.noumea.mairie.appock.core.services.impl;

/*-
 * #%L
 * Logiciel de Gestion des approvisionnements et des stocks des fournitures administratives de la Mairie de Nouma
 * %%
 * Copyright (C) 2017 Mairie de Nouma, Nouvelle-Caldonie
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.io.File;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import nc.noumea.mairie.appock.core.services.BootstrapService;
import nc.noumea.mairie.appock.enums.*;
import nc.noumea.mairie.appock.services.ConfigService;

/**
 * Vrification que l'environnement de dmarrage est correct sur certains critre : tables enum_ bien peuples, rpertoire des pices jointes prsent, etc.
 */
@Service("bootstrapService")
@Scope(value = "singleton", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class BootstrapServiceImpl implements BootstrapService {

    private static Logger log = LoggerFactory.getLogger(BootstrapServiceImpl.class);

    @PersistenceContext
    private EntityManager em;

    @Autowired
    ConfigService configService;

    @PostConstruct
    public void initialize() {
        checkRepertoirePieceJointeExiste();
        checkConcordanceEnumBase();
    }

    /**
     * Permet de vrifier que le repertoire des pices jointes existe bien
     */
    void checkRepertoirePieceJointeExiste() {
        File pieceJointeDir = new File(configService.getPieceJointeDir());
        if (!pieceJointeDir.exists() || !pieceJointeDir.isDirectory()) {
            throw new RuntimeException(
                    "Le rpertoire des pices jointes : " + configService.getPieceJointeDir() + " n'existe pas.");
        }
    }

    /**
     * Permet de contrler ques les tables qui contiennent les values d'ENUM sont cohrentes avec ENUM Java
     */
    void checkConcordanceEnumBase() {

        log.info("checkConcordanceEnumBase");

        // @formatter:off
        checkConcordanceEnum(EtatArticleDemande.class, "enum_etat_article_demande");
        checkConcordanceEnum(EtatCatalogue.class, "enum_etat_catalogue");
        checkConcordanceEnum(EtatCommande.class, "enum_etat_commande");
        checkConcordanceEnum(EtatCommandeService.class, "enum_etat_commande_service");
        checkConcordanceEnum(EtatDemande.class, "enum_etat_demande");
        checkConcordanceEnum(Role.class, "enum_role");
        checkConcordanceEnum(TypeConfig.class, "enum_type_config");
        checkConcordanceEnum(TypeMouvementStock.class, "enum_type_mouvement_stock");
        // @formatter:on
    }

    @SuppressWarnings("unchecked")
    void checkConcordanceEnum(Class<?> classeEnum, String nomTable) {
        String requete = "SELECT name FROM " + nomTable + " ORDER BY ID";

        List<String> listeName = em.createNativeQuery(requete).getResultList();
        if (listeName.size() != ((Class<Enum<?>>) classeEnum).getEnumConstants().length) {
            throw new RuntimeException("L'enumration " + classeEnum.getSimpleName()
                    + " ne contient pas le mme nombre d'lments que dans la table " + nomTable);
        }

        Object[] enumConstants = classeEnum.getEnumConstants();
        for (int i = 0; i < listeName.size(); i++) {
            String name = listeName.get(i);
            try {
                @SuppressWarnings("rawtypes")
                Enum enumValue = Enum.valueOf((Class<Enum>) classeEnum, name);
                if (enumValue != enumConstants[i]) {
                    throw new RuntimeException("L'enumration " + classeEnum.getSimpleName() + "  la position "
                            + i + " ne vaut pas la valeur attendue " + name);
                }
            } catch (IllegalArgumentException e) {
                throw new RuntimeException("L'enumration " + classeEnum.getSimpleName()
                        + " ne contient pas la valeur '" + name + "' prsente dans la table " + nomTable);
            }
        }
    }
}