]> gerrit.simantics Code Review - simantics/district.git/blob
4afe5bde5407b2ea2c9a8b5e8dedc0ddbfe1d20d
[simantics/district.git] /
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.Collections;
7 import java.util.HashMap;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Map;
11
12 import org.apache.commons.csv.CSVFormat;
13 import org.apache.commons.csv.CSVParser;
14 import org.apache.commons.csv.CSVRecord;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.request.ResourceRead;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.district.network.ontology.DistrictNetworkResource;
20 import org.simantics.district.network.techtype.TechTypeUtils;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class TechTypeTableData extends ResourceRead<Map<String, Map<String, String>>> {
25
26         private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableData.class);
27         
28         public TechTypeTableData(Resource table) {
29                 super(table);
30         }
31
32         @Override
33         public Map<String, Map<String, String>> perform(ReadGraph graph) throws DatabaseException {
34                 Resource table = this.resource;
35
36                 String data = graph.getRelatedValue2(table,
37                                 DistrictNetworkResource.getInstance(graph).TechType_TechTypeTable_HasData);
38                 if (data == null)
39                         return Collections.emptyMap();
40                 
41                 String keyName = TechTypeUtils.getKeyPropertyName(graph, table);
42                 if (keyName.startsWith("_"))
43                         keyName = keyName.substring(1);
44
45                 Map<String, Map<String, String>> map = new HashMap<>();
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                         return Collections.emptyMap();
64                 }
65
66                 CSVRecord header = records.remove(0);
67                 records.remove(0); // Eliminate units row
68                 
69                 int keyIndex = 0;
70                 for (int i = 0; i < header.size(); i++) {
71                         if (header.get(i).equals(keyName)) {
72                                 keyIndex = i;
73                                 break;
74                         }
75                 }
76
77                 for (CSVRecord r : records) {
78                         String key = r.get(keyIndex).intern();
79                         if (key == null)
80                                 continue;
81                         
82                         Map<String, String> valueMap = new HashMap<>();
83                         map.put(key, valueMap);
84                         
85                         Iterator<String> h = header.iterator();
86                         Iterator<String> v = r.iterator();
87                         
88                         while (h.hasNext() && v.hasNext()) {
89                                 String name = h.next().intern();
90                                 String value = v.next();
91                                 valueMap.put(name, value);
92                         }
93                 }
94                 
95                 return map;
96         }
97 }