[New] Added new generic report creator

Create a Report as follows:

<Resource Id="stockReport" Name="Stock Report" Type="Report">

  <ParameterBag Id="parameters" Name="parameters" Type="Parameters">
    <Parameter Id="objectType" Name="Object Type" Type="String"
Interpretation="Resource-Ref" Uom="Slot" Value="Slot" />
  </ParameterBag>

  <ParameterBag Id="columns" Name="Display Columns" Type="Display">
    <Parameter Id="location" Name="Location" Type="String"
Interpretation="Resource-Ref" Uom="Location" Value="$name" />
    <Parameter Id="storage" Name="Storage" Type="String"
Interpretation="Resource-Ref" Uom="Storage" Value="$name" />
    <Parameter Id="section" Name="Section" Type="String"
Interpretation="Resource-Ref" Uom="Section" Value="$name" />
    <Parameter Id="slot" Name="Slot" Type="String"
Interpretation="Resource-Ref" Uom="Slot" Value="$name" />
    <Parameter Id="product" Name="Product" Type="String"
Interpretation="Resource-Ref" Uom="Product" Value="$name" />
    <Parameter Id="quantity" Name="Quantity" Type="String"
Interpretation="Resource-Ref" Uom="Slot"
Value="Bags/parameters/quantity" />
    <Parameter Id="maxQuantity" Name="Quantity" Type="String"
Interpretation="Resource-Ref" Uom="Slot"
Value="Bags/parameters/maxQuantity" />
  </ParameterBag>

  <ParameterBag Id="joins" Name="Joins" Type="Joins">
    <Parameter Id="Product" Name="Product" Type="String"
Interpretation="Resource-Ref" Uom="Product" Value="Slot" />
    <Parameter Id="Section" Name="Section" Type="String"
Interpretation="Resource-Ref" Uom="Section" Value="Slot" />
    <Parameter Id="Storage" Name="Storage" Type="String"
Interpretation="Resource-Ref" Uom="Storage" Value="Section" />
    <Parameter Id="Location" Name="Location" Type="String"
Interpretation="Resource-Ref" Uom="Location" Value="Storage" />
  </ParameterBag>

</Resource>

This creates a report for objects of type Slot, where a Join is done on
multiple objects:
  Slot
    -> Product
    -> Section
         -> Storage
               -> Location

Thus the columns can then be fetched from different joing objects. The
joing is done by searching for a Parameter on the joined object on the
ParameterBag "relations". See the example XML on how this is done.

Or ask eitch@eitchnet.ch to write a proper documentation =))

Filtering is missing, and will be added later
This commit is contained in:
Robert von Burg 2017-03-20 19:27:59 +01:00
parent 5fd8e7df8f
commit 30ad0fcaa8
9 changed files with 538 additions and 0 deletions

View File

@ -0,0 +1,204 @@
package li.strolch.report;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
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.*;
/**
* @author Robert von Burg &lt;eitch@eitchnet.ch&gt;
*/
public class GenericReport {
private static final String BAG_RELATIONS = "relations";
private static final String SUFFIX_REF = "-Ref";
private static final String BAG_JOINS = "joins";
private static final String PARAM_OBJECT_TYPE = "objectType";
private static final String BAG_PARAMETERS = "parameters";
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;
// intermediate
private Resource report;
private ParameterBag columnsBag;
public GenericReport(StrolchTransaction tx, String reportId) {
this.tx = tx;
this.reportId = reportId;
}
private Stream<Map<String, StrolchRootElement>> doReport() {
// get the report
this.report = this.tx.getResourceBy(TYPE_REPORT, this.reportId);
this.columnsBag = this.report.getParameterBag(BAG_COLUMNS);
// query the main objects and return a stream
return queryRows().map(e -> evaluateRow(e));
}
public Stream<JsonObject> doReportAsJson() {
// generate the stream and map to JsonObject
return doReport().map(row -> {
// 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;
} else if (columnDef.equals(COL_NAME)) {
columnValue = column.getName();
} else {
String[] locatorParts = columnDef.split(Locator.PATH_SEPARATOR);
if (locatorParts.length != 3)
throw new IllegalStateException(
"Column definition is invalid as column must either be $name, or a three part parameter locator ");
String bagKey = locatorParts[1];
String paramKey = locatorParts[2];
Parameter<?> param = column.getParameter(bagKey, paramKey);
columnValue = param.getValueAsString();
}
return new SimpleImmutableEntry<>(columnId, columnValue);
});
}
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());
}
}
private HashMap<String, StrolchRootElement> evaluateRow(StrolchRootElement resource) {
// 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;
}
private StrolchRootElement addColumnJoin(HashMap<String, StrolchRootElement> refs, ParameterBag joinBag,
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);
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) {
refs.put(joinType, null);
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;
}
}

