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.model;
17  
18  import java.util.Collection;
19  import java.util.List;
20  import java.util.Locale;
21  import java.util.Vector;
22  
23  import javax.mail.internet.AddressException;
24  import javax.mail.internet.InternetAddress;
25  import javax.persistence.EntityManager;
26  
27  import org.w3c.dom.Node;
28  import org.w3c.dom.NodeList;
29  
30  import net.sf.imca.model.entities.AssociationDAO;
31  import net.sf.imca.model.entities.AssociationEntity;
32  import net.sf.imca.model.entities.CommitteeMemberEntity;
33  import net.sf.imca.model.entities.CommitteeMemberDAO;
34  import net.sf.imca.model.entities.PersonEntity;
35  import net.sf.imca.model.entities.PersonDAO;
36  import net.sf.imca.model.entities.MembershipTypeEntity;
37  
38  /**
39   * Business Object representing the properties, persistence and functionality of
40   * the IMCA domain object.
41   *
42   * @author dougculnane
43   */
44  public class AssociationBO {
45  
46      /**
47       * The country code to use for the IMCA World Association
48       */
49      public static final String IMCA_WORLD_COUNTRY_CODE = "";
50  
51      /**
52       * The country code to use for the Australian Association
53       */
54      public static final String IMCA_AU_COUNTRY_CODE = "AU";
55  
56      /**
57       * The country code to use for the Austrian Association
58       */
59      public static final String IMCA_AT_COUNTRY_CODE = "AT";
60  
61      /**
62       * The country code to use for the French Association
63       */
64      public static final String IMCA_FR_COUNTRY_CODE = "FR";
65  
66      /**
67       * The country code to use for the Italian Association
68       */
69      public static final String IMCA_IT_COUNTRY_CODE = "IT";
70  
71      /**
72       * The country code to use for the British Association
73       */
74      public static final String IMCA_UK_COUNTRY_CODE = "GB";
75  
76      /**
77       * The country code to use for the Swedish Association
78       */
79      public static final String IMCA_SE_COUNTRY_CODE = "SE";
80  
81      /**
82       * The country code to use for the Swiss Association
83       */
84      public static final String IMCA_CH_COUNTRY_CODE = "CH";
85  
86      /**
87       * The country code to use for the German Association
88       */
89      public static final String IMCA_DE_COUNTRY_CODE = "DE";
90  
91      /**
92       * The country code to use for the Japanese Association
93       */
94      public static final String IMCA_JP_COUNTRY_CODE = "JP";
95  
96      /**
97       * The country code to use for the Denmark Association
98       */
99      public static final String IMCA_DK_COUNTRY_CODE = "DK";
100 
101     /**
102      * The country code to use for the Dutch Association
103      */
104     public static final String IMCA_NL_COUNTRY_CODE = "NL";
105 
106     /**
107      * The country code to use for the South African Association
108      */
109     public static final String IMCA_ZA_COUNTRY_CODE = "ZA";
110 
111     /**
112      * The country code to use for the New Zealand Association
113      */
114     public static final String IMCA_NZ_COUNTRY_CODE = "NZ";
115 
116     /**
117      * The country code to use for the United States Association
118      */
119     public static final String IMCA_US_COUNTRY_CODE = "US";
120 
121     /**
122      * The country code to use for the Finish Association
123      */
124     public static final String IMCA_FI_COUNTRY_CODE = "FI";
125 
126     /**
127      * Entity for persisting this Business Object.
128      */
129     private AssociationEntity entity;
130 
131     /**
132      * Constucter for a country specific association.
133      * 
134      * @param em
135      *            EntityManager for database interation.
136      * @param countryCode
137      *            2 digit iso country code
138      */
139     public AssociationBO(EntityManager em, String countryCode) {
140         this(em, countryCode, "");
141     }
142 
143     /**
144      * Constructor for a country and area specific association.
145      * 
146      * @param em
147      *            EntityManager for database iteration.
148      * @param countryCode
149      *            2 digit ISO country code
150      * @param area
151      *            Name of the area within the country for association
152      */
153     public AssociationBO(EntityManager em, String countryCode, String area) {
154         AssociationDAO associationDAO = new AssociationDAO();
155         this.setEntity(associationDAO.findAssociation(em, countryCode, area));
156     }
157 
158     /**
159      * Constructor with state entity.
160      * 
161      * @param entity
162      */
163     public AssociationBO(AssociationEntity entity) {
164         this.setEntity(entity);
165     }
166 
167     /**
168      * Constructor with Persistence Entity Manager and XML node list of data.
169      * 
170      * @param em
171      *            Persistence Entity Manager.
172      * @param nodeList
173      *            XML node list with data.
174      */
175     public AssociationBO(EntityManager em, NodeList nodeList) {
176 
177         String countryCode = "";
178         boolean isIMCA = false;
179         String url = "";
180         Vector<CommitteeMemberEntity> committee = new Vector<CommitteeMemberEntity>();
181 
182         PersonDAO personDao = new PersonDAO();
183         CommitteeMemberDAO committeeMemberDAO = new CommitteeMemberDAO();
184 
185         for (int i = 0; i < nodeList.getLength(); i++) {
186             Node node = nodeList.item(i);
187             if ("landcode".equals(node.getNodeName())) {
188                 countryCode = PersonBO.convertOldXmlCountryCodesToISO(node
189                         .getChildNodes().item(0).getNodeValue());
190             } else if ("imca".equals(node.getNodeName())) {
191                 if ("true".equals(node.getChildNodes().item(0).getNodeValue())) {
192                     isIMCA = true;
193                 }
194             } else if ("url".equals(node.getNodeName())) {
195                 if (node.getChildNodes().item(0) != null) {
196                     url = node.getChildNodes().item(0).getNodeValue();
197                 }
198             } else if ("contacts".equals(node.getNodeName())) {
199                 NodeList contactsNodes = node.getChildNodes();
200                 for (int k = 0; k < contactsNodes.getLength(); k++) {
201                     Node contactNode = contactsNodes.item(k);
202                     String positionName = "";
203                     String xmlId = "";
204                     if ("rider".equals(contactNode.getNodeName())) {
205                         NodeList riderNodes = contactNode.getChildNodes();
206                         for (int j = 0; j < riderNodes.getLength(); j++) {
207                             Node n = riderNodes.item(j);
208                             if ("id".equals(n.getNodeName())) {
209                                 xmlId = n.getChildNodes().item(0)
210                                         .getNodeValue();
211                             } else if ("position".equals(n.getNodeName())) {
212                                 positionName = n.getChildNodes().item(0)
213                                         .getNodeValue();
214                             }
215                         }
216                         PersonEntity person = personDao.findPersonByXmlId(em,
217                                 xmlId);
218 
219                         CommitteeMemberEntity comitteeMember = new CommitteeMemberEntity();
220 
221                         comitteeMember.setPositionName(positionName);
222                         comitteeMember.setPerson(person);
223                         committee.add(comitteeMember);
224 
225                     }
226                 }
227             }
228         }
229         AssociationDAO associationDAO = new AssociationDAO();
230 
231         this.setEntity(associationDAO.findAssociation(em, countryCode, ""));
232 
233         if (this.getEntity() == null) {
234             this.setEntity(new AssociationEntity());
235         }
236         this.getEntity().setIsoCountryCode(countryCode);
237         this.getEntity().setIsOfficiallyImca(isIMCA);
238         this.getEntity().setUrl(url);
239         this.getEntity().setCommittee(new Vector<CommitteeMemberEntity>());
240         em.persist(this.getEntity());
241 
242         for (int i = 0; i < committee.size(); i++) {
243             CommitteeMemberEntity cmEnt = committee.get(i);
244             PersonEntity person = cmEnt.getPerson();
245             cmEnt = committeeMemberDAO.find(em, cmEnt.getPositionName(), this
246                     .getEntity());
247             cmEnt.setPerson(person);
248             em.persist(cmEnt);
249             this.getEntity().getCommittee().add(cmEnt);
250         }
251         em.persist(this.getEntity());
252 
253     }
254 
255     /**
256      * Default Constructor.
257      */
258     public AssociationBO() {
259         setEntity(null);
260     }
261 
262     /**
263      * Return the human friendly name of the Association.
264      * 
265      * @return
266      */
267     public String getName() {
268         if (entity == null) {
269             return "";
270         } else {
271             return getEntity().toString();
272         }
273     }
274 
275     /**
276      * Get the persistence entity.
277      * 
278      * @return The persistence entity for this Business Object.
279      */
280     public AssociationEntity getEntity() {
281         return entity;
282     }
283 
284     /**
285      * Set the persistence entity.
286      * 
287      * @param entity
288      *            The new persistence entity for this Business Object.
289      */
290     public void setEntity(AssociationEntity entity) {
291         this.entity = entity;
292     }
293 
294     public List<MembershipTypeEntity> getJoinableMembershipTypes(
295             EntityManager em) {
296         AssociationDAO associationDAO = new AssociationDAO();
297         List<MembershipTypeEntity> list = associationDAO.findMembershipTypes(em, this.getEntity());
298         if (this.getEntity() == null 
299                || !this.getEntity().getIsOfficiallyImca()) {
300             AssociationBO worldAssociation = associationDAO.getWorldAssociation(em);
301             List<MembershipTypeEntity> worldMems = associationDAO.findMembershipTypes(em, worldAssociation.getEntity());
302             for (int i=0; i < worldMems.size(); i++) {
303                 list.add(worldMems.get(i));
304             }
305         } 
306          return list;
307     }
308 
309     public InternetAddress[] getCommitteeEmails() throws AddressException {
310 
311         Collection<CommitteeMemberEntity> collection = this.getEntity()
312                 .getCommittee();
313         java.util.Iterator<CommitteeMemberEntity> iter = collection.iterator();
314 
315         InternetAddress[] emails = new InternetAddress[collection.size()];
316         for (int i = 0; i < emails.length; i++) {
317             emails[i] = new InternetAddress(iter.next().getPerson().getEmail());
318         }
319 
320         return emails;
321     }
322     
323     public String getCountryName(){
324         Locale loc = new Locale("", getEntity().getIsoCountryCode());
325         return loc.getDisplayCountry(Locale.ENGLISH);
326     }
327     
328     public String getShortName(){
329         return (getCountryName() + " " + this.getEntity().getArea()).trim();
330 //        if (this.getEntity().getIsOfficiallyImca()) {
331 //            return ("IMCA " + getCountryName() + " " + this.getEntity().getArea()).trim();
332 //        } else {
333 //            return ("Fleet " + getCountryName() + " " + this.getEntity().getArea()).trim();
334 //        }
335     }
336 
337 }