Skip to content

Commit 3299611

Browse files
Merge pull request #13874 from SORMAS-Foundation/feature-13619-word-document-custom-date-fields-inclusion
Feature 13619 word document custom date fields inclusion
2 parents 8fac782 + d05d5ab commit 3299611

3 files changed

Lines changed: 72 additions & 20 deletions

File tree

sormas-api/src/main/java/de/symeda/sormas/api/docgeneneration/DocumentWorkflow.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static de.symeda.sormas.api.docgeneneration.RootEntityType.ROOT_EVENT_ACTIONS;
2222
import static de.symeda.sormas.api.docgeneneration.RootEntityType.ROOT_EVENT_PARTICIPANT;
2323
import static de.symeda.sormas.api.docgeneneration.RootEntityType.ROOT_EVENT_PARTICIPANTS;
24+
import static de.symeda.sormas.api.docgeneneration.RootEntityType.ROOT_GENERAL;
2425
import static de.symeda.sormas.api.docgeneneration.RootEntityType.ROOT_PATHOGEN_TEST;
2526
import static de.symeda.sormas.api.docgeneneration.RootEntityType.ROOT_PERSON;
2627
import static de.symeda.sormas.api.docgeneneration.RootEntityType.ROOT_SAMPLE;
@@ -54,7 +55,8 @@ public enum DocumentWorkflow {
5455
ROOT_USER,
5556
ROOT_SAMPLE,
5657
ROOT_PATHOGEN_TEST,
57-
ROOT_VACCINATION),
58+
ROOT_VACCINATION,
59+
ROOT_GENERAL),
5860
QUARANTINE_ORDER_CONTACT(DocumentWorkflowType.DOCUMENT,
5961
"quarantineContact",
6062
DOCX,
@@ -64,7 +66,8 @@ public enum DocumentWorkflow {
6466
ROOT_USER,
6567
ROOT_SAMPLE,
6668
ROOT_PATHOGEN_TEST,
67-
ROOT_VACCINATION),
69+
ROOT_VACCINATION,
70+
ROOT_GENERAL),
6871
QUARANTINE_ORDER_EVENT_PARTICIPANT(DocumentWorkflowType.DOCUMENT,
6972
"quarantineEventParticipant",
7073
DOCX,
@@ -74,14 +77,16 @@ public enum DocumentWorkflow {
7477
ROOT_USER,
7578
ROOT_SAMPLE,
7679
ROOT_PATHOGEN_TEST,
77-
ROOT_VACCINATION),
80+
ROOT_VACCINATION,
81+
ROOT_GENERAL),
7882
QUARANTINE_ORDER_TRAVEL_ENTRY(DocumentWorkflowType.DOCUMENT,
7983
"quarantineTravelEntry",
8084
DOCX,
8185
UserRight.DOCUMENT_TEMPLATE_MANAGEMENT,
8286
ROOT_TRAVEL_ENTRY,
8387
ROOT_PERSON,
84-
ROOT_USER),
88+
ROOT_USER,
89+
ROOT_GENERAL),
8590
EVENT_HANDOUT(DocumentWorkflowType.DOCUMENT,
8691
"eventHandout",
8792
HTML,
@@ -127,22 +132,15 @@ public enum DocumentWorkflow {
127132
ROOT_TRAVEL_ENTRY,
128133
ROOT_PERSON,
129134
ROOT_USER),
130-
SURVEY_DOCUMENT(DocumentWorkflowType.DOCUMENT,
131-
"survey",
132-
DOCX,
133-
UserRight.SURVEY_EDIT,
134-
ROOT_CASE,
135-
ROOT_PERSON,
136-
ROOT_USER,
137-
ROOT_SAMPLE),
135+
SURVEY_DOCUMENT(DocumentWorkflowType.DOCUMENT, "survey", DOCX, UserRight.SURVEY_EDIT, ROOT_CASE, ROOT_PERSON, ROOT_USER, ROOT_SAMPLE),
138136
SURVEY_EMAIL(DocumentWorkflowType.EMAIL,
139-
Constants.EMAIL_TEMPLATES_FOLDER + "/surveys",
140-
TXT,
141-
UserRight.SURVEY_EDIT,
142-
ROOT_CASE,
143-
ROOT_PERSON,
144-
ROOT_USER,
145-
ROOT_SAMPLE),;
137+
Constants.EMAIL_TEMPLATES_FOLDER + "/surveys",
138+
TXT,
139+
UserRight.SURVEY_EDIT,
140+
ROOT_CASE,
141+
ROOT_PERSON,
142+
ROOT_USER,
143+
ROOT_SAMPLE),;
146144

