View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
4   */
5   package com.hack23.cia.service.impl.user;
6   
7   import java.util.Date;
8   
9   import com.hack23.cia.model.application.dto.common.UserSessionDTO;
10  import com.hack23.cia.model.application.impl.common.ActionEvent;
11  import com.hack23.cia.model.application.impl.common.UserSession;
12  import com.hack23.cia.model.application.impl.user.UserAccountActionEvent;
13  import com.hack23.cia.model.application.impl.user.UserAccountActionEvent.Operation;
14  import com.hack23.cia.service.api.common.ErrorResponse;
15  import com.hack23.cia.service.api.common.ServiceResponse;
16  import com.hack23.cia.service.api.user.LoginRequest;
17  import com.hack23.cia.service.api.user.UserAccountResponse;
18  import com.hack23.cia.service.impl.common.ParliamentService;
19  import com.hack23.cia.service.impl.common.UserSessionService;
20  
21  /***
22   * The Class LoginRequestService.
23   */
24  public class LoginRequestService extends
25          AbstractUserRequestService<LoginRequest> {
26  
27  	  /*** The parliament service. */
28      private final ParliamentService parliamentService;
29  
30      /***
31       * Instantiates a new login request service.
32       *
33       * @param userSessionService the user session service
34       * @param parliamentService the parliament service
35       */
36      public LoginRequestService(final UserSessionService userSessionService,final ParliamentService parliamentService) {
37          super(userSessionService);
38  		this.parliamentService = parliamentService;
39      }
40  
41      /*
42       * (non-Javadoc)
43       * 
44       * @see
45       * com.hack23.cia.service.impl.common.AbstractGenericService#createActionEvent
46       * (com.hack23.cia.service.api.common.AbstractServiceRequest,
47       * com.hack23.cia.model.application.UserSession)
48       */
49      @Override
50      public final ActionEvent createActionEvent(final LoginRequest request,
51              final UserSession userSession) {
52          return new UserAccountActionEvent(new Date(), userSession,
53                  request.getOperation());
54      }
55  
56      /*
57       * (non-Javadoc)
58       * 
59       * @see com.hack23.cia.service.common.ServiceHandler#getSupportedService()
60       */
61      @SuppressWarnings("unchecked")
62      @Override
63      public final Class getSupportedService() {
64          return LoginRequest.class;
65      }
66  
67      /*
68       * (non-Javadoc)
69       * 
70       * @seecom.hack23.cia.service.common.AbstractGenericServiceHandler#
71       * handleServiceRequest(com.hack23.cia.service.api.AbstractServiceRequest)
72       */
73      @Override
74      public final ServiceResponse handleServiceRequest(
75              final LoginRequest request, final UserSessionDTO userSession) {
76          if (request.getOperation().equals(Operation.Process_login)) {
77              final UserSession loggedInUserSession = this.getUserSessionService().login(
78                      request.getUserName(), request.getPassword(), userSession.getUserSession());
79      
80              if (loggedInUserSession != null) {
81                  return new UserAccountResponse(new UserSessionDTO(loggedInUserSession),parliamentService.getPoliticalParties());
82              } else {
83                  return new ErrorResponse(userSession,
84                          "Invalid Username/password");
85              }
86          } else {
87              return new UserAccountResponse(userSession,parliamentService.getPoliticalParties());
88          }
89      }
90  }