2017-10-18 13:11:39 +02:00
|
|
|
package li.strolch.report.policy;
|
2017-03-20 19:27:59 +01:00
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
import static java.util.Comparator.comparingInt;
|
|
|
|
import static li.strolch.report.ReportConstants.*;
|
2017-10-27 19:22:22 +02:00
|
|
|
import static li.strolch.utils.helper.StringHelper.EMPTY;
|
2017-03-21 07:51:13 +01:00
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.function.Supplier;
|
2017-03-21 08:23:29 +01:00
|
|
|
import java.util.stream.Collector;
|
2017-03-20 19:27:59 +01:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
2017-05-03 15:32:20 +02:00
|
|
|
import li.strolch.agent.api.ComponentContainer;
|
2017-10-18 13:11:39 +02:00
|
|
|
import li.strolch.model.*;
|
|
|
|
import li.strolch.model.parameter.*;
|
2017-05-03 15:32:20 +02:00
|
|
|
import li.strolch.model.policy.PolicyDef;
|
2017-03-21 15:34:10 +01:00
|
|
|
import li.strolch.model.visitor.ElementDateVisitor;
|
|
|
|
import li.strolch.model.visitor.ElementStateVisitor;
|
2017-03-20 19:27:59 +01:00
|
|
|
import li.strolch.persistence.api.StrolchTransaction;
|
2017-05-03 15:32:20 +02:00
|
|
|
import li.strolch.policy.PolicyHandler;
|
2017-10-18 13:11:39 +02:00
|
|
|
import li.strolch.report.ReportElement;
|
2017-03-20 19:27:59 +01:00
|
|
|
import li.strolch.runtime.StrolchConstants;
|
2018-05-29 09:48:07 +02:00
|
|
|
import li.strolch.utils.ObjectHelper;
|
2017-03-21 15:34:10 +01:00
|
|
|
import li.strolch.utils.collections.DateRange;
|
2017-03-21 07:51:13 +01:00
|
|
|
import li.strolch.utils.collections.MapOfSets;
|
2017-03-21 15:34:10 +01:00
|
|
|
import li.strolch.utils.iso8601.ISO8601FormatFactory;
|
2017-03-20 19:27:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
|
|
|
*/
|
2017-10-18 13:11:39 +02:00
|
|
|
public class GenericReport extends ReportPolicy {
|
2017-03-21 07:51:13 +01:00
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
private Resource reportRes;
|
2017-03-20 19:27:59 +01:00
|
|
|
|
2017-05-03 16:06:56 +02:00
|
|
|
private boolean hideObjectTypeFromFilterCriteria;
|
2017-10-18 13:11:39 +02:00
|
|
|
private String objectType;
|
|
|
|
|
2017-03-20 19:27:59 +01:00
|
|
|
private ParameterBag columnsBag;
|
2017-06-27 08:45:26 +02:00
|
|
|
private List<StringParameter> orderingParams;
|
|
|
|
private boolean descending;
|
2017-10-27 19:22:22 +02:00
|
|
|
private boolean allowMissingColumns;
|
2017-09-13 15:28:40 +02:00
|
|
|
private List<String> columnIds;
|
2017-03-21 15:34:10 +01:00
|
|
|
private StringParameter dateRangeSelP;
|
|
|
|
|
|
|
|
private DateRange dateRange;
|
2017-05-03 15:32:20 +02:00
|
|
|
private Map<ReportFilterPolicy, StringParameter> filtersByPolicy;
|
|
|
|
private MapOfSets<String, String> filtersById;
|
2017-03-20 19:27:59 +01:00
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
public GenericReport(ComponentContainer container, StrolchTransaction tx) {
|
|
|
|
super(container, tx);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void initialize(String reportId) {
|
2017-03-27 14:39:03 +02:00
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
// get the reportRes
|
|
|
|
this.reportRes = tx().getResourceBy(TYPE_REPORT, reportId, true);
|
2017-05-03 15:32:20 +02:00
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
BooleanParameter hideObjectTypeFromFilterCriteriaP = this.reportRes
|
|
|
|
.getParameter(BAG_PARAMETERS, PARAM_HIDE_OBJECT_TYPE_FROM_FILTER_CRITERIA);
|
|
|
|
this.hideObjectTypeFromFilterCriteria =
|
|
|
|
hideObjectTypeFromFilterCriteriaP != null && hideObjectTypeFromFilterCriteriaP.getValue();
|
|
|
|
if (this.hideObjectTypeFromFilterCriteria) {
|
|
|
|
this.objectType = this.reportRes.getParameter(BAG_PARAMETERS, PARAM_OBJECT_TYPE).getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.columnsBag = this.reportRes.getParameterBag(BAG_COLUMNS, true);
|
2017-05-03 16:06:56 +02:00
|
|
|
|
2017-09-13 15:28:40 +02:00
|
|
|
this.columnIds = this.columnsBag.getParameters().stream() //
|
2017-10-18 13:11:39 +02:00
|
|
|
.sorted(comparingInt(Parameter::getIndex)) //
|
|
|
|
.map(StrolchElement::getId) //
|
2017-09-13 15:28:40 +02:00
|
|
|
.collect(Collectors.toList());
|
2017-06-27 08:45:26 +02:00
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
if (this.reportRes.hasParameter(BAG_PARAMETERS, PARAM_DESCENDING))
|
|
|
|
this.descending = this.reportRes.getParameter(BAG_PARAMETERS, PARAM_DESCENDING).getValue();
|
2017-10-27 19:22:22 +02:00
|
|
|
if (this.reportRes.hasParameter(BAG_PARAMETERS, PARAM_ALLOW_MISSING_COLUMNS))
|
|
|
|
this.allowMissingColumns = this.reportRes.getParameter(BAG_PARAMETERS, PARAM_ALLOW_MISSING_COLUMNS)
|
|
|
|
.getValue();
|
2017-06-27 08:45:26 +02:00
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
this.dateRangeSelP = this.reportRes.getParameter(BAG_PARAMETERS, PARAM_DATE_RANGE_SEL);
|
2017-05-03 15:32:20 +02:00
|
|
|
|
2017-06-27 08:45:26 +02:00
|
|
|
// evaluate ordering params
|
2017-10-18 13:11:39 +02:00
|
|
|
if (this.reportRes.hasParameterBag(BAG_ORDERING)) {
|
|
|
|
ParameterBag orderingBag = this.reportRes.getParameterBag(BAG_ORDERING);
|
2017-06-27 08:45:26 +02:00
|
|
|
if (orderingBag.hasParameters()) {
|
|
|
|
this.orderingParams = orderingBag.getParameters().stream().map(e -> (StringParameter) e)
|
|
|
|
.collect(Collectors.toList());
|
2017-10-18 13:11:39 +02:00
|
|
|
this.orderingParams.sort(comparingInt(AbstractParameter::getIndex));
|
2017-06-27 08:45:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 15:32:20 +02:00
|
|
|
// evaluate filters
|
|
|
|
this.filtersByPolicy = new HashMap<>();
|
2017-10-18 13:11:39 +02:00
|
|
|
List<ParameterBag> filterBags = this.reportRes.getParameterBagsByType(TYPE_FILTER);
|
2017-05-03 15:32:20 +02:00
|
|
|
for (ParameterBag filterBag : filterBags) {
|
|
|
|
|
|
|
|
// prepare filter function policy
|
|
|
|
StringParameter functionP = filterBag.getParameter(PARAM_POLICY);
|
2017-10-18 13:11:39 +02:00
|
|
|
PolicyHandler policyHandler = getContainer().getComponent(PolicyHandler.class);
|
2017-05-03 15:32:20 +02:00
|
|
|
PolicyDef policyDef = PolicyDef.valueOf(functionP.getInterpretation(), functionP.getUom());
|
2017-10-18 13:11:39 +02:00
|
|
|
ReportFilterPolicy filterFunction = policyHandler.getPolicy(policyDef, tx());
|
2017-05-03 15:32:20 +02:00
|
|
|
filterFunction.init(functionP.getValue());
|
|
|
|
|
|
|
|
StringParameter fieldRefP = filterBag.getParameter(PARAM_FIELD_REF);
|
|
|
|
this.filtersByPolicy.put(filterFunction, fieldRefP);
|
|
|
|
}
|
2017-03-21 15:34:10 +01:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:41:07 +02:00
|
|
|
public boolean hasDateRangeSelector() {
|
|
|
|
return this.dateRangeSelP != null;
|
|
|
|
}
|
|
|
|
|
2017-03-21 15:34:10 +01:00
|
|
|
public GenericReport dateRange(DateRange dateRange) {
|
|
|
|
this.dateRange = dateRange;
|
|
|
|
return this;
|
2017-03-21 07:51:13 +01:00
|
|
|
}
|
|
|
|
|
2017-09-13 15:28:40 +02:00
|
|
|
public List<String> getColumnKeys() {
|
|
|
|
return this.columnIds;
|
2017-03-27 11:51:53 +02:00
|
|
|
}
|
|
|
|
|
2017-03-21 07:51:13 +01:00
|
|
|
public GenericReport filter(String type, String... ids) {
|
2017-05-03 15:32:20 +02:00
|
|
|
if (this.filtersById == null)
|
|
|
|
this.filtersById = new MapOfSets<>();
|
2017-03-21 07:51:13 +01:00
|
|
|
for (String id : ids) {
|
2017-05-03 15:32:20 +02:00
|
|
|
this.filtersById.addElement(type, id);
|
2017-03-21 07:51:13 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GenericReport filter(String type, List<String> ids) {
|
2017-05-03 15:32:20 +02:00
|
|
|
if (this.filtersById == null)
|
|
|
|
this.filtersById = new MapOfSets<>();
|
2017-03-21 07:51:13 +01:00
|
|
|
for (String id : ids) {
|
2017-05-03 15:32:20 +02:00
|
|
|
this.filtersById.addElement(type, id);
|
2017-03-21 07:51:13 +01:00
|
|
|
}
|
|
|
|
return this;
|
2017-03-20 19:27:59 +01:00
|
|
|
}
|
|
|
|
|
2017-03-24 11:25:56 +01:00
|
|
|
public GenericReport filter(String type, Set<String> ids) {
|
2017-05-03 15:32:20 +02:00
|
|
|
if (this.filtersById == null)
|
|
|
|
this.filtersById = new MapOfSets<>();
|
2017-03-24 11:25:56 +01:00
|
|
|
for (String id : ids) {
|
2017-05-03 15:32:20 +02:00
|
|
|
this.filtersById.addElement(type, id);
|
2017-03-24 11:25:56 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-03-21 08:23:29 +01:00
|
|
|
public Stream<Map<String, StrolchRootElement>> buildStream() {
|
2017-03-20 19:27:59 +01:00
|
|
|
|
|
|
|
// query the main objects and return a stream
|
2017-10-18 13:11:39 +02:00
|
|
|
Stream<Map<String, StrolchRootElement>> stream = queryRows().map(this::evaluateRow);
|
2017-06-27 08:45:26 +02:00
|
|
|
|
|
|
|
if (hasFilter())
|
2017-10-18 13:11:39 +02:00
|
|
|
stream = stream.filter(this::filter);
|
2017-06-27 08:45:26 +02:00
|
|
|
|
|
|
|
if (hasOrdering())
|
2017-10-18 13:11:39 +02:00
|
|
|
stream = stream.sorted(this::sort);
|
2017-06-27 08:45:26 +02:00
|
|
|
|
|
|
|
return stream;
|
2017-03-21 07:51:13 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 11:51:53 +02:00
|
|
|
public Stream<ReportElement> doReport() {
|
|
|
|
|
2017-03-27 14:04:39 +02:00
|
|
|
return buildStream().map(e -> new ReportElement(this.columnIds, columnId -> {
|
2017-05-04 20:45:44 +02:00
|
|
|
StringParameter columnDefP = this.columnsBag.getParameter(columnId, true);
|
2018-05-29 09:48:07 +02:00
|
|
|
Object value = evaluateColumnValue(columnDefP, e);
|
|
|
|
if (value instanceof Date)
|
|
|
|
return ISO8601FormatFactory.getInstance().formatDate((Date) value);
|
2018-05-29 10:04:28 +02:00
|
|
|
if (value instanceof Parameter)
|
|
|
|
return ((Parameter) value).getValueAsString();
|
2018-05-29 09:48:07 +02:00
|
|
|
return value.toString();
|
2017-03-27 11:51:53 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
protected boolean filterCriteria(StrolchRootElement element) {
|
|
|
|
if (this.hideObjectTypeFromFilterCriteria)
|
|
|
|
return !element.getType().equals(this.objectType);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-21 15:34:10 +01:00
|
|
|
public MapOfSets<String, StrolchRootElement> generateFilterCriteria() {
|
2017-03-21 08:23:29 +01:00
|
|
|
return buildStream() //
|
2017-10-18 13:11:39 +02:00
|
|
|
|
|
|
|
.flatMap(e -> e.values().stream().filter(this::filterCriteria)) //
|
|
|
|
|
2017-03-21 08:23:29 +01:00
|
|
|
.collect( //
|
|
|
|
Collector.of( //
|
2017-10-18 13:11:39 +02:00
|
|
|
(Supplier<MapOfSets<String, StrolchRootElement>>) MapOfSets::new, //
|
2017-03-21 15:34:10 +01:00
|
|
|
(m, e) -> m.addElement(e.getType(), e), //
|
2017-10-18 13:11:39 +02:00
|
|
|
MapOfSets::addAll, //
|
2017-03-21 08:23:29 +01:00
|
|
|
m -> m));
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
protected boolean hasOrdering() {
|
2017-06-27 08:45:26 +02:00
|
|
|
return this.orderingParams != null && !this.orderingParams.isEmpty();
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
protected int sort(Map<String, StrolchRootElement> row1, Map<String, StrolchRootElement> row2) {
|
2017-06-27 08:45:26 +02:00
|
|
|
|
|
|
|
for (StringParameter fieldRefP : this.orderingParams) {
|
|
|
|
|
|
|
|
String type = fieldRefP.getUom();
|
|
|
|
|
|
|
|
StrolchRootElement column1 = row1.get(type);
|
2017-10-27 19:22:22 +02:00
|
|
|
StrolchRootElement column2 = row2.get(type);
|
|
|
|
if (column1 == null && column2 == null)
|
|
|
|
continue;
|
2017-06-27 08:45:26 +02:00
|
|
|
if (column1 == null)
|
|
|
|
return -1;
|
|
|
|
if (column2 == null)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
int sortVal;
|
|
|
|
if (fieldRefP.getValue().startsWith("$")) {
|
2018-05-29 09:48:07 +02:00
|
|
|
Object columnValue1 = evaluateColumnValue(fieldRefP, row1);
|
|
|
|
Object columnValue2 = evaluateColumnValue(fieldRefP, row2);
|
2017-06-27 08:45:26 +02:00
|
|
|
|
|
|
|
if (this.descending) {
|
2018-05-29 09:48:07 +02:00
|
|
|
sortVal = ObjectHelper.compare(columnValue1, columnValue2, true);
|
2017-06-27 08:45:26 +02:00
|
|
|
} else {
|
2018-05-29 09:48:07 +02:00
|
|
|
sortVal = ObjectHelper.compare(columnValue2, columnValue1, true);
|
2017-06-27 08:45:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2017-10-27 19:22:22 +02:00
|
|
|
Optional<Parameter<?>> param1 = lookupParameter(fieldRefP, column1);
|
|
|
|
Optional<Parameter<?>> param2 = lookupParameter(fieldRefP, column2);
|
|
|
|
|
|
|
|
if (!param1.isPresent() && !param2.isPresent())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (param1.isPresent() && !param2.isPresent())
|
|
|
|
return 1;
|
|
|
|
else if (!param1.isPresent())
|
|
|
|
return -1;
|
2017-06-27 08:45:26 +02:00
|
|
|
|
|
|
|
if (this.descending)
|
2017-10-27 19:22:22 +02:00
|
|
|
sortVal = param1.get().compareTo(param2.get());
|
2017-06-27 08:45:26 +02:00
|
|
|
else
|
2017-10-27 19:22:22 +02:00
|
|
|
sortVal = param2.get().compareTo(param1.get());
|
2017-06-27 08:45:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sortVal != 0)
|
|
|
|
return sortVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
protected boolean hasFilter() {
|
|
|
|
return !this.filtersByPolicy.isEmpty() || this.dateRange != null || (this.filtersById != null
|
|
|
|
&& !this.filtersById.isEmpty());
|
2017-06-27 08:45:26 +02:00
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
protected boolean filter(Map<String, StrolchRootElement> row) {
|
2017-03-21 07:51:13 +01:00
|
|
|
|
2017-05-03 15:32:20 +02:00
|
|
|
// do filtering by policies
|
|
|
|
for (ReportFilterPolicy filterPolicy : this.filtersByPolicy.keySet()) {
|
|
|
|
StringParameter fieldRefP = this.filtersByPolicy.get(filterPolicy);
|
|
|
|
|
|
|
|
String type = fieldRefP.getUom();
|
|
|
|
|
|
|
|
StrolchRootElement column = row.get(type);
|
|
|
|
|
2017-11-02 17:53:49 +01:00
|
|
|
// if column is null, then don't include in result
|
|
|
|
if (column == null)
|
|
|
|
return false;
|
|
|
|
|
2017-05-03 15:32:20 +02:00
|
|
|
if (fieldRefP.getValue().startsWith("$")) {
|
2018-05-29 09:48:07 +02:00
|
|
|
Object value = evaluateColumnValue(fieldRefP, row);
|
2018-06-26 18:30:29 +02:00
|
|
|
if (!filterPolicy.filter(value))
|
2017-05-03 15:32:20 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
2017-10-27 19:22:22 +02:00
|
|
|
Optional<Parameter<?>> param = lookupParameter(fieldRefP, column);
|
|
|
|
if (param.isPresent() && !filterPolicy.filter(param.get()))
|
2017-05-03 15:32:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// do a date range selection, if required
|
2017-03-21 15:34:10 +01:00
|
|
|
if (this.dateRange != null) {
|
|
|
|
if (this.dateRangeSelP == null)
|
|
|
|
throw new IllegalStateException(
|
2017-10-18 13:11:39 +02:00
|
|
|
"DateRange defined, but reportRes does not defined a date range selector!");
|
2017-03-21 15:34:10 +01:00
|
|
|
|
|
|
|
String type = this.dateRangeSelP.getUom();
|
2017-03-21 07:51:13 +01:00
|
|
|
StrolchRootElement element = row.get(type);
|
|
|
|
if (element == null)
|
|
|
|
return false;
|
|
|
|
|
2017-03-21 15:34:10 +01:00
|
|
|
String dateRangeSel = this.dateRangeSelP.getValue();
|
|
|
|
|
|
|
|
Date date;
|
|
|
|
if (dateRangeSel.equals(COL_DATE)) {
|
|
|
|
date = element.accept(new ElementDateVisitor());
|
|
|
|
} else {
|
2017-10-27 19:22:22 +02:00
|
|
|
Optional<Parameter<?>> param = lookupParameter(this.dateRangeSelP, element);
|
|
|
|
if (!param.isPresent() || StrolchValueType.parse(param.get().getType()) != StrolchValueType.DATE)
|
2017-03-21 15:34:10 +01:00
|
|
|
throw new IllegalStateException(
|
2017-10-27 19:22:22 +02:00
|
|
|
"Date Range selector is invalid, as referenced parameter is not a Date but " + (param
|
|
|
|
.isPresent() ? param.get().getType() : "null"));
|
2017-03-21 15:34:10 +01:00
|
|
|
|
2017-10-27 19:22:22 +02:00
|
|
|
date = ((DateParameter) param.get()).getValue();
|
2017-03-21 15:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.dateRange.contains(date))
|
2017-03-21 07:51:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-21 15:34:10 +01:00
|
|
|
// then we do a filter by criteria
|
2017-05-03 15:32:20 +02:00
|
|
|
if (this.filtersById != null && !this.filtersById.isEmpty()) {
|
|
|
|
for (String type : this.filtersById.keySet()) {
|
2017-03-21 15:34:10 +01:00
|
|
|
StrolchRootElement element = row.get(type);
|
|
|
|
if (element == null)
|
|
|
|
return false;
|
|
|
|
|
2017-05-03 15:32:20 +02:00
|
|
|
if (!this.filtersById.getSet(type).contains(element.getId()))
|
2017-03-21 15:34:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise we want to keep this row
|
2017-03-21 07:51:13 +01:00
|
|
|
return true;
|
2017-03-20 19:27:59 +01:00
|
|
|
}
|
|
|
|
|
2018-05-29 09:48:07 +02:00
|
|
|
protected Object evaluateColumnValue(StringParameter columnDefP, Map<String, StrolchRootElement> row) {
|
2017-03-20 19:27:59 +01:00
|
|
|
|
2017-03-27 11:51:53 +02:00
|
|
|
String columnDef = columnDefP.getValue();
|
|
|
|
String refType = columnDefP.getUom();
|
|
|
|
|
|
|
|
// get the referenced object
|
|
|
|
StrolchRootElement column = row.get(refType);
|
|
|
|
|
2018-05-29 09:48:07 +02:00
|
|
|
Object columnValue;
|
2017-03-27 11:51:53 +02:00
|
|
|
|
2018-07-13 12:19:23 +02:00
|
|
|
if (column == null) {
|
2017-10-27 19:22:22 +02:00
|
|
|
columnValue = EMPTY;
|
2018-07-13 12:19:23 +02:00
|
|
|
} else if (columnDef.equals(COL_OBJECT)) {
|
|
|
|
columnValue = column;
|
|
|
|
} else if (columnDef.equals(COL_ID)) {
|
2017-03-27 11:51:53 +02:00
|
|
|
columnValue = column.getId();
|
|
|
|
} else if (columnDef.equals(COL_NAME)) {
|
|
|
|
columnValue = column.getName();
|
|
|
|
} else if (columnDef.equals(COL_TYPE)) {
|
|
|
|
columnValue = column.getType();
|
|
|
|
} else if (columnDef.equals(COL_STATE)) {
|
2018-05-29 10:04:28 +02:00
|
|
|
columnValue = column.accept(new ElementStateVisitor());
|
2017-03-27 11:51:53 +02:00
|
|
|
} else if (columnDef.equals(COL_DATE)) {
|
2018-05-29 09:48:07 +02:00
|
|
|
columnValue = column.accept(new ElementDateVisitor());
|
2017-03-27 11:51:53 +02:00
|
|
|
} else if (columnDef.startsWith(COL_SEARCH)) {
|
|
|
|
Parameter<?> parameter = findParameter(columnDefP, column);
|
|
|
|
if (parameter == null)
|
2017-10-27 19:22:22 +02:00
|
|
|
columnValue = EMPTY;
|
2017-03-27 11:51:53 +02:00
|
|
|
else
|
2018-05-29 10:04:28 +02:00
|
|
|
columnValue = parameter;
|
2017-03-27 11:51:53 +02:00
|
|
|
} else {
|
2018-05-29 10:04:28 +02:00
|
|
|
columnValue = lookupParameter(columnDefP, column).orElseGet(StringParameter::new);
|
2017-03-27 11:51:53 +02:00
|
|
|
}
|
2017-03-20 19:27:59 +01:00
|
|
|
|
2017-03-27 11:51:53 +02:00
|
|
|
return columnValue;
|
2017-03-20 19:27:59 +01:00
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
protected Parameter<?> findParameter(StringParameter columnDefP, StrolchRootElement column) {
|
2017-03-22 14:55:54 +01:00
|
|
|
|
|
|
|
String columnDef = columnDefP.getValue();
|
|
|
|
|
|
|
|
String[] searchParts = columnDef.split(SEARCH_SEPARATOR);
|
|
|
|
if (searchParts.length != 3)
|
2017-10-18 13:11:39 +02:00
|
|
|
throw new IllegalStateException(
|
|
|
|
"Parameter search reference (" + columnDef + ") is invalid as it does not have 3 parts for "
|
|
|
|
+ columnDefP.getLocator());
|
2017-03-22 14:55:54 +01:00
|
|
|
|
|
|
|
String parentParamId = searchParts[1];
|
|
|
|
String paramRef = searchParts[2];
|
|
|
|
|
|
|
|
String[] locatorParts = paramRef.split(Locator.PATH_SEPARATOR);
|
|
|
|
if (locatorParts.length != 3)
|
2017-10-18 13:11:39 +02:00
|
|
|
throw new IllegalStateException(
|
|
|
|
"Parameter search reference (" + paramRef + ") is invalid as it does not have 3 parts for "
|
|
|
|
+ columnDefP.getLocator());
|
2017-03-22 14:55:54 +01:00
|
|
|
|
|
|
|
String bagKey = locatorParts[1];
|
|
|
|
String paramKey = locatorParts[2];
|
|
|
|
|
|
|
|
return findParameter(column, parentParamId, bagKey, paramKey);
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
protected Parameter<?> findParameter(StrolchRootElement element, String parentParamId, String bagKey,
|
2017-03-22 14:55:54 +01:00
|
|
|
String paramKey) {
|
|
|
|
|
|
|
|
Parameter<?> parameter = element.getParameter(bagKey, paramKey);
|
|
|
|
if (parameter != null)
|
|
|
|
return parameter;
|
|
|
|
|
|
|
|
StringParameter parentRefP = element.getParameter(BAG_RELATIONS, parentParamId);
|
|
|
|
if (parentRefP == null)
|
|
|
|
return null;
|
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
Resource parent = tx().getResourceBy(parentRefP);
|
2017-03-22 14:55:54 +01:00
|
|
|
if (parent == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return findParameter(parent, parentParamId, bagKey, paramKey);
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:22:22 +02:00
|
|
|
protected Optional<Parameter<?>> lookupParameter(StringParameter paramRefP, StrolchRootElement column) {
|
2017-03-21 15:34:10 +01:00
|
|
|
String paramRef = paramRefP.getValue();
|
|
|
|
|
|
|
|
String[] locatorParts = paramRef.split(Locator.PATH_SEPARATOR);
|
|
|
|
if (locatorParts.length != 3)
|
2017-10-18 13:11:39 +02:00
|
|
|
throw new IllegalStateException(
|
|
|
|
"Parameter reference (" + paramRef + ") is invalid as it does not have 3 parts for " + paramRefP
|
|
|
|
.getLocator());
|
2017-03-21 15:34:10 +01:00
|
|
|
|
|
|
|
String bagKey = locatorParts[1];
|
|
|
|
String paramKey = locatorParts[2];
|
|
|
|
|
|
|
|
Parameter<?> param = column.getParameter(bagKey, paramKey);
|
2017-10-27 19:22:22 +02:00
|
|
|
if (!allowMissingColumns && param == null)
|
2017-10-18 13:11:39 +02:00
|
|
|
throw new IllegalStateException(
|
|
|
|
"Parameter reference (" + paramRef + ") for " + paramRefP.getLocator() + " not found on " + column
|
|
|
|
.getLocator());
|
2017-03-21 15:34:10 +01:00
|
|
|
|
2017-10-27 19:22:22 +02:00
|
|
|
return Optional.ofNullable(param);
|
2017-03-21 15:34:10 +01:00
|
|
|
}
|
|
|
|
|
2018-03-07 18:15:43 +01:00
|
|
|
protected Stream<? extends StrolchRootElement> queryRows() {
|
2017-03-20 19:27:59 +01:00
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
StringParameter objectTypeP = this.reportRes.getParameter(BAG_PARAMETERS, PARAM_OBJECT_TYPE);
|
2017-03-20 19:27:59 +01:00
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
// find the type of object for which the reportRes is created
|
|
|
|
switch (objectTypeP.getInterpretation()) {
|
2018-03-07 18:15:43 +01:00
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
case StrolchConstants.INTERPRETATION_RESOURCE_REF:
|
2017-03-20 19:27:59 +01:00
|
|
|
|
2018-06-22 13:14:14 +02:00
|
|
|
return tx().streamResources(objectTypeP.getUom());
|
2017-03-20 19:27:59 +01:00
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
case StrolchConstants.INTERPRETATION_ORDER_REF:
|
2017-03-20 19:27:59 +01:00
|
|
|
|
2018-06-22 13:14:14 +02:00
|
|
|
return tx().streamOrders(objectTypeP.getUom());
|
|
|
|
|
|
|
|
case StrolchConstants.INTERPRETATION_ACTIVITY_REF:
|
|
|
|
|
|
|
|
return tx().streamActivities(objectTypeP.getUom());
|
2017-03-20 19:27:59 +01:00
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
default:
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Unhandled element type " + objectTypeP.getInterpretation());
|
2017-03-20 19:27:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
protected Map<String, StrolchRootElement> evaluateRow(StrolchRootElement resource) {
|
2017-03-20 19:27:59 +01:00
|
|
|
|
|
|
|
// interpretation -> Resource-Ref, etc.
|
|
|
|
// uom -> object type
|
|
|
|
// value -> element type where relation is defined for this join
|
|
|
|
|
|
|
|
// create the refs element
|
|
|
|
HashMap<String, StrolchRootElement> refs = new HashMap<>();
|
|
|
|
// and add the starting point
|
|
|
|
refs.put(resource.getType(), resource);
|
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
if (!this.reportRes.hasParameterBag(BAG_JOINS))
|
2017-05-03 16:06:56 +02:00
|
|
|
return refs;
|
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
ParameterBag joinBag = this.reportRes.getParameterBag(BAG_JOINS);
|
2017-03-20 19:27:59 +01:00
|
|
|
for (String paramId : joinBag.getParameterKeySet()) {
|
|
|
|
StringParameter joinP = joinBag.getParameter(paramId);
|
|
|
|
addColumnJoin(refs, joinBag, joinP, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return refs;
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:11:39 +02:00
|
|
|
protected StrolchRootElement addColumnJoin(Map<String, StrolchRootElement> refs, ParameterBag joinBag,
|
2017-03-20 19:27:59 +01:00
|
|
|
StringParameter joinP, boolean optional) {
|
|
|
|
|
2017-09-13 15:28:40 +02:00
|
|
|
String interpretation = joinP.getInterpretation();
|
|
|
|
String elementType = interpretation.substring(0, interpretation.indexOf(SUFFIX_REF));
|
2017-03-20 19:27:59 +01:00
|
|
|
String joinType = joinP.getUom();
|
|
|
|
String dependencyType = joinP.getValue();
|
|
|
|
|
|
|
|
// get dependency
|
|
|
|
StrolchRootElement dependency;
|
|
|
|
if (refs.containsKey(dependencyType)) {
|
|
|
|
dependency = refs.get(dependencyType);
|
|
|
|
} else {
|
|
|
|
// recursively find the dependency
|
|
|
|
StringParameter dependencyP = joinBag.getParameter(dependencyType);
|
|
|
|
dependency = addColumnJoin(refs, joinBag, dependencyP, false);
|
2017-05-03 15:32:20 +02:00
|
|
|
if (dependency == null)
|
|
|
|
return null;
|
2017-03-20 19:27:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ParameterBag relationsBag = dependency.getParameterBag(BAG_RELATIONS);
|
2017-03-21 15:34:10 +01:00
|
|
|
if (relationsBag == null)
|
2017-10-18 13:11:39 +02:00
|
|
|
throw new IllegalStateException(
|
|
|
|
"Invalid join definition value: " + joinP.getValue() + " on: " + joinP.getLocator() + " as "
|
|
|
|
+ dependency.getLocator() + " has no ParameterBag " + BAG_RELATIONS);
|
2017-03-21 15:34:10 +01:00
|
|
|
|
2018-07-03 15:26:12 +02:00
|
|
|
List<Parameter<?>> relationParams = relationsBag.getParametersByInterpretationAndUom(interpretation, joinType)
|
|
|
|
.stream().filter(p -> p.getValueType() == StrolchValueType.STRING).collect(Collectors.toList());
|
2017-10-18 16:33:56 +02:00
|
|
|
|
2017-03-20 19:27:59 +01:00
|
|
|
if (relationParams.isEmpty()) {
|
|
|
|
throw new IllegalStateException(
|
2018-07-03 15:26:12 +02:00
|
|
|
"Found no relation parameters with UOM " + joinType + " of type " + StrolchValueType.STRING
|
|
|
|
.getType() + " on dependency " + dependency.getLocator());
|
2017-03-20 19:27:59 +01:00
|
|
|
}
|
|
|
|
if (relationParams.size() > 1) {
|
2017-10-18 13:11:39 +02:00
|
|
|
throw new IllegalStateException(
|
|
|
|
"Found multiple possible relation parameters for UOM " + joinType + " on dependency " + dependency
|
|
|
|
.getLocator());
|
2017-03-20 19:27:59 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 15:04:55 +02:00
|
|
|
Parameter<?> relationParam = relationParams.get(0);
|
|
|
|
StringParameter relationP = (StringParameter) relationParam;
|
2017-03-20 19:27:59 +01:00
|
|
|
if (relationP.getValue().isEmpty() && optional) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Locator locator = Locator.valueOf(elementType, joinType, relationP.getValue());
|
2017-10-18 13:11:39 +02:00
|
|
|
StrolchRootElement joinElem = tx().findElement(locator, true);
|
2017-05-03 15:32:20 +02:00
|
|
|
if (joinElem == null)
|
|
|
|
return null;
|
2017-03-20 19:27:59 +01:00
|
|
|
|
|
|
|
refs.put(joinType, joinElem);
|
|
|
|
return joinElem;
|
|
|
|
}
|
|
|
|
}
|