1 package org.simantics.district.network.techtype.requests;
3 import java.util.Arrays;
4 import java.util.Collections;
7 import java.util.stream.Collectors;
8 import java.util.stream.Stream;
10 import org.simantics.databoard.Bindings;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
14 import org.simantics.db.common.request.BinaryRead;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.district.network.ontology.DistrictNetworkResource;
17 import org.simantics.district.network.techtype.TechTypeUtils;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Get a map of tech type table records, indexed by the key column.
24 * The query result value is list of records in the form of a map from property name
27 * The second constructor argument allows selection of enabled records only.
31 public class TechTypeTableData extends BinaryRead<Resource, Boolean, Map<String, Map<String, String>>> {
33 final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableData.class);
35 public TechTypeTableData(Resource table) {
36 super(table, Boolean.FALSE);
39 public TechTypeTableData(Resource table, boolean enabledOnly) {
40 super(table, enabledOnly);
44 public Map<String, Map<String, String>> perform(ReadGraph graph) throws DatabaseException {
45 Resource table = this.parameter;
46 boolean enabledOnly = this.parameter2;
48 String keyName = getKeyName(graph, table);
50 List<Map<String, String>> records = graph.syncRequest(new TechTypeTableRecords(table), TransientCacheListener.instance());
51 if (records == null || records.size() < 2)
52 return Collections.emptyMap();
54 Stream<Map<String, String>> enabled;
56 int[] enabledKeys = graph.getRelatedValue2(table, DistrictNetworkResource.getInstance(graph).TechType_TechTypeTable_HasEnabledItems, Bindings.INT_ARRAY);
57 enabled = Arrays.stream(enabledKeys)
58 .filter(i -> i >= 0 && i < records.size())
59 .mapToObj(i -> records.get(i));
61 enabled = records.stream();
65 .filter(r -> r.containsKey(keyName))
66 .collect(Collectors.toMap(r -> r.get(keyName), r -> r));
69 private String getKeyName(ReadGraph graph, Resource table) throws DatabaseException {
70 String keyName = TechTypeUtils.getKeyPropertyName(graph, table);
71 if (keyName.startsWith("_"))
72 keyName = keyName.substring(1);