Coverage Report - org.directdemocracyportal.democracy.model.world.Organisation
 
Classes in this File Line Coverage Branch Coverage Complexity
Organisation
2%
3/101
0%
0/60
2.75
Organisation$OrganisationRoles
0%
0/5
N/A
2.75
 
 1  
 /*
 2  
 Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
 3  
         $Id
 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  7
     private Long numberOfMembers = Long.valueOf(0);
 50  
 
 51  
     /**
 52  
      * Instantiates a new organisation.
 53  
      */
 54  7
     public Organisation() {
 55  7
     }
 56  
 
 57  
     /**
 58  
      * Gets the abbreviation.
 59  
      *
 60  
      * @return the abbreviation
 61  
      */
 62  
     public String getAbbreviation() {
 63  0
         return abbreviation;
 64  
     }
 65  
 
 66  
     /**
 67  
      * Sets the abbreviation.
 68  
      *
 69  
      * @param abbreviation the new abbreviation
 70  
      */
 71  
     public void setAbbreviation(String abbreviation) {
 72  0
         this.abbreviation = abbreviation;
 73  0
     }
 74  
 
 75  
     /**
 76  
      * Gets the country.
 77  
      *
 78  
      * @return the country
 79  
      */
 80  
     @ManyToOne
 81  
     @JoinColumn
 82  
     public Country getCountry() {
 83  0
         return country;
 84  
     }
 85  
 
 86  
     /**
 87  
      * Sets the country.
 88  
      *
 89  
      * @param country the new country
 90  
      */
 91  
     public void setCountry(Country country) {
 92  0
         this.country = country;
 93  0
     }
 94  
 
 95  
     /**
 96  
      * Gets the region.
 97  
      *
 98  
      * @return the region
 99  
      */
 100  
     @ManyToOne
 101  
     @JoinColumn
 102  
     public Region getRegion() {
 103  0
         return region;
 104  
     }
 105  
 
 106  
     /**
 107  
      * Sets the region.
 108  
      *
 109  
      * @param region the new region
 110  
      */
 111  
     public void setRegion(Region region) {
 112  0
         this.region = region;
 113  0
     }
 114  
 
 115  
     /**
 116  
      * Gets the organisation type.
 117  
      *
 118  
      * @return the organisation type
 119  
      */
 120  
     public OrganisationType getOrganisationType() {
 121  0
         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  0
         this.organisationType = organisationType;
 131  0
     }
 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  0
         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  0
         this.numberOfMembers = numberOfMembers;
 150  0
     }
 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  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  
      * Persons active.
 172  
      *
 173  
      * @return the set
 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  
      * Find member by name.
 192  
      *
 193  
      * @param name the name
 194  
      * @return the person
 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  
      * 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  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  
      * 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  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  
      * Find resource by name.
 255  
      *
 256  
      * @param name the name
 257  
      * @return the resource
 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  
      * Gets the active political parties.
 272  
      *
 273  
      * @return the active political parties
 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  
      * Adds the member.
 322  
      *
 323  
      * @param person the person
 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  
      * Removes the member.
 338  
      *
 339  
      * @param person the person
 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  
      * The Enum OrganisationRoles.
 354  
      */
 355  0
     public enum OrganisationRoles {
 356  
 
 357  
         /** The Member. */
 358  0
         Member,
 359  
 
 360  
 /** The Substitute. */
 361  0
 Substitute,
 362  
 
 363  
 /** The President. */
 364  0
 President,
 365  
 
 366  
 /** The Vice_ president. */
 367  0
 Vice_President;
 368  
     }
 369  
 
 370  
 
 371  
  }