[Major] Refactor reports to use ZonedDateTime instead of Date
This commit is contained in:
parent
9a6b78a68d
commit
baecbd5312
|
@ -5,6 +5,7 @@ import static li.strolch.model.StrolchModelConstants.*;
|
||||||
import static li.strolch.report.ReportConstants.*;
|
import static li.strolch.report.ReportConstants.*;
|
||||||
import static li.strolch.utils.helper.StringHelper.EMPTY;
|
import static li.strolch.utils.helper.StringHelper.EMPTY;
|
||||||
|
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collector;
|
import java.util.stream.Collector;
|
||||||
|
@ -18,8 +19,8 @@ import li.strolch.model.parameter.DateParameter;
|
||||||
import li.strolch.model.parameter.Parameter;
|
import li.strolch.model.parameter.Parameter;
|
||||||
import li.strolch.model.parameter.StringParameter;
|
import li.strolch.model.parameter.StringParameter;
|
||||||
import li.strolch.model.policy.PolicyDef;
|
import li.strolch.model.policy.PolicyDef;
|
||||||
import li.strolch.model.visitor.ElementDateVisitor;
|
|
||||||
import li.strolch.model.visitor.ElementStateVisitor;
|
import li.strolch.model.visitor.ElementStateVisitor;
|
||||||
|
import li.strolch.model.visitor.ElementZdtDateVisitor;
|
||||||
import li.strolch.persistence.api.StrolchTransaction;
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
import li.strolch.policy.PolicyHandler;
|
import li.strolch.policy.PolicyHandler;
|
||||||
import li.strolch.report.ReportConstants;
|
import li.strolch.report.ReportConstants;
|
||||||
|
@ -29,7 +30,7 @@ import li.strolch.utils.collections.DateRange;
|
||||||
import li.strolch.utils.collections.MapOfLists;
|
import li.strolch.utils.collections.MapOfLists;
|
||||||
import li.strolch.utils.collections.MapOfSets;
|
import li.strolch.utils.collections.MapOfSets;
|
||||||
import li.strolch.utils.collections.TypedTuple;
|
import li.strolch.utils.collections.TypedTuple;
|
||||||
import li.strolch.utils.iso8601.ISO8601FormatFactory;
|
import li.strolch.utils.iso8601.ISO8601;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Generic Report defines a report as is described at <a href="https://strolch.li/documentation-reports.html">Strolch
|
* A Generic Report defines a report as is described at <a href="https://strolch.li/documentation-reports.html">Strolch
|
||||||
|
@ -405,8 +406,10 @@ public class GenericReport extends ReportPolicy {
|
||||||
return buildStream().map(e -> new ReportElement(this.columnIds, columnId -> {
|
return buildStream().map(e -> new ReportElement(this.columnIds, columnId -> {
|
||||||
StringParameter columnDefP = this.columnsBag.getParameter(columnId, true);
|
StringParameter columnDefP = this.columnsBag.getParameter(columnId, true);
|
||||||
Object value = evaluateColumnValue(columnDefP, e, false);
|
Object value = evaluateColumnValue(columnDefP, e, false);
|
||||||
|
if (value instanceof ZonedDateTime)
|
||||||
|
return ISO8601.toString((ZonedDateTime) value);
|
||||||
if (value instanceof Date)
|
if (value instanceof Date)
|
||||||
return ISO8601FormatFactory.getInstance().formatDate((Date) value);
|
return ISO8601.toString((Date) value);
|
||||||
if (value instanceof Parameter)
|
if (value instanceof Parameter)
|
||||||
return ((Parameter<?>) value).getValueAsString();
|
return ((Parameter<?>) value).getValueAsString();
|
||||||
return value.toString();
|
return value.toString();
|
||||||
|
@ -594,9 +597,9 @@ public class GenericReport extends ReportPolicy {
|
||||||
|
|
||||||
String dateRangeSel = this.dateRangeSelP.getValue();
|
String dateRangeSel = this.dateRangeSelP.getValue();
|
||||||
|
|
||||||
Date date;
|
ZonedDateTime date;
|
||||||
if (dateRangeSel.equals(COL_DATE)) {
|
if (dateRangeSel.equals(COL_DATE)) {
|
||||||
date = element.accept(new ElementDateVisitor());
|
date = element.accept(new ElementZdtDateVisitor());
|
||||||
} else {
|
} else {
|
||||||
Optional<Parameter<?>> param = lookupParameter(this.dateRangeSelP, element);
|
Optional<Parameter<?>> param = lookupParameter(this.dateRangeSelP, element);
|
||||||
if (!param.isPresent() || StrolchValueType.parse(param.get().getType()) != StrolchValueType.DATE)
|
if (!param.isPresent() || StrolchValueType.parse(param.get().getType()) != StrolchValueType.DATE)
|
||||||
|
@ -604,7 +607,7 @@ public class GenericReport extends ReportPolicy {
|
||||||
"Date Range selector is invalid, as referenced parameter is not a Date but " + (param
|
"Date Range selector is invalid, as referenced parameter is not a Date but " + (param
|
||||||
.isPresent() ? param.get().getType() : "null"));
|
.isPresent() ? param.get().getType() : "null"));
|
||||||
|
|
||||||
date = ((DateParameter) param.get()).getValue();
|
date = ((DateParameter) param.get()).getValueZdt();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.dateRange.contains(date))
|
if (!this.dateRange.contains(date))
|
||||||
|
@ -664,7 +667,7 @@ public class GenericReport extends ReportPolicy {
|
||||||
} else if (columnDef.equals(COL_STATE)) {
|
} else if (columnDef.equals(COL_STATE)) {
|
||||||
columnValue = column.accept(new ElementStateVisitor());
|
columnValue = column.accept(new ElementStateVisitor());
|
||||||
} else if (columnDef.equals(COL_DATE)) {
|
} else if (columnDef.equals(COL_DATE)) {
|
||||||
columnValue = column.accept(new ElementDateVisitor());
|
columnValue = column.accept(new ElementZdtDateVisitor());
|
||||||
} else if (columnDef.startsWith(COL_SEARCH)) {
|
} else if (columnDef.startsWith(COL_SEARCH)) {
|
||||||
Parameter<?> parameter = findParameter(columnDefP, column);
|
Parameter<?> parameter = findParameter(columnDefP, column);
|
||||||
if (parameter == null)
|
if (parameter == null)
|
||||||
|
|
|
@ -3,6 +3,7 @@ package li.strolch.report.policy;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.Period;
|
import java.time.Period;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import li.strolch.model.StrolchValueType;
|
import li.strolch.model.StrolchValueType;
|
||||||
|
@ -10,6 +11,7 @@ import li.strolch.model.parameter.Parameter;
|
||||||
import li.strolch.persistence.api.StrolchTransaction;
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
import li.strolch.policy.StrolchPolicy;
|
import li.strolch.policy.StrolchPolicy;
|
||||||
import li.strolch.utils.dbc.DBC;
|
import li.strolch.utils.dbc.DBC;
|
||||||
|
import li.strolch.utils.iso8601.ISO8601;
|
||||||
import li.strolch.utils.iso8601.ISO8601FormatFactory;
|
import li.strolch.utils.iso8601.ISO8601FormatFactory;
|
||||||
|
|
||||||
public abstract class ReportFilterPolicy extends StrolchPolicy {
|
public abstract class ReportFilterPolicy extends StrolchPolicy {
|
||||||
|
@ -47,7 +49,14 @@ public abstract class ReportFilterPolicy extends StrolchPolicy {
|
||||||
DBC.PRE.assertNotNull("value required!", value);
|
DBC.PRE.assertNotNull("value required!", value);
|
||||||
|
|
||||||
Object left;
|
Object left;
|
||||||
if (value instanceof Date) {
|
if (value instanceof ZonedDateTime) {
|
||||||
|
|
||||||
|
if (this.right == null)
|
||||||
|
this.right = parseFilterValueToZdt(this.filterValue);
|
||||||
|
|
||||||
|
left = value;
|
||||||
|
|
||||||
|
} else if (value instanceof Date) {
|
||||||
|
|
||||||
if (this.right == null)
|
if (this.right == null)
|
||||||
this.right = parseFilterValueToDate(this.filterValue);
|
this.right = parseFilterValueToDate(this.filterValue);
|
||||||
|
@ -83,6 +92,7 @@ public abstract class ReportFilterPolicy extends StrolchPolicy {
|
||||||
|
|
||||||
protected Date parseFilterValueToDate(String filterValue) {
|
protected Date parseFilterValueToDate(String filterValue) {
|
||||||
DBC.INTERIM.assertNotEmpty("filterValue must not be empty for date comparisons!", filterValue);
|
DBC.INTERIM.assertNotEmpty("filterValue must not be empty for date comparisons!", filterValue);
|
||||||
|
logger.error("DEPRECATED, use ZonedDateTime");
|
||||||
|
|
||||||
if (!filterValue.startsWith("now"))
|
if (!filterValue.startsWith("now"))
|
||||||
return ISO8601FormatFactory.getInstance().parseDate(filterValue);
|
return ISO8601FormatFactory.getInstance().parseDate(filterValue);
|
||||||
|
@ -99,6 +109,23 @@ public abstract class ReportFilterPolicy extends StrolchPolicy {
|
||||||
return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
|
return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected ZonedDateTime parseFilterValueToZdt(String filterValue) {
|
||||||
|
DBC.INTERIM.assertNotEmpty("filterValue must not be empty for date comparisons!", filterValue);
|
||||||
|
|
||||||
|
if (!filterValue.startsWith("now"))
|
||||||
|
return ISO8601.parseToZdt(filterValue);
|
||||||
|
|
||||||
|
if (filterValue.charAt(3) != '(' || filterValue.charAt(filterValue.length() - 1) != ')')
|
||||||
|
throw new IllegalArgumentException("now() format invalid for " + filterValue);
|
||||||
|
|
||||||
|
String periodS = filterValue.substring(4, filterValue.length() - 1);
|
||||||
|
if (periodS.isEmpty())
|
||||||
|
return ZonedDateTime.now();
|
||||||
|
|
||||||
|
Period period = Period.parse(periodS);
|
||||||
|
return ZonedDateTime.now().plus(period);
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract boolean filter(Object left, Object right, boolean negate);
|
protected abstract boolean filter(Object left, Object right, boolean negate);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue