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.politician.pagemode;
20  
21  import java.util.Arrays;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.springframework.security.access.annotation.Secured;
25  import org.springframework.stereotype.Component;
26  
27  import com.hack23.cia.model.external.riksdagen.person.impl.DetailData;
28  import com.hack23.cia.model.external.riksdagen.person.impl.PersonData;
29  import com.hack23.cia.model.internal.application.data.politician.impl.ViewRiksdagenPolitician;
30  import com.hack23.cia.service.api.DataContainer;
31  import com.hack23.cia.web.impl.ui.application.views.common.labelfactory.LabelFactory;
32  import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
33  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.PageMode;
34  import com.vaadin.data.util.BeanItem;
35  import com.vaadin.data.util.BeanItemContainer;
36  import com.vaadin.server.ExternalResource;
37  import com.vaadin.ui.HorizontalLayout;
38  import com.vaadin.ui.Image;
39  import com.vaadin.ui.Layout;
40  import com.vaadin.ui.Link;
41  import com.vaadin.ui.MenuBar;
42  import com.vaadin.ui.Panel;
43  import com.vaadin.ui.VerticalLayout;
44  
45  /**
46   * The Class OverviewPageModContentFactoryImpl.
47   */
48  @Component
49  public final class PoliticianOverviewPageModContentFactoryImpl extends AbstractPoliticianPageModContentFactoryImpl {
50  
51  	/** The Constant OVERVIEW. */
52  	private static final String OVERVIEW = "overview";
53  
54  
55  	/**
56  	 * Instantiates a new politician overview page mod content factory impl.
57  	 */
58  	public PoliticianOverviewPageModContentFactoryImpl() {
59  		super();
60  	}
61  
62  	@Override
63  	public boolean matches(final String page, final String parameters) {
64  		final String pageId = getPageId(parameters);
65  		return NAME.equals(page) && (StringUtils.isEmpty(parameters) || parameters.equals(pageId)
66  				|| parameters.contains(PageMode.OVERVIEW.toString()));
67  	}
68  
69  	@Secured({ "ROLE_ANONYMOUS", "ROLE_USER", "ROLE_ADMIN" })
70  	@Override
71  	public Layout createContent(final String parameters, final MenuBar menuBar, final Panel panel) {
72  		final VerticalLayout panelContent = createPanelContent();
73  
74  		final String pageId = getPageId(parameters);
75  
76  		final DataContainer<PersonData, String> dataContainer = getApplicationManager()
77  				.getDataContainer(PersonData.class);
78  
79  		final PersonData personData = dataContainer.load(pageId);
80  		if (personData != null) {
81  
82  			final DataContainer<ViewRiksdagenPolitician, String> politicianDataContainer = getApplicationManager()
83  					.getDataContainer(ViewRiksdagenPolitician.class);
84  
85  			final ViewRiksdagenPolitician viewRiksdagenPolitician = politicianDataContainer.load(personData.getId());
86  
87  			getPoliticianMenuItemFactory().createPoliticianMenuBar(menuBar, pageId);
88  
89  			createOverviewContent(panelContent, personData, viewRiksdagenPolitician,pageId);
90  
91  			pageCompleted(parameters, panel, pageId, viewRiksdagenPolitician);
92  
93  		}
94  		return panelContent;
95  
96  	}
97  
98  	/**
99  	 * Creates the overview content.
100 	 *
101 	 * @param panelContent
102 	 *            the panel content
103 	 * @param personData
104 	 *            the person data
105 	 * @param viewRiksdagenPolitician
106 	 *            the view riksdagen politician
107 	 * @param pageId
108 	 */
109 	private void createOverviewContent(final VerticalLayout panelContent, final PersonData personData,
110 			final ViewRiksdagenPolitician viewRiksdagenPolitician, final String pageId) {
111 		LabelFactory.createHeader2Label(panelContent,OVERVIEW);
112 
113 		final VerticalLayout overviewLayout = new VerticalLayout();
114 		overviewLayout.setSizeFull();
115 
116 		panelContent.addComponent(overviewLayout);
117 		panelContent.setExpandRatio(overviewLayout, ContentRatio.LARGE_FORM);
118 
119 		getPoliticianMenuItemFactory().createOverviewPage(overviewLayout, pageId);
120 
121 		final Link createPoliticianPageLink = getPageLinkFactory().createPoliticianPageLink(personData);
122 		panelContent.addComponent(createPoliticianPageLink);
123 
124 		final Image image = new Image("",
125 				new ExternalResource(personData.getImageUrl192().replace("http://", "https://")));
126 
127 		final HorizontalLayout horizontalLayout = new HorizontalLayout();
128 		horizontalLayout.setSizeFull();
129 
130 		panelContent.addComponent(horizontalLayout);
131 
132 
133 
134 		horizontalLayout.addComponent(image);
135 
136 		getFormFactory().addFormPanelTextFields(horizontalLayout, new BeanItem<>(viewRiksdagenPolitician),
137 				ViewRiksdagenPolitician.class,
138 				Arrays.asList(new String[] { "firstName", "lastName", "gender", "bornYear", "party", "active",
139 						"firstAssignmentDate", "lastAssignmentDate", "currentAssignments", "currentMinistryAssignments",
140 						"currentSpeakerAssignments", "currentCommitteeAssignments", "currentPartyAssignments",
141 						"totalMinistryAssignments", "totalCommitteeAssignments", "totalSpeakerAssignments",
142 						"totalPartyAssignments", "totalAssignments", "totalDaysServed", "activeEu", "totalDaysServedEu",
143 						"activeGovernment", "totalDaysServedGovernment", "activeSpeaker", "totalDaysServedSpeaker",
144 						"activeCommittee", "totalDaysServedCommittee", "activeParliament", "totalDaysServedParliament",
145 						"activeParty", "totalDaysServedParty" }));
146 
147 		getGridFactory().createBasicBeanItemGrid(
148 				panelContent, new BeanItemContainer<>(DetailData.class, personData.getPersonDetailData().getDetailList()),
149 				"Detail", new String[] {"code", "detail" }, new String[] { "hjid", "intressentId","detailType" }, null,
150 				null, null);
151 
152 		panelContent.setExpandRatio(createPoliticianPageLink, ContentRatio.SMALL);
153 		panelContent.setExpandRatio(horizontalLayout, ContentRatio.GRID);
154 
155 	}
156 
157 }