Project

General

Profile

Download (8.16 KB) Statistics
| Branch: | Tag: | Revision:
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.media;
11

    
12

    
13
import java.net.URI;
14
import java.util.HashMap;
15
import java.util.Map;
16

    
17
import javax.persistence.Column;
18
import javax.persistence.Entity;
19
import javax.persistence.FetchType;
20
import javax.persistence.MapKeyJoinColumn;
21
import javax.persistence.OneToMany;
22
import javax.validation.constraints.NotNull;
23
import javax.xml.bind.annotation.XmlAccessType;
24
import javax.xml.bind.annotation.XmlAccessorType;
25
import javax.xml.bind.annotation.XmlAttribute;
26
import javax.xml.bind.annotation.XmlElement;
27
import javax.xml.bind.annotation.XmlRootElement;
28
import javax.xml.bind.annotation.XmlType;
29
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
30

    
31
import org.apache.log4j.Logger;
32
import org.hibernate.annotations.Cascade;
33
import org.hibernate.annotations.CascadeType;
34
import org.hibernate.annotations.Type;
35
import org.hibernate.envers.Audited;
36

    
37
import eu.etaxonomy.cdm.jaxb.MultilanguageTextAdapter;
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.common.VersionableEntity;
42

    
43

    
44
/**
45
 * Class that represents a link to any web resource.
46
 * It represents a URL and a semantic description of this URL.
47
 *
48
 * TODO: may become annotatable in future
49
 *
50
 * @author a.mueller
51
 * @date 09.06.2017
52
 *
53
 */
54

    
55
@XmlAccessorType(XmlAccessType.FIELD)
56
@XmlType(name = "ImageFile", propOrder = {
57
    "uri",
58
    "size",
59
    "description"
60
})
61
@XmlRootElement(name = "ExternalLink")
62
@Entity
63
@Audited
64
public class ExternalLink extends VersionableEntity implements Cloneable{
65

    
66
    private static final long serialVersionUID = -5008959652949778843L;
67

    
68
    private static final Logger logger = Logger.getLogger(ExternalLink.class);
69

    
70
    /**
71
     * The {@link ExternalLinkType type} of this link.
72
     */
73
    @XmlAttribute(name ="ExternalLinkType")
74
    @Column(name="linkType", length=10)
75
    @NotNull
76
    @Type(type = "eu.etaxonomy.cdm.hibernate.EnumUserType",
77
        parameters = {@org.hibernate.annotations.Parameter(name  = "enumClass", value = "eu.etaxonomy.cdm.model.media.ExternalLinkType")}
78
    )
79
    @Audited
80
    private ExternalLinkType linkType = ExternalLinkType.Unknown;
81

    
82
    // the URI to link to
83
    @XmlElement(name = "URI")
84
    @Type(type="uriUserType")
85
    private URI uri;
86

    
87
    @XmlElement(name = "Description")
88
    @XmlJavaTypeAdapter(MultilanguageTextAdapter.class)
89
    @OneToMany(fetch = FetchType.LAZY, orphanRemoval=true)
90
    @MapKeyJoinColumn(name="description_mapkey_id")
91
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.DELETE })
92
//    @JoinTable(name = "ExternalLink_LanguageString")
93
    private Map<Language,LanguageString> description = new HashMap<>();
94

    
95
    // the exptected size, mostly relevant for Files, can be null
96
    @XmlElement(name = "Size")
97
    private Integer size;
98

    
99
 // *********************************** FACTORY ******************************/
100

    
101
    public static ExternalLink NewInstance(ExternalLinkType type, URI uri){
102
        return NewInstance(type, uri, null, null);
103
    }
104

    
105
	public static ExternalLink NewInstance(ExternalLinkType type, URI uri, Map<Language,LanguageString> description){
106
		return NewInstance(type, uri, description, null);
107
	}
108

    
109
	public static ExternalLink NewInstance(ExternalLinkType type, URI uri, Map<Language,LanguageString> description, Integer size){
110
        return new ExternalLink(type, uri, description, size);
111
    }
112

    
113
    public static ExternalLink NewWebSiteInstance(URI uri){
114
        return NewInstance(ExternalLinkType.WebSite, uri, null, null);
115
    }
