1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
43
44 public class AssociationBO {
45
46
47
48
49 public static final String IMCA_WORLD_COUNTRY_CODE = "";
50
51
52
53
54 public static final String IMCA_AU_COUNTRY_CODE = "AU";
55
56
57
58
59 public static final String IMCA_AT_COUNTRY_CODE = "AT";
60
61
62
63
64 public static final String IMCA_FR_COUNTRY_CODE = "FR";
65
66
67
68
69 public static final String IMCA_IT_COUNTRY_CODE = "IT";
70
71
72
73
74 public static final String IMCA_UK_COUNTRY_CODE = "GB";
75
76
77
78
79 public static final String IMCA_SE_COUNTRY_CODE = "SE";
80
81
82
83
84 public static final String IMCA_CH_COUNTRY_CODE = "CH";
85
86
87
88
89 public static final String IMCA_DE_COUNTRY_CODE = "DE";
90
91
92
93
94 public static final String IMCA_JP_COUNTRY_CODE = "JP";
95
96
97
98
99 public static final String IMCA_DK_COUNTRY_CODE = "DK";
100
101
102
103
104 public static final String IMCA_NL_COUNTRY_CODE = "NL";
105
106
107
108
109 public static final String IMCA_ZA_COUNTRY_CODE = "ZA";
110
111
112
113
114 public static final String IMCA_NZ_COUNTRY_CODE = "NZ";
115
116
117
118
119 public static final String IMCA_US_COUNTRY_CODE = "US";
120
121
122
123
124 public static final String IMCA_FI_COUNTRY_CODE = "FI";
125
126
127
128
129 private AssociationEntity entity;
130
131
132
133
134
135
136
137
138
139 public AssociationBO(EntityManager em, String countryCode) {
140 this(em, countryCode, "");
141 }
142
143
144
145
146
147
148
149
150
151
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
160
161
162
163 public AssociationBO(AssociationEntity entity) {
164 this.setEntity(entity);
165 }
166
167
168
169
170
171
172
173
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
257
258 public AssociationBO() {
259 setEntity(null);
260 }
261
262
263
264
265
266
267 public String getName() {
268 if (entity == null) {
269 return "";
270 } else {
271 return getEntity().toString();
272 }
273 }
274
275
276
277
278
279
280 public AssociationEntity getEntity() {
281 return entity;
282 }
283
284
285
286
287
288
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
331
332
333
334
335 }
336
337 }