Skip to content

Commit

Permalink
Date from JSON is not printed when formatter is specified for the cor…
Browse files Browse the repository at this point in the history
…responding field #43
  • Loading branch information
NikitaShchienko committed Jun 11, 2019
1 parent 54ee1d2 commit 6d77f6f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public interface ReportFieldFormat extends Serializable {
* Example: ##,# for decimals, dd-MM-yyyy for dates, etc.
*/
String getFormat();

/**
* @return boolean <code>true</code> if the groovy script, otherwise <code>false</code>
*/
Boolean isGroovyScript();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.haulmont.yarg.formatters.impl.xls.DocumentConverterImpl;
import com.haulmont.yarg.structure.BandData;
import com.haulmont.yarg.structure.ReportTemplate;
import com.haulmont.yarg.util.groovy.Scripting;

import java.io.OutputStream;
import java.util.ArrayList;
Expand All @@ -40,8 +41,9 @@ public class DefaultFormatterFactory implements ReportFormatterFactory {
protected HtmlImportProcessor htmlImportProcessor;
protected HtmlToPdfConverterFactory htmlToPdfConverterFactory;
protected String fontsDirectory;
protected Scripting scripting;

protected Map<String, FormatterCreator> formattersMap = new HashMap<String, FormatterCreator>();
protected Map<String, FormatterCreator> formattersMap = new HashMap<>();

protected ReportInlinersProvider inlinersProvider;

Expand All @@ -52,6 +54,7 @@ public DefaultFormatterFactory() {
XLSFormatter xlsFormatter = new XLSFormatter(factoryInput);
xlsFormatter.setDocumentConverter(documentConverter);
xlsFormatter.setDefaultFormatProvider(defaultFormatProvider);
xlsFormatter.setScripting(scripting);
return xlsFormatter;
});

Expand All @@ -70,6 +73,7 @@ public DefaultFormatterFactory() {
htmlFormatter.setDefaultFormatProvider(defaultFormatProvider);
htmlFormatter.setFontsDirectory(getFontsDirectory());
htmlFormatter.setPdfConverterFactory(htmlToPdfConverterFactory);
htmlFormatter.setScripting(scripting);
return htmlFormatter;
};
formattersMap.put("ftl", ftlCreator);
Expand All @@ -79,12 +83,14 @@ public DefaultFormatterFactory() {
docxFormatter.setDefaultFormatProvider(defaultFormatProvider);
docxFormatter.setDocumentConverter(documentConverter);
docxFormatter.setHtmlImportProcessor(htmlImportProcessor);
docxFormatter.setScripting(scripting);
return docxFormatter;
});
FormatterCreator xlsxCreator = factoryInput -> {
XlsxFormatter xlsxFormatter = new XlsxFormatter(factoryInput);
xlsxFormatter.setDefaultFormatProvider(defaultFormatProvider);
xlsxFormatter.setDocumentConverter(documentConverter);
xlsxFormatter.setScripting(scripting);
return xlsxFormatter;
};
formattersMap.put("xlsx", xlsxCreator);
Expand Down Expand Up @@ -133,6 +139,14 @@ public void setHtmlToPdfConverterFactory(HtmlToPdfConverterFactory htmlToPdfConv
this.htmlToPdfConverterFactory = htmlToPdfConverterFactory;
}

public Scripting getScripting() {
return scripting;
}

public void setScripting(Scripting scripting) {
this.scripting = scripting;
}

