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.services;
17  
18  import java.util.Date;
19  import java.util.Hashtable;
20  import java.util.List;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import net.sf.imca.model.PersonBO;
26  import net.sf.imca.model.AssociationBO;
27  import net.sf.imca.model.entities.AssociationDAO;
28  import net.sf.imca.model.entities.AssociationEntity;
29  import net.sf.imca.model.entities.MembershipDAO;
30  import net.sf.imca.model.entities.MembershipEntity;
31  import net.sf.imca.model.entities.PersonEntity;
32  
33  /**
34   * Service User Interface building.
35   *
36   * @author dougculnane
37   */
38  public class UiService extends Service {
39  
40      /**
41       * Default Constructor.
42       */
43      public UiService() {
44          super();
45      }
46  
47      /**
48       * Apache Commons Logger specific to this class.
49       */
50      private Log log = LogFactory.getLog(UiService.class);
51  
52      public AssociationBO[] getAssociationsWithWebsites() {
53          this.startTransaction();
54          AssociationDAO dao = new AssociationDAO();
55          List<AssociationEntity> list = dao.getAssociationsWithWebsites(em);
56          
57          AssociationBO[] associations = new AssociationBO[list.size()];
58          
59          for (int i=0; i < associations.length;i++ ){
60              associations[i] = new AssociationBO(list.get(i));
61          }
62  
63          try {
64              this.endTransaction();
65          } catch (Exception ex) {
66              log.error(ex);
67              return null;
68          }
69          return associations;
70      }
71  
72      public PersonBO[] getMembersWithWebsites() {
73  
74          this.startTransaction();
75  
76          MembershipDAO dao = new MembershipDAO();
77          List<MembershipEntity> list = 
78              dao.findActiveMembersOnDate(em, new Date());
79  
80          // Remove members with no website.
81          for (int i=0; i < list.size(); i++){
82              if (list.get(i).getPerson().getUrl().length() == 0) {
83                  list.remove(i);
84                  i--;
85              }
86          }
87  
88          // One entry per member only.
89          Hashtable<Long, PersonEntity> uniquePersons = 
90                  new Hashtable<Long, PersonEntity>();
91          for (int i=0; i < list.size(); i++){
92              if (uniquePersons.get(new Long(list.get(i).getPerson().getId())) 
93                      != null) {
94                  list.remove(i);
95                  i--;
96              } else {
97                  uniquePersons.put(
98                          new Long(list.get(i).getPerson().getId()), 
99                          list.get(i).getPerson());
100             }
101         }
102 
103         // Entity List to model object array.
104         PersonBO[] members = new PersonBO[list.size()];
105         for (int i=0; i < members.length;i++ ){
106             members[i] = new PersonBO(list.get(i).getPerson());
107         }
108 
109         try {
110             this.endTransaction();
111         } catch (Exception ex) {
112             log.error(ex);
113             return null;
114         }
115         return members;
116     }
117 }