1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.imca.services;
17
18 import java.util.Date;
19 import java.util.List;
20 import java.util.Iterator;
21
22 import javax.mail.MessagingException;
23 import javax.persistence.Query;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import net.sf.imca.model.AssociationBO;
29 import net.sf.imca.model.PersonBO;
30 import net.sf.imca.model.MembershipBO;
31 import net.sf.imca.model.entities.AssociationDAO;
32 import net.sf.imca.model.entities.AssociationEntity;
33 import net.sf.imca.model.entities.MembershipEntity;
34 import net.sf.imca.model.entities.MembershipTypeEntity;
35
36
37
38
39
40
41 public class JoinImcaService extends Service {
42
43
44
45
46 private Log log = LogFactory.getLog(JoinImcaService.class);
47
48 @SuppressWarnings("unchecked")
49 public AssociationBO[] selectAssociations(String countryCode){
50
51 startTransaction();
52 AssociationDAO aDAO = new AssociationDAO();
53 List<AssociationEntity> list = aDAO.getAssociationsInCountry(em, countryCode);
54
55 AssociationBO[] associations = new AssociationBO[list.size()];
56 Iterator<AssociationEntity> it = list.iterator();
57 for (int i=0; i < associations.length; i++) {
58 AssociationEntity ent = it.next();
59 associations[i] = new AssociationBO(em, ent.getIsoCountryCode(),
60 ent.getArea());
61 }
62 try {
63 this.endTransaction();
64 } catch (Exception ex) {
65 log.error(ex);
66 return null;
67 }
68 return associations;
69 }
70
71 public AssociationBO selectAssociation(String countryCode, String area){
72
73 startTransaction();
74 AssociationBO association = new AssociationBO(em, countryCode, area);
75 try {
76 this.endTransaction();
77 } catch (Exception ex) {
78 log.error(ex);
79 return null;
80 }
81 return association;
82 }
83
84 public boolean selectMembership(String associationId, String typeId){
85 return false;
86 }
87
88 public String[] getAreasForCountry(String countryCode) {
89
90 startTransaction();
91 AssociationDAO associationDAO = new AssociationDAO();
92 List<AssociationEntity> list = associationDAO.getAssociationsInCountry(em, countryCode);
93 String[] areas = new String[list.size()];
94 for (int i=0; i < list.size(); i++) {
95 areas[i] = list.get(i).getArea();
96 }
97 try {
98 this.endTransaction();
99 } catch (Exception ex) {
100 log.error(ex);
101 return null;
102 }
103 return areas;
104 }
105
106 public List<MembershipTypeEntity> getJoinableMembershipTypes(
107 String countryCode, String area) {
108 this.startTransaction();
109
110 AssociationBO association = new AssociationBO(em, countryCode, area);
111 List<MembershipTypeEntity> list = association.getJoinableMembershipTypes(em);
112 try {
113 this.endTransaction();
114 } catch (Exception ex) {
115 log.error(ex);
116 return null;
117 }
118 return list;
119 }
120
121 public boolean makeMembershipRequest(PersonBO person, String countryCode,
122 String area, long membershipTypeId) {
123
124 this.startTransaction();
125
126 MembershipTypeEntity memType = em.find(MembershipTypeEntity.class, membershipTypeId);
127 MembershipEntity mem = new MembershipEntity();
128 mem.setPaid(false);
129 mem.setType(memType);
130 mem.setFee(memType.getCurrentFee());
131 mem.setPerson(person.getEntity());
132 mem.setNote("New Membership Request. Date: " + new Date() + ". \n");
133 if ( memType.getAssociation().getMembers() == null) {
134 memType.getAssociation().setMembers(
135 new java.util.HashSet<MembershipEntity>());
136 }
137 memType.getAssociation().getMembers().add(mem);
138
139 em.persist(mem);
140 em.persist(memType.getAssociation());
141
142
143 try {
144 person.sendMembershipRequestMail(em, memType);
145 } catch (MessagingException e) {
146 log.error("Error sending Membership Request Mail: " +
147 e.getMessage(), e);
148 }
149 try {
150 this.endTransaction();
151 } catch (Exception ex) {
152 log.error(ex);
153 return false;
154 }
155 log.info("Membership Request by " + mem.toString());
156 return true;
157 }
158
159 public AssociationBO getJoinableAssociation(String countryCode, String area) {
160
161 this.startTransaction();
162 AssociationBO association = new AssociationBO(em, countryCode, area);
163 if (association == null) {
164 association = getWorldAssociationBO();
165 } else if (association.getEntity() == null) {
166 association = getWorldAssociationBO();
167 } else if (!association.getEntity().getIsOfficiallyImca()) {
168 association = getWorldAssociationBO();
169 }
170 try {
171 this.endTransaction();
172 } catch (Exception ex) {
173 log.error(ex);
174 return null;
175 }
176 return association;
177 }
178
179 private AssociationBO getWorldAssociationBO(){
180 return new AssociationBO(em, AssociationBO.IMCA_WORLD_COUNTRY_CODE, "");
181 }
182
183 public MembershipTypeEntity getMembershipTypeEntity(long membershipTypeId) {
184 this.startTransaction();
185 MembershipTypeEntity memType = em.find(MembershipTypeEntity.class, membershipTypeId);
186 try {
187 this.endTransaction();
188 } catch (Exception ex) {
189 log.error(ex);
190 return null;
191 }
192 return memType;
193 }
194
195 @SuppressWarnings("unchecked")
196 public MembershipBO[] getImcaMemberships(PersonBO personBO) {
197 this.startTransaction();
198
199 Query query = em.createNamedQuery("findMembershipsForPerson");
200 query.setParameter("person", personBO.getEntity());
201 List<MembershipEntity> list = query.getResultList();
202 try {
203 this.endTransaction();
204 } catch (Exception ex) {
205 log.error(ex);
206 return null;
207 }
208 MembershipBO[] mems = new MembershipBO[list.size()];
209 for (int i=0; i < list.size(); i++){
210 mems[i] = new MembershipBO(list.get(i));
211 }
212 return mems;
213 }
214
215 public boolean getActiveMember(PersonBO personBO) {
216 MembershipBO[] mems = getImcaMemberships(personBO);
217 for (int i=0; i < mems.length; i++){
218 if (mems[i].getStatus().equals(MembershipBO.STATUS_ACTIVE)){
219 return true;
220 }
221 }
222 return false;
223 }
224 }