fd8e2f91df6959a60db7b89f23ac7bd7070bed11
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / description / Distribution.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.description;
11
12 import javax.persistence.Entity;
13 import javax.persistence.FetchType;
14 import javax.persistence.ManyToOne;
15 import javax.validation.constraints.NotNull;
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.envers.Audited;
26 import org.hibernate.search.annotations.Indexed;
27
28 import eu.etaxonomy.cdm.model.location.NamedArea;
29 import eu.etaxonomy.cdm.model.taxon.Taxon;
30 import eu.etaxonomy.cdm.validation.Level2;
31
32 /**
33 * This class represents elementary distribution data for a {@link Taxon taxon}.
34 * Only {@link TaxonDescription taxon descriptions} may contain distributions.
35 * A distribution instance consist of a {@link NamedArea named area} and of a {@link PresenceAbsenceTermBase status}
36 * describing the absence or the presence of a taxon (like "extinct"
37 * or "introduced") in this named area.
38 * <P>
39 * This class corresponds partially to: <ul>
40 * <li> CodedDescriptionType according to the the SDD schema
41 * <li> Distribution according to the TDWG ontology
42 * </ul>
43 *
44 * @author m.doering
45 * @version 1.0
46 * @created 08-Nov-2007 13:06:21
47 */
48 @XmlAccessorType(XmlAccessType.FIELD)
49 @XmlType(name = "Distribution", propOrder = {
50 "area",
51 "status"
52 })
53 @XmlRootElement(name = "Distribution")
54 @Entity
55 @Audited
56 @Indexed(index = "eu.etaxonomy.cdm.model.description.DescriptionElementBase")
57 public class Distribution extends DescriptionElementBase {
58 private static final long serialVersionUID = 8366462435651559730L;
59 @SuppressWarnings("unused")
60 private static final Logger logger = Logger.getLogger(Distribution.class);
61
62 @XmlElement(name = "NamedArea")
63 @XmlIDREF
64 @XmlSchemaType(name = "IDREF")
65 @ManyToOne(fetch = FetchType.LAZY)
66 @NotNull(groups = Level2.class)
67 private NamedArea area;
68
69 @XmlElement(name = "PresenceAbsenceStatus")
70 @XmlIDREF
71 @XmlSchemaType(name = "IDREF")
72 @ManyToOne(fetch = FetchType.LAZY)
73 @NotNull(groups = Level2.class)
74 private PresenceAbsenceTermBase<?> status;
75
76
77 /**
78 * Class constructor: creates a new empty distribution instance.
79 * The corresponding {@link Feature feature} is set to {@link Feature#DISTRIBUTION() DISTRIBUTION}.
80 */
81 protected Distribution(){
82 super();
83 }
84
85
86 /**
87 * Creates an empty distribution instance. The corresponding {@link Feature feature}
88 * is set to {@link Feature#DISTRIBUTION() DISTRIBUTION}.
89 *
90 * @see #NewInstance(NamedArea, PresenceAbsenceTermBase)
91 */
92 public static Distribution NewInstance(){
93 Distribution result = new Distribution();
94 result.setType(Feature.DISTRIBUTION());
95 return result;
96 }
97
98 /**
99 * Creates a distribution instance with the given {@link NamedArea named area} and {@link PresenceAbsenceTermBase status}.
100 * The corresponding {@link Feature feature} is set to {@link Feature#DISTRIBUTION() DISTRIBUTION}.
101 *
102 * @param area the named area for the new distribution
103 * @param status the presence or absence term for the new distribution
104 * @see #NewInstance()
105 */
106 public static Distribution NewInstance(NamedArea area, PresenceAbsenceTermBase<?> status){
107 Distribution result = NewInstance();
108 result.setArea(area);
109 result.setStatus(status);
110 return result;
111 }
112
113 /**
114 * Deprecated because {@link Feature feature} should always be {@link Feature#DISTRIBUTION() DISTRIBUTION}
115 * for all distribution instances.
116 */
117 /* (non-Javadoc)
118 * @see eu.etaxonomy.cdm.model.description.DescriptionElementBase#setFeature(eu.etaxonomy.cdm.model.description.Feature)
119 */
120 @Override
121 @Deprecated
122 public void setFeature(Feature feature) {
123 super.setFeature(feature);
124 }
125
126 /**
127 * Returns the {@link NamedArea named area} <i>this</i> distribution applies to.
128 */
129 public NamedArea getArea(){
130 return this.area;
131 }
132 /**
133 * @see #getArea()
134 */
135 public void setArea(NamedArea area){
136 this.area = area;
137 }
138
139 /**
140 * Returns the {@link PresenceAbsenceTermBase presence or absence term} for <i>this</i> distribution.
141 */
142 public PresenceAbsenceTermBase<?> getStatus(){
143 return this.status;
144 }
145 /**
146 * @see #getStatus()
147 */
148 public void setStatus(PresenceAbsenceTermBase<?> status){
149 this.status = status;
150 }
151
152 /**
153 * Special equal method for building an sorted distribution tree
154 * @param dist
155 * @return
156 */
157 public boolean equalsForTree(Distribution dist){
158 boolean result = false;
159 //same area level and area label
160 if (this.getArea().getLabel().compareTo(dist.getArea().getLabel()) == 0 &&
161 this.getArea().getLevel().getLabel().compareTo(dist.getArea().getLevel().getLabel()) == 0){
162 result = true;
163 }
164
165 return result;
166 }
167
168 /**
169 * Special function for building the sorted distribution tree. The function returns true
170 * if the sources of the two different objects are different
171 * @param dist
172 * @return
173 */
174 public boolean isDifferentSources(Distribution dist){
175 boolean result = false;
176 if(this.getSources().equals(dist.getSources())){
177 result = true;
178 }
179 return result;
180 }
181
182 /**
183 * Implementation of the toString() function
184 */
185 public String toString(){
186 String result = "null";
187 if (this.area != null){
188 result = area.getLabel().toString();
189 }
190 return result;
191 }
192
193 }