| 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 | |
|---|
| 13 | import java.util.HashMap; |
|---|
| 14 | import java.util.Map; |
|---|
| 15 | |
|---|
| 16 | import javax.persistence.Entity; |
|---|
| 17 | import javax.persistence.FetchType; |
|---|
| 18 | import javax.persistence.JoinTable; |
|---|
| 19 | import javax.persistence.ManyToOne; |
|---|
| 20 | import javax.persistence.OneToMany; |
|---|
| 21 | import javax.xml.bind.annotation.XmlAccessType; |
|---|
| 22 | import javax.xml.bind.annotation.XmlAccessorType; |
|---|
| 23 | import javax.xml.bind.annotation.XmlElement; |
|---|
| 24 | import javax.xml.bind.annotation.XmlIDREF; |
|---|
| 25 | import javax.xml.bind.annotation.XmlRootElement; |
|---|
| 26 | import javax.xml.bind.annotation.XmlSchemaType; |
|---|
| 27 | import javax.xml.bind.annotation.XmlType; |
|---|
| 28 | import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; |
|---|
| 29 | |
|---|
| 30 | import org.apache.log4j.Logger; |
|---|
| 31 | import org.hibernate.annotations.Cascade; |
|---|
| 32 | import org.hibernate.annotations.CascadeType; |
|---|
| 33 | import org.hibernate.envers.Audited; |
|---|
| 34 | import org.hibernate.search.annotations.Indexed; |
|---|
| 35 | |
|---|
| 36 | import eu.etaxonomy.cdm.jaxb.MultilanguageTextAdapter; |
|---|
| 37 | import eu.etaxonomy.cdm.model.common.IMultiLanguageTextHolder; |
|---|
| 38 | import eu.etaxonomy.cdm.model.common.Language; |
|---|
| 39 | import eu.etaxonomy.cdm.model.common.LanguageString; |
|---|
| 40 | import eu.etaxonomy.cdm.model.common.MultilanguageText; |
|---|
| 41 | import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase; |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * This class represents associations between the described |
|---|
| 45 | * {@link SpecimenOrObservationBase specimen or observation} |
|---|
| 46 | * and a second one (for instance a host). |
|---|
| 47 | * Only {@link SpecimenDescription specimen descriptions} may contain individuals association. |
|---|
| 48 | * The association itself is described by a {@link MultilanguageText multilanguage text}. |
|---|
| 49 | * <P> |
|---|
| 50 | * This class corresponds (partially) to NaturalLanguageDescriptionType |
|---|
| 51 | * according to the SDD schema. |
|---|
| 52 | * |
|---|
| 53 | * @author m.doering |
|---|
| 54 | * @version 1.0 |
|---|
| 55 | * @created 08-Nov-2007 13:06:28 |
|---|
| 56 | */ |
|---|
| 57 | @XmlAccessorType(XmlAccessType.FIELD) |
|---|
| 58 | @XmlType(name = "IndividualsAssociation", propOrder = { |
|---|
| 59 | "description", |
|---|
| 60 | "associatedSpecimenOrObservation" |
|---|
| 61 | }) |
|---|
| 62 | @XmlRootElement(name = "IndividualsAssociation") |
|---|
| 63 | @Entity |
|---|
| 64 | @Audited |
|---|
| 65 | @Indexed(index = "eu.etaxonomy.cdm.model.description.DescriptionElementBase") |
|---|
| 66 | public class IndividualsAssociation extends DescriptionElementBase implements IMultiLanguageTextHolder, Cloneable{ |
|---|
| 67 | private static final long serialVersionUID = -4117554860254531809L; |
|---|
| 68 | @SuppressWarnings("unused") |
|---|
| 69 | private static final Logger logger = Logger.getLogger(IndividualsAssociation.class); |
|---|
| 70 | |
|---|
| 71 | @XmlElement(name = "Description") |
|---|
| 72 | @XmlJavaTypeAdapter(MultilanguageTextAdapter.class) |
|---|
| 73 | @OneToMany(fetch = FetchType.LAZY) |
|---|
| 74 | @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.DELETE, CascadeType.DELETE_ORPHAN }) |
|---|
| 75 | @JoinTable(name = "IndividualAssociation_LanguageString") |
|---|
| 76 | private Map<Language,LanguageString> description = new HashMap<Language,LanguageString>(); |
|---|
| 77 | |
|---|
| 78 | @XmlElement(name = "AssociatedSpecimenOrObservation") |
|---|
| 79 | @XmlIDREF |
|---|
| 80 | @XmlSchemaType(name = "IDREF") |
|---|
| 81 | @ManyToOne(fetch = FetchType.LAZY) |
|---|
| 82 | @Cascade(CascadeType.SAVE_UPDATE) |
|---|
| 83 | private SpecimenOrObservationBase associatedSpecimenOrObservation; |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * Class constructor: creates a new empty individuals association instance. |
|---|
| 87 | */ |
|---|
| 88 | protected IndividualsAssociation(){ |
|---|
| 89 | super(null); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Creates a new empty individuals association instance. |
|---|
| 94 | */ |
|---|
| 95 | public static IndividualsAssociation NewInstance(){ |
|---|
| 96 | IndividualsAssociation result = new IndividualsAssociation(); |
|---|
| 97 | result.setFeature(Feature.INDIVIDUALS_ASSOCIATION()); |
|---|
| 98 | return result; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Returns the second {@link SpecimenOrObservationBase specimen or observation} |
|---|
| 104 | * involved in <i>this</i> individuals association. |
|---|
| 105 | * The first specimen or observation is the specimen or observation |
|---|
| 106 | * described in the corresponding {@link SpecimenDescription specimen description}. |
|---|
| 107 | */ |
|---|
| 108 | public SpecimenOrObservationBase getAssociatedSpecimenOrObservation() { |
|---|
| 109 | return associatedSpecimenOrObservation; |
|---|
| 110 | } |
|---|
| 111 | /** |
|---|
| 112 | * @see #getAssociatedSpecimenOrObservation() |
|---|
| 113 | */ |
|---|
| 114 | public void setAssociatedSpecimenOrObservation( |
|---|
| 115 | SpecimenOrObservationBase associatedSpecimenOrObservation) { |
|---|
| 116 | this.associatedSpecimenOrObservation = associatedSpecimenOrObservation; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * Returns the {@link MultilanguageText multilanguage text} used to describe |
|---|
| 122 | * <i>this</i> individuals association. The different {@link LanguageString language strings} |
|---|
| 123 | * contained in the multilanguage text should all have the same meaning. |
|---|
| 124 | */ |
|---|
| 125 | public Map<Language,LanguageString> getDescription(){ |
|---|
| 126 | return this.description; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | * Adds a translated {@link LanguageString text in a particular language} |
|---|
| 131 | * to the {@link MultilanguageText multilanguage text} used to describe |
|---|
| 132 | * <i>this</i> individuals association. |
|---|
| 133 | * |
|---|
| 134 | * @param description the language string describing the individuals association |
|---|
| 135 | * in a particular language |
|---|
| 136 | * @see #getDescription() |
|---|
| 137 | * @see #putDescription(Language, String) |
|---|
| 138 | * @deprecated should follow the put semantic of maps, this method will be removed in v4.0 |
|---|
| 139 | * Use the {@link #putDescription(LanguageString) putDescription} method instead |
|---|
| 140 | */ |
|---|
| 141 | public void addDescription(LanguageString description){ |
|---|
| 142 | this.putDescription(description); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /** |
|---|
| 146 | * Adds a translated {@link LanguageString text in a particular language} |
|---|
| 147 | * to the {@link MultilanguageText multilanguage text} used to describe |
|---|
| 148 | * <i>this</i> individuals association. |
|---|
| 149 | * |
|---|
| 150 | * @param description the language string describing the individuals association |
|---|
| 151 | * in a particular language |
|---|
| 152 | * @see #getDescription() |
|---|
| 153 | * @see #putDescription(Language, String) |
|---|
| 154 | * |
|---|
| 155 | */ |
|---|
| 156 | public void putDescription(LanguageString description){ |
|---|
| 157 | this.description.put(description.getLanguage(),description); |
|---|
| 158 | } |
|---|
| 159 | /** |
|---|
| 160 | * Creates a {@link LanguageString language string} based on the given text string |
|---|
| 161 | * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text} |
|---|
| 162 | * used to describe <i>this</i> individuals association. |
|---|
| 163 | * |
|---|
| 164 | * @param text the string describing the individuals association |
|---|
| 165 | * in a particular language |
|---|
| 166 | * @param language the language in which the text string is formulated |
|---|
| 167 | * @see #getDescription() |
|---|
| 168 | * @see #putDescription(LanguageString) |
|---|
| 169 | */ |
|---|
| 170 | public void putDescription(Language language, String text){ |
|---|
| 171 | this.description.put(language, LanguageString.NewInstance(text, language)); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /** |
|---|
| 175 | * Creates a {@link LanguageString language string} based on the given text string |
|---|
| 176 | * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text} |
|---|
| 177 | * used to describe <i>this</i> individuals association. |
|---|
| 178 | * |
|---|
| 179 | * @param text the string describing the individuals association |
|---|
| 180 | * in a particular language |
|---|
| 181 | * @param language the language in which the text string is formulated |
|---|
| 182 | * @see #getDescription() |
|---|
| 183 | * @see #putDescription(LanguageString) |
|---|
| 184 | * @deprecated should follow the put semantic of maps, this method will be removed in v4.0 |
|---|
| 185 | * Use the {@link #putDescription(Language, String) putDescription} method instead |
|---|
| 186 | */ |
|---|
| 187 | public void addDescription(String text, Language language){ |
|---|
| 188 | this.putDescription(language, text); |
|---|
| 189 | } |
|---|
| 190 | /** |
|---|
| 191 | * Removes from the {@link MultilanguageText multilanguage text} used to describe |
|---|
| 192 | * <i>this</i> individuals association the one {@link LanguageString language string} |
|---|
| 193 | * with the given {@link Language language}. |
|---|
| 194 | * |
|---|
| 195 | * @param language the language in which the language string to be removed |
|---|
| 196 | * has been formulated |
|---|
| 197 | * @see #getDescription() |
|---|
| 198 | */ |
|---|
| 199 | public void removeDescription(Language language){ |
|---|
| 200 | this.description.remove(language); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | //*********************************** CLONE *****************************************/ |
|---|
| 205 | |
|---|
| 206 | /** |
|---|
| 207 | * Clones <i>this</i> individuals association. This is a shortcut that enables to create |
|---|
| 208 | * a new instance that differs only slightly from <i>this</i> individuals association by |
|---|
| 209 | * modifying only some of the attributes. |
|---|
| 210 | * |
|---|
| 211 | * @see eu.etaxonomy.cdm.model.description.DescriptionElementBase#clone() |
|---|
| 212 | * @see java.lang.Object#clone() |
|---|
| 213 | */ |
|---|
| 214 | @Override |
|---|
| 215 | public Object clone() { |
|---|
| 216 | |
|---|
| 217 | try { |
|---|
| 218 | IndividualsAssociation result = (IndividualsAssociation)super.clone(); |
|---|
| 219 | |
|---|
| 220 | //description |
|---|
| 221 | result.description = new HashMap<Language, LanguageString>(); |
|---|
| 222 | for (Language language : getDescription().keySet()){ |
|---|
| 223 | //TODO clone needed? |
|---|
| 224 | LanguageString newLanguageString = (LanguageString)getDescription().get(language).clone(); |
|---|
| 225 | result.description.put(language, newLanguageString); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | return result; |
|---|
| 230 | //no changes to: associatedSpecimenOrObservation |
|---|
| 231 | } catch (CloneNotSupportedException e) { |
|---|
| 232 | logger.warn("Object does not implement cloneable"); |
|---|
| 233 | e.printStackTrace(); |
|---|
| 234 | return null; |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | } |
|---|