minor
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / description / WorkingSet.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.cdm.model.description;
12
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Set;
17
18 import javax.persistence.Entity;
19 import javax.persistence.FetchType;
20 import javax.persistence.ManyToMany;
21 import javax.persistence.ManyToOne;
22 import javax.persistence.OneToMany;
23 import javax.persistence.Transient;
24 import javax.xml.bind.annotation.XmlAccessType;
25 import javax.xml.bind.annotation.XmlAccessorType;
26 import javax.xml.bind.annotation.XmlElement;
27 import javax.xml.bind.annotation.XmlElementWrapper;
28 import javax.xml.bind.annotation.XmlIDREF;
29 import javax.xml.bind.annotation.XmlRootElement;
30 import javax.xml.bind.annotation.XmlSchemaType;
31 import javax.xml.bind.annotation.XmlType;
32
33 import org.apache.log4j.Logger;
34 import org.hibernate.annotations.Cascade;
35 import org.hibernate.annotations.CascadeType;
36 import org.hibernate.envers.Audited;
37
38 import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
39 import eu.etaxonomy.cdm.model.common.Language;
40 import eu.etaxonomy.cdm.model.common.Representation;
41
42 /**
43 *
44 * The working set class allows the demarcation of a set of descriptions
45 * associated with representations and a set of features and their
46 * dependencies.
47 *
48 * @author h.fradin
49 * @created 12.08.2009
50 * @version 1.0
51 */
52
53 @XmlAccessorType(XmlAccessType.FIELD)
54 @XmlType(name = "WorkingSet", propOrder = {
55 "representations",
56 "descriptiveSystem",
57 "descriptions"
58 })
59 @XmlRootElement(name = "WorkingSet")
60 @Entity
61 @Audited
62
63 public class WorkingSet extends AnnotatableEntity {
64 private static final long serialVersionUID = 3256448866757415686L;
65 private static final Logger logger = Logger.getLogger(WorkingSet.class);
66
67 @XmlElementWrapper(name = "Representations")
68 @XmlElement(name = "Representation")
69 @OneToMany(fetch=FetchType.EAGER)
70 @Cascade( { CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE })
71 private Set<Representation> representations = new HashSet<Representation>();
72
73 @XmlElement(name = "DescriptiveSystem")
74 @XmlIDREF
75 @XmlSchemaType(name = "IDREF")
76 @ManyToOne(fetch = FetchType.LAZY)
77 @Cascade({CascadeType.SAVE_UPDATE})
78 private FeatureTree descriptiveSystem;
79
80 @XmlElementWrapper(name = "Descriptions")
81 @XmlElement(name = "Description")
82 @XmlIDREF
83 @XmlSchemaType(name = "IDREF")
84 @ManyToMany(fetch = FetchType.LAZY)
85 @Cascade(CascadeType.SAVE_UPDATE)
86 private Set<DescriptionBase> descriptions = new HashSet<DescriptionBase>();
87
88 /**
89 * Class constructor: creates a new empty working set instance.
90 */
91 protected WorkingSet() {
92 super();
93 }
94
95 /**
96 * Creates a new empty working set instance.
97 */
98 public static WorkingSet NewInstance(){
99 return new WorkingSet();
100 }
101
102 public Set<Representation> getRepresentations() {
103 return this.representations;
104 }
105
106 public void addRepresentation(Representation representation) {
107 this.representations.add(representation);
108 }
109
110 public void removeRepresentation(Representation representation) {
111 this.representations.remove(representation);
112 }
113
114 public Representation getRepresentation(Language lang) {
115 for (Representation repr : representations){
116 Language reprLanguage = repr.getLanguage();
117 if (reprLanguage != null && reprLanguage.equals(lang)){
118 return repr;
119 }
120 }
121 return null;
122 }
123
124 /**
125 * @see #getPreferredRepresentation(Language)
126 * @param language
127 * @return
128 */
129 public Representation getPreferredRepresentation(Language language) {
130 Representation repr = getRepresentation(language);
131 if(repr == null){
132 repr = getRepresentation(Language.DEFAULT());
133 }
134 if(repr == null){
135 repr = getRepresentations().iterator().next();
136 }
137 return repr;
138 }
139
140 /**
141 * Returns the Representation in the preferred language. Preferred languages
142 * are specified by the parameter languages, which receives a list of
143 * Language instances in the order of preference. If no representation in
144 * any preferred languages is found the method falls back to return the
145 * Representation in Language.DEFAULT() and if necessary further falls back
146 * to return the first element found if any.
147 *
148 * TODO think about this fall-back strategy &
149 * see also {@link TextData#getPreferredLanguageString(List)}
150 *
151 * @param languages
152 * @return
153 */
154 public Representation getPreferredRepresentation(List<Language> languages) {
155 Representation repr = null;
156 if(languages != null){
157 for(Language language : languages) {
158 repr = getRepresentation(language);
159 if(repr != null){
160 return repr;
161 }
162 }
163 }
164 if(repr == null){
165 repr = getRepresentation(Language.DEFAULT());
166 }
167 if(repr == null){
168 Iterator<Representation> it = getRepresentations().iterator();
169 if(it.hasNext()){
170 repr = getRepresentations().iterator().next();
171 }
172 }
173 return repr;
174 }
175
176 @Transient
177 public String getLabel() {
178 if(getLabel(Language.DEFAULT())!=null){
179 Representation repr = getRepresentation(Language.DEFAULT());
180 return (repr == null)? null :repr.getLabel();
181 }else{
182 for (Representation r : representations){
183 return r.getLabel();
184 }
185 }
186 return super.getUuid().toString();
187 }
188
189 public String getLabel(Language lang) {
190 Representation repr = this.getRepresentation(lang);
191 return (repr == null) ? null : repr.getLabel();
192 }
193
194 public void setLabel(String label){
195 Language lang = Language.DEFAULT();
196 setLabel(label, lang);
197 }
198
199 public void setLabel(String label, Language language){
200 if (language != null){
201 Representation repr = getRepresentation(language);
202 if (repr != null){
203 repr.setLabel(label);
204 }else{
205 repr = Representation.NewInstance(null, label, null, language);
206 }
207 this.addRepresentation(repr);
208 }
209 }
210
211 public FeatureTree getDescriptiveSystem() {
212 return descriptiveSystem;
213 }
214 protected void setDescriptiveSystem(FeatureTree descriptiveSystem) {
215 this.descriptiveSystem = descriptiveSystem;
216 }
217
218 /**
219 * Returns the {@link DescriptionBase descriptions} of
220 * <i>this</i> working set.
221 *
222 * @see #addDescription(DescriptionBase)
223 * @see #removeDescription(DescriptionBase)
224 */
225 public Set<DescriptionBase> getDescriptions() {
226 return descriptions;
227 }
228
229 /**
230 * Adds an existing {@link DescriptionBase description} to the set of
231 * {@link #getDescriptions() descriptions} of <i>this</i>
232 * working set.
233 *
234 * @param description the description to be added to <i>this</i> working set
235 * @see #getDescriptions()
236 * @see WorkingSet#addDescription(DescriptionBase)
237 */
238 public void addDescription(DescriptionBase description) {
239 logger.debug("addDescription");
240 this.descriptions.add(description);
241 }
242
243 /**
244 * Removes one element from the set of {@link #getDescriptions() descriptions} involved
245 * in <i>this</i> working set.<BR>
246 *
247 * @param description the description which should be removed
248 * @see #getDescriptions()
249 * @see #addDescription(DescriptionBase)
250 * @see WorkingSet#removeDescription(DescriptionBase)
251 */
252 public void removeDescription(DescriptionBase description) {
253 this.descriptions.remove(description);
254 }
255
256 }