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;
20  
21  import java.util.Map;
22  
23  import javax.annotation.PostConstruct;
24  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.springframework.beans.factory.annotation.Autowired;
28  import org.springframework.security.access.AccessDeniedException;
29  import org.springframework.web.context.request.RequestContextHolder;
30  
31  import com.hack23.cia.service.api.action.application.LogoutRequest;
32  import com.hack23.cia.web.impl.ui.application.action.PageActionEventHelper;
33  import com.hack23.cia.web.impl.ui.application.util.UserContextUtil;
34  import com.hack23.cia.web.impl.ui.application.views.common.labelfactory.LabelFactory;
35  import com.hack23.cia.web.impl.ui.application.views.common.pagelinks.api.PageLinkFactory;
36  import com.hack23.cia.web.impl.ui.application.views.common.pagemode.PageModeContentFactory;
37  import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
38  import com.hack23.cia.web.impl.ui.application.views.pageclicklistener.LogoutClickListener;
39  import com.vaadin.navigator.View;
40  import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
41  import com.vaadin.server.FontAwesome;
42  import com.vaadin.server.ThemeResource;
43  import com.vaadin.ui.Alignment;
44  import com.vaadin.ui.Button;
45  import com.vaadin.ui.HorizontalLayout;
46  import com.vaadin.ui.Image;
47  import com.vaadin.ui.Label;
48  import com.vaadin.ui.Link;
49  import com.vaadin.ui.MenuBar;
50  import com.vaadin.ui.Panel;
51  import com.vaadin.ui.VerticalLayout;
52  
53  /**
54   * The Class AbstractView.
55   */
56  public abstract class AbstractView extends Panel implements View {
57  
58  	/** The Constant serialVersionUID. */
59  	private static final long serialVersionUID = 1L;
60  
61  	/** The Constant LOGGER. */
62  	private static final Logger LOGGER = LoggerFactory.getLogger(AbstractView.class);
63  
64  	/** The Constant LOGOUT. */
65  	private static final String LOGOUT = "Logout";
66  
67  	/** The Constant ROLE_USER. */
68  	private static final String ROLE_USER = "ROLE_USER";
69  
70  	/** The Constant ROLE_ADMIN. */
71  	private static final String ROLE_ADMIN = "ROLE_ADMIN";
72  
73  	/** The page mode content factory map. */
74  	private final transient Map<String, PageModeContentFactory> pageModeContentFactoryMap;
75  
76  	/** The page name. */
77  	private final String pageName;
78  
79  	/** The barmenu. */
80  	private final MenuBar barmenu = new MenuBar();
81  
82  	/** The top header right panel. */
83  	private final HorizontalLayout topHeaderRightPanel = new HorizontalLayout();
84  
85  	/** The panel. */
86  	private Panel panel;
87  
88  	/** The page link factory. */
89  	@Autowired
90  	protected transient PageLinkFactory pageLinkFactory;
91  
92  	/** The page action event helper. */
93  	@Autowired
94  	protected transient PageActionEventHelper pageActionEventHelper;
95  
96  	/**
97  	 * Instantiates a new abstract view.
98  	 */
99  	protected AbstractView(final Map<String, PageModeContentFactory> pageModeContentFactoryMap, final String pageName) {
100 		super();
101 		this.pageModeContentFactoryMap = pageModeContentFactoryMap;
102 		this.pageName = pageName;
103 	}
104 
105 	/**
106 	 * Post construct.
107 	 */
108 	@PostConstruct
109 	public final void postConstruct() {
110 		setSizeFull();
111 		createBasicLayoutWithPanelAndFooter(pageName);
112 	}
113 
114 
115 	@Override
116 	public final void enter(final ViewChangeEvent event) {
117 		try {
118 
119 			final String parameters = event.getParameters();
120 			for (final PageModeContentFactory pageModeContentFactory : pageModeContentFactoryMap.values()) {
121 				if (pageModeContentFactory.matches(pageName, parameters)) {
122 					getPanel().setContent(pageModeContentFactory.createContent(parameters, getBarmenu(), getPanel()));
123 					return;
124 				}
125 			}
126 		} catch (final AccessDeniedException e ) {
127 			LOGGER.warn("Access denided:" +pageName,e);
128 			final VerticalLayout panelContent = new VerticalLayout();
129 			panelContent.setMargin(true);
130 			panelContent.setWidth(100, Unit.PERCENTAGE);
131 			panelContent.setHeight(100, Unit.PERCENTAGE);
132 			LabelFactory.createHeader2Label(panelContent,"Access denided:" +pageName);
133 			getPanel().setContent(panelContent);
134 			getPanel().setCaption("Access denied");
135 		}
136 	}
137 
138 	/**
139 	 * Creates the basic layout with panel and footer.
140 	 *
141 	 * @param panelName
142 	 *            the panel name
143 	 */
144 	protected final void createBasicLayoutWithPanelAndFooter(final String panelName) {
145 		final VerticalLayout layout = new VerticalLayout();
146 		layout.setMargin(true);
147 		layout.setSpacing(true);
148 		layout.setWidth(100, Unit.PERCENTAGE);
149 		layout.setHeight(100, Unit.PERCENTAGE);
150 
151 
152 		final VerticalLayout pageModeContent = new VerticalLayout();
153 		pageModeContent.setMargin(true);
154 		pageModeContent.setSpacing(true);
155 		pageModeContent.setWidth(100, Unit.PERCENTAGE);
156 		pageModeContent.setHeight(100, Unit.PERCENTAGE);
157 
158 		layout.addComponent(pageModeContent);
159 
160 		final ThemeResource ciaLogoResource = new ThemeResource("cia-logo.png");
161 
162 		final Image ciaLogoImage = new Image(null,ciaLogoResource);
163 
164 		final HorizontalLayout topHeader = new HorizontalLayout();
165 
166 		topHeader.addComponent(ciaLogoImage);
167 		ciaLogoImage.setWidth("75px");
168 		ciaLogoImage.setHeight("75px");
169 		topHeader.setComponentAlignment(ciaLogoImage, Alignment.MIDDLE_LEFT);
170 		topHeader.setExpandRatio(ciaLogoImage, ContentRatio.SMALL);
171 
172 
173 		final HorizontalLayout topTitleHeadertPanel = new HorizontalLayout();
174 
175 
176 		final Label titleLabel = new Label("Citizen Intelligence Agency");
177 		titleLabel.setStyleName("Header");
178 		topTitleHeadertPanel.addComponent(titleLabel);
179 		topTitleHeadertPanel.setComponentAlignment(titleLabel, Alignment.MIDDLE_LEFT);
180 
181 		final Label sloganLabel = new Label("// Tracking politicians like bugs!");
182 		sloganLabel.setStyleName("HeaderSlogan");
183 		topTitleHeadertPanel.addComponent(sloganLabel);
184 		topTitleHeadertPanel.setComponentAlignment(sloganLabel, Alignment.MIDDLE_RIGHT);
185 
186 		topHeader.addComponent(topTitleHeadertPanel);
187 		topHeader.setComponentAlignment(topTitleHeadertPanel, Alignment.MIDDLE_LEFT);
188 		topHeader.setExpandRatio(topTitleHeadertPanel, ContentRatio.GRID);
189 
190 
191 		topHeaderRightPanel.removeAllComponents();
192 		topHeader.addComponent(topHeaderRightPanel);
193 		topHeader.setComponentAlignment(topHeaderRightPanel, Alignment.MIDDLE_RIGHT);
194 		topHeader.setExpandRatio(topHeaderRightPanel, ContentRatio.LARGE);
195 
196 
197 
198 		if (UserContextUtil.allowRoleInSecurityContext(ROLE_ADMIN) || UserContextUtil.allowRoleInSecurityContext(ROLE_USER)) {
199 
200 
201 			final Link userHomePageLink = pageLinkFactory.createUserHomeViewPageLink();
202 			topHeaderRightPanel.addComponent(userHomePageLink);
203 			topHeaderRightPanel.setComponentAlignment(userHomePageLink, Alignment.MIDDLE_RIGHT);
204 
205 
206 			final Button logoutButton = new Button(LOGOUT,FontAwesome.SIGN_OUT);
207 
208 			final LogoutRequest logoutRequest = new LogoutRequest();
209 			logoutRequest.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());
210 			logoutButton.addClickListener(new LogoutClickListener(logoutRequest));
211 
212 			topHeaderRightPanel.addComponent(logoutButton);
213 			topHeaderRightPanel.setComponentAlignment(logoutButton, Alignment.MIDDLE_RIGHT);
214 
215 		} else {
216 			final Link createRegisterPageLink = pageLinkFactory.createRegisterPageLink();
217 			topHeaderRightPanel.addComponent(createRegisterPageLink);
218 			topHeaderRightPanel.setComponentAlignment(createRegisterPageLink, Alignment.MIDDLE_RIGHT);
219 
220 			final Link createLoginPageLink = pageLinkFactory.createLoginPageLink();
221 			topHeaderRightPanel.addComponent(createLoginPageLink);
222 			topHeaderRightPanel.setComponentAlignment(createLoginPageLink, Alignment.MIDDLE_RIGHT);
223 		}
224 
225 
226 		topHeaderRightPanel.setWidth("100%");
227 		topHeaderRightPanel.setHeight("60px");
228 
229 		topHeader.setWidth("100%");
230 		topHeader.setHeight("60px");
231 
232 		pageModeContent.addComponent(topHeader);
233 		pageModeContent.setComponentAlignment(topHeader, Alignment.TOP_CENTER);
234 
235 
236 		pageModeContent.addComponent(getBarmenu());
237 		pageModeContent.setComponentAlignment(getBarmenu(), Alignment.TOP_CENTER);
238 
239 		panel = new Panel(panelName);
240 
241 		panel.setSizeFull();
242 		pageModeContent.addComponent(panel);
243 		pageModeContent.setExpandRatio(panel, ContentRatio.FULL_SIZE);
244 
245 		pageModeContent.addComponent(pageLinkFactory.createMainViewPageLink());
246 		setContent(layout);
247 
248 		setWidth(100, Unit.PERCENTAGE);
249 		setHeight(100, Unit.PERCENTAGE);
250 		setSizeFull();
251 
252 	}
253 
254 	/**
255 	 * Gets the barmenu.
256 	 *
257 	 * @return the barmenu
258 	 */
259 	public final MenuBar getBarmenu() {
260 		return barmenu;
261 	}
262 
263 	/**
264 	 * Gets the panel.
265 	 *
266 	 * @return the panel
267 	 */
268 	protected final Panel getPanel() {
269 		return panel;
270 	}
271 
272 	/**
273 	 * Gets the top header right panel.
274 	 *
275 	 * @return the top header right panel
276 	 */
277 	protected final HorizontalLayout getTopHeaderRightPanel() {
278 		return topHeaderRightPanel;
279 	}
280 
281 }