]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network/src/org/simantics/district/network/techtype/requests/TechTypeTableData.java
Updating of component properties when type code changes
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / techtype / requests / TechTypeTableData.java
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
new file mode 100644 (file)
index 0000000..2ff575b
--- /dev/null
@@ -0,0 +1,97 @@
+package org.simantics.district.network.techtype.requests;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.Collections;
+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.simantics.district.network.techtype.TechTypeUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TechTypeTableData extends ResourceRead<Map<String, Map<String, String>>> {
+
+       private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableData.class);
+       
+       protected TechTypeTableData(Resource table) {
+               super(table);
+       }
+
+       @Override
+       public Map<String, Map<String, String>> perform(ReadGraph graph) throws DatabaseException {
+               Resource table = this.resource;
+
+               String data = graph.getRelatedValue2(table,
+                               DistrictNetworkResource.getInstance(graph).TechType_TechTypeTable_HasData);
+               if (data == null)
+                       return Collections.emptyMap();
+               
+               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);
+                       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).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().intern();
+                               String value = v.next();
+                               valueMap.put(name, value);
+                       }
+               }
+               
+               return map;
+       }
+}
\ No newline at end of file