]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/techtype/requests/TechTypeTableRecords.java
Add enable/disable feature for tech type tables
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / techtype / requests / TechTypeTableRecords.java
1 package org.simantics.district.network.techtype.requests;
2
3 import java.io.IOException;
4 import java.io.StringReader;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Map;
10
11 import org.apache.commons.csv.CSVFormat;
12 import org.apache.commons.csv.CSVParser;
13 import org.apache.commons.csv.CSVRecord;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.request.ResourceRead;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.district.network.ontology.DistrictNetworkResource;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Get a list of the tech type table records in the order of appearance.
24  * 
25  * The query result value is list of records in the form of a map from property name
26  * to string value.
27  * 
28  * @author Reino Ruusu
29  */
30 public class TechTypeTableRecords extends ResourceRead<List<Map<String, String>>> {
31         
32         final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableRecords.class);
33         
34         public TechTypeTableRecords(Resource resource) {
35                 super(resource);
36         }
37         
38         @Override
39         public List<Map<String, String>> perform(ReadGraph graph) throws DatabaseException {
40                 Resource table = this.resource;
41                 
42                 String data = graph.getRelatedValue2(table,
43                                 DistrictNetworkResource.getInstance(graph).TechType_TechTypeTable_HasData);
44                 if (data == null)
45                         return null;
46                 
47                 long ncommas = data.chars().filter(c -> c == ',').count();
48                 long nsemis = data.chars().filter(c -> c == ';').count();
49                 char delim = nsemis > ncommas ? ';' : ',';
50                 StringReader reader = new StringReader(data);
51                 
52                 List<CSVRecord> records = new ArrayList<>();
53                 try {
54                         CSVFormat format = CSVFormat.newFormat(delim).withQuote('"');
55                         try (CSVParser parser = format.parse(reader)) {
56                                 Iterator<CSVRecord> it = parser.iterator();
57                                 while (it.hasNext()) {
58                                         records.add(it.next());
59                                 }
60                         }
61                 } catch (IOException e) {
62                         LOGGER.error("Error reading CSV data", e);
63                         throw new DatabaseException("Invalid CSV data in table " + table, e);
64                 }
65                 
66                 CSVRecord header = records.remove(0);
67                 records.remove(0); // Eliminate units row
68                 
69                 List<Map<String, String>> result = new ArrayList<>();
70                 for (CSVRecord r : records) {
71                         Map<String, String> valueMap = new HashMap<>();
72                         result.add(valueMap);
73                         
74                         Iterator<String> h = header.iterator();
75                         Iterator<String> v = r.iterator();
76                         
77                         while (h.hasNext() && v.hasNext()) {
78                                 String name = h.next().trim().intern();
79                                 String value = v.next().trim();
80                                 valueMap.put(name, value);
81                         }
82                 }
83                 
84                 return result;
85         }
86 }