116

    
117
    public static ExternalLink NewWebSiteInstance(URI uri, Map<Language,LanguageString> description, Integer size){
118
        return NewInstance(ExternalLinkType.WebSite, uri, description, size);
119
    }
120

    
121

    
122
// *********************************** CONSTRUCTOR ******************************/
123

    
124
	protected ExternalLink(){
125
		super();
126
	}
127

    
128
	protected ExternalLink(ExternalLinkType type, URI uri, Map<Language,LanguageString> description, Integer size){
129
		super();
130
		if (type == null){
131
		    throw new NullPointerException("ExternalLinkType must not be null");
132
		}
133
		this.linkType = type;
134
		this.uri = uri;
135
		this.description = description;
136
		this.size = size;
137
	}
138

    
139
// ******************************* GETTER / SETTER ********************************/
140

    
141
	   public ExternalLinkType getLinkType() {
142
	        return this.linkType;
143
	    }
144
	    public void setLinkType(ExternalLinkType linkType) {
145
	        this.linkType = linkType;
146
	    }
147

    
148
	/**
149
	 * The URI of the external link
150
	 */
151
	public URI getUri() {
152
        return this.uri;
153
    }
154
    public void setUri(URI uri) {
155
        this.uri = uri;
156
    }
157

    
158
    /**
159
     * The download size of the external link. Mostly relevant for files.
160
     */
161
    public Integer getSize() {
162
        return this.size;
163
    }
164
    public void setSize(Integer size) {
165
        this.size = size;
166
    }
167
    /**
168
     * Returns the {@link MultilanguageText multilanguage text} used to describe
169
     * <i>this</i> individuals association. The different {@link LanguageString language strings}
170
     * contained in the multilanguage text should all have the same meaning.
171
     */
172
    public Map<Language,LanguageString> getDescription(){
173
        return this.description;
174
    }
175

    
176

    
177
    /**
178
     * Adds a translated {@link LanguageString text in a particular language}
179
     * to the {@link MultilanguageText multilanguage text} used to describe
180
     * <i>this</i> individuals association.
181
     *
182
     * @param description   the language string describing the individuals association
183
     *                      in a particular language
184
     * @see                 #getDescription()
185
     * @see                 #putDescription(Language, String)
186
     *
187
     */
188
    public void putDescription(LanguageString description){
189
        this.description.put(description.getLanguage(),description);
190
    }
191
    /**
192
     * Creates a {@link LanguageString language string} based on the given text string
193
     * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
194
     * used to describe <i>this</i> individuals association.
195
     *
196
     * @param text      the string describing the individuals association
197
     *                  in a particular language
198
     * @param language  the language in which the text string is formulated
199
     * @see             #getDescription()
200
     * @see             #putDescription(LanguageString)
201
     */
202
    public void putDescription(Language language, String text){
203
        this.description.put(language, LanguageString.NewInstance(text, language));
204
    }
205

    
206
    /**
207
     * Removes from the {@link MultilanguageText multilanguage text} used to describe
208
     * <i>this</i> individuals association the one {@link LanguageString language string}
209
     * with the given {@link Language language}.
210
     *
211
     * @param  language the language in which the language string to be removed
212
     *                  has been formulated
213
     * @see             #getDescription()
214
     */
215
    public void removeDescription(Language language){
216
        this.description.remove(language);
217
    }
218

    
219
  //*********************************** CLONE *****************************************/
220

    
221
    /**
222
     * Clones <i>this</i> external link. This is a shortcut that enables to create
223
     * a new instance that differs only slightly from <i>this</i> external link by
224
     * modifying only some of the attributes.
225
     *
226
     * @see eu.etaxonomy.cdm.model.description.DescriptionElementBase#clone()
227
     * @see java.lang.Object#clone()
228
     */
229
    @Override
230
    public Object clone() {
231

    
232
        try {
233
            ExternalLink result = (ExternalLink)super.clone();
234

    
235
            //description
236
            result.description = cloneLanguageString(getDescription());
237

    
238
            return result;
239
            //TODO do we need to clone URI?
240
            //no changes to: URI, size
241
        } catch (CloneNotSupportedException e) {
242
            logger.warn("Object does not implement cloneable");
243
            e.printStackTrace();
244
            return null;
245
        }
246
    }
247

    
248
}
(2-2/15)