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.pageclicklistener;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import org.springframework.web.context.request.RequestContextHolder;
24  
25  import com.ejt.vaadin.loginform.LoginForm.LoginEvent;
26  import com.ejt.vaadin.loginform.LoginForm.LoginListener;
27  import com.hack23.cia.service.api.action.application.LoginRequest;
28  import com.hack23.cia.service.api.action.application.LoginResponse;
29  import com.hack23.cia.service.api.action.common.ServiceResponse.ServiceResult;
30  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.UserViews;
31  import com.vaadin.ui.Notification;
32  import com.vaadin.ui.UI;
33  
34  /**
35   * The Class ApplicationLoginListener.
36   */
37  public final class ApplicationLoginListener implements LoginListener {
38  
39  	/** The Constant LOG_MSG_LOGIN_REQUEST_FAILURE. */
40  	private static final String LOG_MSG_LOGIN_REQUEST_FAILURE = "LoginRequest {} failure";
41  
42  	/** The Constant ERROR_MESSAGE. */
43  	private static final String ERROR_MESSAGE = "Error message";
44  
45  	/** The Constant LOGIN_FAILED. */
46  	private static final String LOGIN_FAILED = "Login failed";
47  
48  	/** The Constant LOG_MSG_LOGIN_REQUEST. */
49  	private static final String LOG_MSG_LOGIN_REQUEST = "LoginRequest {}";
50  
51  	/** The Constant serialVersionUID. */
52  	private static final long serialVersionUID = 1L;
53  
54  	/** The Constant LOGGER. */
55  	private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationLoginListener.class);
56  
57  	/** The login request. */
58  	private final LoginRequest loginRequest;
59  
60  	/**
61  	 * Instantiates a new application login listener.
62  	 *
63  	 * @param loginRequest
64  	 *            the login request
65  	 */
66  	public ApplicationLoginListener(final LoginRequest loginRequest) {
67  		this.loginRequest = loginRequest;
68  	}
69  
70  
71  	@Override
72  	public void onLogin(final LoginEvent event) {
73  		loginRequest.setEmail(event.getUserName());
74  		loginRequest.setUserpassword(event.getPassword());
75  		loginRequest.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());
76  
77  		final LoginResponse response = (LoginResponse) ApplicationMangerAccess.getApplicationManager().service(loginRequest);
78  		if (ServiceResult.SUCCESS == response.getResult()) {
79  			LOGGER.info(LOG_MSG_LOGIN_REQUEST,event.getUserName());
80  
81  			UI.getCurrent().getNavigator().navigateTo(UserViews.USERHOME_VIEW_NAME);
82  		} else {
83  			Notification.show(LOGIN_FAILED,
84  	                  ERROR_MESSAGE,
85  	                  Notification.Type.WARNING_MESSAGE);
86  			LOGGER.info(LOG_MSG_LOGIN_REQUEST_FAILURE,event.getUserName());
87  		}
88  
89  	}
90  }