Search in sources :

Example 1 with DocumentTemplateException

use of de.symeda.sormas.api.docgeneneration.DocumentTemplateException in project SORMAS-Project by hzi-braunschweig.

the class DocumentTemplateEntitiesBuilder method getQuarantineOrderEntities.

public DocumentTemplateEntities getQuarantineOrderEntities(DocumentWorkflow workflow, ReferenceDto rootEntityRef, SampleReferenceDto sampleRef, PathogenTestReferenceDto pathogenTestRef, VaccinationReferenceDto vaccinationRef) throws DocumentTemplateException {
    SampleDto sample = null;
    if (sampleRef != null) {
        sample = sampleFacadeEjb.getSampleByUuid(sampleRef.getUuid());
    }
    PathogenTestDto pathogenTest = null;
    if (pathogenTestRef != null) {
        pathogenTest = pathogenTestFacade.getByUuid(pathogenTestRef.getUuid());
    }
    VaccinationDto vaccination = null;
    if (vaccinationRef != null) {
        vaccination = vaccinationFacade.getByUuid(vaccinationRef.getUuid());
    }
    String rootEntityUuid = rootEntityRef.getUuid();
    switch(workflow) {
        case QUARANTINE_ORDER_CASE:
            CaseDataDto caseDataDto = caseFacade.getCaseDataByUuid(rootEntityUuid);
            return buildEntities(RootEntityType.ROOT_CASE, caseDataDto, caseDataDto.getPerson(), sample, pathogenTest, vaccination);
        case QUARANTINE_ORDER_CONTACT:
            ContactDto contactDto = contactFacade.getByUuid(rootEntityUuid);
            return buildEntities(ROOT_CONTACT, contactDto, contactDto.getPerson(), sample, pathogenTest, vaccination);
        case QUARANTINE_ORDER_EVENT_PARTICIPANT:
            EventParticipantDto eventParticipantDto = eventParticipantFacade.getByUuid(rootEntityUuid);
            return buildEntities(ROOT_EVENT_PARTICIPANT, eventParticipantDto, eventParticipantDto.getPerson().toReference(), sample, pathogenTest, vaccination);
        case QUARANTINE_ORDER_TRAVEL_ENTRY:
            TravelEntryDto travelEntryDto = travelEntryFacade.getByUuid(rootEntityUuid);
            return buildEntities(ROOT_TRAVEL_ENTRY, travelEntryDto, travelEntryDto.getPerson(), null, null, null);
        default:
            throw new DocumentTemplateException(I18nProperties.getString(Strings.errorQuarantineOnlySupportedEntities));
    }
}
Also used : DocumentTemplateException(de.symeda.sormas.api.docgeneneration.DocumentTemplateException) CaseDataDto(de.symeda.sormas.api.caze.CaseDataDto) TravelEntryDto(de.symeda.sormas.api.travelentry.TravelEntryDto) ContactDto(de.symeda.sormas.api.contact.ContactDto) VaccinationDto(de.symeda.sormas.api.vaccination.VaccinationDto) EventParticipantDto(de.symeda.sormas.api.event.EventParticipantDto) PathogenTestDto(de.symeda.sormas.api.sample.PathogenTestDto) SampleDto(de.symeda.sormas.api.sample.SampleDto)

Example 2 with DocumentTemplateException

use of de.symeda.sormas.api.docgeneneration.DocumentTemplateException in project SORMAS-Project by hzi-braunschweig.

the class DocumentTemplateFacadeEjb method writeDocumentTemplate.

