View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   * Created by Charles on 11/15/12
16   */
17  package org.kuali.student.enrollment.class2.courseoffering.refdata;
18  
19  import org.apache.log4j.Logger;
20  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.TreeMap;
27  
28  /**
29   * Stores the content of a spreadsheet with fieldNames and values
30   * The map is a list of a header to a list of values (basically, column data)
31   *
32   * @author Kuali Student Team
33   */
34  public class SpreadsheetData implements Iterable<SpreadsheetRowData> {
35      private static Logger LOGGER = Logger.getLogger(SpreadsheetData.class);
36      List<String> fieldNames;
37      Map<String, List<String>> headerToValueList;
38      boolean isValid = false;
39  
40      public SpreadsheetData() {
41  
42      }
43  
44      public void addHeaders(List<String> fieldNames) {
45          if (!isValid && fieldNames != null) {
46              this.fieldNames = new ArrayList<String>();
47              this.fieldNames.addAll(fieldNames); // Copy the fieldNames
48              headerToValueList = new TreeMap<String, List<String>>();
49              // TODO: Make sure fieldNames have unique names
50              for (String field: fieldNames) {
51                  // Stub out the map
52                  headerToValueList.put(field, new ArrayList<String>());
53              }
54              isValid = true;
55          } else {
56              LOGGER.warn("Field names already set");
57          }
58      }
59  
60      /**
61       *
62       * @param row A list of strings in the order of the fieldNames
63       * @throws InvalidParameterException due to a variety of errors that could occur in row
64       */
65      public void addRow(List<String> row) throws InvalidParameterException {
66          if (row == null) {
67              throw new InvalidParameterException("Row is null");
68          } else if (!isValid) {
69              throw new InvalidParameterException("Headers are not set");
70          } else if (row.size() != fieldNames.size()) {
71              throw new InvalidParameterException();
72          } else {
73              int index = 0;
74              for (String val: row) {
75                  if (val == null || val.isEmpty()) {
76                      // Put null for empty strings
77                      val = null;
78                  }
79                  // add the values at the end of appropriate list
80                  headerToValueList.get(fieldNames.get(index)).add(val);
81                  index++;
82              }
83          }
84      }
85  
86      public int numRows() {
87          // All columns should have the same length
88          int firstFieldName = 0;
89          return headerToValueList.get(fieldNames.get(firstFieldName)).size();
90      }
91  
92      public SpreadsheetRowData getRowAt(int index) {
93          if (index >= numRows() || index < 0) {
94              return null;
95          } else {
96              SpreadsheetRowData rowData = new SpreadsheetRowData();
97              for (String field: fieldNames) {
98                  String value = headerToValueList.get(field).get(index);
99                  // Added in same order as fieldNames
100                 rowData.addData(field, value);
101             }
102             return rowData;
103         }
104     }
105 
106     public void showContents() {
107         for (String str: fieldNames) {
108             System.err.print(str + "\t");
109         }
110         System.err.println();
111         System.err.println("------------------------------------------------------");
112         int rows = numRows();
113         for (int i = 0; i < rows; i++) {
114             for (String data: getRowAt(i)) {
115                 if (data == null) {
116                     data = "null";
117                 }
118                 System.err.print(data + "\t");
119             }
120             System.err.println();
121         }
122     }
123 
124 
125     @Override
126     public Iterator<SpreadsheetRowData> iterator() {
127         return new Iterator<SpreadsheetRowData>() {
128             int index = 0;
129             @Override
130             public boolean hasNext() {
131                 return index < numRows();
132             }
133 
134             @Override
135             public SpreadsheetRowData next() {
136                 if (!hasNext()) {
137                     throw new IndexOutOfBoundsException("No more rows left");
138                 }
139                 SpreadsheetRowData rowData = getRowAt(index);
140                 index++;
141                 return rowData;
142             }
143 
144             @Override
145             public void remove() {
146                 throw new RuntimeException("Operation not supported");
147             }
148         };
149     }
150 }