Search in sources :

Example 1 with ByteArrayImageProvider

use of fr.opensagres.xdocreport.document.images.ByteArrayImageProvider in project esup-ecandidat by EsupPortail.

the class CandidatureController method generateLettre.

/**
 * @param  template
 * @param  data
 * @param  fichierSignature
 * @param  locale
 * @param  sendNotification
 * @return                  l'inputstram de la lettre
 */
public InputStream generateLettre(final InputStream template, final ExportLettreCandidat data, final InputStream fichierSignature, final String locale, final Boolean sendNotification) {
    // InputStream template = MethodUtils.getXDocReportTemplate(templateLettre, locale, cacheController.getLangueDefault().getCodLangue());
    if (template == null) {
        return null;
    }
    final ByteArrayInOutStream out = new ByteArrayInOutStream();
    try {
        /* Ajout date et heure */
        data.setDateHeure(formatterDateTime.format(LocalDateTime.now()));
        data.setDate(formatterDate.format(LocalDateTime.now()));
        /* Generation du template */
        final IXDocReport report = XDocReportRegistry.getRegistry().loadReport(template, TemplateEngineKind.Velocity);
        /* Variables */
        final IContext context = report.createContext();
        context.put("data", data);
        /* Pour l'image */
        final FieldsMetadata metadata = new FieldsMetadata();
        if (fichierSignature != null) {
            metadata.addFieldAsImage("imageSignature");
            final IImageProvider originalSizeLogo = new ByteArrayImageProvider(fichierSignature, true);
            context.put("imageSignature", originalSizeLogo);
        } else {
            /* Si pas de fichier, on supprime l'image */
            metadata.addFieldAsImage("imageSignature", NullImageBehaviour.RemoveImageTemplate);
        }
        report.setFieldsMetadata(metadata);
        /* On vérifie si on affiche le tableau de signataire */
        if (data.getLibelleSignature().equals("") && fichierSignature == null) {
            context.put("showSignataire", false);
        } else {
            context.put("showSignataire", true);
        }
        /* Transformation en pdf */
        final Options options = Options.getTo(ConverterTypeTo.PDF).via(ConverterTypeVia.XWPF);
        report.convert(context, options, out);
        return pdfManager.cryptAndSignPdf(out, new Locale(locale != null ? locale : "fr"));
    } catch (final Exception e) {
        // probleme de taille de signature XDocConverterException + StackOverflowError
        if (e.getClass() != null && e instanceof XDocConverterException && e.getCause() != null && e.getCause() instanceof StackOverflowError) {
            if (sendNotification) {
                Notification.show(applicationContext.getMessage("candidature.lettre.download.sign.error", null, UI.getCurrent().getLocale()), Type.WARNING_MESSAGE);
            }
        } else {
            if (sendNotification) {
                Notification.show(applicationContext.getMessage("candidature.lettre.download.error", null, UI.getCurrent().getLocale()), Type.WARNING_MESSAGE);
            }
            logger.error("erreur a la création de la lettre", e);
        }
        return null;
    } finally {
        // fermeture des fichiers
        MethodUtils.closeRessource(fichierSignature);
        MethodUtils.closeRessource(template);
        MethodUtils.closeRessource(out);
    }
}
Also used : Locale(java.util.Locale) Options(fr.opensagres.xdocreport.converter.Options) ByteArrayInOutStream(fr.univlorraine.ecandidat.utils.ByteArrayInOutStream) IXDocReport(fr.opensagres.xdocreport.document.IXDocReport) FieldsMetadata(fr.opensagres.xdocreport.template.formatter.FieldsMetadata) IImageProvider(fr.opensagres.xdocreport.document.images.IImageProvider) IContext(fr.opensagres.xdocreport.template.IContext) XDocConverterException(fr.opensagres.xdocreport.converter.XDocConverterException) ByteArrayImageProvider(fr.opensagres.xdocreport.document.images.ByteArrayImageProvider) IOException(java.io.IOException) XDocConverterException(fr.opensagres.xdocreport.converter.XDocConverterException) XDocReportException(fr.opensagres.xdocreport.core.XDocReportException)

Aggregations

Options (fr.opensagres.xdocreport.converter.Options)1 XDocConverterException (fr.opensagres.xdocreport.converter.XDocConverterException)1 XDocReportException (fr.opensagres.xdocreport.core.XDocReportException)1 IXDocReport (fr.opensagres.xdocreport.document.IXDocReport)1 ByteArrayImageProvider (fr.opensagres.xdocreport.document.images.ByteArrayImageProvider)1 IImageProvider (fr.opensagres.xdocreport.document.images.IImageProvider)1 IContext (fr.opensagres.xdocreport.template.IContext)1 FieldsMetadata (fr.opensagres.xdocreport.template.formatter.FieldsMetadata)1 ByteArrayInOutStream (fr.univlorraine.ecandidat.utils.ByteArrayInOutStream)1 IOException (java.io.IOException)1 Locale (java.util.Locale)1