@Override
public void writeDocumentTemplate(DocumentWorkflow documentWorkflow, String templateName, byte[] document) throws DocumentTemplateException {
    if (!documentWorkflow.getFileExtension().equalsIgnoreCase(FilenameUtils.getExtension(templateName))) {
        throw new DocumentTemplateException(I18nProperties.getString(Strings.headingWrongFileType));
    }
    String path = FilenameUtils.getPath(templateName);
    if (StringUtils.isNotBlank(path)) {
        throw new DocumentTemplateException(String.format(I18nProperties.getString(Strings.errorIllegalFilename), templateName));
    }
    ByteArrayInputStream templateInputStream = new ByteArrayInputStream(document);
    if (documentWorkflow.isDocx()) {
        templateEngine.validateTemplateDocx(templateInputStream);
    } else {
        templateEngine.validateTemplateTxt(templateInputStream);
    }
    Path workflowTemplateDirPath = getWorkflowTemplateDirPath(documentWorkflow);
    try {
        Files.createDirectories(workflowTemplateDirPath);
    } catch (IOException e) {
        throw new DocumentTemplateException(I18nProperties.getString(Strings.errorCreatingTemplateDirectory));
    }
    try (FileOutputStream fileOutputStream = new FileOutputStream(new File(workflowTemplateDirPath.resolve(FilenameUtils.getName(templateName)).toUri()))) {
        fileOutputStream.write(document);
    } catch (IOException e) {
        throw new DocumentTemplateException(I18nProperties.getString(Strings.errorWritingTemplate));
    }
}
Also used : Path(java.nio.file.Path) DocumentTemplateException(de.symeda.sormas.api.docgeneneration.DocumentTemplateException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 3 with DocumentTemplateException

use of de.symeda.sormas.api.docgeneneration.DocumentTemplateException in project SORMAS-Project by hzi-braunschweig.

the class DocumentTemplateFacadeEjb method generateDocumentTxtFromEntities.

@Override
public String generateDocumentTxtFromEntities(DocumentWorkflow documentWorkflow, String templateName, DocumentTemplateEntities entities, Properties extraProperties) throws DocumentTemplateException {
    if (documentWorkflow.isDocx()) {
        throw new DocumentTemplateException(String.format(I18nProperties.getString(Strings.messageWrongTemplateFileType), documentWorkflow, documentWorkflow.getFileExtension()));
    }
    // 1. Read template from custom directory
    File templateFile = getTemplateFile(documentWorkflow, templateName);
    // 2. Extract document variables
    DocumentVariables documentVariables = getTemplateVariablesTxt(templateFile);
    // 3. prepare properties
    Properties properties = prepareProperties(documentWorkflow, entities, extraProperties, documentVariables);
    // 4. generate document
    return generateDocumentTxt(templateFile, properties);
}
Also used : DocumentTemplateException(de.symeda.sormas.api.docgeneneration.DocumentTemplateException) DocumentVariables(de.symeda.sormas.api.docgeneneration.DocumentVariables) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) Properties(java.util.Properties) File(java.io.File)

Example 4 with DocumentTemplateException

use of de.symeda.sormas.api.docgeneneration.DocumentTemplateException in project SORMAS-Project by hzi-braunschweig.

the class EventDocumentFacadeEjb method getGeneratedDocument.

@Override
public String getGeneratedDocument(String templateName, EventReferenceDto eventReference, Properties extraProperties, Boolean shouldUploadGeneratedDoc) throws DocumentTemplateException {
    DocumentTemplateEntities entities = new DocumentTemplateEntities();
    entities.addEntity(RootEntityType.ROOT_EVENT, eventReference);
    ActionCriteria actionCriteria = new ActionCriteria().event(eventReference);
    entities.addEntity(RootEntityType.ROOT_EVENT_ACTIONS, actionFacade.getActionList(actionCriteria, null, null));
    entities.addEntity(RootEntityType.ROOT_EVENT_PARTICIPANTS, eventParticipantFacade.getAllActiveEventParticipantsByEvent(eventReference.getUuid()));
    String body = documentTemplateFacade.generateDocumentTxtFromEntities(DOCUMENT_WORKFLOW, templateName, entities, extraProperties);
    String styledHtml = createStyledHtml(templateName, body);
    if (shouldUploadGeneratedDoc) {
        // mandatory UTF_8
        byte[] documentToSave = styledHtml.getBytes(StandardCharsets.UTF_8);
        try {
            helper.saveDocument(helper.getDocumentFileName(eventReference, templateName), // default type will be applied: "application/octet-stream" for /*"text/html"*/ it will work as well in the same way.
            null, documentToSave.length, helper.getDocumentRelatedEntityType(eventReference), eventReference.getUuid(), documentToSave);
        } catch (Exception e) {
            throw new DocumentTemplateException(I18nProperties.getString(Strings.errorProcessingTemplate));
        }
    }
    return styledHtml;
}
Also used : DocumentTemplateException(de.symeda.sormas.api.docgeneneration.DocumentTemplateException) DocumentTemplateEntities(de.symeda.sormas.api.docgeneneration.DocumentTemplateEntities) ActionCriteria(de.symeda.sormas.api.action.ActionCriteria) DocumentTemplateException(de.symeda.sormas.api.docgeneneration.DocumentTemplateException) IOException(java.io.IOException)

