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 }
45 catch (IOException e) {
46 e.printStackTrace();
47 throw new RuntimeException("Cannot Load From File: " + filePath, e);
48 }
49 }
50 else {
51 throw new RuntimeException("filePath Cannnot be null");
52 }
53 }
54
55
56
57
58
59
60 private void parseRecords(Properties properties) {
61 TreeSet<Object> sortedKeys = new TreeSet<Object>();
62 sortedKeys.addAll(properties.keySet());
63 int currentRecIndex = getCurrentRecordIndex((String) sortedKeys.first());
64 int previousRecIndex = currentRecIndex;
65 Map<String, Object> record = new HashMap<String, Object>();
66 while (!sortedKeys.isEmpty()) {
67 String key = (String) sortedKeys.pollFirst();
68 currentRecIndex = getCurrentRecordIndex(key);
69
70 if (currentRecIndex != previousRecIndex) {
71 records.add(record);
72 record = new HashMap<String, Object>();
73 }
74
75 String subKey = key.substring(key.indexOf('.') + 1, key.length());
76 int ind2 = subKey.indexOf('.');
77 if (ind2 == -1) {
78 record.put(subKey, properties.getProperty(key));
79 }
80 else {
81 String fieldName = subKey.substring(0, subKey.indexOf('.'));
82 if (record.get(fieldName) == null) {
83 List<Object> values = new ArrayList<Object>();
84 values.add(properties.get(key));
85 record.put(fieldName, values);
86 }
87 else {
88 ((List<Object>) record.get(fieldName)).add(properties.get(key));
89 }
90 }
91 previousRecIndex = currentRecIndex;
92 }
93 if (record != null && record.size() != 0) {
94 records.add(record);
95 }
96 }
97
98
99
100
101
102
103 public List<Map<String, Object>> getRecords() {
104 return Collections.unmodifiableList(records);
105 }
106
107 private int getCurrentRecordIndex(String key) {
108 return Integer.parseInt(key.substring(3, key.indexOf('.')));
109 }
110
111 }