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