3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.model
.description
;
13 import java
.util
.HashSet
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
18 import javax
.persistence
.Entity
;
19 import javax
.persistence
.FetchType
;
20 import javax
.persistence
.JoinColumn
;
21 import javax
.persistence
.JoinTable
;
22 import javax
.persistence
.ManyToMany
;
23 import javax
.persistence
.ManyToOne
;
24 import javax
.persistence
.OneToMany
;
25 import javax
.persistence
.Transient
;
26 import javax
.validation
.constraints
.NotNull
;
27 import javax
.xml
.bind
.annotation
.XmlAccessType
;
28 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
29 import javax
.xml
.bind
.annotation
.XmlElement
;
30 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
31 import javax
.xml
.bind
.annotation
.XmlIDREF
;
32 import javax
.xml
.bind
.annotation
.XmlRootElement
;
33 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
34 import javax
.xml
.bind
.annotation
.XmlType
;
36 import org
.apache
.log4j
.Logger
;
37 import org
.hibernate
.annotations
.Cascade
;
38 import org
.hibernate
.annotations
.CascadeType
;
39 import org
.hibernate
.envers
.Audited
;
41 import eu
.etaxonomy
.cdm
.model
.common
.AnnotatableEntity
;
42 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
43 import eu
.etaxonomy
.cdm
.model
.common
.Representation
;
47 * The working set class allows the demarcation of a set of descriptions
48 * associated with representations and a set of features and their
56 @XmlAccessorType(XmlAccessType
.FIELD
)
57 @XmlType(name
= "WorkingSet", propOrder
= {
62 @XmlRootElement(name
= "WorkingSet")
66 public class WorkingSet
extends AnnotatableEntity
{
67 private static final long serialVersionUID
= 3256448866757415686L;
68 private static final Logger logger
= Logger
.getLogger(WorkingSet
.class);
70 @XmlElementWrapper(name
= "Representations")
71 @XmlElement(name
= "Representation")
72 @OneToMany(fetch
=FetchType
.EAGER
)
73 @Cascade( { CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
, CascadeType
.DELETE
})
74 private Set
<Representation
> representations
= new HashSet
<Representation
>();
76 @XmlElement(name
= "DescriptiveSystem")
78 @XmlSchemaType(name
= "IDREF")
79 @ManyToOne(fetch
= FetchType
.LAZY
)
80 @Cascade({CascadeType
.SAVE_UPDATE
})
81 private FeatureTree descriptiveSystem
;
83 @XmlElementWrapper(name
= "Descriptions")
84 @XmlElement(name
= "Description")
86 @XmlSchemaType(name
= "IDREF")
87 @ManyToMany(fetch
= FetchType
.LAZY
)
89 name
="WorkingSet_DescriptionBase",
90 joinColumns
=@JoinColumn(name
="WorkingSet_id"),
91 inverseJoinColumns
=@JoinColumn(name
="descriptions_id")
93 @Cascade(CascadeType
.SAVE_UPDATE
)
95 private Set
<DescriptionBase
> descriptions
= new HashSet
<DescriptionBase
>();
98 * Class constructor: creates a new empty working set instance.
100 protected WorkingSet() {
105 * Creates a new empty working set instance.
107 public static WorkingSet
NewInstance(){
108 return new WorkingSet();
111 public Set
<Representation
> getRepresentations() {
112 return this.representations
;
115 public void addRepresentation(Representation representation
) {
116 this.representations
.add(representation
);
119 public void removeRepresentation(Representation representation
) {
120 this.representations
.remove(representation
);
123 public Representation
getRepresentation(Language lang
) {
124 for (Representation repr
: representations
){
125 Language reprLanguage
= repr
.getLanguage();
126 if (reprLanguage
!= null && reprLanguage
.equals(lang
)){
134 * @see #getPreferredRepresentation(Language)
138 public Representation
getPreferredRepresentation(Language language
) {
139 Representation repr
= getRepresentation(language
);
141 repr
= getRepresentation(Language
.DEFAULT());
144 repr
= getRepresentations().iterator().next();
150 * Returns the Representation in the preferred language. Preferred languages
151 * are specified by the parameter languages, which receives a list of
152 * Language instances in the order of preference. If no representation in
153 * any preferred languages is found the method falls back to return the
154 * Representation in Language.DEFAULT() and if necessary further falls back
155 * to return the first element found if any.
157 * TODO think about this fall-back strategy &
158 * see also {@link TextData#getPreferredLanguageString(List)}
163 public Representation
getPreferredRepresentation(List
<Language
> languages
) {
164 Representation repr
= null;
165 if(languages
!= null){
166 for(Language language
: languages
) {
167 repr
= getRepresentation(language
);
174 repr
= getRepresentation(Language
.DEFAULT());
177 Iterator
<Representation
> it
= getRepresentations().iterator();
179 repr
= getRepresentations().iterator().next();
186 public String
getLabel() {
187 if(getLabel(Language
.DEFAULT())!=null){
188 Representation repr
= getRepresentation(Language
.DEFAULT());
189 return (repr
== null)?
null :repr
.getLabel();
191 for (Representation r
: representations
){
195 return super.getUuid().toString();
198 public String
getLabel(Language lang
) {
199 Representation repr
= this.getRepresentation(lang
);
200 return (repr
== null) ?
null : repr
.getLabel();
203 public void setLabel(String label
){
204 Language lang
= Language
.DEFAULT();
205 setLabel(label
, lang
);
208 public void setLabel(String label
, Language language
){
209 if (language
!= null){
210 Representation repr
= getRepresentation(language
);
212 repr
.setLabel(label
);
214 repr
= Representation
.NewInstance(null, label
, null, language
);
216 this.addRepresentation(repr
);
220 public FeatureTree
getDescriptiveSystem() {
221 return descriptiveSystem
;
223 public void setDescriptiveSystem(FeatureTree descriptiveSystem
) {
224 this.descriptiveSystem
= descriptiveSystem
;
228 * Returns the {@link DescriptionBase descriptions} of
229 * <i>this</i> working set.
231 * @see #addDescription(DescriptionBase)
232 * @see #removeDescription(DescriptionBase)
234 public Set
<DescriptionBase
> getDescriptions() {
239 * Adds an existing {@link DescriptionBase description} to the set of
240 * {@link #getDescriptions() descriptions} of <i>this</i>
243 * @param description the description to be added to <i>this</i> working set
244 * @see #getDescriptions()
245 * @see WorkingSet#addDescription(DescriptionBase)
247 public boolean addDescription(DescriptionBase description
) {
248 boolean result
= this.descriptions
.add(description
);
249 if (! description
.getWorkingSets().contains(this)){
250 description
.addWorkingSet(this);
256 * Removes one element from the set of {@link #getDescriptions() descriptions} involved
257 * in <i>this</i> working set.<BR>
259 * @param description the description which should be removed
260 * @see #getDescriptions()
261 * @see #addDescription(DescriptionBase)
262 * @see WorkingSet#removeDescription(DescriptionBase)
264 public boolean removeDescription(DescriptionBase description
) {
265 boolean result
= this.descriptions
.remove(description
);
266 if (description
.getWorkingSets().contains(this)){
267 description
.removeWorkingSet(this);
272 //*********************** CLONE ********************************************************/
275 * Clones <i>this</i> WorkingSet. This is a shortcut that enables to create
276 * a new instance that differs only slightly from <i>this</i> WorkingSet by
277 * modifying only some of the attributes.
278 * The descriptions and the descriptive system are the same, the representations
281 * @see eu.etaxonomy.cdm.model.common.AnnotatableEntity#clone()
282 * @see java.lang.Object#clone()
285 public Object
clone() {
288 result
= (WorkingSet
)super.clone();
289 result
.descriptions
= new HashSet
<DescriptionBase
>();
291 for (DescriptionBase desc
: this.descriptions
){
292 result
.addDescription(desc
);
295 result
.representations
= new HashSet
<Representation
>();
296 for (Representation rep
: this.representations
){
297 result
.addRepresentation((Representation
)rep
.clone());
301 }catch (CloneNotSupportedException e
) {
302 logger
.warn("Object does not implement cloneable");