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