package org.simantics.district.network.techtype.requests;
-import java.io.IOException;
-import java.io.StringReader;
-import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
-import org.apache.commons.csv.CSVFormat;
-import org.apache.commons.csv.CSVParser;
-import org.apache.commons.csv.CSVRecord;
+import org.simantics.databoard.Bindings;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
-import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.BinaryRead;
import org.simantics.db.exception.DatabaseException;
import org.simantics.district.network.ontology.DistrictNetworkResource;
import org.simantics.district.network.techtype.TechTypeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class TechTypeTableData extends ResourceRead<Map<String, Map<String, String>>> {
+/**
+ * Get a map of tech type table records, indexed by the key column.
+ *
+ * The query result value is list of records in the form of a map from property name
+ * to string value.
+ *
+ * The second constructor argument allows selection of enabled records only.
+ *
+ * @author Reino Ruusu
+ */
+public class TechTypeTableData extends BinaryRead<Resource, Boolean, Map<String, Map<String, String>>> {
- private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableData.class);
+ final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableData.class);
public TechTypeTableData(Resource table) {
- super(table);
+ super(table, Boolean.FALSE);
+ }
+
+ public TechTypeTableData(Resource table, boolean enabledOnly) {
+ super(table, enabledOnly);
}
@Override
public Map<String, Map<String, String>> perform(ReadGraph graph) throws DatabaseException {
- Resource table = this.resource;
+ Resource table = this.parameter;
+ boolean enabledOnly = this.parameter2;
- String data = graph.getRelatedValue2(table,
- DistrictNetworkResource.getInstance(graph).TechType_TechTypeTable_HasData);
- if (data == null)
- return Collections.emptyMap();
+ String keyName = getKeyName(graph, table);
- String keyName = TechTypeUtils.getKeyPropertyName(graph, table);
- if (keyName.startsWith("_"))
- keyName = keyName.substring(1);
-
- Map<String, Map<String, String>> map = new HashMap<>();
-
- long ncommas = data.chars().filter(c -> c == ',').count();
- long nsemis = data.chars().filter(c -> c == ';').count();
- char delim = nsemis > ncommas ? ';' : ',';
- StringReader reader = new StringReader(data);
-
- List<CSVRecord> records = new ArrayList<>();
- try {
- CSVFormat format = CSVFormat.newFormat(delim).withQuote('"');
- try (CSVParser parser = format.parse(reader)) {
- Iterator<CSVRecord> it = parser.iterator();
- while (it.hasNext()) {
- records.add(it.next());
- }
- }
- } catch (IOException e) {
- LOGGER.error("Error reading CSV data", e);
+ List<Map<String, String>> records = graph.syncRequest(new TechTypeTableRecords(table), TransientCacheListener.instance());
+ if (records == null || records.size() < 2)
return Collections.emptyMap();
- }
-
- CSVRecord header = records.remove(0);
- records.remove(0); // Eliminate units row
- int keyIndex = 0;
- for (int i = 0; i < header.size(); i++) {
- if (header.get(i).equals(keyName)) {
- keyIndex = i;
- break;
- }
- }
-
- for (CSVRecord r : records) {
- String key = r.get(keyIndex).trim().intern();
- if (key == null)
- continue;
-
- Map<String, String> valueMap = new HashMap<>();
- map.put(key, valueMap);
-
- Iterator<String> h = header.iterator();
- Iterator<String> v = r.iterator();
-
- while (h.hasNext() && v.hasNext()) {
- String name = h.next().trim().intern();
- String value = v.next().trim();
- valueMap.put(name, value);
- }
+ Stream<Map<String, String>> enabled;
+ if (enabledOnly) {
+ int[] enabledKeys = graph.getRelatedValue2(table, DistrictNetworkResource.getInstance(graph).TechType_TechTypeTable_HasEnabledItems, Bindings.INT_ARRAY);
+ enabled = Arrays.stream(enabledKeys)
+ .filter(i -> i >= 0 && i < records.size())
+ .mapToObj(i -> records.get(i));
+ } else {
+ enabled = records.stream();
}
- return map;
+ return enabled
+ .filter(r -> r.containsKey(keyName))
+ .collect(Collectors.toMap(r -> r.get(keyName), r -> r));
+ }
+
+ private String getKeyName(ReadGraph graph, Resource table) throws DatabaseException {
+ String keyName = TechTypeUtils.getKeyPropertyName(graph, table);
+ if (keyName.startsWith("_"))
+ keyName = keyName.substring(1);
+ return keyName;
}
-}
\ No newline at end of file
+}