Skip to content

Commit

Permalink
micromata#42: Date-Format in generated doc is ISO (Firefox)
Browse files Browse the repository at this point in the history
  • Loading branch information
kreinhard committed Oct 31, 2018
1 parent 18c8457 commit 669b722
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import de.reinhard.merlin.app.json.JsonUtils;
import de.reinhard.merlin.app.storage.Storage;
import de.reinhard.merlin.app.user.UserUtils;
import de.reinhard.merlin.logging.MDCHandler;
import de.reinhard.merlin.logging.MDCKey;
import de.reinhard.merlin.persistency.PersistencyRegistry;
import de.reinhard.merlin.utils.MerlinFileUtils;
import de.reinhard.merlin.word.WordDocument;
import de.reinhard.merlin.word.templating.Template;
import de.reinhard.merlin.word.templating.TemplateDefinition;
import de.reinhard.merlin.word.templating.TemplateRunContext;
import de.reinhard.merlin.word.templating.WordTemplateRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -18,6 +20,7 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Map;

@Path("/templates")
public class TemplateRunnerRest {
Expand Down Expand Up @@ -63,7 +66,10 @@ public Response runTemplate(String json) {
try {
WordDocument doc = WordDocument.load(path);
WordTemplateRunner runner = new WordTemplateRunner(templateDefinition, doc);
WordDocument result = runner.run(data.getVariables());
TemplateRunContext context = new TemplateRunContext();
context.setLocale(UserUtils.getUserLocale());
Map<String, Object> variables = context.convertVariables(data.getVariables(), templateDefinition);
WordDocument result = runner.run(variables);
String filename = runner.createFilename(path.getFileName().toString(), data.getVariables());
byte[] byteArray = result.getAsByteArrayOutputStream().toByteArray();
Response.ResponseBuilder builder = Response.ok(byteArray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
* For defining formats, such as number formats, date formats etc.
Expand All @@ -26,6 +28,8 @@ public class TemplateRunContext {

DateFormat dateFormatter;
DateFormat[] dateFormatters;
DateFormat dateFormatterGerman;

Locale locale;
private String excelDateFormatPattern;
private I18n i18n;
Expand All @@ -34,18 +38,48 @@ public TemplateRunContext() {
i18n = CoreI18n.getDefault();
this.locale = Locale.getDefault();
dateFormatter = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
dateFormatterGerman = new SimpleDateFormat("dd.MM.yyyy");
dateFormatters = new DateFormat[]{
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("d/M/yyyy"),
new SimpleDateFormat("d/M/yy"),
new SimpleDateFormat("dd.MM.yyyy"),
dateFormatterGerman,
new SimpleDateFormat("dd.MM.yy"),
new SimpleDateFormat("d.M.yyyy"),
new SimpleDateFormat("d.M.yy")
};
excelDateFormatPattern = DateFormatConverter.convert(locale, DEFAULT_DATE_PATTERN);
}

/**
* Convert the values matching the user's locale.
*
* @param variables
* @param templateDefinition For getting definitions of variables. If not given, the parameter variables is returen unchanged.
* @return
*/
public Map<String, Object> convertVariables(Map<String, Object> variables, TemplateDefinition templateDefinition) {
if (templateDefinition == null) {
return variables;
}
Map<String, Object> result = new HashMap<>();
for (Map.Entry<String, Object> entry : variables.entrySet()) {
String varname = entry.getKey();
VariableDefinition variableDefinition = templateDefinition.getVariableDefinition(varname);
Object value = entry.getValue();
if (variableDefinition != null) {
if (variableDefinition.getType() == VariableType.DATE) {
Date date = parseDate(value);
if (date != null) {
value = dateToString(date);
}
}
}
result.put(entry.getKey(), value);
}
return result;
}

/**
* Default is "dd/MM/yyyy".
*
Expand Down Expand Up @@ -167,6 +201,15 @@ private Date parseDate(DateFormat dateFormatter, String val) {
return null;
}

public String dateToString(Date date) {
if (locale == null) {
return dateFormatter.format(date);
} else if (locale.getLanguage().equals("de")) {
return dateFormatterGerman.format(date);
}
return dateFormatter.format(date);
}

public static String getBooleanAsString(boolean value) {
return value ? "X" : "";
}
Expand Down Expand Up @@ -198,6 +241,7 @@ public I18n getI18n() {

/**
* Sets i18n and locale. If you want to use your own i18n, please call {@link #setI18n(I18n)} after {@link #setLocale(Locale)}.
*
* @param locale
*/
public void setLocale(Locale locale) {
Expand Down

0 comments on commit 669b722

Please sign in to comment.