1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31
32
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);
48 headerToValueList = new TreeMap<String, List<String>>();
49
50 for (String field: fieldNames) {
51
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
63
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
77 val = null;
78 }
79
80 headerToValueList.get(fieldNames.get(index)).add(val);
81 index++;
82 }
83 }
84 }
85
86 public int numRows() {
87
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
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 }