1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
package org.directdemocracyportal.democracy.model.world; |
6 | |
|
7 | |
import java.util.HashMap; |
8 | |
import java.util.HashSet; |
9 | |
import java.util.Map; |
10 | |
import java.util.Set; |
11 | |
|
12 | |
import javax.persistence.Column; |
13 | |
import javax.persistence.Entity; |
14 | |
import javax.persistence.JoinColumn; |
15 | |
import javax.persistence.ManyToOne; |
16 | |
import javax.persistence.Transient; |
17 | |
|
18 | |
import org.directdemocracyportal.democracy.model.core.Agent; |
19 | |
import org.directdemocracyportal.democracy.model.core.GroupAgent; |
20 | |
import org.directdemocracyportal.democracy.model.core.Resource; |
21 | |
import org.directdemocracyportal.democracy.model.core.Role; |
22 | |
import org.hibernate.annotations.Cache; |
23 | |
import org.hibernate.annotations.CacheConcurrencyStrategy; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
@Entity |
29 | |
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) |
30 | |
public class Organisation extends GroupAgent |
31 | |
{ |
32 | |
|
33 | |
|
34 | |
private static final long serialVersionUID = -2353229177440934376L; |
35 | |
|
36 | |
|
37 | |
private String abbreviation; |
38 | |
|
39 | |
|
40 | |
private Country country; |
41 | |
|
42 | |
|
43 | |
private Region region; |
44 | |
|
45 | |
|
46 | |
private OrganisationType organisationType; |
47 | |
|
48 | |
|
49 | 7 | private Long numberOfMembers = Long.valueOf(0); |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 7 | public Organisation() { |
55 | 7 | } |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public String getAbbreviation() { |
63 | 0 | return abbreviation; |
64 | |
} |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public void setAbbreviation(String abbreviation) { |
72 | 0 | this.abbreviation = abbreviation; |
73 | 0 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
@ManyToOne |
81 | |
@JoinColumn |
82 | |
public Country getCountry() { |
83 | 0 | return country; |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public void setCountry(Country country) { |
92 | 0 | this.country = country; |
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
@ManyToOne |
101 | |
@JoinColumn |
102 | |
public Region getRegion() { |
103 | 0 | return region; |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public void setRegion(Region region) { |
112 | 0 | this.region = region; |
113 | 0 | } |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public OrganisationType getOrganisationType() { |
121 | 0 | return organisationType; |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public void setOrganisationType(OrganisationType organisationType) { |
130 | 0 | this.organisationType = organisationType; |
131 | 0 | } |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
@Column(columnDefinition = "bigint(20) default 0") |
139 | |
public Long getNumberOfMembers() { |
140 | 0 | return numberOfMembers; |
141 | |
} |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public void setNumberOfMembers(Long numberOfMembers) { |
149 | 0 | this.numberOfMembers = numberOfMembers; |
150 | 0 | } |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
@Transient |
159 | |
public boolean playsRoleInOrganisation(Agent player) { |
160 | 0 | for (Role role : getDefinedRoles()) { |
161 | 0 | for (Agent agent : role.getPlayers()) { |
162 | 0 | if (player.equals(agent)) { |
163 | 0 | return true; |
164 | |
} |
165 | 0 | } |
166 | 0 | } |
167 | 0 | return false; |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
@Transient |
176 | |
public Set<Person> personsActive() { |
177 | 0 | Set<Person> persons = new HashSet<Person>(); |
178 | 0 | Role role = findDefinedRoleByName(OrganisationRoles.Member.toString()); |
179 | |
|
180 | 0 | for (Agent agent : role.getPlayers()) { |
181 | 0 | if (agent instanceof Person) { |
182 | 0 | persons.add((Person) agent); |
183 | |
} |
184 | 0 | } |
185 | |
|
186 | 0 | return persons; |
187 | |
} |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
@Transient |
197 | |
public Person findMemberByName(String name) { |
198 | 0 | Role role = findDefinedRoleByName(OrganisationRoles.Member.toString()); |
199 | |
|
200 | 0 | for (Agent agent : role.getPlayers()) { |
201 | 0 | if (name.equals(agent.getName())) { |
202 | 0 | return (Person) agent; |
203 | |
} |
204 | 0 | } |
205 | 0 | return null; |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
@Transient |
217 | |
public Person findMemberByFullNameAndParty(String fname, String lname, |
218 | |
String party) { |
219 | 0 | Role role = findDefinedRoleByName(OrganisationRoles.Member.toString()); |
220 | |
|
221 | 0 | String firstName = fname.replace(" ", "").trim(); |
222 | 0 | String lastName = lname.replace(" ", "").trim(); |
223 | |
|
224 | 0 | for (Agent agent : role.getPlayers()) { |
225 | 0 | String name = agent.getName().replaceAll(" ", "").trim(); |
226 | 0 | if (name.contains(firstName) && name.contains(lastName)) { |
227 | 0 | return (Person) agent; |
228 | |
} |
229 | 0 | } |
230 | 0 | return null; |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
@Transient |
240 | |
public Organisation findOrgByAbbr(String abbr) { |
241 | |
|
242 | 0 | for (GroupAgent agent : getChildren()) { |
243 | 0 | if (agent instanceof Organisation) { |
244 | 0 | Organisation organisation = (Organisation) agent; |
245 | 0 | if (abbr.toLowerCase().equals(organisation.getAbbreviation().toLowerCase())) { |
246 | 0 | return organisation; |
247 | |
} |
248 | |
} |
249 | 0 | } |
250 | 0 | return null; |
251 | |
} |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
@Transient |
260 | |
public Resource findResourceByName(String name) { |
261 | 0 | for (Resource resource : getResources()) { |
262 | 0 | if (name.toLowerCase().equals(resource.getName().toLowerCase())) { |
263 | 0 | return resource; |
264 | |
} |
265 | 0 | } |
266 | 0 | return null; |
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
@Transient |
276 | |
public Map<PoliticalParty, Long> getActivePoliticalParties() { |
277 | 0 | Map<PoliticalParty, Long> politicalParties = new HashMap<PoliticalParty, Long>(); |
278 | |
|
279 | 0 | for (Role role : getDefinedRoles()) { |
280 | 0 | for (Agent agent : role.getPlayers()) { |
281 | 0 | if (agent instanceof Person) { |
282 | 0 | Person person = (Person) agent; |
283 | 0 | PoliticalParty politicalParty = person.getPoliticalParty(); |
284 | 0 | if (politicalParty != null) { |
285 | 0 | Long positions = politicalParties.get(politicalParty); |
286 | 0 | if (positions == null) { |
287 | 0 | politicalParties.put(politicalParty, 1L); |
288 | |
} else { |
289 | 0 | politicalParties.put(politicalParty, positions + 1); |
290 | |
} |
291 | |
} |
292 | |
} |
293 | 0 | } |
294 | 0 | } |
295 | |
|
296 | 0 | for (GroupAgent groupAgent : getChildren()) { |
297 | 0 | for (Role role : groupAgent.getDefinedRoles()) { |
298 | 0 | for (Agent agent : role.getPlayers()) { |
299 | 0 | if (agent instanceof Person) { |
300 | 0 | Person person = (Person) agent; |
301 | 0 | PoliticalParty politicalParty = person |
302 | |
.getPoliticalParty(); |
303 | 0 | if (politicalParty != null) { |
304 | 0 | Long positions = politicalParties |
305 | |
.get(politicalParty); |
306 | 0 | if (positions == null) { |
307 | 0 | politicalParties.put(politicalParty, 1L); |
308 | |
} else { |
309 | 0 | politicalParties.put(politicalParty, |
310 | |
positions + 1); |
311 | |
} |
312 | |
} |
313 | |
} |
314 | 0 | } |
315 | 0 | } |
316 | 0 | } |
317 | 0 | return politicalParties; |
318 | |
} |
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
@Transient |
326 | |
public void addMember(Person person) { |
327 | 0 | for (Role role : getDefinedRoles()) { |
328 | 0 | if (role.getName().equals(OrganisationRoles.Member.toString())) { |
329 | 0 | person.getRoles().add(role); |
330 | 0 | role.getPlayers().add(person); |
331 | 0 | numberOfMembers++; |
332 | |
} |
333 | 0 | } |
334 | 0 | } |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
@Transient |
342 | |
public void removeMember(Person person) { |
343 | 0 | for (Role role : getDefinedRoles()) { |
344 | 0 | if (role.getName().equals(OrganisationRoles.Member.toString())) { |
345 | 0 | person.getRoles().remove(role); |
346 | 0 | role.getPlayers().remove(person); |
347 | 0 | numberOfMembers--; |
348 | |
} |
349 | 0 | } |
350 | 0 | } |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | 0 | public enum OrganisationRoles { |
356 | |
|
357 | |
|
358 | 0 | Member, |
359 | |
|
360 | |
|
361 | 0 | Substitute, |
362 | |
|
363 | |
|
364 | 0 | President, |
365 | |
|
366 | |
|
367 | 0 | Vice_President; |
368 | |
} |
369 | |
|
370 | |
|
371 | |
} |