1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.docstore.discovery.service;
17
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Properties;
25 import java.util.TreeSet;
26
27 public class SolrFieldsFileReader {
28
29 private String filePath = null;
30 private List<Map<String, Object>> records = new ArrayList<Map<String, Object>>();
31
32
33
34
35
36
37 public SolrFieldsFileReader(String filePath) {
38 if (filePath != null) {
39 this.filePath = filePath;
40 Properties properties = new Properties();
41 try {
42 properties.load(getClass().getResourceAsStream(filePath));
43 parseRecords(properties);
44 } catch (IOException e) {
45 e.printStackTrace();
46 throw new RuntimeException("Cannot Load From File: " + filePath, e);
47 }
48 } else {
49 throw new RuntimeException("filePath Cannnot be null");
50 }
51 }
52
53
54
55
56
57
58 private void parseRecords(Properties properties) {
59 TreeSet<Object> sortedKeys = new TreeSet<Object>();
60 sortedKeys.addAll(properties.keySet());
61 int currentRecIndex = getCurrentRecordIndex((String) sortedKeys.first());
62 int previousRecIndex = currentRecIndex;
63 Map<String, Object> record = new HashMap<String, Object>();
64 while (!sortedKeys.isEmpty()) {
65 String key = (String) sortedKeys.pollFirst();
66 currentRecIndex = getCurrentRecordIndex(key);
67
68 if (currentRecIndex != previousRecIndex) {
69 records.add(record);
70 record = new HashMap<String, Object>();
71 }
72
73 String subKey = key.substring(key.indexOf('.') + 1, key.length());
74 int ind2 = subKey.indexOf('.');
75 if (ind2 == -1) {
76 record.put(subKey, properties.getProperty(key));
77 } else {
78 String fieldName = subKey.substring(0, subKey.indexOf('.'));
79 if (record.get(fieldName) == null) {
80 List<Object> values = new ArrayList<Object>();
81 values.add(properties.get(key));
82 record.put(fieldName, values);
83 } else {
84 ((List<Object>) record.get(fieldName)).add(properties.get(key));
85 }
86 }
87 previousRecIndex = currentRecIndex;
88 }
89 if (record != null && record.size() != 0) {
90 records.add(record);
91 }
92 }
93
94
95
96
97
98
99 public List<Map<String, Object>> getRecords() {
100 return Collections.unmodifiableList(records);
101 }
102
103 private int getCurrentRecordIndex(String key) {
104 return Integer.parseInt(key.substring(3, key.indexOf('.')));
105 }
106
107 }