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.Arrays;
22  import java.util.List;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.springframework.security.access.annotation.Secured;
26  import org.springframework.stereotype.Component;
27  
28  import com.hack23.cia.model.internal.application.data.committee.impl.RiksdagenVoteDataBallotEmbeddedId;
29  import com.hack23.cia.model.internal.application.data.committee.impl.RiksdagenVoteDataBallotEmbeddedId_;
30  import com.hack23.cia.model.internal.application.data.committee.impl.RiksdagenVoteDataBallotPartyEmbeddedId;
31  import com.hack23.cia.model.internal.application.data.committee.impl.RiksdagenVoteDataBallotPartyEmbeddedId_;
32  import com.hack23.cia.model.internal.application.data.committee.impl.ViewRiksdagenCommitteeBallotDecisionEmbeddedId;
33  import com.hack23.cia.model.internal.application.data.committee.impl.ViewRiksdagenCommitteeBallotDecisionSummary;
34  import com.hack23.cia.model.internal.application.data.committee.impl.ViewRiksdagenCommitteeBallotDecisionSummary_;
35  import com.hack23.cia.model.internal.application.data.committee.impl.ViewRiksdagenVoteDataBallotPartySummary;
36  import com.hack23.cia.model.internal.application.data.committee.impl.ViewRiksdagenVoteDataBallotPartySummary_;
37  import com.hack23.cia.model.internal.application.data.committee.impl.ViewRiksdagenVoteDataBallotSummary;
38  import com.hack23.cia.model.internal.application.data.committee.impl.ViewRiksdagenVoteDataBallotSummary_;
39  import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
40  import com.hack23.cia.service.api.DataContainer;
41  import com.hack23.cia.web.impl.ui.application.action.ViewAction;
42  import com.hack23.cia.web.impl.ui.application.views.common.labelfactory.LabelFactory;
43  import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
44  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.PageMode;
45  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.UserViews;
46  import com.hack23.cia.web.impl.ui.application.views.pageclicklistener.PageItemPropertyClickListener;
47  import com.vaadin.data.util.BeanItem;
48  import com.vaadin.data.util.BeanItemContainer;
49  import com.vaadin.ui.Layout;
50  import com.vaadin.ui.MenuBar;
51  import com.vaadin.ui.Panel;
52  import com.vaadin.ui.VerticalLayout;
53  
54  /**
55   * The Class BallotOverviewPageModContentFactoryImpl.
56   */
57  @Component
58  public final class BallotOverviewPageModContentFactoryImpl extends AbstractBallotPageModContentFactoryImpl {
59  
60  	private static final int FIRST_OBJECT = 0;
61  
62  	/** The Constant COMMITTEE. */
63  	private static final String BALLOT = "Ballot:";
64  
65  	/** The Constant OVERVIEW. */
66  	private static final String OVERVIEW = "overview";
67  
68  	/**
69  	 * Instantiates a new ballot overview page mod content factory impl.
70  	 */
71  	public BallotOverviewPageModContentFactoryImpl() {
72  		super();
73  	}
74  
75  	@Override
76  	public boolean matches(final String page, final String parameters) {
77  		final String pageId = getPageId(parameters);
78  		return NAME.equals(page) && (StringUtils.isEmpty(parameters) || parameters.equals(pageId)
79  				|| parameters.contains(PageMode.OVERVIEW.toString()));
80  	}
81  
82  	@Secured({ "ROLE_ANONYMOUS", "ROLE_USER", "ROLE_ADMIN" })
83  	@Override
84  	public Layout createContent(final String parameters, final MenuBar menuBar, final Panel panel) {
85  		final VerticalLayout panelContent = createPanelContent();
86  
87  		final String pageId = getPageId(parameters);
88  
89  		final DataContainer<ViewRiksdagenVoteDataBallotSummary, RiksdagenVoteDataBallotEmbeddedId> dataContainer = getApplicationManager()
90  				.getDataContainer(ViewRiksdagenVoteDataBallotSummary.class);
91  
92  		final DataContainer<ViewRiksdagenVoteDataBallotPartySummary, RiksdagenVoteDataBallotPartyEmbeddedId> dataPartyContainer = getApplicationManager()
93  				.getDataContainer(ViewRiksdagenVoteDataBallotPartySummary.class);
94  
95  		final DataContainer<ViewRiksdagenCommitteeBallotDecisionSummary, ViewRiksdagenCommitteeBallotDecisionEmbeddedId> dataDecisionContainer = getApplicationManager()
96  				.getDataContainer(ViewRiksdagenCommitteeBallotDecisionSummary.class);
97  
98  		final List<ViewRiksdagenVoteDataBallotSummary> ballots = dataContainer.findListByEmbeddedProperty(
99  				ViewRiksdagenVoteDataBallotSummary.class, ViewRiksdagenVoteDataBallotSummary_.embeddedId,
100 				RiksdagenVoteDataBallotEmbeddedId.class, RiksdagenVoteDataBallotEmbeddedId_.ballotId, pageId);
101 
102 		final List<ViewRiksdagenVoteDataBallotPartySummary> partyBallotList = dataPartyContainer.findListByEmbeddedProperty(
103 				ViewRiksdagenVoteDataBallotPartySummary.class, ViewRiksdagenVoteDataBallotPartySummary_.embeddedId,
104 				RiksdagenVoteDataBallotPartyEmbeddedId.class, RiksdagenVoteDataBallotPartyEmbeddedId_.ballotId, pageId);
105 
106 		final List<ViewRiksdagenCommitteeBallotDecisionSummary> decisionSummaries = dataDecisionContainer
107 				.getAllBy(ViewRiksdagenCommitteeBallotDecisionSummary_.ballotId, pageId);
108 
109 		if (!ballots.isEmpty()) {
110 			getBallotMenuItemFactory().createBallotMenuBar(menuBar, pageId);
111 
112 			LabelFactory.createHeader2Label(panelContent,OVERVIEW);
113 
114 			final VerticalLayout overviewLayout = new VerticalLayout();
115 			overviewLayout.setSizeFull();
116 
117 			panelContent.addComponent(overviewLayout);
118 			panelContent.setExpandRatio(overviewLayout, ContentRatio.LARGE_FORM);
119 
120 			getBallotMenuItemFactory().createOverviewPage(overviewLayout, pageId);
121 
122 			for (final ViewRiksdagenVoteDataBallotSummary viewRiksdagenVoteDataBallotSummary : ballots) {
123 
124 				if (!decisionSummaries.isEmpty()) {
125 
126 					getFormFactory().addFormPanelTextFields(panelContent, new BeanItem<>(decisionSummaries.get(FIRST_OBJECT)),
127 							ViewRiksdagenCommitteeBallotDecisionSummary.class,
128 							Arrays.asList(new String[] { "embeddedId.id", "ballotId", "rm", "voteDate", "org",
129 									"committeeReport", "embeddedId.issue", "title", "subTitle", "decisionType",
130 									"embeddedId.concern", "ballotType", "winner", "totalVotes", "yesVotes", "noVotes",
131 									"abstainVotes", "absentVotes", "approved", "endNumber", "againstProposalParties",
132 									"againstProposalNumber" }));
133 
134 				} else {
135 
136 					getFormFactory().addFormPanelTextFields(panelContent, new BeanItem<>(viewRiksdagenVoteDataBallotSummary),
137 							ViewRiksdagenVoteDataBallotSummary.class,
138 							Arrays.asList(new String[] { "embeddedId.ballotId", "rm", "voteDate", "embeddedId.issue",
139 									"embeddedId.concern", "ballotType", "label", "totalVotes", "yesVotes", "noVotes",
140 									"abstainVotes", "absentVotes", "approved" }));
141 				}
142 
143 			}
144 
145 
146 			final BeanItemContainer<ViewRiksdagenVoteDataBallotPartySummary> partyBallotDataSource = new BeanItemContainer<>(
147 					ViewRiksdagenVoteDataBallotPartySummary.class, partyBallotList);
148 
149 			getGridFactory().createBasicBeanItemNestedPropertiesGrid(panelContent,
150 					partyBallotDataSource, "Party Ballot Summary",
151 					new String[] { "embeddedId.ballotId", "embeddedId.concern", "embeddedId.issue",
152 							"embeddedId.party" },
153 					new String[] { "embeddedId.party", "voteDate", "rm", "label", "embeddedId.concern",
154 							"embeddedId.issue", "approved", "partyApproved", "totalVotes", "partyTotalVotes",
155 							"yesVotes", "partyYesVotes", "noVotes", "partyNoVotes", "partyAbstainVotes", "abstainVotes",
156 							"partyAbsentVotes", "absentVotes", "partyAvgBornYear", "avgBornYear", "partyPercentageMale",
157 							"percentageMale", "ballotType", "embeddedId.ballotId" },
158 					new String[] { "embeddedId", "partyNoWinner", "partyPercentageYes", "partyPercentageNo",
159 							"partyPercentageAbsent", "partyPercentageAbstain", "percentageYes", "percentageNo",
160 							"percentageAbsent", "percentageAbstain", "voteDate", "rm", "label", "embeddedId.concern",
161 							"totalVotes", "approved", "yesVotes", "noVotes", "ballotType",
162 							"abstainVotes", "absentVotes", "embeddedId.ballotId", "noWinner" },
163 					new PageItemPropertyClickListener(UserViews.PARTY_VIEW_NAME, "embeddedId.party"), "embeddedId.party", null);
164 
165 
166 			panel.setCaption(BALLOT + pageId);
167 			getPageActionEventHelper().createPageEvent(ViewAction.VISIT_BALLOT_VIEW, ApplicationEventGroup.USER, NAME,
168 					parameters, pageId);
169 		}
170 		return panelContent;
171 
172 	}
173 
174 }