ref #9331 add NamedSource and NamedSourceBase and use it, SecundumSource inherits...
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / SingleSourcedEntityBase.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 package eu.etaxonomy.cdm.model.common;
10
11 import javax.persistence.FetchType;
12 import javax.persistence.MappedSuperclass;
13 import javax.persistence.OneToOne;
14 import javax.persistence.Transient;
15 import javax.xml.bind.annotation.XmlAccessType;
16 import javax.xml.bind.annotation.XmlAccessorType;
17 import javax.xml.bind.annotation.XmlElement;
18 import javax.xml.bind.annotation.XmlIDREF;
19 import javax.xml.bind.annotation.XmlRootElement;
20 import javax.xml.bind.annotation.XmlSchemaType;
21 import javax.xml.bind.annotation.XmlType;
22
23 import org.apache.commons.lang3.StringUtils;
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.reference.NamedSource;
30 import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
31 import eu.etaxonomy.cdm.model.reference.Reference;
32
33 /**
34 * Abstract class for all objects that may have a reference
35 * @author m.doering
36 * @since 08-Nov-2007 13:06:47
37 */
38 @XmlAccessorType(XmlAccessType.FIELD)
39 @XmlType(name = "SingleSourcedEntityBase", propOrder = {
40 "source"
41 })
42 @XmlRootElement(name = "SingleSourcedEntityBase")
43 @MappedSuperclass
44 @Audited
45 public abstract class SingleSourcedEntityBase
46 extends AnnotatableEntity {
47
48 static final long serialVersionUID = 2035568689268762760L;
49 @SuppressWarnings("unused")
50 private static final Logger logger = Logger.getLogger(SingleSourcedEntityBase.class);
51
52 //the source for this single sourced entity
53 @XmlElement(name = "source")
54 @XmlIDREF
55 @XmlSchemaType(name = "IDREF")
56 @OneToOne(fetch = FetchType.LAZY, orphanRemoval=true)
57 @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.DELETE})
58 private NamedSource source;
59
60 // ************ CONSTRUCTOR ********************************************/
61
62 //for hibernate use only
63 protected SingleSourcedEntityBase() {
64 super();
65 }
66
67 protected SingleSourcedEntityBase(Reference reference, String microReference,
68 String originalNameString) {
69 super();
70 this.setCitation(reference);
71 this.setCitationMicroReference(microReference);
72 if (StringUtils.isNotEmpty(originalNameString)){
73 getSource(true).setOriginalNameString(originalNameString);
74 }
75 }
76
77 protected SingleSourcedEntityBase(NamedSource source) {
78 this.source = source;
79 }
80
81 //********************* GETTER / SETTER *******************************/
82
83 @Transient
84 public String getCitationMicroReference() {
85 return source == null ? null : this.source.getCitationMicroReference();
86 }
87 public void setCitationMicroReference(String microReference) {
88 this.getSource(true).setCitationMicroReference(isBlank(microReference)? null : microReference);
89 checkNullSource();
90 }
91
92 @Transient
93 public Reference getCitation() {
94 return (this.source == null) ? null : source.getCitation();
95 }
96 public void setCitation(Reference reference) {
97 getSource(true).setCitation(reference);
98 checkNullSource();
99 }
100
101 public NamedSource getSource() {
102 return source;
103 }
104 public void setSource(NamedSource source) {
105 this.source = source;
106 }
107
108 private void checkNullSource() {
109 if (this.source != null && this.source.checkEmpty(true)){
110 this.source = null;
111 }
112 }
113
114 private NamedSource getSource(boolean createIfNotExist){
115 if (this.source == null && createIfNotExist){
116 this.source = NamedSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource);
117 }
118 return source;
119 }
120
121 // **************** EMPTY ************************/
122
123 @Override
124 protected boolean checkEmpty(){
125 return super.checkEmpty()
126 && this.source == null
127 ;
128 }
129
130 //****************** CLONE ************************************************/
131
132 @Override
133 public SingleSourcedEntityBase clone() throws CloneNotSupportedException{
134 SingleSourcedEntityBase result = (SingleSourcedEntityBase)super.clone();
135
136 if (this.source != null){
137 result.source = source.clone();
138 }
139
140 //no changes to: --
141 return result;
142 }
143
144 //*********************************** EQUALS *********************************************************/
145
146
147 }