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.Arrays;
19 import java.util.Collection;
20 import java.util.Date;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Vector;
24
25 import javax.mail.MessagingException;
26
27 import net.sf.imca.model.AssociationComparator;
28 import net.sf.imca.model.CommitteeMemberComparator;
29 import net.sf.imca.model.MembershipTypeBO;
30 import net.sf.imca.model.PersonBO;
31 import net.sf.imca.model.AssociationBO;
32 import net.sf.imca.model.entities.AssociationDAO;
33 import net.sf.imca.model.entities.AssociationEntity;
34 import net.sf.imca.model.entities.CommitteeMemberDAO;
35 import net.sf.imca.model.entities.CommitteeMemberEntity;
36 import net.sf.imca.model.entities.FeeEntity;
37 import net.sf.imca.model.entities.MembershipDAO;
38 import net.sf.imca.model.entities.MembershipEntity;
39 import net.sf.imca.model.entities.MembershipTypeEntity;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44
45
46
47
48
49 public class CommitteeService extends Service {
50
51
52
53
54 private Log log = LogFactory.getLog(CommitteeService.class);
55
56
57
58
59
60
61
62 public MembershipEntity[] getMembershipList(PersonBO person) {
63 return getMembershipList(person, new Date());
64 }
65
66
67
68
69
70
71
72
73 public MembershipEntity[] getMembershipList(PersonBO person, Date refDate) {
74
75 this.startTransaction();
76 MembershipEntity[] memberArray = getFilteredMembershipList(person, true, refDate);
77 try {
78 this.endTransaction();
79 } catch (Exception ex) {
80 log.error(ex);
81 return null;
82 }
83 return memberArray;
84
85 }
86
87 public AssociationBO[] getCommitteeMemberships(PersonBO person) {
88 this.startTransaction();
89 AssociationBO[] associations = person.getCommitteeMemberships(em);
90 try {
91 this.endTransaction();
92 } catch (Exception ex) {
93 log.error(ex);
94 return null;
95 }
96 return associations;
97 }
98
99 public AssociationBO[] getOficialIMCACommitteeMemberships(PersonBO person) {
100 this.startTransaction();
101 AssociationBO[] associations = person.getOficialIMCACommitteeMemberships(em);
102 try {
103 this.endTransaction();
104 } catch (Exception ex) {
105 log.error(ex);
106 return null;
107 }
108 return associations;
109 }
110
111 public AssociationBO[] getAllAssociations() {
112 this.startTransaction();
113 AssociationDAO dao = new AssociationDAO();
114 List<AssociationEntity> list = dao.getAllAssociations(em);
115
116 AssociationBO[] associations = new AssociationBO[list.size()];
117
118 for (int i=0; i < associations.length;i++ ){
119 associations[i] = new AssociationBO(list.get(i));
120 }
121 try {
122 this.endTransaction();
123 } catch (Exception ex) {
124 log.error(ex);
125 return null;
126 }
127 return associations;
128 }
129
130
131 public AssociationBO getAssociation(String countryCode, String area) {
132 this.startTransaction();
133 AssociationBO ass = new AssociationBO(em, countryCode, area);
134 try {
135 this.endTransaction();
136 } catch (Exception ex) {
137 log.error(ex);
138 }
139 return ass;
140 }
141
142
143
144 public AssociationBO[] getAssociations(boolean official) {
145 this.startTransaction();
146 AssociationDAO dao = new AssociationDAO();
147 List<AssociationEntity> list = dao.getAssociations(em, official);
148
149 AssociationBO[] array = new AssociationBO[list.size()];
150 for (int i=0; i < array.length; i++) {
151 array[i] = new AssociationBO(list.get(i));
152 }
153
154 Arrays.sort(array, new AssociationComparator());
155
156 return array;
157 }
158
159 public CommitteeMemberEntity[] getImcaWorldAssociationCommittee() {
160 return getAssociationCommittee(AssociationBO.IMCA_WORLD_COUNTRY_CODE, "");
161 }
162
163 public CommitteeMemberEntity[] getAssociationCommittee(String landCode, String area) {
164 this.startTransaction();
165
166 CommitteeMemberDAO dao = new CommitteeMemberDAO();
167 List<CommitteeMemberEntity> list = dao.getAssociationsComitteeInCountry(
168 em, landCode, area);
169
170 CommitteeMemberEntity[] committee = new CommitteeMemberEntity[list.size()];
171
172 for (int i=0; i < committee.length; i++ ){
173 committee[i] = list.get(i);
174 committee[i].getPerson().getAddress();
175 }
176
177 try {
178 this.endTransaction();
179 } catch (Exception ex) {
180 log.error(ex);
181 }
182
183 Arrays.sort(committee, new CommitteeMemberComparator());
184
185 return committee;
186 }
187
188 public boolean getOnImcaCommittee(PersonBO person) {
189
190 if (person == null || person.getEntity() == null) {
191 return false;
192 }
193 if (getOficialIMCACommitteeMemberships(person).length > 0 ){
194 return true;
195 }
196 return false;
197 }
198
199 public void saveNewMembershipType(long associationId, String membershipType,
200 String currencyCode, double fee, Date startDate, Date endDate) {
201
202 this.startTransaction();
203
204 AssociationEntity associationEntity =
205 em.find(AssociationEntity.class, associationId);
206
207 FeeEntity feeEntity = new FeeEntity();
208 feeEntity.setAmount(fee);
209 feeEntity.setCurrency(currencyCode);
210
211
212 MembershipTypeEntity memType = new MembershipTypeEntity();
213 memType.setCurrentFee(feeEntity);
214 memType.setAssociation(associationEntity);
215 memType.setValidFrom(startDate);
216 memType.setValidTo(endDate);
217 memType.setName(membershipType);
218
219 em.persist(feeEntity);
220 em.persist(memType);
221 em.persist(associationEntity);
222 try {
223 this.endTransaction();
224 } catch (Exception ex) {
225 log.error(ex);
226 }
227 }
228
229 public MembershipEntity[] getMembershipRequestList(PersonBO person) {
230
231 this.startTransaction();
232 MembershipEntity[] memberArray = getFilteredMembershipList(person, false);
233 try {
234 this.endTransaction();
235 } catch (Exception ex) {
236 log.error(ex);
237 return null;
238 }
239 return memberArray;
240
241 }
242
243
244
245
246
247
248
249
250
251 private MembershipEntity[] getFilteredMembershipList(PersonBO person, boolean paidFilter) {
252
253 AssociationBO[] associations = person.getCommitteeMemberships(em);
254 Vector<MembershipEntity> members = new Vector<MembershipEntity>();
255
256 for (int i=0; i < associations.length; i++) {
257 Collection<MembershipEntity> mems = associations[i].getEntity().getMembers();
258 Iterator<MembershipEntity> iter = mems.iterator();
259 while(iter.hasNext()) {
260 MembershipEntity membershipEntity = iter.next();
261 if (paidFilter == membershipEntity.getPaid()) {
262 members.add(membershipEntity);
263 }
264 }
265
266 }
267
268 MembershipEntity[] memberArray = new MembershipEntity[members.size()];
269 for (int i=0; i < memberArray.length; i++) {
270 memberArray[i] = members.get(i);
271 }
272 return memberArray;
273 }
274
275
276
277
278
279
280
281
282
283 private MembershipEntity[] getFilteredMembershipList(PersonBO person,
284 boolean paidFilter, Date refDate) {
285
286 AssociationBO[] associations = person.getCommitteeMemberships(em);
287 Vector<MembershipEntity> members = new Vector<MembershipEntity>();
288
289 for (int i=0; i < associations.length; i++) {
290 Collection<MembershipEntity> mems = associations[i].getEntity().getMembers();
291 Iterator<MembershipEntity> iter = mems.iterator();
292 while(iter.hasNext()) {
293 MembershipEntity membershipEntity = iter.next();
294 if (paidFilter == membershipEntity.getPaid() &&
295 dateinRange(membershipEntity.getType().getValidFrom(),
296 membershipEntity.getType().getValidTo(), refDate)){
297 members.add(membershipEntity);
298 }
299 }
300 }
301
302 MembershipEntity[] memberArray = new MembershipEntity[members.size()];
303 for (int i=0; i < memberArray.length; i++) {
304 memberArray[i] = members.get(i);
305 }
306
307 return memberArray;
308 }
309
310
311
312
313
314
315
316
317
318 private boolean dateinRange(Date from, Date to, Date ref) {
319 if (ref.equals(from)) {
320 return true;
321 } else if (ref.equals(to)) {
322 return true;
323 } else {
324 return from.before(ref) && to.after(ref);
325 }
326 }
327
328
329
330
331
332
333
334 public boolean deleteMembershipEntity(long id) {
335 this.startTransaction();
336 MembershipEntity memberEntity = em.find(MembershipEntity.class, id);
337 log.info("Deleting Membership Entity: " + memberEntity);
338 memberEntity.getType().getAssociation().getMembers().remove(memberEntity);
339 em.persist(memberEntity.getType().getAssociation());
340 em.remove(memberEntity);
341 try {
342 this.endTransaction();
343 } catch (Exception ex) {
344 log.error(ex);
345 return false;
346 }
347 return true;
348 }
349
350
351
352
353
354
355
356 public boolean deleteMembershipTypeEntity(long id) {
357 this.startTransaction();
358 MembershipTypeEntity memberTypeEntity = em.find(MembershipTypeEntity.class, id);
359 log.info("Deleting Membership Type Entity: " + memberTypeEntity);
360 em.remove(memberTypeEntity);
361 try {
362 this.endTransaction();
363 } catch (Exception ex) {
364 log.error(ex);
365 return false;
366 }
367 return true;
368 }
369
370
371
372
373
374
375
376 public boolean confirmPaidMembershipEntity(long id) {
377 this.startTransaction();
378 MembershipEntity memberEntity = em.find(MembershipEntity.class, id);
379 log.info("Confirm Paid Membership Entity: " + memberEntity);
380 memberEntity.setPaid(true);
381 memberEntity.setNote(memberEntity.getNote() + "Confirmed Paid " +
382 new Date() + ". \n");
383 em.persist(memberEntity);
384
385 PersonBO person = new PersonBO(em, memberEntity.getPerson().getId());
386 try {
387 person.sendMembershipConfirmationMail(em, memberEntity.getType());
388 } catch (MessagingException e) {
389 log.error("Error sending confirmation email. " + e.getMessage(), e);
390 }
391 try {
392 this.endTransaction();
393 } catch (Exception ex) {
394 log.error(ex);
395 return false;
396 }
397 return true;
398 }
399
400 public AssociationBO getAssociation(long associationId) {
401 this.startTransaction();
402
403 AssociationBO association =
404 new AssociationBO(em.find(AssociationEntity.class, associationId));
405 try {
406 this.endTransaction();
407 } catch (Exception ex) {
408 log.error(ex);
409 return null;
410 }
411 return association;
412 }
413
414 public void saveAssociationData(AssociationEntity entity) {
415 this.startTransaction();
416 em.find(AssociationEntity.class, entity.getId());
417 }
418
419
420
421
422
423
424
425
426 public boolean saveAssociationData(long associationId,
427 String paymentExplanation, String url) {
428
429 this.startTransaction();
430 AssociationEntity entity = em.find(AssociationEntity.class, associationId);
431 entity.setPaymentExplanation(paymentExplanation);
432 entity.setUrl(url);
433 em.persist(entity);
434 try {
435 this.endTransaction();
436 } catch (Exception ex) {
437 log.error(ex);
438 return false;
439 }
440 return true;
441 }
442
443 public MembershipTypeBO getMembershipType(long membershipTypeId) {
444 this.startTransaction();
445
446 MembershipTypeBO membershipType =
447 new MembershipTypeBO(em.find(MembershipTypeEntity.class, membershipTypeId));
448 try {
449 this.endTransaction();
450 } catch (Exception ex) {
451 log.error(ex);
452 return null;
453 }
454 return membershipType;
455 }
456
457 public MembershipTypeBO[] getEditableMembershipTypes(PersonBO person) {
458
459 AssociationBO[] assosiations = this.getOficialIMCACommitteeMemberships(person);
460 Vector<MembershipTypeEntity> memTypes = new Vector<MembershipTypeEntity>();
461
462 this.startTransaction();
463 for (int i=0; i < assosiations.length; i++) {
464 List<MembershipTypeEntity> list = assosiations[i].getJoinableMembershipTypes(em);
465 Iterator<MembershipTypeEntity> iterator = list.iterator();
466 while (iterator.hasNext()){
467 memTypes.add(iterator.next());
468 }
469 }
470 try {
471 this.endTransaction();
472 } catch (Exception ex) {
473 log.error(ex);
474 return null;
475 }
476
477 MembershipTypeBO[] returnArray = new MembershipTypeBO[memTypes.size()];
478 for (int i=0; i < memTypes.size(); i++) {
479 returnArray[i] = new MembershipTypeBO(memTypes.get(i));
480 }
481 return returnArray;
482 }
483
484
485
486
487
488
489
490
491
492
493
494
495 public boolean saveMembershipType(long membershipTypeId, String membershipType,
496 String currencyCode, double fee, Date startDate, Date endDate) {
497
498 this.startTransaction();
499
500 FeeEntity feeEntity = new FeeEntity();
501 feeEntity.setAmount(fee);
502 feeEntity.setCurrency(currencyCode);
503
504 MembershipTypeEntity memType = em.find(MembershipTypeEntity.class, membershipTypeId);
505 memType.setCurrentFee(feeEntity);
506 memType.setValidFrom(startDate);
507 memType.setValidTo(endDate);
508 memType.setName(membershipType);
509
510 em.persist(feeEntity);
511 em.persist(memType);
512 try {
513 this.endTransaction();
514 } catch (Exception ex) {
515 log.error(ex);
516 return false;
517 }
518 return true;
519 }
520
521 public boolean getHasLinkedMemberships(long membershipTypeId) {
522
523 MembershipTypeBO membershipType = getMembershipType(membershipTypeId);
524 this.startTransaction();
525
526 MembershipDAO dao = new MembershipDAO();
527 boolean hasLinkedMemberships =
528 dao.getHasLinkedMemberships(em, membershipType.getEntity());
529 try {
530 this.endTransaction();
531 } catch (Exception ex) {
532 log.error(ex);
533 return false;
534 }
535 return hasLinkedMemberships;
536 }
537
538 public boolean addMember(long membershipTypeId, String email) {
539
540 MembershipTypeBO membershipType = getMembershipType(membershipTypeId);
541 this.startTransaction();
542 PersonBO person = new PersonBO(em, email);
543 boolean success = false;
544 if (person.getEntity() != null && membershipType.getEntity() != null) {
545 MembershipEntity memEnt = new MembershipEntity();
546 memEnt.setPerson(person.getEntity());
547 memEnt.setFee(membershipType.getEntity().getCurrentFee());
548 memEnt.setType(membershipType.getEntity());
549 memEnt.setPaid(true);
550 memEnt.setNote("Added member:" + (new Date()).toString());
551 membershipType.getEntity().getAssociation().getMembers().add(memEnt);
552
553 AssociationEntity association = em.find(AssociationEntity.class, membershipType.getEntity().getAssociation().getId());
554 association.getMembers().add(memEnt);
555
556 em.persist(memEnt);
557 em.persist(association);
558
559 success = true;
560 }
561 try {
562 this.endTransaction();
563 } catch (Exception ex) {
564 log.error(ex);
565 return false;
566 }
567 return success;
568 }
569 }