X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Ftechtype%2Frequests%2FTechTypeTableData.java;fp=org.simantics.district.network%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Ftechtype%2Frequests%2FTechTypeTableData.java;h=60929eddc999c56aad719616ba81bc21f90ade44;hb=29af77f249a4842bfd3f9280755121c9c98b32a1;hp=cbedd1acdf9e47ba3bc7dedef49954326b29b496;hpb=62f9a86961adc4fd44782e3c2f79852b1269810d;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/TechTypeTableData.java b/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/TechTypeTableData.java index cbedd1ac..60929edd 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/TechTypeTableData.java +++ b/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/TechTypeTableData.java @@ -1,97 +1,75 @@ 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>> { +/** + * 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>> { - 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> 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> 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 records = new ArrayList<>(); - try { - CSVFormat format = CSVFormat.newFormat(delim).withQuote('"'); - try (CSVParser parser = format.parse(reader)) { - Iterator it = parser.iterator(); - while (it.hasNext()) { - records.add(it.next()); - } - } - } catch (IOException e) { - LOGGER.error("Error reading CSV data", e); + List> 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 valueMap = new HashMap<>(); - map.put(key, valueMap); - - Iterator h = header.iterator(); - Iterator v = r.iterator(); - - while (h.hasNext() && v.hasNext()) { - String name = h.next().trim().intern(); - String value = v.next().trim(); - valueMap.put(name, value); - } + Stream> 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 +}