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));
}
}
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));
}
}
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);
}
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;
}
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()));
}
}
Aggregations