nc.noumea.mairie.organigramme.services.exportGraphML.impl.AbstractExportGraphMLService.java Source code

Java tutorial

Introduction

Here is the source code for nc.noumea.mairie.organigramme.services.exportGraphML.impl.AbstractExportGraphMLService.java

Source

package nc.noumea.mairie.organigramme.services.exportGraphML.impl;

/*
 * #%L
 * Logiciel de Gestion des Organigrammes de la Ville de Nouma
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2015 Mairie de Nouma
 * %%
 * 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.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import nc.noumea.mairie.organigramme.core.ws.SirhWSConsumer;
import nc.noumea.mairie.organigramme.dto.EntiteDto;
import nc.noumea.mairie.organigramme.dto.FichePosteDto;
import nc.noumea.mairie.organigramme.dto.FichePosteTreeNodeDto;
import nc.noumea.mairie.organigramme.enums.FiltreStatut;
import nc.noumea.mairie.organigramme.enums.StatutFichePoste;
import nc.noumea.mairie.organigramme.services.ExportGraphMLService;
import nc.noumea.mairie.organigramme.utils.ComparatorUtil;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class AbstractExportGraphMLService implements ExportGraphMLService {

    @Autowired
    SirhWSConsumer sirhWSConsumer;

    @Override
    public void exportGraphMLFromEntite(EntiteDto entiteDto, FiltreStatut filtreStatut,
            Map<String, Boolean> mapIdLiOuvert) throws IOException {
        return;
    }

    @Override
    public void exportGraphMLFromFichesPoste(FichePosteTreeNodeDto node, boolean isAfficheAgent,
            Map<String, Boolean> mapIdLiOuvert, EntiteDto entiteDto) throws IOException {
        return;
    }

    /**
     * Initialise l entete du graphML
     * 
     * @param root
     *            Element
     */
    protected void initHeader(Element root) {

        root.addAttribute("xmlns", "http://graphml.graphdrawing.org/xmlns");
        root.addAttribute("xsi:schemaLocation",
                "http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd");
        root.add(new Namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));
        root.add(new Namespace("y", "http://www.yworks.com/xml/graphml"));
        root.addElement("key").addAttribute("yfiles.type", "nodegraphics").addAttribute("for", "node")
                .addAttribute("id", "d6");
        root.addElement("key").addAttribute("yfiles.type", "edgegraphics").addAttribute("for", "edge")
                .addAttribute("id", "d7");
        root.addElement("key").addAttribute("yfiles.type", "resources").addAttribute("for", "graphml")
                .addAttribute("id", "d13");
    }

    protected Element initRoot(Element root) {

        Element graph = root.addElement("graph");
        graph.addAttribute("id", "1");
        graph.addAttribute("edgedefault", "undirected");

        return graph;
    }

    /**
     * Permet d'ajouter le logo de la mairie au fichier d'export
     * 
     * @param graph
     *            : l'lment graph
     * @throws IOException
     *             : si le logo ne peux pas tre lu ou ajout au fichier
     */
    protected void ajoutLogoMairie(Element graph) throws IOException {
        Element elKeyResource = graph.getParent().addElement("data").addAttribute("key", "d13");
        Element elResource = elKeyResource.addElement("y:Resources").addElement("y:Resource");
        elResource.addAttribute("id", "1").addAttribute("type", "java.awt.image.BufferedImage");
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("logo-mairie.jpg");
        BufferedImage img = ImageIO.read(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img, "jpg", baos);
        baos.flush();
        String encodedImage = Base64.encodeBase64String(baos.toByteArray());
        baos.close();
        elResource.setText(encodedImage);

        Element el = graph.addElement("node").addAttribute("id", "0");
        Element elD6 = el.addElement("data").addAttribute("key", "d6");
        Element elImageNode = elD6.addElement("y:ImageNode");
        elImageNode.addElement("y:Geometry").addAttribute("height", "136.3").addAttribute("width", "143.0")
                .addAttribute("x", "256.0").addAttribute("y", "-291.0");
        elImageNode.addElement("y:Image").addAttribute("alphaImage", "true").addAttribute("refid", "1");
    }

    /**
     * Ajouter un tableau rcapitulatif du nombre de FDP par catgorie
     * 
     * @param graph
     *            : l'lment graph
     * @param entiteDto
     *            : l'entit exporte
     */
    protected void construitTableauStats(Element graph, EntiteDto entiteDto) {

        String listIdStatutFDP = StatutFichePoste.getListIdStatutActif();
        Map<String, Double> mapCategorieNombreETP = new LinkedHashMap<String, Double>();

        List<FichePosteDto> listeFichePosteDto = sirhWSConsumer.getFichePosteByIdEntite(entiteDto.getIdEntite(),
                listIdStatutFDP, true);
        Collections.sort(listeFichePosteDto, new ComparatorUtil.FichePosteCategorieComparator());

        int tailleLibelleMax = 0;
        for (FichePosteDto fichePosteDto : listeFichePosteDto) {
            Double nombreFdpCategorie = mapCategorieNombreETP.get(fichePosteDto.getLibelleGradeCategorie());
            // On ne met dans les stats que les rglementaires
            if (StringUtils.isNotBlank(fichePosteDto.getReglementaire())
                    && !fichePosteDto.getReglementaire().toLowerCase().equals("non")) {
                if (nombreFdpCategorie == null) {
                    nombreFdpCategorie = fichePosteDto.getTauxETP();
                } else {
                    nombreFdpCategorie += fichePosteDto.getTauxETP();
                }

                String libelleGradeCategorie = fichePosteDto.getLibelleGradeCategorie();
                if (libelleGradeCategorie.length() > tailleLibelleMax) {
                    tailleLibelleMax = libelleGradeCategorie.length();
                }

                mapCategorieNombreETP.put(libelleGradeCategorie, nombreFdpCategorie);
            }
        }

        List<String> listeResultat = new ArrayList<String>();
        Double total = new Double(0);

        for (Map.Entry<String, Double> entry : mapCategorieNombreETP.entrySet()) {
            total += entry.getValue();
        }

        DecimalFormat df = new DecimalFormat("#.##");
        int longueurChiffreTotal = df.format(total).toString().length();

        for (Map.Entry<String, Double> entry : mapCategorieNombreETP.entrySet()) {
            int longueurChiffreEntry = df.format(entry.getValue()).toString().length();
            int nombrePointAjoutFin = tailleLibelleMax + (longueurChiffreTotal - longueurChiffreEntry) + 1;
            listeResultat.add(StringUtils.capitalize(StringUtils.rightPad(entry.getKey(), nombrePointAjoutFin, "."))
                    + df.format(entry.getValue()).toString());
        }

        listeResultat.add("");
        listeResultat.add(StringUtils.rightPad("Total", tailleLibelleMax + 1, ".") + df.format(total));

        String height = listeResultat.size() == 0 ? "20" : String.valueOf(listeResultat.size() * 20);
        String width = tailleLibelleMax == 0 ? "50" : String.valueOf(tailleLibelleMax * 10);

        Element elNodeCategorie = graph.addElement("node").addAttribute("id", "-1");
        Element elD6Categorie = elNodeCategorie.addElement("data").addAttribute("key", "d6");
        Element elGenericNodeCategorie = elD6Categorie.addElement("y:ShapeNode");
        elGenericNodeCategorie.addElement("y:Geometry").addAttribute("height", height).addAttribute("width", width);
        elGenericNodeCategorie.addElement("y:NodeLabel").addAttribute("alignment", "left")
                .addAttribute("fontFamily", "Courier New").setText(StringUtils.join(listeResultat, "\n"));
    }
}