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.Optional;
22  
23  import org.dussan.vaadin.dcharts.DCharts;
24  import org.springframework.beans.factory.annotation.Autowired;
25  
26  import com.hack23.cia.model.internal.application.data.party.impl.ViewRiksdagenParty;
27  import com.hack23.cia.service.api.ApplicationManager;
28  import com.hack23.cia.service.api.DataContainer;
29  import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
30  import com.vaadin.server.Page;
31  import com.vaadin.server.Sizeable.Unit;
32  import com.vaadin.ui.AbstractOrderedLayout;
33  import com.vaadin.ui.HorizontalLayout;
34  import com.vaadin.ui.Panel;
35  
36  /**
37   * The Class AbstractChartDataManagerImpl.
38   */
39  public abstract class AbstractChartDataManagerImpl {
40  
41  
42  	/** The Constant WINDOW_WIDTH_REDUCTION. */
43  	private static final int WINDOW_WIDTH_REDUCTION = 50;
44  
45  	/** The Constant WINDOW_HEIGHT_REDUCTION. */
46  	private static final int WINDOW_HEIGHT_REDUCTION = 200;
47  
48  	/** The Constant CHART_HEIGHT_REDUCTION. */
49  	private static final int CHART_HEIGHT_REDUCTION = 100;
50  
51  	/** The Constant CHART_WIDTH_REDUCTION. */
52  	private static final int CHART_WIDTH_REDUCTION = 50;
53  
54  	/** The Constant CHART_MARGIN_SIZE. */
55  	private static final int CHART_MARGIN_SIZE = 5;
56  
57  	/** The application manager. */
58  	@Autowired
59  	private ApplicationManager applicationManager;
60  
61  	/**
62  	 * Instantiates a new abstract chart data manager impl.
63  	 */
64  	public AbstractChartDataManagerImpl() {
65  		super();
66  	}
67  
68  	/**
69  	 * Adds the chart.
70  	 *
71  	 * @param content
72  	 *            the content
73  	 * @param caption
74  	 *            the caption
75  	 * @param chart
76  	 *            the chart
77  	 */
78  	protected final void addChart(final AbstractOrderedLayout content,final String caption, final DCharts chart) {
79  		final HorizontalLayout horizontalLayout = new HorizontalLayout();
80  
81  		final int browserWindowWidth = Page.getCurrent().getBrowserWindowWidth() - WINDOW_WIDTH_REDUCTION;
82  		final int browserWindowHeight = Page.getCurrent().getBrowserWindowHeight() - WINDOW_HEIGHT_REDUCTION;
83  
84  		horizontalLayout.setWidth(browserWindowWidth, Unit.PIXELS);
85  		horizontalLayout.setHeight(browserWindowHeight, Unit.PIXELS);
86  
87  
88  		final Panel formPanel = new Panel();
89  		formPanel.setSizeFull();
90  		formPanel.setContent(horizontalLayout);
91  
92  		content.addComponent(formPanel);
93  		content.setExpandRatio(formPanel, ContentRatio.LARGE);
94  
95  
96  		chart.setWidth(browserWindowWidth - CHART_WIDTH_REDUCTION, Unit.PIXELS);
97  		chart.setHeight(browserWindowHeight - CHART_HEIGHT_REDUCTION, Unit.PIXELS);
98  		chart.setMarginRight(CHART_MARGIN_SIZE);
99  		chart.setMarginLeft(CHART_MARGIN_SIZE);
100 		chart.setMarginBottom(CHART_MARGIN_SIZE);
101 		chart.setMarginTop(CHART_MARGIN_SIZE);
102 
103 		horizontalLayout.addComponent(chart);
104 		chart.setCaption(caption);
105 	}
106 
107 	/**
108 	 * Gets the party name.
109 	 *
110 	 * @param party
111 	 *            the party
112 	 * @return the party name
113 	 */
114 	protected final String getPartyName(final String party) {
115 		final DataContainer<ViewRiksdagenParty, String> dataContainer = applicationManager
116 				.getDataContainer(ViewRiksdagenParty.class);
117 
118 		final Optional<ViewRiksdagenParty> matchingObjects =dataContainer.getAll().stream().
119 			    filter(p -> p.getPartyId().equalsIgnoreCase(party)).
120 			    findFirst();
121 
122 		if (matchingObjects.isPresent()) {
123 			return matchingObjects.get().getPartyName();
124 
125 		} else {
126 			return party;
127 		}
128 	}
129 
130 
131 }