View File

@ -0,0 +1,81 @@
package li.strolch.report;
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.privilege.model.Certificate;
import li.strolch.testbase.runtime.RuntimeMock;
public class GenericReportTest {
private static final String RUNTIME_PATH = "target/GenericReportTest/"; //$NON-NLS-1$
private static final String CONFIG_SRC = "src/test/resources/reporttest"; //$NON-NLS-1$
private static RuntimeMock runtimeMock;
private static Certificate certificate;
@BeforeClass
public static void beforeClass() {
runtimeMock = new RuntimeMock().mockRuntime(RUNTIME_PATH, CONFIG_SRC);
runtimeMock.startContainer();
certificate = runtimeMock.loginTest();
}
@AfterClass
public static void afterClass() {
runtimeMock.logout(certificate);
runtimeMock.destroyRuntime();
}
@Test
public void test() {
try (StrolchTransaction tx = runtimeMock.openUserTx(certificate)) {
GenericReport report = new GenericReport(tx, "stockReport");
report.doReportAsJson().forEach(e -> {
if (e.get("slot").getAsString().equals("Slot 1")) {
assertEquals("Product 01", e.get("product").getAsString());
assertEquals("20.0", e.get("quantity").getAsString());
assertEquals("40.0", e.get("maxQuantity").getAsString());
assertEquals("Section 001", e.get("section").getAsString());
assertEquals("Storage 01", e.get("storage").getAsString());
assertEquals("Location 01", e.get("location").getAsString());
} else if (e.get("slot").getAsString().equals("Slot 2")) {
assertEquals("Product 02", e.get("product").getAsString());
assertEquals("18.0", e.get("quantity").getAsString());
assertEquals("20.0", e.get("maxQuantity").getAsString());
assertEquals("Section 001", e.get("section").getAsString());
assertEquals("Storage 01", e.get("storage").getAsString());
assertEquals("Location 01", e.get("location").getAsString());
} else if (e.get("slot").getAsString().equals("Slot 3")) {
assertEquals("Product 01", e.get("product").getAsString());
assertEquals("11.0", e.get("quantity").getAsString());
assertEquals("40.0", e.get("maxQuantity").getAsString());
assertEquals("Section 002", e.get("section").getAsString());
assertEquals("Storage 02", e.get("storage").getAsString());
assertEquals("Location 02", e.get("location").getAsString());
} else if (e.get("slot").getAsString().equals("Slot 4")) {
assertEquals("Product 02", e.get("product").getAsString());
assertEquals("16.0", e.get("quantity").getAsString());
assertEquals("20.0", e.get("maxQuantity").getAsString());
assertEquals("Section 002", e.get("section").getAsString());
assertEquals("Storage 02", e.get("storage").getAsString());
assertEquals("Location 02", e.get("location").getAsString());
}
});
}
}
}

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<Privilege>
<Container>
<Parameters>
<!-- parameters for the container itself -->
<Parameter name="autoPersistOnUserChangesData" value="true" />
</Parameters>
<EncryptionHandler class="li.strolch.privilege.handler.DefaultEncryptionHandler">
<Parameters>
<Parameter name="hashAlgorithm" value="SHA-256" />
</Parameters>
</EncryptionHandler>
<PersistenceHandler class="li.strolch.privilege.handler.XmlPersistenceHandler">
<Parameters>
<Parameter name="usersXmlFile" value="PrivilegeUsers.xml" />
<Parameter name="rolesXmlFile" value="PrivilegeRoles.xml" />
</Parameters>
</PersistenceHandler>
<UserChallengeHandler class="li.strolch.privilege.handler.ConsoleUserChallengeHandler">
</UserChallengeHandler>
</Container>
<Policies>
<Policy name="DefaultPrivilege" class="li.strolch.privilege.policy.DefaultPrivilege" />
<Policy name="RoleAccessPrivilege" class="li.strolch.privilege.policy.RoleAccessPrivilege" />
<Policy name="UserAccessPrivilege" class="li.strolch.privilege.policy.UserAccessPrivilege" />
</Policies>
</Privilege>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<Roles>
<Role name="agent">
<Privilege name="li.strolch.privilege.handler.SystemAction" policy="DefaultPrivilege">
<Allow>li.strolch.runtime.privilege.StrolchSystemAction</Allow>
<Allow>li.strolch.runtime.privilege.StrolchSystemActionWithResult</Allow>
<Allow>li.strolch.persistence.postgresql.PostgreSqlSchemaInitializer</Allow>
</Privilege>
<Privilege name="li.strolch.service.api.Service" policy="DefaultPrivilege">
<AllAllowed>true</AllAllowed>
</Privilege>
<Privilege name="li.strolch.model.query.StrolchQuery" policy="DefaultPrivilege">
<AllAllowed>true</AllAllowed>
</Privilege>
</Role>
<Role name="AppUser">
<Privilege name="li.strolch.service.api.Service" policy="DefaultPrivilege">
<AllAllowed>true</AllAllowed>
</Privilege>
<Privilege name="li.strolch.model.query.StrolchQuery" policy="DefaultPrivilege">
<AllAllowed>true</AllAllowed>
</Privilege>
</Role>
</Roles>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<Users>
<User userId="1" username="agent">
<State>SYSTEM</State>
<Roles>
<Role>agent</Role>
</Roles>
</User>
<User userId="3" username="test" password="9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08">
<Firstname>Application</Firstname>
<Lastname>Administrator</Lastname>
<State>ENABLED</State>
<Locale>en_GB</Locale>
<Roles>
<Role>AppUser</Role>
</Roles>
<Properties>
<Property name="realm" value="execution" />
</Properties>
</User>
</Users>

