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              }
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       * Method to parseRecords
57       *
58       * @param properties
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       * Method to getRecords.
100      *
101      * @return
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 }