2017-03-20 19:27:59 +01:00
|
|
|
package li.strolch.report;
|
|
|
|
|
2017-03-21 07:51:13 +01:00
|
|
|
import static li.strolch.utils.helper.StringHelper.DASH;
|
|
|
|
|
2017-03-20 19:27:59 +01:00
|
|
|
import java.util.AbstractMap.SimpleImmutableEntry;
|
2017-03-21 15:34:10 +01:00
|
|
|
import java.util.Date;
|
2017-03-20 19:27:59 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2017-03-24 11:25:56 +01:00
|
|
|
import java.util.Set;
|
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;
|
|
|
|
import java.util.stream.StreamSupport;
|
|
|
|
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
|
|
|
|
import li.strolch.model.Locator;
|
|
|
|
import li.strolch.model.ParameterBag;
|
|
|
|
import li.strolch.model.Resource;
|
|
|
|
import li.strolch.model.StrolchRootElement;
|
2017-03-21 15:34:10 +01:00
|
|
|
import li.strolch.model.StrolchValueType;
|
|
|
|
import li.strolch.model.parameter.DateParameter;
|
2017-03-20 19:27:59 +01:00
|
|
|
import li.strolch.model.parameter.Parameter;
|
|
|
|
import li.strolch.model.parameter.StringParameter;
|
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;
|
|
|
|
import li.strolch.runtime.StrolchConstants;
|
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>
|
|
|
|
*/
|
|
|
|
public class GenericReport {
|
|
|
|
|
2017-03-21 15:34:10 +01:00
|
|
|
private static final String TYPE_REPORT = "Report";
|
|
|
|
|
2017-03-20 19:27:59 +01:00
|
|
|
private static final String BAG_RELATIONS = "relations";
|
|
|
|
private static final String BAG_JOINS = "joins";
|
|
|
|
private static final String BAG_PARAMETERS = "parameters";
|
|
|
|
private static final String BAG_COLUMNS = "columns";
|
2017-03-21 15:34:10 +01:00
|
|
|
|
|
|
|
private static final String PARAM_OBJECT_TYPE = "objectType";
|
|
|
|
private static final String PARAM_DATE_RANGE_SEL = "dateRangeSel";
|
|
|
|
|
|
|
|
private static final String COL_ID = "$id";
|
|
|
|
private static final String COL_NAME = "$name";
|
|
|
|
private static final String COL_TYPE = "$type";
|
|
|
|
private static final String COL_STATE = "$state";
|
|
|
|
private static final String COL_DATE = "$date";
|
2017-03-22 14:55:54 +01:00
|
|
|
private static final String COL_SEARCH = "$search";
|
|
|
|
|
|
|
|
private static final String SEARCH_SEPARATOR = ":";
|
2017-03-21 15:34:10 +01:00
|
|
|
|
|
|
|
private static final String SUFFIX_REF = "-Ref";
|
2017-03-21 07:51:13 +01:00
|
|
|
|
2017-03-20 19:27:59 +01:00
|
|
|
// input
|
|
|
|
private StrolchTransaction tx;
|
|
|
|
private String reportId;
|
|
|
|
|
|
|
|
private Resource report;
|
|
|
|
private ParameterBag columnsBag;
|
2017-03-21 15:34:10 +01:00
|
|
|
private StringParameter dateRangeSelP;
|
|
|
|
|
|
|
|
private DateRange dateRange;
|
|
|
|
private MapOfSets<String, String> filtersByType;
|
2017-03-20 19:27:59 +01:00
|
|
|
|
|
|
|
public GenericReport(StrolchTransaction tx, String reportId) {
|
|
|
|
this.tx = tx;
|
|
|
|
this.reportId = reportId;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public GenericReport filter(String type, String... ids) {
|
2017-03-21 15:34:10 +01:00
|
|
|
if (this.filtersByType == null)
|
|
|
|
this.filtersByType = new MapOfSets<>();
|
2017-03-21 07:51:13 +01:00
|
|
|
for (String id : ids) {
|
|
|
|
this.filtersByType.addElement(type, id);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GenericReport filter(String type, List<String> ids) {
|
2017-03-21 15:34:10 +01:00
|
|
|
if (this.filtersByType == null)
|
|
|
|
this.filtersByType = new MapOfSets<>();
|
2017-03-21 07:51:13 +01:00
|
|
|
for (String id : ids) {
|
|
|
|
this.filtersByType.addElement(type, id);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
if (this.filtersByType == null)
|
|
|
|
this.filtersByType = new MapOfSets<>();
|
|
|
|
for (String id : ids) {
|
|
|
|
this.filtersByType.addElement(type, id);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-03-21 08:23:29 +01:00
|
|
|
public Stream<Map<String, StrolchRootElement>> buildStream() {
|
2017-03-20 19:27:59 +01:00
|
|
|
|
|
|
|
// get the report
|
|
|
|
this.report = this.tx.getResourceBy(TYPE_REPORT, this.reportId);
|
|
|
|
this.columnsBag = this.report.getParameterBag(BAG_COLUMNS);
|
2017-03-21 15:34:10 +01:00
|
|
|
this.dateRangeSelP = this.report.getParameter(BAG_PARAMETERS, PARAM_DATE_RANGE_SEL);
|
2017-03-20 19:27:59 +01:00
|
|
|
|
|
|
|
// query the main objects and return a stream
|
2017-03-21 15:34:10 +01:00
|
|
|
return queryRows().map(e -> evaluateRow(e)).filter(e -> filter(e));
|
2017-03-21 07:51:13 +01:00
|
|
|
}
|
|
|
|
|
2017-03-21 15:34:10 +01:00
|
|
|
public MapOfSets<String, StrolchRootElement> generateFilterCriteria() {
|
2017-03-21 08:23:29 +01:00
|
|
|
return buildStream() //
|
|
|
|
.flatMap(e -> e.values().stream()) //
|
|
|
|
.collect( //
|
|
|
|
Collector.of( //
|
2017-03-21 15:34:10 +01:00
|
|
|
() -> new MapOfSets<String, StrolchRootElement>(), //
|
|
|
|
(m, e) -> m.addElement(e.getType(), e), //
|
2017-03-21 08:23:29 +01:00
|
|
|
(m1, m2) -> m1, //
|
|
|
|
m -> m));
|
|
|
|
}
|
|
|
|
|
2017-03-21 07:51:13 +01:00
|
|
|
private boolean filter(Map<String, StrolchRootElement> row) {
|
|
|
|
|
2017-03-21 15:34:10 +01:00
|
|
|
// first we do a date range selection, if required
|
|
|
|
if (this.dateRange != null) {
|
|
|
|
if (this.dateRangeSelP == null)
|
|
|
|
throw new IllegalStateException(
|
|
|
|
"DateRange defined, but report does not defined a date range selector!");
|
|
|
|
|
|
|
|
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-03-22 14:55:54 +01:00
|
|
|
Parameter<?> param = lookupParameter(this.dateRangeSelP, element);
|
2017-03-21 15:34:10 +01:00
|
|
|
if (StrolchValueType.parse(param.getType()) != StrolchValueType.DATE)
|
|
|
|
throw new IllegalStateException(
|
|
|
|
"Date Range selector is invalid, as referenced parameter is not a Date but a "
|
|
|
|
+ param.getType());
|
|
|
|
|
|
|
|
date = ((DateParameter) param).getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if (this.filtersByType != null && !this.filtersByType.isEmpty()) {
|
|
|
|
for (String type : this.filtersByType.keySet()) {
|
|
|
|
StrolchRootElement element = row.get(type);
|
|
|
|
if (element == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!this.filtersByType.getSet(type).contains(element.getId()))
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public Stream<JsonObject> doReportAsJson() {
|
|
|
|
|
|
|
|
// generate the stream and map to JsonObject
|
2017-03-21 08:23:29 +01:00
|
|
|
return buildStream().map(row -> {
|
2017-03-20 19:27:59 +01:00
|
|
|
|
|
|
|
// new json object
|
|
|
|
JsonObject jsonObject = new JsonObject();
|
|
|
|
|
|
|
|
// add columns
|
|
|
|
evaluateColumns(row).forEach(e -> jsonObject.addProperty(e.getKey(), e.getValue()));
|
|
|
|
|
|
|
|
return jsonObject;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private Stream<SimpleImmutableEntry<String, String>> evaluateColumns(Map<String, StrolchRootElement> row) {
|
|
|
|
|
|
|
|
// get iterator to columns
|
|
|
|
Iterable<Parameter<?>> iterable = () -> this.columnsBag.getParameters().iterator();
|
|
|
|
|
|
|
|
// generate a stream
|
|
|
|
return StreamSupport.stream(iterable.spliterator(), false).map(p -> {
|
|
|
|
|
|
|
|
// create the column id/value pair
|
|
|
|
|
|
|
|
StringParameter columnDefP = (StringParameter) p;
|
|
|
|
|
|
|
|
String columnId = columnDefP.getId();
|
|
|
|
String refType = columnDefP.getUom();
|
|
|
|
String columnDef = columnDefP.getValue();
|
|
|
|
|
|
|
|
// get the referenced object
|
|
|
|
StrolchRootElement column = row.get(refType);
|
|
|
|
|
|
|
|
String columnValue;
|
|
|
|
if (column == null) {
|
|
|
|
columnValue = DASH;
|
2017-03-21 15:34:10 +01:00
|
|
|
} else if (columnDef.equals(COL_ID)) {
|
|
|
|
columnValue = column.getId();
|
2017-03-20 19:27:59 +01:00
|
|
|
} else if (columnDef.equals(COL_NAME)) {
|
|
|
|
columnValue = column.getName();
|
2017-03-21 15:34:10 +01:00
|
|
|
} else if (columnDef.equals(COL_TYPE)) {
|
|
|
|
columnValue = column.getType();
|
|
|
|
} else if (columnDef.equals(COL_STATE)) {
|
|
|
|
columnValue = column.accept(new ElementStateVisitor()).name();
|
|
|
|
} else if (columnDef.equals(COL_DATE)) {
|
|
|
|
columnValue = ISO8601FormatFactory.getInstance().formatDate(column.accept(new ElementDateVisitor()));
|
2017-03-22 14:55:54 +01:00
|
|
|
} else if (columnDef.startsWith(COL_SEARCH)) {
|
|
|
|
Parameter<?> parameter = findParameter(columnDefP, column);
|
|
|
|
if (parameter == null)
|
|
|
|
columnValue = DASH;
|
|
|
|
else
|
|
|
|
columnValue = parameter.getValueAsString();
|
2017-03-20 19:27:59 +01:00
|
|
|
} else {
|
2017-03-22 14:55:54 +01:00
|
|
|
columnValue = lookupParameter(columnDefP, column).getValueAsString();
|
2017-03-20 19:27:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return new SimpleImmutableEntry<>(columnId, columnValue);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:55:54 +01:00
|
|
|
private Parameter<?> findParameter(StringParameter columnDefP, StrolchRootElement column) {
|
|
|
|
|
|
|
|
String columnDef = columnDefP.getValue();
|
|
|
|
|
|
|
|
String[] searchParts = columnDef.split(SEARCH_SEPARATOR);
|
|
|
|
if (searchParts.length != 3)
|
|
|
|
throw new IllegalStateException("Parameter search reference (" + columnDef
|
|
|
|
+ ") is invalid as it does not have 3 parts for " + columnDefP.getLocator());
|
|
|
|
|
|
|
|
String parentParamId = searchParts[1];
|
|
|
|
String paramRef = searchParts[2];
|
|
|
|
|
|
|
|
String[] locatorParts = paramRef.split(Locator.PATH_SEPARATOR);
|
|
|
|
if (locatorParts.length != 3)
|
|
|
|
throw new IllegalStateException("Parameter search reference (" + paramRef
|
|
|
|
+ ") is invalid as it does not have 3 parts for " + columnDefP.getLocator());
|
|
|
|
|
|
|
|
String bagKey = locatorParts[1];
|
|
|
|
String paramKey = locatorParts[2];
|
|
|
|
|
|
|
|
return findParameter(column, parentParamId, bagKey, paramKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Parameter<?> findParameter(StrolchRootElement element, String parentParamId, String bagKey,
|
|
|
|
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;
|
|
|
|
|
|
|
|
Resource parent = this.tx.getResourceBy(parentRefP);
|
|
|
|
if (parent == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return findParameter(parent, parentParamId, bagKey, paramKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
private 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)
|
|
|
|
throw new IllegalStateException("Parameter reference (" + paramRef
|
|
|
|
+ ") is invalid as it does not have 3 parts for " + paramRefP.getLocator());
|
|
|
|
|
|
|
|
String bagKey = locatorParts[1];
|
|
|
|
String paramKey = locatorParts[2];
|
|
|
|
|
|
|
|
Parameter<?> param = column.getParameter(bagKey, paramKey);
|
|
|
|
if (param == null)
|
|
|
|
throw new IllegalStateException("Parameter reference (" + paramRef + ") for " + paramRefP.getLocator()
|
|
|
|
+ " not found on " + column.getLocator());
|
|
|
|
|
|
|
|
return param;
|
|
|
|
}
|
|
|
|
|
2017-03-20 19:27:59 +01:00
|
|
|
private Stream<StrolchRootElement> queryRows() {
|
|
|
|
|
|
|
|
// find the type of object for which the report is created
|
|
|
|
StringParameter objectTypeP = this.report.getParameter(BAG_PARAMETERS, PARAM_OBJECT_TYPE);
|
|
|
|
|
|
|
|
if (objectTypeP.getInterpretation().equals(StrolchConstants.INTERPRETATION_RESOURCE_REF)) {
|
|
|
|
|
|
|
|
return this.tx.getResourceMap().getElementsBy(this.tx, objectTypeP.getUom()).stream()
|
|
|
|
.map(e -> e.getRootElement());
|
|
|
|
|
|
|
|
} else if (objectTypeP.getInterpretation().equals(StrolchConstants.INTERPRETATION_ORDER_REF)) {
|
|
|
|
|
|
|
|
return this.tx.getOrderMap().getElementsBy(this.tx, objectTypeP.getUom()).stream()
|
|
|
|
.map(e -> e.getRootElement());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Unhandled element type " + objectTypeP.getInterpretation());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 07:51:13 +01:00
|
|
|
private 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
|
|
|
|
ParameterBag joinBag = this.report.getParameterBag(BAG_JOINS);
|
|
|
|
|
|
|
|
// create the refs element
|
|
|
|
HashMap<String, StrolchRootElement> refs = new HashMap<>();
|
|
|
|
// and add the starting point
|
|
|
|
refs.put(resource.getType(), resource);
|
|
|
|
|
|
|
|
for (String paramId : joinBag.getParameterKeySet()) {
|
|
|
|
StringParameter joinP = joinBag.getParameter(paramId);
|
|
|
|
addColumnJoin(refs, joinBag, joinP, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return refs;
|
|
|
|
}
|
|
|
|
|
2017-03-21 07:51:13 +01:00
|
|
|
private StrolchRootElement addColumnJoin(Map<String, StrolchRootElement> refs, ParameterBag joinBag,
|
2017-03-20 19:27:59 +01:00
|
|
|
StringParameter joinP, boolean optional) {
|
|
|
|
|
|
|
|
String elementType = joinP.getInterpretation().substring(0, joinP.getInterpretation().indexOf(SUFFIX_REF));
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
ParameterBag relationsBag = dependency.getParameterBag(BAG_RELATIONS);
|
2017-03-21 15:34:10 +01:00
|
|
|
if (relationsBag == null)
|
|
|
|
throw new IllegalStateException("Invalid join definition value: " + joinP.getValue() + " on: "
|
|
|
|
+ joinP.getLocator() + " as " + dependency.getLocator() + " has no ParameterBag " + BAG_RELATIONS);
|
|
|
|
|
2017-03-20 19:27:59 +01:00
|
|
|
List<Parameter<?>> relationParams = relationsBag.getParameters().stream()
|
|
|
|
.filter(p -> p.getUom().equals(joinType)).collect(Collectors.toList());
|
|
|
|
if (relationParams.isEmpty()) {
|
|
|
|
throw new IllegalStateException(
|
|
|
|
"Found no relation parameters with UOM " + joinType + " on dependency " + dependency.getLocator());
|
|
|
|
}
|
|
|
|
if (relationParams.size() > 1) {
|
|
|
|
throw new IllegalStateException("Found multiple possible relation parameters for UOM " + joinType
|
|
|
|
+ " on dependency " + dependency.getLocator());
|
|
|
|
}
|
|
|
|
|
|
|
|
StringParameter relationP = (StringParameter) relationParams.get(0);
|
|
|
|
if (relationP.getValue().isEmpty() && optional) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Locator locator = Locator.valueOf(elementType, joinType, relationP.getValue());
|
|
|
|
StrolchRootElement joinElem;
|
|
|
|
try {
|
|
|
|
joinElem = this.tx.findElement(locator);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new IllegalStateException("Failed to find join element " + joinType + " for dependency "
|
|
|
|
+ dependency.getLocator() + " with locator " + locator);
|
|
|
|
}
|
|
|
|
|
|
|
|
refs.put(joinType, joinElem);
|
|
|
|
return joinElem;
|
|
|
|
}
|
|
|
|
}
|