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.home.pagemode;
20  
21  import java.util.Arrays;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.springframework.beans.factory.annotation.Autowired;
25  import org.springframework.security.access.annotation.Secured;
26  import org.springframework.stereotype.Component;
27  import org.springframework.web.context.request.RequestContextHolder;
28  
29  import com.hack23.cia.model.internal.application.system.impl.ApplicationActionEvent;
30  import com.hack23.cia.model.internal.application.system.impl.ApplicationActionEvent_;
31  import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
32  import com.hack23.cia.model.internal.application.user.impl.UserAccount;
33  import com.hack23.cia.service.api.DataContainer;
34  import com.hack23.cia.service.api.action.application.LogoutRequest;
35  import com.hack23.cia.web.impl.ui.application.action.ViewAction;
36  import com.hack23.cia.web.impl.ui.application.util.UserContextUtil;
37  import com.hack23.cia.web.impl.ui.application.views.common.labelfactory.LabelFactory;
38  import com.hack23.cia.web.impl.ui.application.views.common.menufactory.api.UserHomeMenuItemFactory;
39  import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
40  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.AdminViews;
41  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.CommonsViews;
42  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.PageMode;
43  import com.hack23.cia.web.impl.ui.application.views.pageclicklistener.LogoutClickListener;
44  import com.hack23.cia.web.impl.ui.application.views.pageclicklistener.PageItemPropertyClickListener;
45  import com.vaadin.data.util.BeanItem;
46  import com.vaadin.data.util.BeanItemContainer;
47  import com.vaadin.server.FontAwesome;
48  import com.vaadin.ui.Button;
49  import com.vaadin.ui.Layout;
50  import com.vaadin.ui.MenuBar;
51  import com.vaadin.ui.Panel;
52  import com.vaadin.ui.UI;
53  import com.vaadin.ui.VerticalLayout;
54  
55  /**
56   * The Class UserHomeOverviewPageModContentFactoryImpl.
57   */
58  @Component
59  public final class UserHomeOverviewPageModContentFactoryImpl extends AbstractUserHomePageModContentFactoryImpl {
60  
61  	/** The Constant LOGOUT. */
62  	private static final String LOGOUT = "Logout";
63  
64  	/** The Constant USERHOME. */
65  	private static final String USERHOME = "Userhome:";
66  
67  	/** The Constant OVERVIEW. */
68  	private static final String OVERVIEW = "Overview";
69  
70  	/** The user home menu item factory. */
71  	@Autowired
72  	private UserHomeMenuItemFactory userHomeMenuItemFactory;
73  
74  	/**
75  	 * Instantiates a new user home overview page mod content factory impl.
76  	 */
77  	public UserHomeOverviewPageModContentFactoryImpl() {
78  		super();
79  	}
80  
81  	@Override
82  	public boolean matches(final String page, final String parameters) {
83  		final String pageId = getPageId(parameters);
84  		return NAME.equals(page) && (StringUtils.isEmpty(parameters) || parameters.equals(pageId)
85  				|| parameters.contains(PageMode.OVERVIEW.toString()));
86  	}
87  
88  	@Secured({"ROLE_USER", "ROLE_ADMIN" })
89  	@Override
90  	public Layout createContent(final String parameters, final MenuBar menuBar, final Panel panel) {
91  		final VerticalLayout panelContent = createPanelContent();
92  
93  		final String pageId = getPageId(parameters);
94  
95  
96  		userHomeMenuItemFactory.createUserHomeMenuBar(menuBar, pageId);
97  
98  		LabelFactory.createHeader2Label(panelContent,OVERVIEW);
99  
100 
101 		final Button logoutButton = new Button(LOGOUT,FontAwesome.SIGN_OUT);
102 
103 		final LogoutRequest logoutRequest = new LogoutRequest();
104 		logoutRequest.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());
105 		logoutButton.addClickListener(new LogoutClickListener(logoutRequest));
106 
107 		panelContent.addComponent(logoutButton);
108 
109 
110 		final DataContainer<UserAccount, Long> dataContainer = getApplicationManager().getDataContainer(UserAccount.class);
111 
112 
113 			final Long userIdFromSecurityContext = UserContextUtil.getUserInternalIdFromSecurityContext();
114 
115 			if (userIdFromSecurityContext == null) {
116 				UI.getCurrent().getNavigator().navigateTo(CommonsViews.MAIN_VIEW_NAME);
117 			} else {
118 
119 				final UserAccount userAccount = dataContainer.load(userIdFromSecurityContext);
120 
121 
122 				getFormFactory().addFormPanelTextFields(panelContent, new BeanItem<>(userAccount), UserAccount.class,
123 						Arrays.asList(new String[] { "username","createdDate","email","country","numberOfVisits" }));
124 
125 
126 
127 				final DataContainer<ApplicationActionEvent, Long> eventDataContainer = getApplicationManager().getDataContainer(ApplicationActionEvent.class);
128 
129 
130 				final BeanItemContainer<ApplicationActionEvent> politicianDocumentDataSource = new BeanItemContainer<>(ApplicationActionEvent.class,
131 						eventDataContainer.findOrderedListByProperty(ApplicationActionEvent_.userId,userAccount.getUserId(),ApplicationActionEvent_.createdDate));
132 
133 				getGridFactory().createBasicBeanItemGrid(panelContent, politicianDocumentDataSource,
134 						"ApplicationActionEvent",
135 						new String[] { "hjid", "createdDate", "eventGroup", "applicationOperation","actionName","page","pageMode","elementId", "applicationMessage","errorMessage", "modelObjectVersion" }, new String[] { "hjid","userId","sessionId", "modelObjectId","modelObjectVersion" },
136 						new PageItemPropertyClickListener(AdminViews.ADMIN_APPLICATIONS_EVENTS_VIEW_NAME, "hjid"), null, null);
137 
138 				panelContent.setExpandRatio(logoutButton, ContentRatio.SMALL);
139 
140 			}
141 
142 			panel.setCaption(USERHOME);
143 
144 			getPageActionEventHelper().createPageEvent(ViewAction.VISIT_USER_HOME_VIEW, ApplicationEventGroup.USER, NAME,
145 					parameters, pageId);
146 
147 		return panelContent;
148 
149 	}
150 
151 }