View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<StrolchConfiguration>
<env id="dev">
<Runtime>
<applicationName>StrolchRuntimeTest</applicationName>
<Properties>
<verbose>true</verbose>
</Properties>
</Runtime>
<Component>
<name>PrivilegeHandler</name>
<api>li.strolch.runtime.privilege.PrivilegeHandler</api>
<impl>li.strolch.runtime.privilege.DefaultStrolchPrivilegeHandler</impl>
<Properties>
<privilegeConfigFile>PrivilegeConfig.xml</privilegeConfigFile>
</Properties>
</Component>
<Component>
<name>RealmHandler</name>
<api>li.strolch.agent.api.RealmHandler</api>
<impl>li.strolch.agent.impl.DefaultRealmHandler</impl>
<depends>PrivilegeHandler</depends>
<Properties>
<realms>execution</realms>
<tryLockTimeUnit.execution>SECONDS</tryLockTimeUnit.execution>
<tryLockTime.execution>1</tryLockTime.execution>
<dataStoreMode.execution>TRANSIENT</dataStoreMode.execution>
<dataStoreFile.execution>StrolchModel.xml</dataStoreFile.execution>
</Properties>
</Component>
<Component>
<name>ServiceHandler</name>
<api>li.strolch.service.api.ServiceHandler</api>
<impl>li.strolch.service.api.DefaultServiceHandler</impl>
<Properties>
<verbose>true</verbose>
</Properties>
</Component>
<Component>
<name>PolicyHandler</name>
<api>li.strolch.policy.PolicyHandler</api>
<impl>li.strolch.policy.DefaultPolicyHandler</impl>
<Properties>
<readPolicyFile>true</readPolicyFile>
<policyConfigFile>StrolchPolicies.xml</policyConfigFile>
</Properties>
</Component>
<Component>
<name>ExecutionHandler</name>
<api>li.strolch.execution.ExecutionHandler</api>
<impl>li.strolch.execution.EventBasedExecutionHandler</impl>
</Component>
</env>
</StrolchConfiguration>

