ref #6588 add jaxb mapping to ExternalLink related
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / OriginalSourceBase.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.common;
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.Inheritance;
20 import javax.persistence.InheritanceType;
21 import javax.persistence.OneToMany;
22 import javax.validation.constraints.NotNull;
23 import javax.xml.bind.annotation.XmlAccessType;
24 import javax.xml.bind.annotation.XmlAccessorType;
25 import javax.xml.bind.annotation.XmlAttribute;
26 import javax.xml.bind.annotation.XmlElement;
27 import javax.xml.bind.annotation.XmlElementWrapper;
28 import javax.xml.bind.annotation.XmlRootElement;
29 import javax.xml.bind.annotation.XmlType;
30
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.log4j.Logger;
33 import org.hibernate.annotations.Cascade;
34 import org.hibernate.annotations.CascadeType;
35 import org.hibernate.annotations.Table;
36 import org.hibernate.annotations.Type;
37 import org.hibernate.envers.Audited;
38 import org.springframework.util.Assert;
39
40 import eu.etaxonomy.cdm.common.CdmUtils;
41 import eu.etaxonomy.cdm.model.media.ExternalLink;
42 import eu.etaxonomy.cdm.strategy.merge.Merge;
43 import eu.etaxonomy.cdm.strategy.merge.MergeMode;
44
45 /**
46 * Abstract base class for classes implementing {@link eu.etaxonomy.cdm.model.common.IOriginalSource IOriginalSource}.
47 * @see eu.etaxonomy.cdm.model.common.IOriginalSource
48 *
49 * @author m.doering
50 * @since 08-Nov-2007 13:06:22
51 */
52
53 @XmlAccessorType(XmlAccessType.FIELD)
54 @XmlType(name = "OriginalSource", propOrder = {
55 "type",
56 "idInSource",
57 "idNamespace",
58 "links"
59 })
60 @XmlRootElement(name = "OriginalSource")
61 @Entity
62 @Audited
63 @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
64 @Table(appliesTo="OriginalSourceBase")
65 public abstract class OriginalSourceBase<T extends ISourceable>
66 extends ReferencedEntityBase
67 implements IOriginalSource<T>, IIntextReferenceTarget,Cloneable {
68 private static final long serialVersionUID = -1972959999261181462L;
69 @SuppressWarnings("unused")
70 private static final Logger logger = Logger.getLogger(OriginalSourceBase.class);
71
72 /**
73 * The {@link OriginalSourceType type} of this source. According to PROV the type has to be thought as
74 * an activity that leads from the source entity to the current entity. It is not a property of the
75 * source itself.
76 */
77 @XmlAttribute(name ="type")
78 @Column(name="sourceType")
79 @NotNull
80 @Type(type = "eu.etaxonomy.cdm.hibernate.EnumUserType",
81 parameters = {@org.hibernate.annotations.Parameter(name="enumClass", value="eu.etaxonomy.cdm.model.common.OriginalSourceType")}
82 )
83 @Audited
84 private OriginalSourceType type;
85
86 //The object's ID in the source, where the alternative string comes from
87 @XmlElement(name = "IdInSource")
88 private String idInSource;
89
90 @XmlElement(name = "IdNamespace")
91 private String idNamespace;
92
93 @XmlElementWrapper(name = "Links", nillable = true)
94 @XmlElement(name = "Link")
95 @OneToMany(fetch=FetchType.LAZY, orphanRemoval=true)
96 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
97 @Merge(MergeMode.ADD_CLONE)
98 private Set<ExternalLink> links = new HashSet<>();
99
100 //***************** CONSTRUCTOR ***********************/
101
102 //for hibernate use only
103 protected OriginalSourceBase() {
104
105 }
106
107 /**
108 * Constructor
109 * @param type2
110 */
111 protected OriginalSourceBase(OriginalSourceType type){
112 if (type == null){
113 throw new IllegalArgumentException("OriginalSourceType must not be null");
114 }
115 this.type = type;
116 }
117
118 //**************** GETTER / SETTER *******************************/
119
120
121 @Override
122 public String getIdInSource(){
123 return this.idInSource;
124 }
125 @Override
126 public void setIdInSource(String idInSource){
127 this.idInSource = idInSource;
128 }
129
130
131 @Override
132 public String getIdNamespace() {
133 return idNamespace;
134 }
135 @Override
136 public void setIdNamespace(String idNamespace) {
137 this.idNamespace = idNamespace;
138 }
139
140
141 @Override
142 public OriginalSourceType getType() {
143 return type;
144 }
145 @Override
146 public void setType(OriginalSourceType type) {
147 Assert.notNull(type, "OriginalSourceType must not be null");
148 this.type = type;
149 }
150
151 //********************** External Links **********************************************
152
153 public Set<ExternalLink> getLinks(){
154 return this.links;
155 }
156 public void addLink(ExternalLink link){
157 if (link != null){
158 links.add(link);
159 }
160 }
161 public void removeLink(ExternalLink link){
162 if(links.contains(link)) {
163 links.remove(link);
164 }
165 }
166
167 //********************** CLONE ************************************************/
168
169 @Override
170 public Object clone() throws CloneNotSupportedException{
171 OriginalSourceBase<?> result = (OriginalSourceBase<?>)super.clone();
172
173 //no changes to: idInSource
174 return result;
175 }
176
177
178 //************************ toString ***************************************/
179 @Override
180 public String toString(){
181 if (StringUtils.isNotBlank(idInSource) || StringUtils.isNotBlank(idNamespace) ){
182 return "OriginalSource:" + CdmUtils.concat(":", idNamespace, idInSource);
183 }else{
184 return super.toString();
185 }
186 }
187
188 //*********************************** EQUALS *********************************************************/
189
190 /**
191 * {@inheritDoc}
192 */
193 @Override
194 public boolean equalsByShallowCompare(ReferencedEntityBase other) {
195
196 if(!super.equalsByShallowCompare(other)) {
197 return false;
198 }
199 OriginalSourceBase<T> theOther = (OriginalSourceBase<T>)other;
200 if(!StringUtils.equals(this.getIdInSource(), theOther.getIdInSource())
201 || !StringUtils.equals(this.getIdNamespace(), theOther.getIdNamespace())) {
202 return false;
203 }
204
205 return true;
206 }
207
208 }