Example 5 with DocumentTemplateException

use of de.symeda.sormas.api.docgeneneration.DocumentTemplateException in project SORMAS-Project by hzi-braunschweig.

the class TemplateEngine method generateDocumentDocx.

public byte[] generateDocumentDocx(Properties properties, File templateFile) throws DocumentTemplateException {
    try {
        FileInputStream templateInputStream = new FileInputStream(templateFile);
        IXDocReport report = readXDocReport(templateInputStream);
        IContext context = report.createContext();
        for (Object key : properties.keySet()) {
            if (key instanceof String) {
                Object property = properties.get(key);
                // Sanitize property to avoid XML parsing errors
                if (property instanceof Enum<?> && property.toString() != null) {
                    property = returnSanitizedString(property.toString());
                } else if (property instanceof String) {
                    property = returnSanitizedString((String) property);
                }
                if (property != null && !(property instanceof String && ((String) property).isEmpty())) {
                    context.put((String) key, property);
                }
            }
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        report.process(context, outputStream);
        return outputStream.toByteArray();
    } catch (IOException | XDocReportException | VelocityException e) {
        throw new DocumentTemplateException(String.format(I18nProperties.getString(Strings.errorDocumentGeneration), templateFile.getName()));
    }
}
Also used : IXDocReport(fr.opensagres.xdocreport.document.IXDocReport) IContext(fr.opensagres.xdocreport.template.IContext) DocumentTemplateException(de.symeda.sormas.api.docgeneneration.DocumentTemplateException) VelocityException(org.apache.velocity.exception.VelocityException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XDocReportException(fr.opensagres.xdocreport.core.XDocReportException)

Aggregations

DocumentTemplateException (de.symeda.sormas.api.docgeneneration.DocumentTemplateException)14 IOException (java.io.IOException)9 XDocReportException (fr.opensagres.xdocreport.core.XDocReportException)4 FieldExtractor (fr.opensagres.xdocreport.template.FieldExtractor)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 File (java.io.File)4 DocumentVariables (de.symeda.sormas.api.docgeneneration.DocumentVariables)3 I18nProperties (de.symeda.sormas.api.i18n.I18nProperties)3 Properties (java.util.Properties)3 DocumentTemplateEntities (de.symeda.sormas.api.docgeneneration.DocumentTemplateEntities)2 IXDocReport (fr.opensagres.xdocreport.document.IXDocReport)2 FileInputStream (java.io.FileInputStream)2 FileDownloader (com.vaadin.server.FileDownloader)1 StreamResource (com.vaadin.server.StreamResource)1 Button (com.vaadin.ui.Button)1 Notification (com.vaadin.ui.Notification)1 EntityDtoAccessHelper (de.symeda.sormas.api.EntityDtoAccessHelper)1 HasUuid (de.symeda.sormas.api.HasUuid)1 ReferenceDto (de.symeda.sormas.api.ReferenceDto)1 ActionCriteria (de.symeda.sormas.api.action.ActionCriteria)1