--- /dev/null
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package com.axyus.generator.pespj.utils;
+
+import com.axyus.generator.pespj.UnExpectedException;
+import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseAlphanum2;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseAlphanum20;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseAlphanum3;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseAlphanum50;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseAnnee;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseNum14;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseNum2;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseNum20;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseTexte100;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseTexte15;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseTexte250;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseTexte32;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseTexte38;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.BaseTexte7;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.TDomaine;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.TEnTetePES;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.TEnveloppe;
+import fr.gouv.minefi.cp.helios.pes_v2.commun.TParametres;
+import fr.gouv.minefi.cp.helios.pes_v2.rev0.aller.PESAller;
+import fr.gouv.minefi.cp.helios.pes_v2.rev0.aller.TPESPJ;
+import fr.gouv.minefi.cp.helios.pes_v2.rev0.aller.TPESPJs;
+import fr.gouv.minefi.cp.helios.pes_v2.rev0.aller.TPESRefCompta;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.zip.GZIPOutputStream;
+import javax.xml.datatype.XMLGregorianCalendar;
+import org.apache.commons.codec.binary.Base64OutputStream;
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ *
+ * @author jp.tessier
+ */
+public class GeneratePesUtils {
+ private static Log logger = LogFactory.getLog(GeneratePesUtils.class);
+
+ // NODE NAMES
+ private static final String NODENAME_PESALLER = "PES_ALLER";
+ // XPATHS
+ private static final String XPATH_PES_FACTURE = "/n:PESFacture";
+ private static final String XPATH_PES_FACTURE_ID_VER = "/n:PESFacture/n:IdVer/@V";
+ private static final String XPATH_PES_FACTURE_ANNEE = "/n:PESFacture/n:Annee/@V";
+ private static final String XPATH_PES_FACTURE_NUM_SEQUENCE = "/n:PESFacture/n:NumSequence/@V";
+ private static final String XPATH_PES_FACTURE_TYPE_FACTURE = "/n:PESFacture/n:TypeFact/@V";
+ private static final String XPATH_PES_FACTURE_DATE = "/n:PESFacture/n:Date/@V";
+ private static final String XPATH_PES_FACTURE_EMETTEUR_INFO_TIERS_ID = "/n:PESFacture/n:Emetteur/n:InfoTiers/n:IdTiers/@V";
+ private static final String XPATH_PES_FACTURE_EMETTEUR_INFO_TIERS_DATE_MALADE = "/n:PESFacture/n:Emetteur/n:InfoTiers/n:DteMalade/@V";
+ private static final String XPATH_PES_FACTURE_EMETTEUR_INFO_TIERS_TYPE_TIERS = "/n:PESFacture/n:Emetteur/n:InfoTiers/n:TypTiers/@V";
+ private static final String XPATH_PES_FACTURE_EMETTEUR_INFO_TIERS_NAT_ID_TIERS = "/n:PESFacture/n:Emetteur/n:InfoTiers/n:NatIdTiers/@V";
+ private static final String XPATH_PES_FACTURE_EMETTEUR_INFO_TIERS_DATE_ID_TIERS = "/n:PESFacture/n:Emetteur/n:InfoTiers/n:DteIdTiers/@V";
+ private static final String XPATH_PES_FACTURE_EMETTEUR_INFO_TIERS_REF_TIERS = "/n:PESFacture/n:Emetteur/n:InfoTiers/n:RefTiers/@V";
+ private static final String XPATH_PES_FACTURE_EMETTEUR_INFO_TIERS_CAT_TIERS = "/n:PESFacture/n:Emetteur/n:InfoTiers/n:CatTiers/@V";
+ private static final String XPATH_PES_FACTURE_EMETTEUR_INFO_TIERS_NAT_JUR = "/n:PESFacture/n:Emetteur/n:InfoTiers/n:NatJur/@V";
+ private static final String XPATH_PES_FACTURE_EMETTEUR_INFO_TIERS_CIVILITE = "/n:PESFacture/n:Emetteur/n:InfoTiers/n:Civilite/@V";
+ private static final String XPATH_PES_FACTURE_EMETTEUR_INFO_TIERS_NOM = "/n:PESFacture/n:Emetteur/n:InfoTiers/n:Nom/@V";
+ private static final String XPATH_PES_FACTURE_EMETTEUR_INFO_TIERS_COMPL_NOM = "/n:PESFacture/n:Emetteur/n:InfoTiers/n:ComplNom/@V";
+ private static final String XPATH_PES_FACTURE_EMETTEUR_INFO_TIERS_PRENOM = "/n:PESFacture/n:Emetteur/n:InfoTiers/n:Prenom/@V";
+ private static final List<String> typeFacture = Arrays.asList(new String[]{"002", "003"});
+
+ public static void generateEntetePES(PESAller pesaller, String idPost, String idColl, String codeColl, String codeBudg, String finess, String libelleColBudg) {
+ // Creation de l'enveloppe
+ TEnveloppe enveloppe = new TEnveloppe();
+
+ TParametres params = new TParametres();
+ BaseNum2 version = new BaseNum2();
+ version.setV("1");
+ params.setVersion(version);
+
+ BaseTexte100 nomFic = new BaseTexte100();
+ nomFic.setV("");
+ params.setNomFic(nomFic);
+
+ BaseTexte32 typeFic = new BaseTexte32();
+ typeFic.setV(NODENAME_PESALLER);
+ params.setTypFic(typeFic);
+
+ enveloppe.setParametres(params);
+ pesaller.setEnveloppe(enveloppe);
+
+ // Creaton de l'entete
+ TEnTetePES entete = new TEnTetePES();
+
+ BaseTexte7 idPoste = new BaseTexte7();
+ idPoste.setV(idPost);
+ entete.setIdPost(idPoste);
+
+ BaseNum14 idCollectivite = new BaseNum14();
+ idCollectivite.setV(idColl);
+ entete.setIdColl(idCollectivite);
+
+ BaseAlphanum3 codeCollectivite = new BaseAlphanum3();
+ codeCollectivite.setV(codeColl);
+ entete.setCodCol(codeCollectivite);
+
+ BaseAlphanum2 codeBudget = new BaseAlphanum2();
+ codeBudget.setV(codeBudg);
+ entete.setCodBud(codeBudget);
+
+ BaseNum20 finessJuridique = new BaseNum20();
+ finessJuridique.setV(finess);
+ entete.setFinJur(finessJuridique);
+
+ BaseTexte38 libelleCollectiviteBudget = new BaseTexte38();
+ libelleCollectiviteBudget.setV(libelleColBudg);
+ entete.setLibelleColBud(libelleCollectiviteBudget);
+
+ //BaseDate date = new BaseDate();
+ //static XMLGregorianCalendar calendarDate = XMLGregorianCalendarImpl.createDateTime(Calendar.get(Calendar.YEAR), Calendar.get(Calendar.MONTH), Calendar.get(Calendar.DAY_OF_MONTH), Calendar.get(Calendar.HOUR_OF_DAY), Calendar.get(Calendar.MINUTE), Calendar.get(Calendar.SECOND));
+ //date.setV();
+ //entete.setDteStr(date);
+ pesaller.setEnTetePES(entete);
+ }
+
+ public static TPESPJs.PJ generatePj(String pieceJustificative, String typePjKey, String nomPj, String idUnique, String description) throws
+ UnExpectedException {
+ TPESPJs.PJ pj = new TPESPJs.PJ();
+ TPESPJ.Contenu contenu = new TPESPJ.Contenu();
+ //pj.setUrlFile(pieceJustificative);
+
+ try {
+ compressAndBase64Encoding(pieceJustificative, contenu, pj);
+ } catch (Exception ex) {
+ logger.error("Problème lors du compressAndBase64Encoding()",ex);
+ throw new UnExpectedException(ex);
+ }
+ BaseAlphanum50 idunique = new BaseAlphanum50();
+ idunique.setV(idUnique);
+ pj.setIdUnique(idunique);
+
+ BaseTexte100 nompj = new BaseTexte100();
+ nompj.setV(nomPj);
+ pj.setNomPJ(nompj);
+
+ BaseAlphanum3 typpj = new BaseAlphanum3();
+ typpj.setV(typePjKey);
+ pj.setTypePJ(typpj);
+
+ BaseTexte250 descriptionAttr = new BaseTexte250();
+ descriptionAttr.setV(description);
+ pj.setDescription(descriptionAttr);
+
+ return pj;
+ }
+
+ public static TPESRefCompta generateRefCompta(Pair domain, String exercice, Pair typepce, String numpce, String numlignepce,
+ String idtiers) throws UnExpectedException {
+ TPESRefCompta refCompta = new TPESRefCompta();
+
+ TDomaine domaine = new TDomaine();
+ domaine.setV(domain.key);
+ refCompta.setDomaine(domaine);
+
+ BaseAnnee annee = new BaseAnnee();
+ XMLGregorianCalendar anneeCalendar = new XMLGregorianCalendarImpl();
+ anneeCalendar.setYear(Integer.parseInt(exercice));
+ annee.setV(anneeCalendar);
+ refCompta.setExercice(annee);
+
+ BaseNum2 typ = new BaseNum2();
+ typ.setV(typepce.key);
+ refCompta.setTypeObjet(typ);
+
+ if(numpce != null && !numpce.isEmpty()){
+ BaseAlphanum20 id = new BaseAlphanum20();
+ id.setV(numpce);
+ refCompta.setIdentifiant(id);
+ }
+
+ if(numlignepce != null && !numlignepce.isEmpty()){
+ BaseAlphanum20 complId = new BaseAlphanum20();
+ complId.setV(numlignepce);
+ refCompta.setComplementIdentifiant(complId);
+ }
+
+ if(idtiers != null && !idtiers.isEmpty()){
+ BaseTexte15 identifiantTiers = new BaseTexte15();
+ identifiantTiers.setV(idtiers);
+ refCompta.setIdTiers(identifiantTiers);
+ }
+
+ return refCompta;
+ }
+
+ public static TPESPJs generatePesPj(PESAller pesAller, List<PJfile> pjs) {
+ TPESPJs pesPj = new TPESPJs();
+
+ TPESPJs.EnTetePESPJ entePesPj = new TPESPJs.EnTetePESPJ();
+
+ BaseNum2 idVersion = new BaseNum2();
+ idVersion.setV("1");
+ entePesPj.setIdVer(idVersion);
+ entePesPj.setIdVer(idVersion);
+
+ pesPj.setEnTetePESPJ(entePesPj);
+
+ for (PJfile pj : pjs) {
+ TPESPJs.PJ pieceJustificative = pj.getPj();
+ pesPj.getPJ().add(pieceJustificative);
+ }
+
+ return pesPj;
+ }
+
+ private static void compressAndBase64Encoding(String pieceJustificative, TPESPJ.Contenu contenu, TPESPJ pj) throws FileNotFoundException,
+ IOException {
+ TPESPJ.Contenu.Fichier fichier = new TPESPJ.Contenu.Fichier();
+
+ String extensionFichier = FilenameUtils.getExtension(pieceJustificative);
+ MimeType mime = MimeType.getMimeTypeByExtension(extensionFichier);
+ String contentType = mime==null?extensionFichier:mime.getContentType();
+ fichier.setMIMEType(contentType);
+ // Creation d'un inputStream sur la PJ
+ FileInputStream fis = new FileInputStream(new File(pieceJustificative));
+
+ // Creation du gzip et d'un outputStream
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ GZIPOutputStream gzo = new GZIPOutputStream(baos);
+ try {
+ // GZIP du flux
+ IOUtils.copy(fis, gzo);
+ gzo.flush();
+ } finally {
+ fis.close();
+ gzo.close();
+ }
+ // Jaxb se charge de l'encodage en base 64
+ fichier.setValue(baos.toByteArray());
+ contenu.setFichier(fichier);
+ pj.setContenu(contenu);
+ }
+}