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.model.internal.application.data.document.impl.RiksdagenDocumentTypeSummaryEmbeddedId;
34  import com.hack23.cia.model.internal.application.data.document.impl.ViewRiksdagenDocumentTypeDailySummary;
35  import com.hack23.cia.service.api.ApplicationManager;
36  import com.hack23.cia.service.api.DataContainer;
37  import com.hack23.cia.web.impl.ui.application.views.common.chartfactory.api.ChartOptions;
38  import com.hack23.cia.web.impl.ui.application.views.common.chartfactory.api.DocumentChartDataManager;
39  import com.vaadin.ui.AbstractOrderedLayout;
40  
41  /**
42   * The Class DocumentChartDataManagerImpl.
43   */
44  @Service
45  public final class DocumentChartDataManagerImpl extends AbstractChartDataManagerImpl implements DocumentChartDataManager {
46  
47  	/** The Constant MOT_PROP_BET. */
48  	private static final String MOT_PROP_BET = "mot:prop:bet";
49  
50  	/** The Constant YEAR_PREFIX. */
51  	private static final String YEAR_PREFIX = "19";
52  
53  	/** The Constant EMPTY_STRING. */
54  	private static final String EMPTY_STRING = "";
55  
56  	/** The application manager. */
57  	@Autowired
58  	private ApplicationManager applicationManager;
59  
60  	/** The chart options. */
61  	@Autowired
62  	private ChartOptions chartOptions;
63  
64  
65  	/**
66  	 * Instantiates a new document chart data manager impl.
67  	 */
68  	public DocumentChartDataManagerImpl() {
69  		super();
70  	}
71  
72  
73  	/**
74  	 * Gets the document type map.
75  	 *
76  	 * @return the document type map
77  	 */
78  	private Map<String, List<ViewRiksdagenDocumentTypeDailySummary>> getDocumentTypeMap() {
79  		final DataContainer<ViewRiksdagenDocumentTypeDailySummary, RiksdagenDocumentTypeSummaryEmbeddedId> documentTypeSummaryDailyDataContainer = applicationManager
80  				.getDataContainer(ViewRiksdagenDocumentTypeDailySummary.class);
81  
82  		return documentTypeSummaryDailyDataContainer.getAll().parallelStream()
83  				.filter(t -> t != null && !t.getEmbeddedId().getPublicDate().startsWith(YEAR_PREFIX)
84  						&& MOT_PROP_BET.contains(t.getEmbeddedId().getDocumentType()))
85  				.collect(Collectors.groupingBy(t -> t.getEmbeddedId().getDocumentType()));
86  	}
87  
88  
89  
90  	@Override
91  	public void createDocumentTypeChart(final AbstractOrderedLayout content) {
92  
93  		final Map<String, List<ViewRiksdagenDocumentTypeDailySummary>> map = getDocumentTypeMap();
94  
95  		final DataSeries dataSeries = new DataSeries();
96  
97  		final Series series = new Series();
98  
99  		for (final Entry<String, List<ViewRiksdagenDocumentTypeDailySummary>> entry : map.entrySet()) {
100 
101 			if (entry.getKey() != null && !EMPTY_STRING.equals(entry.getKey())) {
102 
103 				series.addSeries(new XYseries().setLabel(entry.getKey()));
104 
105 				dataSeries.newSeries();
106 				final List<ViewRiksdagenDocumentTypeDailySummary> list = entry.getValue();
107 				for (final ViewRiksdagenDocumentTypeDailySummary viewRiksdagenVoteDataBallotPartySummaryDaily : list) {
108 					if (viewRiksdagenVoteDataBallotPartySummaryDaily != null) {
109 						dataSeries.add(viewRiksdagenVoteDataBallotPartySummaryDaily.getEmbeddedId().getPublicDate(),
110 								viewRiksdagenVoteDataBallotPartySummaryDaily.getTotal());
111 					}
112 				}
113 			}
114 
115 		}
116 
117 		addChart(content,"Document type", new DCharts().setDataSeries(dataSeries).setOptions(chartOptions.createOptionsXYDateFloatLegendOutside(series)).show());
118 	}
119 
120 }