public ReportFormatter createFormatter(FormatterFactoryInput factoryInput) {
String templateExtension = factoryInput.templateExtension;
BandData rootBand = factoryInput.rootBand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import com.haulmont.yarg.exception.ReportingInterruptedException;
import com.haulmont.yarg.formatters.ReportFormatter;
import com.haulmont.yarg.formatters.factory.FormatterFactoryInput;
import com.haulmont.yarg.formatters.impl.inline.BitmapContentInliner;
import com.haulmont.yarg.formatters.impl.inline.ContentInliner;
import com.haulmont.yarg.formatters.impl.inline.HtmlContentInliner;
import com.haulmont.yarg.formatters.impl.inline.ImageContentInliner;
import com.haulmont.yarg.structure.BandData;
import com.haulmont.yarg.structure.ReportFieldFormat;
import com.haulmont.yarg.structure.ReportOutputType;
import com.haulmont.yarg.structure.ReportTemplate;
import com.haulmont.yarg.util.groovy.DefaultScriptingImpl;
import com.haulmont.yarg.util.groovy.Scripting;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.ConstructorUtils;
Expand All @@ -55,6 +54,7 @@ public abstract class AbstractFormatter implements ReportFormatter {
public static final Pattern UNIVERSAL_ALIAS_PATTERN = Pattern.compile(UNIVERSAL_ALIAS_REGEXP, Pattern.CASE_INSENSITIVE);
public static final Pattern ALIAS_WITH_BAND_NAME_PATTERN = Pattern.compile(ALIAS_WITH_BAND_NAME_REGEXP);
public static final Pattern BAND_NAME_DECLARATION_PATTERN = Pattern.compile(BAND_NAME_DECLARATION_REGEXP);
public static final String VALUE = "value";


protected BandData rootBand;
Expand All @@ -63,12 +63,17 @@ public abstract class AbstractFormatter implements ReportFormatter {
protected OutputStream outputStream;
protected Set<ReportOutputType> supportedOutputTypes = new HashSet<>();
protected DefaultFormatProvider defaultFormatProvider;
protected Scripting scripting = new DefaultScriptingImpl();

/**
* Chain of responsibility for content inliners
*/
protected List<ContentInliner> contentInliners = new ArrayList<>();

public void setScripting(Scripting scripting) {
this.scripting = scripting;
}

protected AbstractFormatter(FormatterFactoryInput formatterFactoryInput) {
checkNotNull(formatterFactoryInput.getRootBand(), "\"rootBand\" parameter can not be null");
checkNotNull(formatterFactoryInput.getReportTemplate(), "\"reportTemplate\" parameter can not be null");
Expand Down Expand Up @@ -120,7 +125,9 @@ protected String formatValue(Object value, String parameterName, String fullPara
String valueString;
String formatString = getFormatString(parameterName, fullParameterName);
if (formatString != null) {
if (formatString.startsWith("class:")) {
if (Boolean.TRUE.equals(isGroovyScript(parameterName, fullParameterName))) {
valueString = scripting.evaluateGroovy(formatString, Collections.singletonMap(VALUE, value));
} else if (formatString.startsWith("class:")) {
String className = formatString.replaceFirst("class:", "");
ValueFormat valueFormat;
try {
Expand Down Expand Up @@ -167,6 +174,19 @@ protected String getFormatString(String parameterName, String fullParameterName)
return formatString;
}

protected Boolean isGroovyScript(String parameterName, String fullParameterName) {
Map<String, ReportFieldFormat> formats = rootBand.getReportFieldFormats();
Boolean groovyFormat = false;
if (formats != null) {
if (formats.containsKey(fullParameterName)) {
groovyFormat = formats.get(fullParameterName).isGroovyScript();
} else if (formats.containsKey(parameterName)) {
groovyFormat = formats.get(parameterName).isGroovyScript();
}
}
return groovyFormat;
}

protected String applyStringFunction(String valueString, String stringFunction) {
if (stringFunction.matches(STRING_FUNCTION_GROUP)) {
Integer index = Integer.valueOf(stringFunction.replaceAll("[^\\d]", ""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@
public class ReportFieldFormatImpl implements ReportFieldFormat {
protected String name;
protected String format;
protected Boolean groovyScript;

public ReportFieldFormatImpl(String name, String format) {
Preconditions.checkNotNull(name, "\"name\" parameter can not be null");
Preconditions.checkNotNull(name, "\"format\" parameter can not be null");
Preconditions.checkNotNull(format, "\"format\" parameter can not be null");

this.name = name;
this.format = format;
this.groovyScript = false;
}

public ReportFieldFormatImpl(String name, String format, Boolean groovyScript) {
this(name, format);
Preconditions.checkNotNull(groovyScript, "\"groovyScript\" parameter can not be null");
this.groovyScript = groovyScript;
}

@Override
Expand All @@ -40,4 +48,9 @@ public String getName() {
public String getFormat() {
return format;
}

@Override
public Boolean isGroovyScript() {
return groovyScript;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@ protected List<ReportFieldFormat> parseValueFormats(Element rootElement) throws
for (Element parameter : parameters) {
String name = parameter.attribute("name").getText();
String format = parameter.attribute("format").getText();
reportParameters.add(new ReportFieldFormatImpl(name, format));
Attribute groovyScriptAttribute = parameter.attribute("groovyScript");
if (groovyScriptAttribute != null) {
Boolean groovyFlag = (Boolean) groovyScriptAttribute.getData();
reportParameters.add(new ReportFieldFormatImpl(name, format, groovyFlag));
} else {
reportParameters.add(new ReportFieldFormatImpl(name, format));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,50 @@ public class FormatTest {

@Test
public void testNull() {
AbstractFormatter abstractFormatter = createFormatter("a.number", "##.##");
AbstractFormatter abstractFormatter = createFormatter("a.number", "##.##", false);
assertEquals("", abstractFormatter.formatValue(null, "number", "a.number"));
}

@Test
public void testNumber() {
AbstractFormatter abstractFormatter = createFormatter("a.number", "##.##");
AbstractFormatter abstractFormatter = createFormatter("a.number", "##.##", false);

assertEquals("5,57", abstractFormatter.formatValue(5.5678, "number", "a.number").replace(".", ","));
}

@Test
public void testDate() throws ParseException {
AbstractFormatter abstractFormatter = createFormatter("a.number", "yyyy-mm-dd");
AbstractFormatter abstractFormatter = createFormatter("a.number", "yyyy-mm-dd", false);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-mm-yyyy");

assertEquals("2009-09-01", abstractFormatter.formatValue(simpleDateFormat.parse("01-09-2009"), "number", "a.number"));
}

@Test
public void testClass() throws ParseException {
AbstractFormatter abstractFormatter = createFormatter("a.number", "class:com.haulmont.yarg.formatters.impl.TestValueFormat");
AbstractFormatter abstractFormatter = createFormatter("a.number", "class:com.haulmont.yarg.formatters.impl.TestValueFormat", false);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-mm-yyyy");

assertEquals("Test", abstractFormatter.formatValue(null, "number", "a.number"));
assertEquals("Test", abstractFormatter.formatValue(12345, "number", "a.number"));
assertEquals("Test", abstractFormatter.formatValue(simpleDateFormat.parse("01-09-2009"), "number", "a.number"));
}

private AbstractFormatter createFormatter(String formatName, String formatValue) {
@Test
public void testGroovyFormat() {
AbstractFormatter abstractFormatter = createFormatter("a.text", "return value.replace('-', '/')", true);
assertEquals("01/09/2009", abstractFormatter.formatValue("01-09-2009", "text", "a.text"));
}

@Test
public void testNullGroovyFormat() {
AbstractFormatter abstractFormatter = createFormatter("a.text", "return value", true);
assertEquals("", abstractFormatter.formatValue(null, "text", "a.text"));
}

private AbstractFormatter createFormatter(String formatName, String formatValue, Boolean groovyScript) {
BandData rootBand = new BandData(BandData.ROOT_BAND_NAME);
rootBand.addReportFieldFormats(Collections.singletonList(new ReportFieldFormatImpl(formatName, formatValue)));
rootBand.addReportFieldFormats(Collections.singletonList(new ReportFieldFormatImpl(formatName, formatValue, groovyScript)));
FormatterFactoryInput formatterFactoryInput = new FormatterFactoryInput("xlsx", rootBand, reportTemplate, ReportOutputType.xlsx, null);
return new AbstractFormatter(formatterFactoryInput) {
@Override
Expand Down

0 comments on commit 6d77f6f

Please sign in to comment.