147145
private final DocumentWorkflowType type;
148146
private final String templateDirectory;

sormas-api/src/main/java/de/symeda/sormas/api/docgeneneration/RootEntityType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public enum RootEntityType {
2828
ROOT_EVENT_ACTIONS("eventActions"),
2929
ROOT_EVENT_PARTICIPANTS("eventParticipants"),
3030
ROOT_TRAVEL_ENTRY("travelEntry"),
31-
ROOT_VACCINATION("vaccination");
31+
ROOT_VACCINATION("vaccination"),
32+
ROOT_GENERAL("general");
3233

3334
private final String entityName;
3435

sormas-backend/src/main/java/de/symeda/sormas/backend/docgeneration/DocumentTemplateFacadeEjb.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.nio.charset.StandardCharsets;
22+
import java.time.LocalDate;
23+
import java.time.format.DateTimeFormatter;
24+
import java.time.format.FormatStyle;
2225
import java.util.List;
26+
import java.util.Optional;
2327
import java.util.Properties;
2428
import java.util.Set;
2529
import java.util.regex.Matcher;
@@ -229,6 +233,12 @@ private Properties prepareProperties(
229233
continue;
230234
}
231235

236+
// check if the entity is from a general.
237+
// General entities are used for common and dynamic data that can be referenced across different entities.
238+
if (rootEntityType == RootEntityType.ROOT_GENERAL) {
239+
fillGeneralValues(documentVariables, properties, propertyKey);
240+
}
241+
232242
Object entity = entities.getEntity(rootEntityType);
233243
if (entity instanceof HasUuid) {
234244
if (documentWorkflow.isDocx() || propertyKey.contains(propertySeparator)) {
@@ -284,6 +294,49 @@ private Properties prepareProperties(
284294
return properties;
285295
}
286296

297+
/**
298+
* Fills general values into the properties based on the provided document variables and properties.
299+
*
300+
* @param documentVariables
301+
* The document variables to use for filling general values.
302+
* @param properties
303+
* The properties to fill with general values.
304+
* @param propertyKey
305+
* The property key to use for general value retrieval.
306+
*/
307+
private void fillGeneralValues(DocumentVariables documentVariables, Properties properties, String propertyKey) {
308+
// finding the general property key. Based on the type, formatStyle is deciding.
309+
// general properties are allowed only doc-formatted files.
310+
Optional<String> generalPropertyOpt = documentVariables.getVariables()
311+
.stream()
312+
.filter(e -> e.startsWith(RootEntityType.ROOT_GENERAL.getEntityName() + "."))
313+
.filter(e -> e.equals(propertyKey))
314+
.findAny();
315+
316+
if (generalPropertyOpt.isPresent()) {
317+
String generalProperty = generalPropertyOpt.get();
318+
String dateType = generalProperty.substring(generalProperty.lastIndexOf('.') + 1);
319+
FormatStyle formatStyle;
320+
switch (dateType) {
321+
case "long":
322+
formatStyle = FormatStyle.LONG;
323+
break;
324+
case "full":
325+
formatStyle = FormatStyle.FULL;
326+
break;
327+
case "medium":
328+
formatStyle = FormatStyle.MEDIUM;
329+
break;
330+
case "short":
331+
default:
332+
formatStyle = FormatStyle.SHORT;
333+
}
334+
String propertyValue =
335+
LocalDate.now().format(DateTimeFormatter.ofLocalizedDate(formatStyle).withLocale(I18nProperties.getUserLanguage().getLocale()));
336+
properties.setProperty(generalProperty, propertyValue);
337+
}
338+
}
339+
287340
private byte[] generateDocumentDocx(File templateFile, Properties properties) throws DocumentTemplateException {
288341
return templateEngine.generateDocumentDocx(properties, templateFile);
289342
}

0 commit comments

Comments
 (0)