1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.hack23.cia.service.external.esv.impl;
20
21 import java.io.IOException;
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 import org.apache.poi.hssf.usermodel.HSSFSheet;
29 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
30 import org.apache.poi.ss.usermodel.Row;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.stereotype.Component;
34
35 import com.hack23.cia.service.external.esv.api.GovernmentBodyAnnualSummary;
36
37
38
39
40 @Component
41 final class EsvExcelReaderImpl implements EsvExcelReader {
42
43
44 private static final int COMMENT_CELL = 9;
45
46
47 private static final int VAT_CELL = 8;
48
49
50 private static final int ANNUAL_HEADCOUNT_CELL = 7;
51
52
53 private static final int HEADCOUNT_CELL = 6;
54
55
56 private static final int ORG_NUMBER_CELL = 5;
57
58
59 private static final int MINISTRY_CELL = 4;
60
61
62 private static final int MCODE_CELL = 3;
63
64
65 private static final int GOVERNMENT_BODY_ID_CELL = 2;
66
67
68 private static final int CONSECUTIVE_NUMBER_CELL = 1;
69
70
71 private static final int NAME_CELL = 0;
72
73
74 private static final int EXPECTED_COLUMN_LENGTH = 10;
75
76
77 private static final Logger LOGGER = LoggerFactory.getLogger(EsvExcelReaderImpl.class);
78
79
80
81
82 public EsvExcelReaderImpl() {
83 super();
84 }
85
86 @Override
87 public Map<Integer, List<GovernmentBodyAnnualSummary>> getDataPerMinistry(final String name) {
88 final Map<Integer, List<GovernmentBodyAnnualSummary>> map = new TreeMap<>();
89 try {
90 final HSSFWorkbook myWorkBook = new HSSFWorkbook(
91 EsvExcelReaderImpl.class.getResourceAsStream("/Myndighetsinformation.xls"));
92
93 for (int sheetNr = 0; sheetNr < myWorkBook.getNumberOfSheets(); sheetNr++) {
94 addMinistryPerYearToMap(name, map, myWorkBook.getSheetAt(sheetNr));
95 }
96
97 myWorkBook.close();
98 } catch (
99
100 final IOException e) {
101 LOGGER.warn("Problem loading", e);
102 }
103
104 return map;
105 }
106
107
108
109
110
111
112
113
114
115
116
117 private static void addMinistryPerYearToMap(final String name,
118 final Map<Integer, List<GovernmentBodyAnnualSummary>> map, final HSSFSheet mySheet) {
119 if (mySheet.getSheetName().chars().allMatch(Character::isDigit)) {
120
121 final int year = Integer.parseInt(mySheet.getSheetName());
122
123 final List<GovernmentBodyAnnualSummary> yearList = new ArrayList<>();
124 final Iterator<Row> rowIterator = mySheet.iterator();
125
126
127 rowIterator.next();
128
129 while (rowIterator.hasNext()) {
130 addGovernmentBodyAnnualSummaryToList(name, year, yearList, rowIterator.next());
131 }
132 map.put(year, yearList);
133 }
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148 private static void addGovernmentBodyAnnualSummaryToList(final String name, final int year,
149 final List<GovernmentBodyAnnualSummary> yearList, final Row row) {
150 if (row.getLastCellNum() == EXPECTED_COLUMN_LENGTH) {
151
152 final GovernmentBodyAnnualSummary governmentBodyAnnualSummary = createGovernmentBodyAnnualSummaryFromRow(
153 year, row);
154
155 if (name == null || name.equalsIgnoreCase(governmentBodyAnnualSummary.getMinistry())) {
156 yearList.add(governmentBodyAnnualSummary);
157 }
158 }
159 }
160
161
162
163
164
165
166
167
168 private static int getInteger(final String str) {
169 if (str == null || str.trim().length() == 0) {
170 return 0;
171 } else {
172 return Integer.parseInt(str);
173 }
174 }
175
176 @Override
177 public Map<Integer, GovernmentBodyAnnualSummary> getDataPerGovernmentBody(final String name) {
178 final Map<Integer, GovernmentBodyAnnualSummary> map = new TreeMap<>();
179 try {
180 final HSSFWorkbook myWorkBook = new HSSFWorkbook(
181 EsvExcelReaderImpl.class.getResourceAsStream("/Myndighetsinformation.xls"));
182
183 for (int sheetNr = 0; sheetNr < myWorkBook.getNumberOfSheets(); sheetNr++) {
184 final HSSFSheet mySheet = myWorkBook.getSheetAt(sheetNr);
185
186 addDataForYearToMap(name, map, mySheet);
187 }
188 myWorkBook.close();
189 } catch (
190
191 final IOException e) {
192 LOGGER.warn("Problem loading", e);
193 }
194
195 return map;
196 }
197
198
199
200
201
202
203
204
205
206
207
208 private static void addDataForYearToMap(final String name, final Map<Integer, GovernmentBodyAnnualSummary> map,
209 final HSSFSheet mySheet) {
210 if (mySheet.getSheetName().chars().allMatch(Character::isDigit)) {
211 final int year = Integer.parseInt(mySheet.getSheetName());
212 final Iterator<Row> rowIterator = mySheet.iterator();
213
214 rowIterator.next();
215
216 while (rowIterator.hasNext()) {
217 addGovernmentBodyAnnualSummaryToMap(name, map, year, rowIterator.next());
218 }
219 }
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234 private static void addGovernmentBodyAnnualSummaryToMap(final String name, final Map<Integer, GovernmentBodyAnnualSummary> map,
235 final int year, final Row row) {
236 if (row.getLastCellNum() == EXPECTED_COLUMN_LENGTH) {
237
238 final GovernmentBodyAnnualSummary governmentBodyAnnualSummary = createGovernmentBodyAnnualSummaryFromRow(
239 year, row);
240
241 if (name == null || name.equalsIgnoreCase(governmentBodyAnnualSummary.getName())) {
242 map.put(year, governmentBodyAnnualSummary);
243 }
244 }
245 }
246
247
248
249
250
251
252
253
254
255
256 private static GovernmentBodyAnnualSummary createGovernmentBodyAnnualSummaryFromRow(final int year, final Row row) {
257 return new GovernmentBodyAnnualSummary(year, row.getCell(NAME_CELL).toString(), getInteger(row.getCell(CONSECUTIVE_NUMBER_CELL).toString()),
258 row.getCell(GOVERNMENT_BODY_ID_CELL).toString(), row.getCell(MCODE_CELL).toString(), row.getCell(MINISTRY_CELL).toString(),
259 row.getCell(ORG_NUMBER_CELL).toString(), getInteger(row.getCell(HEADCOUNT_CELL).toString()), getInteger(row.getCell(ANNUAL_HEADCOUNT_CELL).toString()),
260 row.getCell(VAT_CELL).toString(), row.getCell(COMMENT_CELL).toString());
261 }
262
263 }