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.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.stereotype.Component;
30  
31  import com.hack23.cia.service.external.esv.api.EsvApi;
32  import com.hack23.cia.service.external.esv.api.GovernmentBodyAnnualSummary;
33  
34  /**
35   * The Class EsvApiImpl.
36   */
37  @Component
38  final class EsvApiImpl implements EsvApi {
39  
40  	/** The Constant NO_MINISTRY. */
41  	private static final String NO_MINISTRY = "Inget departement";
42  
43  	/** The Constant ministryNameSet. */
44  	private static final Set<String> ministryNameSet = new HashSet<>();
45  
46  	/** The Constant governmentBodyNameSet. */
47  	private static final Set<String> governmentBodyNameSet = new HashSet<>();
48  
49  	/** The Constant governmentBodyNameSetMinistryMap. */
50  	private static final Map<String, Set<String>> governmentBodyNameSetMinistryMap = new HashMap<>();
51  
52  	@Autowired
53  	private EsvExcelReader esvExcelReader;
54  
55  	/**
56  	 * Instantiates a new esv api impl.
57  	 */
58  	public EsvApiImpl() {
59  		super();
60  	}
61  
62  	@Override
63  	public Map<Integer, List<GovernmentBodyAnnualSummary>> getData() {
64  		return esvExcelReader.getDataPerMinistry(null);
65  	}
66  
67  	@Override
68  	public Map<Integer, List<GovernmentBodyAnnualSummary>> getDataPerMinistry(final String name) {
69  		return esvExcelReader.getDataPerMinistry(name);
70  	}
71  
72  	@Override
73  	public List<GovernmentBodyAnnualSummary> getDataPerMinistryAndYear(final String name, final int year) {
74  		final Map<Integer, List<GovernmentBodyAnnualSummary>> map = getDataPerMinistry(name);
75  
76  		if (map.containsKey(year)) {
77  			return map.get(year);
78  		} else {
79  			return new ArrayList<>();
80  		}
81  	}
82  
83  	@Override
84  	public List<String> getGovernmentBodyNames() {
85  		if (governmentBodyNameSet.isEmpty()) {
86  
87  			final Map<Integer, List<GovernmentBodyAnnualSummary>> data = getData();
88  
89  			for (final List<GovernmentBodyAnnualSummary> list : data.values()) {
90  				for (final GovernmentBodyAnnualSummary governmentBodyAnnualSummary : list) {
91  					if (!governmentBodyNameSet.contains(governmentBodyAnnualSummary.getName())
92  							&& governmentBodyAnnualSummary.getHeadCount() > 0) {
93  						governmentBodyNameSet.add(governmentBodyAnnualSummary.getName());
94  					}
95  				}
96  			}
97  		}
98  		return new ArrayList<>(governmentBodyNameSet);
99  	}
100 
101 	@Override
102 	public List<String> getMinistryNames() {
103 		if (ministryNameSet.isEmpty()) {
104 			final Map<Integer, List<GovernmentBodyAnnualSummary>> data = getData();
105 
106 			for (final List<GovernmentBodyAnnualSummary> list : data.values()) {
107 				for (final GovernmentBodyAnnualSummary governmentBodyAnnualSummary : list) {
108 					if (!ministryNameSet.contains(governmentBodyAnnualSummary.getMinistry())
109 							&& governmentBodyAnnualSummary.getHeadCount() > 0
110 							&& !NO_MINISTRY.equalsIgnoreCase(governmentBodyAnnualSummary.getMinistry())) {
111 						ministryNameSet.add(governmentBodyAnnualSummary.getMinistry());
112 					}
113 				}
114 			}
115 		}
116 		return new ArrayList<>(ministryNameSet);
117 	}
118 
119 	@Override
120 	public Map<Integer, GovernmentBodyAnnualSummary> getDataPerGovernmentBody(final String name) {
121 		return esvExcelReader.getDataPerGovernmentBody(name);
122 	}
123 
124 	@Override
125 	public List<String> getGovernmentBodyNames(final String ministry) {
126 		if (!governmentBodyNameSetMinistryMap.containsKey(ministry)) {
127 
128 			final Set<String> governmentBodyNameSetMapEntry = new HashSet<>();
129 			governmentBodyNameSetMinistryMap.put(ministry, governmentBodyNameSetMapEntry);
130 
131 			final Map<Integer, List<GovernmentBodyAnnualSummary>> data = getData();
132 
133 			for (final List<GovernmentBodyAnnualSummary> list : data.values()) {
134 				for (final GovernmentBodyAnnualSummary governmentBodyAnnualSummary : list) {
135 					if (ministry.equalsIgnoreCase(governmentBodyAnnualSummary.getMinistry())
136 							&& !governmentBodyNameSetMapEntry.contains(governmentBodyAnnualSummary.getName())
137 							&& governmentBodyAnnualSummary.getHeadCount() > 0) {
138 						governmentBodyNameSetMapEntry.add(governmentBodyAnnualSummary.getName());
139 					}
140 				}
141 			}
142 		}
143 		return new ArrayList<>(governmentBodyNameSetMinistryMap.get(ministry));
144 	}
145 
146 }