View Javadoc

1   /*
2    * Copyright 2007 The International Moth Class Association (IMCA)
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  package net.sf.imca.web.backingbeans;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import javax.faces.model.SelectItem;
22  
23  import net.sf.imca.model.entities.MembershipTypeEntity;
24  import net.sf.imca.model.AssociationBO;
25  import net.sf.imca.model.ImcaUtils;
26  import net.sf.imca.services.JoinImcaService;
27  
28  /**
29   * This is the web application backing bean for the join the IMCA use cases. 
30   *
31   * @author dougculnane
32   */
33  public class JoinBean {
34  
35      private String area = "";
36  
37      private String countryCode = Utils.getWebUser().getPerson().getDefaultCountryCode();
38  
39      private Long membershipTypeId;
40  
41      /**
42       * User feedback message.
43       */
44      private String userFeedbackMessage = "";
45      
46      private String membershipTypeInfo = "";
47      
48      private String fee = "";
49      
50      private boolean savedRequest = false;
51      
52      private SelectItem[] membershipTypeItems = new SelectItem[0];
53      
54      private SelectItem[] areaItems = new SelectItem[0];
55      
56      private JoinImcaService service = new JoinImcaService();
57      
58      public String actionJoin() {
59          savedRequest = false;
60          if (selectionOK()) {
61              userFeedbackMessage = "";
62              return Utils.ACTION_CONFIRM;
63          }
64          if (membershipTypeItems.length == 0) {
65              userFeedbackMessage = Utils.getMessage("msgNoMembershipTypesForLocalIMCA");
66          } else {
67              userFeedbackMessage = Utils.getMessage("msgSelectAssociationAndType");
68          }
69          return null;
70      }
71      
72      public String actionBackToSelect(){
73          userFeedbackMessage = "";
74          return Utils.ACTION_BACK;
75      }
76      
77      public String actionConfirm() {
78          savedRequest = false;
79          if (!selectionOK()) {
80              return Utils.ACTION_BACK;
81          }
82          savedRequest = service.makeMembershipRequest(Utils.getWebUser().getPerson(), 
83                  countryCode, area, membershipTypeId);
84          userFeedbackMessage = Utils.getMessage("msgMembershipRequestSuccess");
85          return Utils.ACTION_SUCCESS;
86      }
87      
88      
89      public SelectItem[] getAreaItems(){
90          return areaItems;
91      }
92      private void updateAreaItems() {
93          if (countryCode != null) {
94              String[] areas = service.getAreasForCountry(countryCode);
95              
96              areaItems = new SelectItem[areas.length];
97              for (int i=0; i < areaItems.length; i++) {
98                  areaItems[i] = new SelectItem(areas[i]);
99              }
100         }
101     }
102     
103     public SelectItem[] getMembershipTypeItems() {
104         return membershipTypeItems;
105     }
106     
107     private void updateMembershipTypeItems(){
108         
109             
110         List<MembershipTypeEntity> list = 
111             service.getJoinableMembershipTypes(countryCode, area);
112         
113         membershipTypeItems = new SelectItem [list.size()];
114         Iterator<MembershipTypeEntity> iter = list.iterator();
115         for (int i=0; i < membershipTypeItems.length; i++){
116             MembershipTypeEntity memType =  iter.next();
117             membershipTypeItems[i] = new SelectItem(new Long(memType.getId()), 
118                     memType.getName() + " (" + 
119                     ImcaUtils.formatDate(memType.getValidFrom()) + " - " + 
120                     ImcaUtils.formatDate(memType.getValidTo()) + ")");
121         }
122         
123         if (membershipTypeItems.length == 0) {
124             userFeedbackMessage = Utils.getMessage("msgNoMembershipTypesForLocalIMCA");
125         }
126     }
127     
128     private boolean selectionOK() {
129         
130         if (membershipTypeId == null){
131             return false;
132         }
133         if ("".equals(countryCode)){
134             return false;
135         }
136         
137         List<MembershipTypeEntity> memTypes = 
138             service.getJoinableMembershipTypes(countryCode, area);
139         
140         for (int i=0; i < memTypes.size(); i++) {
141             MembershipTypeEntity type = memTypes.get(i);
142             if (type.getId() == membershipTypeId.intValue()) {
143                 return true;
144             }
145         }
146         return false;
147     }
148 
149     public boolean getCountryHasAreas(){
150         if (getAreaItems().length > 1) {
151             return true;
152         } else {
153             return false;
154         }
155     }
156     public boolean getCountryHasTypes(){
157         if (this.getMembershipTypeItems().length >= 1) {
158             return true;
159         } else {
160             return false;
161         }
162     }
163     
164     
165     public String getArea() {
166         return area;
167     }
168 
169     public void setArea(String area) {
170         this.area = area;
171     }
172 
173     public String getCountryCode() {
174         return countryCode;
175     }
176 
177     public void setCountryCode(String countryCode) {
178         this.countryCode = countryCode;
179         try {
180             updateAreaItems();
181             updateMembershipTypeItems();
182         } catch (Exception e)  {
183             e.printStackTrace(System.out);
184         }
185     }
186 
187     public String getUserFeedbackMessage() {
188         return userFeedbackMessage;
189     }
190 
191     public void setUserFeedbackMessage(String userFeedbackMessage) {
192         this.userFeedbackMessage = userFeedbackMessage;
193     }
194 
195     public String getMembershipTypeInfo() {
196         return membershipTypeInfo;
197     }
198         
199     public AssociationBO getAssociation() {
200         return service.getJoinableAssociation(countryCode, area);
201     }
202 
203     public Long getMembershipTypeId() {
204         return membershipTypeId;
205     }
206 
207     public void setMembershipTypeId(Long membershipTypeId) {
208         this.membershipTypeId = membershipTypeId;
209         updateInfoFields();
210     }
211 
212     private void updateInfoFields() {
213         MembershipTypeEntity memType = service.getMembershipTypeEntity(
214                 membershipTypeId.intValue());
215         
216         this.fee = memType.getCurrentFee().toString();
217         this.membershipTypeInfo = memType.toString();
218         
219     }
220 
221     public String getPaymentInfo() {
222         return getAssociation().getEntity().getPaymentExplanation();
223     }
224 
225     public String getFee() {
226         return fee;
227     }
228 
229     public boolean getSavedRequest() {
230         return savedRequest;
231     }
232 
233 
234 }