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   import java.util.List;
9   
10  import com.hack23.cia.model.application.dto.common.UserSessionDTO;
11  import com.hack23.cia.model.application.impl.common.ActionEvent;
12  import com.hack23.cia.model.application.impl.common.UserSession;
13  import com.hack23.cia.model.application.impl.user.TopListActionEvent;
14  import com.hack23.cia.model.sweden.impl.ParliamentMember;
15  import com.hack23.cia.service.api.common.ServiceResponse;
16  import com.hack23.cia.service.api.user.TopListRequest;
17  import com.hack23.cia.service.api.user.TopListResponse;
18  import com.hack23.cia.service.impl.common.ParliamentService;
19  import com.hack23.cia.service.impl.common.UserSessionService;
20  
21  /***
22   * The Class TopListRequestService.
23   */
24  public class TopListRequestService extends
25          AbstractUserRequestService<TopListRequest> {
26  
27      /*** The number in top list. */
28      private final int numberInTopList;
29  
30      /*** The parliament service. */
31      private final ParliamentService parliamentService;
32  
33      /***
34       * Instantiates a new top list request service.
35       *
36       * @param userSessionService the user session service
37       * @param parliamentService the parliament service
38       * @param numberInTopList the number in top list
39       */
40      public TopListRequestService(final UserSessionService userSessionService,
41              final ParliamentService parliamentService, final int numberInTopList) {
42          super(userSessionService);
43          this.parliamentService = parliamentService;
44          this.numberInTopList = numberInTopList;
45      }
46  
47      /*
48       * (non-Javadoc)
49       * 
50       * @see
51       * com.hack23.cia.service.impl.common.AbstractGenericService#createActionEvent
52       * (com.hack23.cia.service.api.common.AbstractServiceRequest,
53       * com.hack23.cia.model.application.UserSession)
54       */
55      @Override
56      public final ActionEvent createActionEvent(final TopListRequest request,
57              final UserSession userSession) {
58          return new TopListActionEvent(new Date(), userSession, request
59                  .getOperation());
60      }
61  
62      /*
63       * (non-Javadoc)
64       * 
65       * @see com.hack23.cia.service.common.ServiceHandler#getSupportedService()
66       */
67      @SuppressWarnings("unchecked")
68      @Override
69      public final Class getSupportedService() {
70          return TopListRequest.class;
71      }
72  
73      /*
74       * (non-Javadoc)
75       * 
76       * @seecom.hack23.cia.service.common.AbstractGenericServiceHandler#
77       * handleServiceRequest(com.hack23.cia.service.api.AbstractServiceRequest)
78       */
79      @Override
80      public final ServiceResponse handleServiceRequest(
81              final TopListRequest request, final UserSessionDTO userSession) {
82          List<ParliamentMember> members = null;
83          	
84          switch (request.getOperation()) {
85          case TopAbsent:
86              members = parliamentService
87                      .getCurrentTopListAbsent(numberInTopList);
88              break;
89          case TopPresent:
90              members = parliamentService
91                      .getCurrentTopListPresent(numberInTopList);
92              break;
93          case TopWinners:
94              members = parliamentService
95                      .getCurrentTopListWinners(numberInTopList);
96              break;
97          case TopLosers:
98              members = parliamentService
99                      .getCurrentTopListLosers(numberInTopList);
100             break;
101         case TopLoyals:
102             members = parliamentService.getCurrentTopListLoyal(numberInTopList);
103             break;
104         case TopRebels:
105             members = parliamentService
106                     .getCurrentTopListRebels(numberInTopList);
107             break;
108         default:
109             break;
110         }
111 
112         final TopListResponse partyResponse = new TopListResponse(userSession,
113                 members);
114         return partyResponse;
115     }
116 }