View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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   *	$Id$
17   *  $HeadURL$
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   * The Class EsvExcelReaderImpl.
39   */
40  @Component
41  final class EsvExcelReaderImpl implements EsvExcelReader {
42  
43  	/** The Constant COMMENT_CELL. */
44  	private static final int COMMENT_CELL = 9;
45  
46  	/** The Constant VAT_CELL. */
47  	private static final int VAT_CELL = 8;
48  
49  	/** The Constant ANNUAL_HEADCOUNT_CELL. */
50  	private static final int ANNUAL_HEADCOUNT_CELL = 7;
51  
52  	/** The Constant HEADCOUNT_CELL. */
53  	private static final int HEADCOUNT_CELL = 6;
54  
55  	/** The Constant ORG_NUMBER_CELL. */
56  	private static final int ORG_NUMBER_CELL = 5;
57  
58  	/** The Constant MINISTRY_CELL. */
59  	private static final int MINISTRY_CELL = 4;
60  
61  	/** The Constant MCODE_CELL. */
62  	private static final int MCODE_CELL = 3;
63  
64  	/** The Constant GOVERNMENT_BODY_ID_CELL. */
65  	private static final int GOVERNMENT_BODY_ID_CELL = 2;
66  
67  	/** The Constant CONSECUTIVE_NUMBER_CELL. */
68  	private static final int CONSECUTIVE_NUMBER_CELL = 1;
69  
70  	/** The Constant NAME_CELL. */
71  	private static final int NAME_CELL = 0;
72  
73  	/** The Constant EXPECTED_COLUMN_LENGTH. */
74  	private static final int EXPECTED_COLUMN_LENGTH = 10;
75  
76  	/** The Constant LOGGER. */
77  	private static final Logger LOGGER = LoggerFactory.getLogger(EsvExcelReaderImpl.class);
78  
79  	/**
80  	 * Instantiates a new esv excel reader impl.
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 	 * Adds the ministry per year to map.
109 	 *
110 	 * @param name
111 	 *            the name
112 	 * @param map
113 	 *            the map
114 	 * @param mySheet
115 	 *            the my sheet
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 			// Skip header row, ignore first
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 	 * Adds the government body annual summary to list.
138 	 *
139 	 * @param name
140 	 *            the name
141 	 * @param year
142 	 *            the year
143 	 * @param yearList
144 	 *            the year list
145 	 * @param row
146 	 *            the row
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 	 * Gets the integer.
163 	 *
164 	 * @param str
165 	 *            the str
166 	 * @return the integer
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 	 * Adds the data for year to map.
200 	 *
201 	 * @param name
202 	 *            the name
203 	 * @param map
204 	 *            the map
205 	 * @param mySheet
206 	 *            the my sheet
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 	 * Adds the government body annual summary to map.
224 	 *
225 	 * @param name
226 	 *            the name
227 	 * @param map
228 	 *            the map
229 	 * @param year
230 	 *            the year
231 	 * @param row
232 	 *            the row
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 	 * Creates the government body annual summary from row.
249 	 *
250 	 * @param year
251 	 *            the year
252 	 * @param row
253 	 *            the row
254 	 * @return the government body annual summary
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 }