root/trunk/cdmlib/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/DefinedTermBase.java

Revision 12230, 11.0 kB (checked in by a.mueller, 12 months ago)

remove imports

  • Property svn:keywords set to Id
Line 
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
10package eu.etaxonomy.cdm.model.common;
11
12import java.net.URI;
13import java.util.HashSet;
14import java.util.List;
15import java.util.Map;
16import java.util.Set;
17import java.util.UUID;
18
19import javax.persistence.Entity;
20import javax.persistence.FetchType;
21import javax.persistence.Inheritance;
22import javax.persistence.InheritanceType;
23import javax.persistence.ManyToMany;
24import javax.persistence.ManyToOne;
25import javax.persistence.OneToMany;
26import javax.persistence.Transient;
27import javax.xml.bind.annotation.XmlAccessType;
28import javax.xml.bind.annotation.XmlAccessorType;
29import javax.xml.bind.annotation.XmlElement;
30import javax.xml.bind.annotation.XmlElementWrapper;
31import javax.xml.bind.annotation.XmlIDREF;
32import javax.xml.bind.annotation.XmlRootElement;
33import javax.xml.bind.annotation.XmlSchemaType;
34import javax.xml.bind.annotation.XmlSeeAlso;
35import javax.xml.bind.annotation.XmlTransient;
36import javax.xml.bind.annotation.XmlType;
37
38import org.apache.log4j.Logger;
39import org.hibernate.annotations.Cascade;
40import org.hibernate.annotations.CascadeType;
41import org.hibernate.envers.Audited;
42
43import au.com.bytecode.opencsv.CSVWriter;
44import eu.etaxonomy.cdm.model.agent.InstitutionType;
45import eu.etaxonomy.cdm.model.description.Feature;
46import eu.etaxonomy.cdm.model.description.MeasurementUnit;
47import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
48import eu.etaxonomy.cdm.model.description.TextFormat;
49import eu.etaxonomy.cdm.model.location.NamedAreaType;
50import eu.etaxonomy.cdm.model.location.ReferenceSystem;
51import eu.etaxonomy.cdm.model.media.Media;
52import eu.etaxonomy.cdm.model.media.RightsTerm;
53import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
54import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
55import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
56
57
58/**
59 * walkaround for enumerations, base type according to TDWG.  For linear ordering
60 * use partOf relation and BreadthFirst. Default iterator order should therefore
61 * be BreadthFirst (not DepthFirst)
62 * @author m.doering
63 * @version 1.0
64 * @created 08-Nov-2007 13:06:19
65 */
66@XmlAccessorType(XmlAccessType.FIELD)
67@XmlType(name = "DefinedTermBase", propOrder = {
68    "media",
69    "vocabulary"
70})
71@XmlRootElement(name = "DefinedTermBase")
72@XmlSeeAlso({
73        AnnotationType.class,
74        DerivationEventType.class,
75        ExtensionType.class,
76    Feature.class,
77    InstitutionType.class,
78    Language.class,
79    MarkerType.class,
80    MeasurementUnit.class,
81    NamedAreaType.class,
82    NomenclaturalCode.class,
83    PreservationMethod.class,
84    ReferenceSystem.class,
85    RightsTerm.class,
86    StatisticalMeasure.class,
87    TextFormat.class
88})
89@Entity
90@Audited
91@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
92public abstract class DefinedTermBase<T extends DefinedTermBase> extends TermBase implements ILoadableTerm<T>, IDefinedTerm<T> {
93        private static final long serialVersionUID = 2931811562248571531L;
94        private static final Logger logger = Logger.getLogger(DefinedTermBase.class);
95               
96//      @XmlElement(name = "KindOf")
97//    @XmlIDREF
98//    @XmlSchemaType(name = "IDREF")
99        @XmlTransient
100    @ManyToOne(fetch = FetchType.LAZY, targetEntity = DefinedTermBase.class)
101    @Cascade(CascadeType.SAVE_UPDATE)
102        private T kindOf;
103        /**
104         * FIXME - Hibernate retuns this as a collection of CGLibProxy$$DefinedTermBase objects
105         * which can't be cast to instances of T - can we explicitly initialize these terms using
106         * Hibernate.initialize(), does this imply a distinct load, and find methods in the dao?
107         */
108//      @XmlElementWrapper(name = "Generalizations")
109//      @XmlElement(name = "GeneralizationOf")
110//    @XmlIDREF
111//    @XmlSchemaType(name = "IDREF")
112        @XmlTransient
113    @OneToMany(fetch=FetchType.LAZY, mappedBy = "kindOf", targetEntity = DefinedTermBase.class)
114        @Cascade({CascadeType.SAVE_UPDATE})
115        private Set<T> generalizationOf = new HashSet<T>();
116       
117//      @XmlElement(name = "PartOf")
118//      @XmlIDREF
119//  @XmlSchemaType(name = "IDREF")
120        @XmlTransient
121        @ManyToOne(fetch = FetchType.LAZY, targetEntity = DefinedTermBase.class)
122        @Cascade(CascadeType.SAVE_UPDATE)
123        protected T partOf;
124       
125        /**
126         * FIXME - Hibernate retuns this as a collection of CGLibProxy$$DefinedTermBase objects
127         * which can't be cast to instances of T - can we explicitly initialize these terms using
128         * Hibernate.initialize(), does this imply a distinct load, and find methods in the dao?
129         */
130//      @XmlElementWrapper(name = "Includes")
131//      @XmlElement(name = "Include")
132//      @XmlIDREF
133//    @XmlSchemaType(name = "IDREF")
134        @XmlTransient
135        @OneToMany(fetch=FetchType.LAZY, mappedBy = "partOf", targetEntity = DefinedTermBase.class)
136        @Cascade({CascadeType.SAVE_UPDATE})
137        private Set<T> includes = new HashSet<T>();
138       
139        @XmlElementWrapper(name = "Media")
140        @XmlElement(name = "Medium")
141    @XmlIDREF
142    @XmlSchemaType(name = "IDREF")
143    @ManyToMany(fetch = FetchType.LAZY)
144        @Cascade({CascadeType.SAVE_UPDATE})
145        private Set<Media> media = new HashSet<Media>();
146       
147        @XmlElement(name = "TermVocabulary")
148        @XmlIDREF
149        @XmlSchemaType(name = "IDREF")
150        @ManyToOne(fetch=FetchType.LAZY)
151        @Cascade(CascadeType.SAVE_UPDATE)
152        protected TermVocabulary<T> vocabulary; 
153       
154//***************************** CONSTRUCTOR *******************************************/       
155       
156        public DefinedTermBase() {
157                super();
158        }
159        public DefinedTermBase(String term, String label, String labelAbbrev) {
160                super(term, label, labelAbbrev);
161        }
162
163//******************************* METHODS ******************************************************/
164       
165        public abstract void resetTerms();
166
167        protected abstract void setDefaultTerms(TermVocabulary<T> termVocabulary);
168       
169       
170        /* (non-Javadoc)
171         * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
172         */
173        public T readCsvLine(Class<T> termClass, List<String> csvLine, Map<UUID,DefinedTermBase> terms) {
174                try {
175                        T newInstance = termClass.newInstance();
176                    return readCsvLine(newInstance, csvLine, Language.CSV_LANGUAGE());
177                } catch (Exception e) {
178                        logger.error(e);
179                        for(StackTraceElement ste : e.getStackTrace()) {
180                                logger.error(ste);
181                        }
182                }
183               
184            return null;
185        }
186
187        protected static <TERM extends DefinedTermBase> TERM readCsvLine(TERM newInstance, List<String> csvLine, Language lang) {
188                        newInstance.setUuid(UUID.fromString(csvLine.get(0)));
189                        newInstance.setUri( URI.create(csvLine.get(1)));
190                        String label = csvLine.get(2).trim();
191                        String text = csvLine.get(3);
192                        String abbreviatedLabel = csvLine.get(4);
193                        newInstance.addRepresentation(Representation.NewInstance(text, label, abbreviatedLabel, lang) );
194                        return newInstance;
195        }
196
197        /* (non-Javadoc)
198         * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#writeCsvLine(au.com.bytecode.opencsv.CSVWriter)
199         */
200        public void writeCsvLine(CSVWriter writer, T term) {
201                String [] line = new String[4];
202                line[0] = term.getUuid().toString();
203                line[1] = term.getUri().toString();
204                line[2] = term.getLabel();
205                line[3] = term.getDescription();
206                writer.writeNext(line);
207        }
208       
209        /* (non-Javadoc)
210         * @see eu.etaxonomy.cdm.model.common.IDefinedTerm#getByUuid(java.util.UUID)
211         */
212        @Transient
213        public T getByUuid(UUID uuid){
214                return this.vocabulary.findTermByUuid(uuid);
215        }
216       
217        /* (non-Javadoc)
218         * @see eu.etaxonomy.cdm.model.common.IDefinedTerm#getKindOf()
219         */
220        public T getKindOf(){
221                return (T)DefinedTermBase.deproxy(this.kindOf, this.getClass());
222        }
223
224        public void setKindOf(T kindOf){
225                this.kindOf = kindOf;
226        }
227
228        /* (non-Javadoc)
229         * @see eu.etaxonomy.cdm.model.common.IDefinedTerm#getGeneralizationOf()
230         */
231        public Set<T> getGeneralizationOf(){
232                return this.generalizationOf;
233        }
234       
235        protected void setGeneralizationOf(Set<T> value) {
236                this.generalizationOf = value;
237        }
238       
239        /* (non-Javadoc)
240         * @see eu.etaxonomy.cdm.model.common.IDefinedTerm#addGeneralizationOf(T)
241         */
242        public void addGeneralizationOf(T generalization) {
243                generalization.setKindOf(this);
244                this.generalizationOf.add(generalization);
245        }
246       
247        /* (non-Javadoc)
248         * @see eu.etaxonomy.cdm.model.common.IDefinedTerm#removeGeneralization(T)
249         */
250        public void removeGeneralization(T generalization) {
251                if(generalizationOf.contains(generalization)){
252                        generalization.setKindOf(null);
253                    this.generalizationOf.remove(generalization);
254                }
255        }
256
257        /* (non-Javadoc)
258         * @see eu.etaxonomy.cdm.model.common.IDefinedTerm#getPartOf()
259         */
260        public T getPartOf(){
261                return (T)DefinedTermBase.deproxy(this.partOf, this.getClass());
262        }
263       
264        /* (non-Javadoc)
265         * @see eu.etaxonomy.cdm.model.common.IDefinedTerm#setPartOf(T)
266         */
267        public void setPartOf(T partOf){
268                this.partOf = partOf;
269        }
270
271        /* (non-Javadoc)
272         * @see eu.etaxonomy.cdm.model.common.IDefinedTerm#getIncludes()
273         */
274        public Set<T> getIncludes(){
275                return this.includes;
276        }
277       
278        protected void setIncludes(Set<T> includes) {
279                this.includes = includes;
280        }
281       
282        /* (non-Javadoc)
283         * @see eu.etaxonomy.cdm.model.common.IDefinedTerm#addIncludes(T)
284         */
285        public void addIncludes(T includes) {
286                includes.setPartOf(this);
287                this.includes.add(includes);
288        }
289        /* (non-Javadoc)
290         * @see eu.etaxonomy.cdm.model.common.IDefinedTerm#removeIncludes(T)
291         */
292        public void removeIncludes(T includes) {
293                if(this.includes.contains(includes)) {
294                        includes.setPartOf(null);
295                    this.includes.remove(includes);
296                }
297        }
298
299        /* (non-Javadoc)
300         * @see eu.etaxonomy.cdm.model.common.IDefinedTerm#getMedia()
301         */
302        public Set<Media> getMedia(){
303                return this.media;
304        }
305       
306        /* (non-Javadoc)
307         * @see eu.etaxonomy.cdm.model.common.IDefinedTerm#addMedia(eu.etaxonomy.cdm.model.media.Media)
308         */
309        public void addMedia(Media media) {
310                this.media.add(media);
311        }
312        public void removeMedia(Media media) {
313                this.media.remove(media);
314        }
315
316        /**
317         * @return
318         */
319        public TermVocabulary<T> getVocabulary() {
320                return this.vocabulary;
321        }
322
323        //for bedirectional use only, use vocabulary.addTerm instead
324        /**
325         * @param newVocabulary
326         */
327        protected void setVocabulary(TermVocabulary<T> newVocabulary) {
328                this.vocabulary = newVocabulary;               
329        }       
330       
331//*********************** CLONE ********************************************************/
332       
333        /**
334         * Clones <i>this</i> DefinedTermBase. This is a shortcut that enables to create
335         * a new instance that differs only slightly from <i>this</i> defined term base by
336         * modifying only some of the attributes.
337         *
338         * @see eu.etaxonomy.cdm.model.common.TermBase#clone()
339         * @see java.lang.Object#clone()
340         */
341        @Override
342        public Object clone() {
343                DefinedTermBase result;
344                try {
345                        result = (DefinedTermBase) super.clone();
346                }catch (CloneNotSupportedException e) {
347                        logger.warn("Object does not implement cloneable");
348                        e.printStackTrace();
349                        return null;
350                }
351               
352                result.generalizationOf = new HashSet<DefinedTermBase>();
353                for (DefinedTermBase generalizationOf : this.generalizationOf){
354                        result.generalizationOf.add((DefinedTermBase) generalizationOf.clone());
355                }
356               
357                result.includes = new HashSet<DefinedTermBase>();
358               
359                for (DefinedTermBase include: this.includes){
360                        result.includes.add((DefinedTermBase) include.clone());
361                }
362               
363                result.media = new HashSet<Media>();
364               
365                for (Media media: this.media){
366                        result.addMedia(media);
367                }
368               
369               
370               
371               
372                return result;
373               
374        }
375}
Note: See TracBrowser for help on using the browser.