]> gerrit.simantics Code Review - simantics/district.git/blob
60929eddc999c56aad719616ba81bc21f90ade44
[simantics/district.git] /
1 package org.simantics.district.network.techtype.requests;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.stream.Collectors;
8 import java.util.stream.Stream;
9
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;
20
21 /**
22  * Get a map of tech type table records, indexed by the key column.
23  *
24  * The query result value is list of records in the form of a map from property name
25  * to string value.
26  * 
27  * The second constructor argument allows selection of enabled records only.
28  *
29  * @author Reino Ruusu
30  */
31 public class TechTypeTableData extends BinaryRead<Resource, Boolean, Map<String, Map<String, String>>> {
32
33         final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableData.class);
34         
35         public TechTypeTableData(Resource table) {
36                 super(table, Boolean.FALSE);
37         }
38         
39         public TechTypeTableData(Resource table, boolean enabledOnly) {
40                 super(table, enabledOnly);
41         }
42
43         @Override
44         public Map<String, Map<String, String>> perform(ReadGraph graph) throws DatabaseException {
45                 Resource table = this.parameter;
46                 boolean enabledOnly = this.parameter2;
47
48                 String keyName = getKeyName(graph, table);
49                 
50                 List<Map<String, String>> records = graph.syncRequest(new TechTypeTableRecords(table), TransientCacheListener.instance());
51                 if (records == null || records.size() < 2)
52                         return Collections.emptyMap();
53                 
54                 Stream<Map<String, String>> enabled;
55                 if (enabledOnly) {
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));
60                 } else {
61                         enabled = records.stream();
62                 }
63                 
64                 return enabled
65                                 .filter(r -> r.containsKey(keyName))
66                                 .collect(Collectors.toMap(r -> r.get(keyName), r -> r));
67         }
68
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);
73                 return keyName;
74         }
75 }