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.user.ballot.pagemode;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Comparator;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.security.access.annotation.Secured;
30  import org.springframework.stereotype.Component;
31  
32  import com.hack23.cia.model.internal.application.data.committee.impl.RiksdagenVoteDataBallotEmbeddedId;
33  import com.hack23.cia.model.internal.application.data.committee.impl.RiksdagenVoteDataBallotEmbeddedId_;
34  import com.hack23.cia.model.internal.application.data.committee.impl.RiksdagenVoteDataBallotPartyEmbeddedId;
35  import com.hack23.cia.model.internal.application.data.committee.impl.RiksdagenVoteDataBallotPartyEmbeddedId_;
36  import com.hack23.cia.model.internal.application.data.committee.impl.ViewRiksdagenVoteDataBallotPartySummary;
37  import com.hack23.cia.model.internal.application.data.committee.impl.ViewRiksdagenVoteDataBallotPartySummary_;
38  import com.hack23.cia.model.internal.application.data.committee.impl.ViewRiksdagenVoteDataBallotSummary;
39  import com.hack23.cia.model.internal.application.data.committee.impl.ViewRiksdagenVoteDataBallotSummary_;
40  import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
41  import com.hack23.cia.service.api.DataContainer;
42  import com.hack23.cia.web.impl.ui.application.action.ViewAction;
43  import com.hack23.cia.web.impl.ui.application.views.common.chartfactory.api.BallotChartDataManager;
44  import com.hack23.cia.web.impl.ui.application.views.common.labelfactory.LabelFactory;
45  import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
46  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.PageMode;
47  import com.vaadin.server.Sizeable.Unit;
48  import com.vaadin.ui.HorizontalLayout;
49  import com.vaadin.ui.Layout;
50  import com.vaadin.ui.MenuBar;
51  import com.vaadin.ui.Panel;
52  import com.vaadin.ui.TabSheet;
53  import com.vaadin.ui.TabSheet.Tab;
54  import com.vaadin.ui.VerticalLayout;
55  
56  /**
57   * The Class BallotChartsPageModContentFactoryImpl.
58   */
59  @Component
60  public final class BallotChartsPageModContentFactoryImpl extends AbstractBallotPageModContentFactoryImpl {
61  
62  	/** The Constant COMMITTEE. */
63  	private static final String BALLOT = "Ballot:";
64  
65  	/** The Constant OVERVIEW. */
66  	private static final String CHARTS = "Charts";
67  
68  	@Autowired
69  	private BallotChartDataManager ballotChartDataManager;
70  
71  	/**
72  	 * Instantiates a new ballot charts page mod content factory impl.
73  	 */
74  	public BallotChartsPageModContentFactoryImpl() {
75  		super();
76  	}
77  
78  	@Override
79  	public boolean matches(final String page, final String parameters) {
80  		return NAME.equals(page) && parameters.contains(PageMode.CHARTS.toString());
81  	}
82  
83  	@Secured({ "ROLE_ANONYMOUS", "ROLE_USER", "ROLE_ADMIN" })
84  	@Override
85  	public Layout createContent(final String parameters, final MenuBar menuBar, final Panel panel) {
86  		final VerticalLayout panelContent = createPanelContent();
87  
88  		final String pageId = getPageId(parameters);
89  
90  
91  		final DataContainer<ViewRiksdagenVoteDataBallotSummary, RiksdagenVoteDataBallotEmbeddedId> dataContainer = getApplicationManager()
92  				.getDataContainer(ViewRiksdagenVoteDataBallotSummary.class);
93  
94  		final DataContainer<ViewRiksdagenVoteDataBallotPartySummary, RiksdagenVoteDataBallotPartyEmbeddedId> dataPartyContainer = getApplicationManager()
95  				.getDataContainer(ViewRiksdagenVoteDataBallotPartySummary.class);
96  
97  
98  		final List<ViewRiksdagenVoteDataBallotSummary> ballots = dataContainer.findListByEmbeddedProperty(ViewRiksdagenVoteDataBallotSummary.class, ViewRiksdagenVoteDataBallotSummary_.embeddedId, RiksdagenVoteDataBallotEmbeddedId.class, RiksdagenVoteDataBallotEmbeddedId_.ballotId, pageId);
99  
100 		final List<ViewRiksdagenVoteDataBallotPartySummary> partyBallotList = dataPartyContainer.findListByEmbeddedProperty(ViewRiksdagenVoteDataBallotPartySummary.class, ViewRiksdagenVoteDataBallotPartySummary_.embeddedId, RiksdagenVoteDataBallotPartyEmbeddedId.class, RiksdagenVoteDataBallotPartyEmbeddedId_.ballotId, pageId);
101 
102 		if (!ballots.isEmpty()) {
103 			getBallotMenuItemFactory().createBallotMenuBar(menuBar, pageId);
104 
105 
106 
107 
108 				LabelFactory.createHeader2Label(panelContent,CHARTS);
109 
110 				final TabSheet tabsheet = new TabSheet();
111 				tabsheet.setWidth(100, Unit.PERCENTAGE);
112 				tabsheet.setHeight(100, Unit.PERCENTAGE);
113 
114 				panelContent.addComponent(tabsheet);
115 				panelContent.setExpandRatio(tabsheet, ContentRatio.LARGE);
116 
117 				Collections.sort(ballots, (Comparator<ViewRiksdagenVoteDataBallotSummary>) (o1, o2) -> (o1.getEmbeddedId().getIssue() + o2.getEmbeddedId().getConcern()).compareTo(o1.getEmbeddedId().getIssue() + o2.getEmbeddedId().getConcern()));
118 
119 				for (final ViewRiksdagenVoteDataBallotSummary viewRiksdagenVoteDataBallotSummary : ballots) {
120 					final HorizontalLayout tabContent = new HorizontalLayout();
121 					tabContent.setWidth(100, Unit.PERCENTAGE);
122 					tabContent.setHeight(100, Unit.PERCENTAGE);
123 					final Tab tab = tabsheet.addTab(tabContent);
124 
125 					ballotChartDataManager.createChart(tab,tabContent,viewRiksdagenVoteDataBallotSummary);
126 				}
127 
128 				final Map<String, List<ViewRiksdagenVoteDataBallotPartySummary>> concernIssuePartyBallotSummaryMap = createIssueConcernMap(partyBallotList);
129 
130 				for (final List<ViewRiksdagenVoteDataBallotPartySummary> partyBallotSummaryList : concernIssuePartyBallotSummaryMap.values()) {
131 					final HorizontalLayout tabContent = new HorizontalLayout();
132 					tabContent.setWidth(100, Unit.PERCENTAGE);
133 					tabContent.setHeight(100, Unit.PERCENTAGE);
134 					final Tab tab = tabsheet.addTab(tabContent);
135 
136 					ballotChartDataManager.createChart(tab,tabContent,partyBallotSummaryList);
137 				}
138 
139 
140 				panel.setCaption(BALLOT + pageId);
141 				getPageActionEventHelper().createPageEvent(ViewAction.VISIT_BALLOT_VIEW, ApplicationEventGroup.USER, NAME, parameters, pageId);
142 		}
143 		return panelContent;
144 
145 	}
146 
147 	/**
148 	 * Creates the issue concern map.
149 	 *
150 	 * @param partyBallotList
151 	 *            the party ballot list
152 	 * @return the map
153 	 */
154 	private static Map<String,List<ViewRiksdagenVoteDataBallotPartySummary>> createIssueConcernMap(final List<ViewRiksdagenVoteDataBallotPartySummary> partyBallotList) {
155 		final Map<String,List<ViewRiksdagenVoteDataBallotPartySummary>> concernIssuePartyBallotSummaryMap = new HashMap<>();
156 		for (final ViewRiksdagenVoteDataBallotPartySummary partySummary: partyBallotList) {
157 
158 			if (partySummary.getEmbeddedId().getIssue() !=null || partySummary.getEmbeddedId().getConcern() != null ) {
159 				final String key = partySummary.getEmbeddedId().getIssue() + partySummary.getEmbeddedId().getConcern();
160 				if (concernIssuePartyBallotSummaryMap.get(key) == null) {
161 					concernIssuePartyBallotSummaryMap.put(key, new ArrayList<>());
162 				}
163 				concernIssuePartyBallotSummaryMap.get(key).add(partySummary);
164 			}
165 		}
166 
167 		return concernIssuePartyBallotSummaryMap;
168 	}
169 
170 }