commit after merge from trunc
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / agent / InstitutionalMembership.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.model.agent;
11
12
13 import javax.persistence.Entity;
14 import javax.persistence.FetchType;
15 import javax.persistence.ManyToOne;
16 import javax.xml.bind.annotation.XmlAccessType;
17 import javax.xml.bind.annotation.XmlAccessorType;
18 import javax.xml.bind.annotation.XmlElement;
19 import javax.xml.bind.annotation.XmlIDREF;
20 import javax.xml.bind.annotation.XmlRootElement;
21 import javax.xml.bind.annotation.XmlSchemaType;
22 import javax.xml.bind.annotation.XmlType;
23
24 import org.apache.log4j.Logger;
25 import org.hibernate.annotations.Cascade;
26 import org.hibernate.annotations.CascadeType;
27 import org.hibernate.envers.Audited;
28
29 import eu.etaxonomy.cdm.model.common.TimePeriod;
30 import eu.etaxonomy.cdm.model.common.VersionableEntity;
31
32 /**
33 * This class allows to hold one {@link Institution institution} to which a {@link Person person}
34 * is affiliated. It includes {@link eu.etaxonomy.cdm.model.common.TimePeriod time period} of membership and role of
35 * the person in this institution. In case one person belongs to several
36 * institutions a corresponding number of instances of InstitutionalMembership
37 * have to be created.
38 *
39 * @author m.doering
40 * @version 1.0
41 * @created 08-Nov-2007 13:06:30
42 */
43 @XmlAccessorType(XmlAccessType.FIELD)
44 @XmlType(name = "InstitutionalMembership", propOrder = {
45 "period",
46 "department",
47 "role",
48 "institute",
49 "person"
50 })
51 @XmlRootElement(name = "InstitutionalMembership")
52 @Entity
53 @Audited
54 public class InstitutionalMembership extends VersionableEntity implements Cloneable{
55 private static final long serialVersionUID = -800814712134999042L;
56 public static final Logger logger = Logger.getLogger(InstitutionalMembership.class);
57
58 /*Time period a person belonged to the institution*/
59 @XmlElement(name = "Period")
60 private TimePeriod period = TimePeriod.NewInstance();
61
62 //Department of the institution this person was working in
63 @XmlElement(name = "Department")
64 private String department;
65
66 //Role this person had in the institution
67 @XmlElement(name = "Role")
68 private String role;
69
70 //current institute the person belongs to
71 @XmlElement(name = "Institution", required = true)
72 @XmlIDREF
73 @XmlSchemaType(name = "IDREF")
74 @ManyToOne(fetch = FetchType.LAZY)
75 @Cascade(CascadeType.SAVE_UPDATE)
76 private Institution institute;
77
78 @XmlElement(name = "Person", required = true)
79 @XmlIDREF
80 @XmlSchemaType(name = "IDREF")
81 @ManyToOne(fetch = FetchType.LAZY)
82 @Cascade(CascadeType.SAVE_UPDATE)
83 private Person person;
84
85 public static InstitutionalMembership NewInstance() {
86 InstitutionalMembership mship = new InstitutionalMembership();
87 return mship;
88 }
89
90 protected InstitutionalMembership() {
91 super();
92 }
93
94 /**
95 * Class constructor using an {@link Institution institution}, a {@link Person person}, a {@link common.TimePeriod time period},
96 * a department name string and a role string.
97 * Adds this membership to the persons memberships.
98 *
99 * @param institute the institution in which the person is a member
100 * @param person the person who is a member of the institution
101 * @param period the time period during which the person belonged
102 * to the institution
103 * @param department the name string of the department (within the institution)
104 * this person is working in
105 * @param role the string which identifies the role played by the person
106 * in the institution (or in the department)
107 * @see Person
108 * @see Institution
109 */
110 public InstitutionalMembership(Institution institute, Person person, TimePeriod period, String department,
111 String role) {
112 super();
113 this.period = period;
114 this.department = department;
115 this.role = role;
116 this.institute = institute;
117 this.person = person;
118 person.addInstitutionalMembership(this);
119 }
120
121 /**
122 * Returns the {@link Person person} involved in <i>this</i> institutional membership.
123 *
124 * @see Person#institutionalMemberships
125 * @see Person#addInstitutionalMembership(Institution, TimePeriod, String, String)
126 */
127 public Person getPerson() {
128 return person;
129 }
130
131 /**
132 * Assigns a new {@link Person person} (replacing the actual one) to <i>this</i> institutional membership.
133 * This method also updates both sets of institutions
134 * the two persons (the new one and the substituted one) belong to.
135 *
136 * @param newPerson the new person to be included in <i>this</i> institutional membership
137 * @see #getPerson()
138 * @see Person#removeInstitutionalMembership(InstitutionalMembership)
139 */
140 protected void setPerson(Person person) {
141 this.person = person;
142 }
143
144 /**
145 * Returns the {@link Institution institution} corresponding to <i>this</i> institutional membership.
146 */
147 public Institution getInstitute(){
148 return this.institute;
149 }
150 /**
151 * Assigns an new institution (replacing the actual one)
152 * to <i>this</i> institutional membership.
153 *
154 * @param newInstitute the new institution
155 * @see #getInstitute()
156 */
157 public void setInstitute(Institution newInstitute){
158 this.institute = newInstitute;
159 }
160
161 /**
162 * Returns the {@link TimePeriod time period} during which
163 * the {@link Person person} involved in <i>this</i> institutional membership belonged
164 * to the {@link Institution institution} also involved in it.
165 */
166 public TimePeriod getPeriod(){
167 return this.period;
168 }
169
170 /**
171 * @see #getPeriod()
172 */
173 public void setPeriod(TimePeriod period){
174 this.period = period;
175 }
176
177 /**
178 * Returns the string representing the name of the department (within
179 * the {@link Institution institution} involved in <i>this</i> institutional membership) to which
180 * the {@link Person person} belongs.
181 */
182 public String getDepartment(){
183 return this.department;
184 }
185
186 /**
187 * @see #getDepartment()
188 */
189 public void setDepartment(String department){
190 this.department = department;
191 }
192
193 /**
194 * Returns the string representing the role played by the {@link Person person} within
195 * the {@link Institution institution} (or within the department) involved
196 * in <i>this</i> institutional membership.
197 */
198 public String getRole(){
199 return this.role;
200 }
201
202 /**
203 * @see #getRole()
204 */
205 public void setRole(String role){
206 this.role = role;
207 }
208
209 //*********************** CLONE ********************************************************/
210
211 /**
212 * Clones <i>this</i> InstitutionalMembership. This is a shortcut that enables to create
213 * a new instance that differs only slightly from <i>this</i> InstitutionalMembership.
214 *
215 * @see eu.etaxonomy.cdm.model.common.VersionableEntity
216 * @see java.lang.Object#clone()
217 */
218 @Override
219 public Object clone() {
220 try{
221 InstitutionalMembership result = (InstitutionalMembership) super.clone();
222 //no changes to department, institute, period, person, role
223 return result;
224 }catch (CloneNotSupportedException e){
225 logger.warn("Object does not implement cloneable");
226 e.printStackTrace();
227 return null;
228 }
229 }
230 }