View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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       * Creates SolrFieldsFileReader for a given Properties file.
34       *
35       * @param filePath - relative classpath for the input properties file.
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       * Method to parseRecords
55       *
56       * @param properties
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       * Method to getRecords.
96       *
97       * @return
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 }