X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Ftechtype%2Frequests%2FTechTypeTableRecords.java;fp=org.simantics.district.network%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Ftechtype%2Frequests%2FTechTypeTableRecords.java;h=81775b72510cd89bb1b73e304bbe68961fc069c2;hb=29af77f249a4842bfd3f9280755121c9c98b32a1;hp=0000000000000000000000000000000000000000;hpb=62f9a86961adc4fd44782e3c2f79852b1269810d;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/TechTypeTableRecords.java b/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/TechTypeTableRecords.java new file mode 100644 index 00000000..81775b72 --- /dev/null +++ b/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/TechTypeTableRecords.java @@ -0,0 +1,86 @@ +package org.simantics.district.network.techtype.requests; + +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; +import org.apache.commons.csv.CSVRecord; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ResourceRead; +import org.simantics.db.exception.DatabaseException; +import org.simantics.district.network.ontology.DistrictNetworkResource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Get a list of the tech type table records in the order of appearance. + * + * The query result value is list of records in the form of a map from property name + * to string value. + * + * @author Reino Ruusu + */ +public class TechTypeTableRecords extends ResourceRead>> { + + final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableRecords.class); + + public TechTypeTableRecords(Resource resource) { + super(resource); + } + + @Override + public List> perform(ReadGraph graph) throws DatabaseException { + Resource table = this.resource; + + String data = graph.getRelatedValue2(table, + DistrictNetworkResource.getInstance(graph).TechType_TechTypeTable_HasData); + if (data == null) + return null; + + 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); + throw new DatabaseException("Invalid CSV data in table " + table, e); + } + + CSVRecord header = records.remove(0); + records.remove(0); // Eliminate units row + + List> result = new ArrayList<>(); + for (CSVRecord r : records) { + Map valueMap = new HashMap<>(); + result.add(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); + } + } + + return result; + } +}