View File

@ -0,0 +1,3 @@
<StrolchPolicies>
</StrolchPolicies>

View File

@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<StrolchModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://strolch.li/xsd/StrolchModel-1.4.xsd">
<Resource Id="stockReport" Name="Stock Report" Type="Report">
<ParameterBag Id="parameters" Name="parameters" Type="Parameters">
<Parameter Id="objectType" Name="Object Type" Type="String" Interpretation="Resource-Ref" Uom="Slot" Value="Slot" />
</ParameterBag>
<ParameterBag Id="columns" Name="Display Columns" Type="Display">
<Parameter Id="location" Name="Location" Type="String" Interpretation="Resource-Ref" Uom="Location" Value="$name" />
<Parameter Id="storage" Name="Storage" Type="String" Interpretation="Resource-Ref" Uom="Storage" Value="$name" />
<Parameter Id="section" Name="Section" Type="String" Interpretation="Resource-Ref" Uom="Section" Value="$name" />
<Parameter Id="slot" Name="Slot" Type="String" Interpretation="Resource-Ref" Uom="Slot" Value="$name" />
<Parameter Id="product" Name="Product" Type="String" Interpretation="Resource-Ref" Uom="Product" Value="$name" />
<Parameter Id="quantity" Name="Quantity" Type="String" Interpretation="Resource-Ref" Uom="Slot" Value="Bags/parameters/quantity" />
<Parameter Id="maxQuantity" Name="Quantity" Type="String" Interpretation="Resource-Ref" Uom="Slot" Value="Bags/parameters/maxQuantity" />
</ParameterBag>
<ParameterBag Id="joins" Name="Joins" Type="Joins">
<Parameter Id="Product" Name="Product" Type="String" Interpretation="Resource-Ref" Uom="Product" Value="Slot" />
<Parameter Id="Section" Name="Section" Type="String" Interpretation="Resource-Ref" Uom="Section" Value="Slot" />
<Parameter Id="Storage" Name="Storage" Type="String" Interpretation="Resource-Ref" Uom="Storage" Value="Section" />
<Parameter Id="Location" Name="Location" Type="String" Interpretation="Resource-Ref" Uom="Location" Value="Storage" />
</ParameterBag>
</Resource>
<Resource Id="product01" Name="Product 01" Type="Product">
</Resource>
<Resource Id="product02" Name="Product 02" Type="Product">
</Resource>
<Resource Id="location01" Name="Location 01" Type="Location">
<ParameterBag Id="parameters" Name="Parameters" Type="Parameters">
<Parameter Id="description" Name="Description" Type="String" Value="Just a location" />
</ParameterBag>
</Resource>
<Resource Id="location02" Name="Location 02" Type="Location">
<ParameterBag Id="parameters" Name="Parameters" Type="Parameters">
<Parameter Id="description" Name="Description" Type="String" Value="Just a location" />
</ParameterBag>
</Resource>
<Resource Id="storage01" Name="Storage 01" Type="Storage">
<ParameterBag Id="relations" Name="Relations" Type="Parameters">
<Parameter Id="location" Name="Location" Type="String" Interpretation="Resource-Ref" Uom="Location" Value="location01" />
</ParameterBag>
</Resource>
<Resource Id="storage02" Name="Storage 02" Type="Storage">
<ParameterBag Id="relations" Name="Relations" Type="Parameters">
<Parameter Id="location" Name="Location" Type="String" Interpretation="Resource-Ref" Uom="Location" Value="location02" />
</ParameterBag>
</Resource>
<Resource Id="section001" Name="Section 001" Type="Section">
<ParameterBag Id="relations" Name="Relations" Type="Parameters">
<Parameter Id="parentStorage" Name="Storage" Type="String" Interpretation="Resource-Ref" Uom="Storage" Value="storage01" />
</ParameterBag>
</Resource>
<Resource Id="section002" Name="Section 002" Type="Section">
<ParameterBag Id="relations" Name="Relations" Type="Parameters">
<Parameter Id="parentStorage" Name="Storage" Type="String" Interpretation="Resource-Ref" Uom="Storage" Value="storage02" />
</ParameterBag>
</Resource>
<Resource Id="slot001" Name="Slot 1" Type="Slot">
<ParameterBag Id="parameters" Name="Parameters" Type="Parameters">
<Parameter Id="quantity" Name="Quantity" Type="Float" Value="20.0" />
<Parameter Id="maxQuantity" Name="max. quantity of items" Type="Float" Value="40.0" />
</ParameterBag>
<ParameterBag Id="relations" Name="Relations" Type="Parameters">
<Parameter Id="parentSection" Name="Section" Type="String" Interpretation="Resource-Ref" Uom="Section" Value="section001" />
<Parameter Id="product" Name="Product" Type="String" Interpretation="Resource-Ref" Uom="Product" Value="product01" />
</ParameterBag>
</Resource>
<Resource Id="slot002" Name="Slot 2" Type="Slot">
<ParameterBag Id="parameters" Name="Parameters" Type="Parameters">
<Parameter Id="quantity" Name="Quantity" Type="Float" Value="18.0" />
<Parameter Id="maxQuantity" Name="max. quantity of items" Type="Float" Value="20.0" />
</ParameterBag>
<ParameterBag Id="relations" Name="Relations" Type="Parameters">
<Parameter Id="parentSection" Name="Section" Type="String" Interpretation="Resource-Ref" Uom="Section" Value="section001" />
<Parameter Id="product" Name="Product" Type="String" Interpretation="Resource-Ref" Uom="Product" Value="product02" />
</ParameterBag>
</Resource>
<Resource Id="slot003" Name="Slot 3" Type="Slot">
<ParameterBag Id="parameters" Name="Parameters" Type="Parameters">
<Parameter Id="quantity" Name="Quantity" Type="Float" Value="11.0" />
<Parameter Id="maxQuantity" Name="max. quantity of items" Type="Float" Value="40.0" />
</ParameterBag>
<ParameterBag Id="relations" Name="Relations" Type="Parameters">
<Parameter Id="parentSection" Name="Section" Type="String" Interpretation="Resource-Ref" Uom="Section" Value="section002" />
<Parameter Id="product" Name="Product" Type="String" Interpretation="Resource-Ref" Uom="Product" Value="product01" />
</ParameterBag>
</Resource>
<Resource Id="slot004" Name="Slot 4" Type="Slot">
<ParameterBag Id="parameters" Name="Parameters" Type="Parameters">
<Parameter Id="quantity" Name="Quantity" Type="Float" Value="16.0" />
<Parameter Id="maxQuantity" Name="max. quantity of items" Type="Float" Value="20.0" />
</ParameterBag>
<ParameterBag Id="relations" Name="Relations" Type="Parameters">
<Parameter Id="parentSection" Name="Section" Type="String" Interpretation="Resource-Ref" Uom="Section" Value="section002" />
<Parameter Id="product" Name="Product" Type="String" Interpretation="Resource-Ref" Uom="Product" Value="product02" />
</ParameterBag>
</Resource>
</StrolchModel>

View File

@ -0,0 +1 @@
/sessions.dat