[New] Added filtering to GenericReport

This commit is contained in:
Robert von Burg 2017-03-21 07:51:13 +01:00
parent 30ad0fcaa8
commit b0e57d6425
2 changed files with 88 additions and 5 deletions

View File

@ -1,5 +1,7 @@
package li.strolch.report;
import static li.strolch.utils.helper.StringHelper.DASH;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.HashMap;
import java.util.List;
@ -18,7 +20,7 @@ import li.strolch.model.parameter.Parameter;
import li.strolch.model.parameter.StringParameter;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.runtime.StrolchConstants;
import static li.strolch.utils.helper.StringHelper.*;
import li.strolch.utils.collections.MapOfSets;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -33,9 +35,11 @@ public class GenericReport {
private static final String COL_NAME = "$name";
private static final String BAG_COLUMNS = "columns";
private static final String TYPE_REPORT = "Report";
// input
private StrolchTransaction tx;
private String reportId;
private MapOfSets<String, String> filtersByType;
// intermediate
private Resource report;
@ -44,6 +48,21 @@ public class GenericReport {
public GenericReport(StrolchTransaction tx, String reportId) {
this.tx = tx;
this.reportId = reportId;
this.filtersByType = new MapOfSets<>();
}
public GenericReport filter(String type, String... ids) {
for (String id : ids) {
this.filtersByType.addElement(type, id);
}
return this;
}
public GenericReport filter(String type, List<String> ids) {
for (String id : ids) {
this.filtersByType.addElement(type, id);
}
return this;
}
private Stream<Map<String, StrolchRootElement>> doReport() {
@ -54,7 +73,26 @@ public class GenericReport {
this.columnsBag = this.report.getParameterBag(BAG_COLUMNS);
// query the main objects and return a stream
return queryRows().map(e -> evaluateRow(e));
Stream<Map<String, StrolchRootElement>> stream = queryRows().map(e -> evaluateRow(e));
if (!this.filtersByType.isEmpty())
stream = stream.filter(e -> filter(e));
return stream;
}
private boolean filter(Map<String, StrolchRootElement> row) {
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;
}
return true;
}
public Stream<JsonObject> doReportAsJson() {
@ -134,7 +172,7 @@ public class GenericReport {
}
}
private HashMap<String, StrolchRootElement> evaluateRow(StrolchRootElement resource) {
private Map<String, StrolchRootElement> evaluateRow(StrolchRootElement resource) {
// interpretation -> Resource-Ref, etc.
// uom -> object type
@ -154,7 +192,7 @@ public class GenericReport {
return refs;
}
private StrolchRootElement addColumnJoin(HashMap<String, StrolchRootElement> refs, ParameterBag joinBag,
private StrolchRootElement addColumnJoin(Map<String, StrolchRootElement> refs, ParameterBag joinBag,
StringParameter joinP, boolean optional) {
String elementType = joinP.getInterpretation().substring(0, joinP.getInterpretation().indexOf(SUFFIX_REF));

View File

@ -1,6 +1,7 @@
package li.strolch.report;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@ -32,7 +33,7 @@ public class GenericReportTest {
}
@Test
public void test() {
public void shouldGenerateJsonReport() {
try (StrolchTransaction tx = runtimeMock.openUserTx(certificate)) {
@ -74,6 +75,50 @@ public class GenericReportTest {
assertEquals("Section 002", e.get("section").getAsString());
assertEquals("Storage 02", e.get("storage").getAsString());
assertEquals("Location 02", e.get("location").getAsString());
} else {
fail("Unhandled result element: \n" + e.toString());
}
});
}
}
@Test
public void shouldFilterReport1() {
try (StrolchTransaction tx = runtimeMock.openUserTx(certificate)) {
GenericReport report = new GenericReport(tx, "stockReport");
report.filter("Product", "product01").doReportAsJson().forEach(e -> {
String slotName = e.get("slot").getAsString();
switch (slotName) {
case "Slot 1":
case "Slot 3":
break;
default:
fail("Unexpected slot name " + slotName + ", should have been filtered!");
break;
}
});
}
}
@Test
public void shouldFilterReport2() {
try (StrolchTransaction tx = runtimeMock.openUserTx(certificate)) {
GenericReport report = new GenericReport(tx, "stockReport");
report.filter("Product", "product01").filter("Location", "location02").doReportAsJson().forEach(e -> {
String slotName = e.get("slot").getAsString();
switch (slotName) {
case "Slot 3":
break;
default:
fail("Unexpected slot name " + slotName + ", should have been filtered!");
break;
}
});
}