View Javadoc
1   /*
2    * Copyright 2014 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.web.impl.ui.application.views.common.chartfactory.impl;
20  
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Map.Entry;
24  import java.util.stream.Collectors;
25  
26  import org.dussan.vaadin.dcharts.DCharts;
27  import org.dussan.vaadin.dcharts.base.elements.XYseries;
28  import org.dussan.vaadin.dcharts.data.DataSeries;
29  import org.dussan.vaadin.dcharts.options.Series;
30  import org.springframework.beans.factory.annotation.Autowired;
31  import org.springframework.stereotype.Service;
32  
33  import com.hack23.cia.service.external.esv.api.EsvApi;
34  import com.hack23.cia.service.external.esv.api.GovernmentBodyAnnualSummary;
35  import com.hack23.cia.web.impl.ui.application.views.common.chartfactory.api.ChartOptions;
36  import com.hack23.cia.web.impl.ui.application.views.common.chartfactory.api.GovernmentBodyChartDataManager;
37  import com.vaadin.ui.AbstractOrderedLayout;
38  
39  /**
40   * The Class GovernmentBodyChartDataManagerImpl.
41   */
42  @Service
43  public final class GovernmentBodyChartDataManagerImpl extends AbstractChartDataManagerImpl implements GovernmentBodyChartDataManager {
44  
45  	/** The Constant ANNUAL_HEADCOUNT_SUMMARY_ALL_GOVERNMENT_BODIES. */
46  	private static final String ANNUAL_HEADCOUNT_SUMMARY_ALL_GOVERNMENT_BODIES = " Annual headcount summary, all government bodies";
47  
48  	/** The Constant ANNUAL_HEADCOUNT_SUMMARY. */
49  	private static final String ANNUAL_HEADCOUNT_SUMMARY = " Annual headcount summary";
50  
51  	/** The Constant ANNUAL_HEADCOUNT_ALL_MINISTRIES. */
52  	private static final String ANNUAL_HEADCOUNT_ALL_MINISTRIES = "Annual headcount, all ministries";
53  
54  	/** The Constant FIRST_OF_JAN. */
55  	private static final String FIRST_OF_JAN = "01-JAN-";
56  
57  	/** The esv api. */
58  	@Autowired
59  	private EsvApi esvApi;
60  
61  	/** The chart options. */
62  	@Autowired
63  	private ChartOptions chartOptions;
64  
65  	/**
66  	 * Instantiates a new government body chart data manager impl.
67  	 */
68  	public GovernmentBodyChartDataManagerImpl() {
69  		super();
70  	}
71  
72  	@Override
73  	public void createGovernmentBodyHeadcountChart(final AbstractOrderedLayout content, final String name) {
74  		final Map<Integer, GovernmentBodyAnnualSummary> map = esvApi.getDataPerGovernmentBody(name);
75  
76  		final DataSeries dataSeries = new DataSeries();
77  
78  		final Series series = new Series();
79  
80  		series.addSeries(new XYseries().setLabel(name));
81  
82  		dataSeries.newSeries();
83  
84  		for (final Entry<Integer, GovernmentBodyAnnualSummary> entry : map.entrySet()) {
85  			final GovernmentBodyAnnualSummary item = entry.getValue();
86  			if (entry.getKey() != null && item != null && item.getHeadCount() > 0) {
87  				dataSeries.add(FIRST_OF_JAN + item.getYear(), item.getHeadCount());
88  			}
89  		}
90  
91  		addChart(content, name + ANNUAL_HEADCOUNT_SUMMARY, new DCharts().setDataSeries(dataSeries)
92  				.setOptions(chartOptions.createOptionsXYDateFloatLogYAxisLegendOutside(series)).show());
93  	}
94  
95  	@Override
96  	public void createMinistryGovernmentBodyHeadcountSummaryChart(final AbstractOrderedLayout content,
97  			final String name) {
98  		final Map<Integer, List<GovernmentBodyAnnualSummary>> map = esvApi.getDataPerMinistry(name);
99  		final List<String> governmentBodyNames = esvApi.getGovernmentBodyNames(name);
100 
101 		final DataSeries dataSeries = new DataSeries();
102 		final Series series = new Series();
103 
104 		for (final String govBodyName : governmentBodyNames) {
105 
106 			series.addSeries(new XYseries().setLabel(govBodyName));
107 
108 			dataSeries.newSeries();
109 
110 			for (final Entry<Integer, List<GovernmentBodyAnnualSummary>> entry : map.entrySet()) {
111 
112 				final List<GovernmentBodyAnnualSummary> item = entry.getValue();
113 				final Integer totalHeadcount = item.stream().filter(p -> p.getName().equalsIgnoreCase(govBodyName))
114 						.collect(Collectors.summingInt(GovernmentBodyAnnualSummary::getHeadCount));
115 
116 				if (entry.getKey() != null && item != null && totalHeadcount > 0) {
117 					dataSeries.add(FIRST_OF_JAN + entry.getKey(), totalHeadcount);
118 				}
119 			}
120 		}
121 
122 		addChart(content, name + ANNUAL_HEADCOUNT_SUMMARY_ALL_GOVERNMENT_BODIES,
123 				new DCharts().setDataSeries(dataSeries)
124 						.setOptions(chartOptions.createOptionsXYDateFloatLogYAxisLegendOutside(series)).show());
125 
126 	}
127 
128 	@Override
129 	public void createMinistryGovernmentBodyHeadcountSummaryChart(final AbstractOrderedLayout content) {
130 		final Map<Integer, List<GovernmentBodyAnnualSummary>> map = esvApi.getData();
131 		final List<String> ministryNames = esvApi.getMinistryNames();
132 
133 		final DataSeries dataSeries = new DataSeries();
134 
135 		final Series series = new Series();
136 
137 		for (final String ministryName : ministryNames) {
138 
139 			series.addSeries(new XYseries().setLabel(ministryName));
140 
141 			dataSeries.newSeries();
142 
143 			for (final Entry<Integer, List<GovernmentBodyAnnualSummary>> entry : map.entrySet()) {
144 
145 				final List<GovernmentBodyAnnualSummary> item = entry.getValue();
146 				final Integer totalHeadcount = item.stream().filter(p -> p.getMinistry().equalsIgnoreCase(ministryName))
147 						.collect(Collectors.summingInt(GovernmentBodyAnnualSummary::getHeadCount));
148 
149 				if (entry.getKey() != null && item != null && totalHeadcount > 0) {
150 					dataSeries.add(FIRST_OF_JAN + entry.getKey(), totalHeadcount);
151 				}
152 			}
153 		}
154 
155 		addChart(content, ANNUAL_HEADCOUNT_ALL_MINISTRIES, new DCharts().setDataSeries(dataSeries)
156 				.setOptions(chartOptions.createOptionsXYDateFloatLogYAxisLegendOutside(series)).show());
157 
158 	}
159 
160 }