Merge branch 'release/5.45.0'
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / agent / Institution.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 java.util.HashSet;
14 import java.util.Set;
15
16 import javax.persistence.Column;
17 import javax.persistence.Entity;
18 import javax.persistence.FetchType;
19 import javax.persistence.ManyToMany;
20 import javax.persistence.ManyToOne;
21 import javax.xml.bind.annotation.XmlAccessType;
22 import javax.xml.bind.annotation.XmlAccessorType;
23 import javax.xml.bind.annotation.XmlElement;
24 import javax.xml.bind.annotation.XmlElementWrapper;
25 import javax.xml.bind.annotation.XmlIDREF;
26 import javax.xml.bind.annotation.XmlRootElement;
27 import javax.xml.bind.annotation.XmlSchemaType;
28 import javax.xml.bind.annotation.XmlType;
29
30 import org.apache.logging.log4j.LogManager;
31 import org.apache.logging.log4j.Logger;
32 import org.hibernate.annotations.Cascade;
33 import org.hibernate.annotations.CascadeType;
34 import org.hibernate.envers.Audited;
35 import org.hibernate.search.annotations.Field;
36 import org.springframework.beans.factory.annotation.Configurable;
37
38 import eu.etaxonomy.cdm.model.term.DefinedTerm;
39 import eu.etaxonomy.cdm.strategy.cache.agent.InstitutionDefaultCacheStrategy;
40 import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
41
42 /**
43 * This class represents public or private institutions.
44 * It includes name, contact details and institution type.
45 * <P>
46 * This class corresponds to: <ul>
47 * <li> Institution according to the TDWG ontology
48 * <li> Institution according to the TCS
49 * <li> Organisation (Institution) according to the ABCD schema
50 * </ul>
51 *
52 * @author m.doering
53 * @since 08-Nov-2007 13:06:29
54 */
55 @XmlAccessorType(XmlAccessType.FIELD)
56 @XmlType(name = "Institution", propOrder = {
57 "code",
58 "name",
59 "types",
60 "isPartOf"
61 })
62 @XmlRootElement(name = "Institution")
63 @Entity
64 // @Indexed disabled to reduce clutter in indexes, since this type is not used by any search
65 //@Indexed(index = "eu.etaxonomy.cdm.model.agent.AgentBase")
66 @Audited
67 @Configurable
68 public class Institution extends AgentBase<IIdentifiableEntityCacheStrategy<Institution>> {
69
70 private static final long serialVersionUID = -951321271656955808L;
71 private static final Logger logger = LogManager.getLogger();
72
73 @XmlElement(name = "Code")
74 @Field
75 //TODO Val #3379
76 // @NullOrNotEmpty
77 @Column(length=255)
78 private String code;
79
80 @XmlElement(name = "Name")
81 @Field
82 //TODO Val #3379
83 // @NullOrNotEmpty
84 @Column(length=255)
85 private String name;
86
87 @XmlElementWrapper(name = "Types", nillable = true)
88 @XmlElement(name = "Type")
89 @XmlIDREF
90 @XmlSchemaType(name = "IDREF")
91 @ManyToMany(fetch = FetchType.LAZY)
92 private Set<DefinedTerm> types; //InstitutionTypes
93
94 @XmlElement(name = "IsPartOf")
95 @XmlIDREF
96 @XmlSchemaType(name = "IDREF")
97 @ManyToOne(fetch = FetchType.LAZY)
98 @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
99 private Institution isPartOf;
100
101 /**
102 * Creates a new empty institution instance.
103 */
104 public static Institution NewInstance(){
105 return new Institution();
106 }
107
108 /**
109 * Creates a new empty institution instance.
110 */
111 public static Institution NewNamedInstance(String name){
112 Institution result = new Institution();
113 result.setName(name);
114 return result;
115 }
116
117 //******************* CONSTRUCTOR ***********************/
118
119 /**
120 * Class constructor.
121 */
122 protected Institution() {
123 super();
124 }
125
126 @Override
127 protected void initDefaultCacheStrategy() {
128 this.cacheStrategy = InstitutionDefaultCacheStrategy.NewInstance();
129 }
130
131 //*************** Methods ******************************/
132
133 /**
134 * Returns the set of institution types (categories)
135 * used to describe or circumscribe <i>this</i> institution's activities.
136 * Institution types are items of a controlled {@link eu.etaxonomy.cdm.model.term.TermVocabulary vocabulary}.
137 *
138 * @return the set of institution types
139 */
140 public Set<DefinedTerm> getTypes(){
141 if(types == null) {
142 this.types = new HashSet<>();
143 }
144 return this.types;
145 }
146
147 /**
148 * Adds a new institutional type (from the corresponding {@link eu.etaxonomy.cdm.model.term.TermVocabulary vocabulary})
149 * to the set of institution types of <i>this</i> institution.
150 *
151 * @param t any type of institution
152 * @see #getTypes()
153 */
154 public void addType(DefinedTerm type){
155 getTypes().add(type);
156 }
157
158 /**
159 * Removes one element from the set of institution types for <i>this</i> institution.
160 *
161 * @param t the institution type which should be deleted
162 * @see #getTypes()
163 */
164 public void removeType(DefinedTerm type){
165 getTypes().remove(type);
166 }
167
168 /**
169 * Returns the parent institution of this institution.
170 * This is for instance the case when this institution is a herbarium
171 * belonging to a parent institution such as a museum.
172 */
173 public Institution getIsPartOf(){
174 return this.isPartOf;
175 }
176
177 /**
178 * Assigns a parent institution to which this institution belongs.
179 *
180 * @param isPartOf the parent institution
181 * @see #getIsPartOf()
182 */
183 public void setIsPartOf(Institution parentInstitution){
184 this.isPartOf = parentInstitution;
185 }
186
187 /**
188 * Returns the string representing the code (can also be an acronym or initials)
189 * by which this institution is known among experts.
190 */
191 public String getCode(){
192 return this.code;
193 }
194 /**
195 * @see #getCode()
196 */
197 public void setCode(String code){
198 this.code = isBlank(code) ? null : code;
199 }
200
201
202 /**
203 * Returns the full name, as distinct from a code, an acronym or initials,
204 * by which this institution is generally known.
205 */
206 public String getName(){
207 return this.name;
208 }
209 /**
210 * @see #getName()
211 */
212 public void setName(String name){
213 this.name = isBlank(name) ? null: name;
214 }
215
216 //*********************** CLONE ********************************************************/
217
218 /**
219 * Clones <i>this</i> Institution. This is a shortcut that enables to create
220 * a new instance that differs only slightly from <i>this</i> Institution.
221 *
222 * @see java.lang.Object#clone()
223 */
224 @Override
225 public Institution clone() {
226 try{
227 Institution result = (Institution) super.clone();
228 //no changes to code, isPartOf, name, types
229 return result;
230 }catch (CloneNotSupportedException e){
231 logger.warn("Object does not implement cloneable");
232 e.printStackTrace();
233 return null;
234 }
235 }
236 }