| 1 | /** |
|---|
| 2 | * Copyright (C) 2009 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.common; |
|---|
| 11 | |
|---|
| 12 | import java.net.URI; |
|---|
| 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.FetchType; |
|---|
| 19 | import javax.persistence.MappedSuperclass; |
|---|
| 20 | import javax.persistence.OneToMany; |
|---|
| 21 | import javax.persistence.Transient; |
|---|
| 22 | import javax.xml.bind.annotation.XmlAccessType; |
|---|
| 23 | import javax.xml.bind.annotation.XmlAccessorType; |
|---|
| 24 | import javax.xml.bind.annotation.XmlElement; |
|---|
| 25 | import javax.xml.bind.annotation.XmlElementWrapper; |
|---|
| 26 | import javax.xml.bind.annotation.XmlSeeAlso; |
|---|
| 27 | import javax.xml.bind.annotation.XmlType; |
|---|
| 28 | |
|---|
| 29 | import org.apache.log4j.Logger; |
|---|
| 30 | import org.hibernate.LazyInitializationException; |
|---|
| 31 | import org.hibernate.annotations.Cascade; |
|---|
| 32 | import org.hibernate.annotations.CascadeType; |
|---|
| 33 | import org.hibernate.annotations.Type; |
|---|
| 34 | import org.hibernate.search.annotations.Field; |
|---|
| 35 | import org.hibernate.search.annotations.IndexedEmbedded; |
|---|
| 36 | import org.hibernate.validator.constraints.Length; |
|---|
| 37 | |
|---|
| 38 | import eu.etaxonomy.cdm.model.description.FeatureTree; |
|---|
| 39 | import eu.etaxonomy.cdm.model.description.TextData; |
|---|
| 40 | import eu.etaxonomy.cdm.strategy.cache.common.TermDefaultCacheStrategy; |
|---|
| 41 | import eu.etaxonomy.cdm.validation.annotation.NullOrNotEmpty; |
|---|
| 42 | |
|---|
| 43 | @XmlAccessorType(XmlAccessType.FIELD) |
|---|
| 44 | @XmlType(name = "TermBase", propOrder = { |
|---|
| 45 | "uri", |
|---|
| 46 | "representations" |
|---|
| 47 | }) |
|---|
| 48 | @XmlSeeAlso({ |
|---|
| 49 | DefinedTermBase.class, |
|---|
| 50 | TermVocabulary.class, |
|---|
| 51 | FeatureTree.class |
|---|
| 52 | }) |
|---|
| 53 | @MappedSuperclass |
|---|
| 54 | public abstract class TermBase extends IdentifiableEntity{ |
|---|
| 55 | private static final long serialVersionUID = 1471561531632115822L; |
|---|
| 56 | @SuppressWarnings("unused") |
|---|
| 57 | private static final Logger logger = Logger.getLogger(TermBase.class); |
|---|
| 58 | |
|---|
| 59 | @XmlElement(name = "URI") |
|---|
| 60 | @Field(index=org.hibernate.search.annotations.Index.UN_TOKENIZED) |
|---|
| 61 | @NullOrNotEmpty |
|---|
| 62 | @Length(max = 255) |
|---|
| 63 | @Type(type="uriUserType") |
|---|
| 64 | private URI uri; |
|---|
| 65 | |
|---|
| 66 | @XmlElementWrapper(name = "Representations") |
|---|
| 67 | @XmlElement(name = "Representation") |
|---|
| 68 | @OneToMany(fetch=FetchType.EAGER) |
|---|
| 69 | @Cascade( { CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE, CascadeType.DELETE_ORPHAN}) |
|---|
| 70 | @IndexedEmbedded(depth = 2) |
|---|
| 71 | private Set<Representation> representations = new HashSet<Representation>(); |
|---|
| 72 | |
|---|
| 73 | public TermBase(){ |
|---|
| 74 | super(); |
|---|
| 75 | initCacheStrategy(); |
|---|
| 76 | |
|---|
| 77 | } |
|---|
| 78 | private void initCacheStrategy() { |
|---|
| 79 | this.cacheStrategy = new TermDefaultCacheStrategy<TermBase>(); |
|---|
| 80 | } |
|---|
| 81 | public TermBase(String term, String label, String labelAbbrev) { |
|---|
| 82 | super(); |
|---|
| 83 | this.addRepresentation(new Representation(term, label, labelAbbrev, Language.DEFAULT()) ); |
|---|
| 84 | initCacheStrategy(); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | public Set<Representation> getRepresentations() { |
|---|
| 88 | return this.representations; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | public void addRepresentation(Representation representation) { |
|---|
| 92 | this.representations.add(representation); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | public void removeRepresentation(Representation representation) { |
|---|
| 96 | this.representations.remove(representation); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | public Representation getRepresentation(Language lang) { |
|---|
| 100 | for (Representation repr : representations){ |
|---|
| 101 | Language reprLanguage = repr.getLanguage(); |
|---|
| 102 | if (reprLanguage != null && reprLanguage.equals(lang)){ |
|---|
| 103 | return repr; |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | return null; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * @see #getPreferredRepresentation(Language) |
|---|
| 111 | * @param language |
|---|
| 112 | * @return |
|---|
| 113 | */ |
|---|
| 114 | public Representation getPreferredRepresentation(Language language) { |
|---|
| 115 | Representation repr = getRepresentation(language); |
|---|
| 116 | if(repr == null){ |
|---|
| 117 | repr = getRepresentation(Language.DEFAULT()); |
|---|
| 118 | } |
|---|
| 119 | if(repr == null){ |
|---|
| 120 | repr = getRepresentations().iterator().next(); |
|---|
| 121 | } |
|---|
| 122 | return repr; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * Returns the Representation in the preferred language. Preferred languages |
|---|
| 127 | * are specified by the parameter languages, which receives a list of |
|---|
| 128 | * Language instances in the order of preference. If no representation in |
|---|
| 129 | * any preferred languages is found the method falls back to return the |
|---|
| 130 | * Representation in Language.DEFAULT() and if necessary further falls back |
|---|
| 131 | * to return the first element found if any. |
|---|
| 132 | * |
|---|
| 133 | * TODO think about this fall-back strategy & |
|---|
| 134 | * see also {@link TextData#getPreferredLanguageString(List)} |
|---|
| 135 | * |
|---|
| 136 | * @param languages |
|---|
| 137 | * @return |
|---|
| 138 | */ |
|---|
| 139 | public Representation getPreferredRepresentation(List<Language> languages) { |
|---|
| 140 | Representation repr = null; |
|---|
| 141 | if(languages != null){ |
|---|
| 142 | for(Language language : languages) { |
|---|
| 143 | repr = getRepresentation(language); |
|---|
| 144 | if(repr != null){ |
|---|
| 145 | return repr; |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | if(repr == null){ |
|---|
| 150 | repr = getRepresentation(Language.DEFAULT()); |
|---|
| 151 | } |
|---|
| 152 | if(repr == null){ |
|---|
| 153 | Iterator<Representation> it = getRepresentations().iterator(); |
|---|
| 154 | if(it.hasNext()){ |
|---|
| 155 | repr = getRepresentations().iterator().next(); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | return repr; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | public URI getUri() { |
|---|
| 162 | return this.uri; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | public void setUri(URI uri) { |
|---|
| 166 | this.uri = uri; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /* (non-Javadoc) |
|---|
| 170 | * @see eu.etaxonomy.cdm.model.common.ITerm#getLabel() |
|---|
| 171 | */ |
|---|
| 172 | @Transient |
|---|
| 173 | public String getLabel() { |
|---|
| 174 | if(getLabel(Language.DEFAULT())!=null){ |
|---|
| 175 | Representation repr = getRepresentation(Language.DEFAULT()); |
|---|
| 176 | return (repr == null)? null :repr.getLabel(); |
|---|
| 177 | }else{ |
|---|
| 178 | for (Representation r : representations){ |
|---|
| 179 | return r.getLabel(); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | return super.getUuid().toString(); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | /* (non-Javadoc) |
|---|
| 186 | * @see eu.etaxonomy.cdm.model.common.ITerm#getLabel(eu.etaxonomy.cdm.model.common.Language) |
|---|
| 187 | */ |
|---|
| 188 | public String getLabel(Language lang) { |
|---|
| 189 | Representation repr = this.getRepresentation(lang); |
|---|
| 190 | return (repr == null) ? null : repr.getLabel(); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | public void setLabel(String label){ |
|---|
| 194 | Language lang = Language.DEFAULT(); |
|---|
| 195 | setLabel(label, lang); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | public void setLabel(String label, Language language){ |
|---|
| 199 | if (language != null){ |
|---|
| 200 | Representation repr = getRepresentation(language); |
|---|
| 201 | if (repr != null){ |
|---|
| 202 | repr.setLabel(label); |
|---|
| 203 | }else{ |
|---|
| 204 | repr = Representation.NewInstance(null, label, null, language); |
|---|
| 205 | } |
|---|
| 206 | this.addRepresentation(repr); |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | /* (non-Javadoc) |
|---|
| 211 | * @see eu.etaxonomy.cdm.model.common.ITerm#getDescription() |
|---|
| 212 | */ |
|---|
| 213 | @Transient |
|---|
| 214 | public String getDescription() { |
|---|
| 215 | return this.getDescription(Language.DEFAULT()); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | /* (non-Javadoc) |
|---|
| 219 | * @see eu.etaxonomy.cdm.model.common.ITerm#getDescription(eu.etaxonomy.cdm.model.common.Language) |
|---|
| 220 | */ |
|---|
| 221 | public String getDescription(Language lang) { |
|---|
| 222 | Representation repr = this.getRepresentation(lang); |
|---|
| 223 | return (repr == null) ? null :repr.getDescription(); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | @Override |
|---|
| 227 | public boolean equals(Object obj) { |
|---|
| 228 | if (obj == null){ |
|---|
| 229 | return false; |
|---|
| 230 | } |
|---|
| 231 | if (TermBase.class.isAssignableFrom(obj.getClass())){ |
|---|
| 232 | TermBase dtb = (TermBase)obj; |
|---|
| 233 | if (dtb.getUuid().equals(this.getUuid())){ |
|---|
| 234 | return true; |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | return false; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | @Override |
|---|
| 241 | public String toString() { |
|---|
| 242 | //TODO eliminate nasty LazyInitializationException loggings |
|---|
| 243 | try { |
|---|
| 244 | return super.toString(); |
|---|
| 245 | } catch (LazyInitializationException e) { |
|---|
| 246 | return super.toString()+" "+this.getUuid(); |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | //*********************** CLONE ********************************************************/ |
|---|
| 251 | |
|---|
| 252 | /** |
|---|
| 253 | * Clones <i>this</i> TermBase. This is a shortcut that enables to create |
|---|
| 254 | * a new instance that differs only slightly from <i>this</i> TermBase by |
|---|
| 255 | * modifying only some of the attributes. |
|---|
| 256 | * |
|---|
| 257 | * @see eu.etaxonomy.cdm.model.common.IdentifiableEntity#clone() |
|---|
| 258 | * @see java.lang.Object#clone() |
|---|
| 259 | */ |
|---|
| 260 | @Override |
|---|
| 261 | public Object clone()throws CloneNotSupportedException { |
|---|
| 262 | |
|---|
| 263 | TermBase result = (TermBase) super.clone(); |
|---|
| 264 | |
|---|
| 265 | result.representations = new HashSet<Representation>(); |
|---|
| 266 | for (Representation rep : this.representations){ |
|---|
| 267 | result.representations.add((Representation)rep.clone()); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | return result; |
|---|
| 273 | |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | } |
|---|