Project

General

Profile

Download (8.59 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 = "ExternalLink", 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
    private Map<Language,LanguageString> description = new HashMap<>();
93

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

    
98
 // *********************************** FACTORY ******************************/
99

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

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

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

    
112
	/**
113
	 * New default language instance
114
	 * @param type
115
	 * @param uri
116
	 * @param description
117
	 * @param size
118
	 * @return
119
	 */
120
	public static ExternalLink NewInstance(ExternalLinkType type, URI uri, String description, Integer size){
121
	    Map<Language, LanguageString> descMap = new HashMap<>();
122
	    Language language = Language.DEFAULT();
123
	    descMap.put(language, LanguageString.NewInstance(description, language));
124
	    return new ExternalLink(type, uri, descMap, size);
125
	}
126

    
127
    public static ExternalLink NewWebSiteInstance(URI uri){
128
        return NewInstance(ExternalLinkType.WebSite, uri, (String)null, null);
129
    }
130

    
131
    public static ExternalLink NewWebSiteInstance(URI uri, Map<Language,LanguageString> description, Integer size){
132
        return NewInstance(ExternalLinkType.WebSite, uri, description, size);
133
    }
134

    
135

    
136
// *********************************** CONSTRUCTOR ******************************/
137

    
138
	protected ExternalLink(){
139
		super();
140
	}
141

    
142
	protected ExternalLink(ExternalLinkType type, URI uri, Map<Language,LanguageString> description, Integer size){
143
		super();
144
		if (type == null){
145
		    throw new NullPointerException("ExternalLinkType must not be null");
146
		}
147
		this.linkType = type;
148
		this.uri = uri;
149
		this.description = description;
150
		this.size = size;
151
	}
152

    
153
// ******************************* GETTER / SETTER ********************************/
154

    
155
	   public ExternalLinkType getLinkType() {
156
	        return this.linkType;
157
	    }
158
	    public void setLinkType(ExternalLinkType linkType) {
159
	        this.linkType = linkType;
160
	    }
161

    
162
	/**
163
	 * The URI of the external link
164
	 */
165
	public URI getUri() {
166
        return this.uri;
167
    }
168
    public void setUri(URI uri) {
169
        this.uri = uri;
170
    }
171

    
172
    /**
173
     * The download size of the external link. Mostly relevant for files.
174
     */
175
    public Integer getSize() {
176
        return this.size;
177
    }
178
    public void setSize(Integer size) {
179
        this.size = size;
180
    }
181
    /**
182
     * Returns the {@link MultilanguageText multilanguage text} used to describe
183
     * <i>this</i> individuals association. The different {@link LanguageString language strings}
184
     * contained in the multilanguage text should all have the same meaning.
185
     */
186
    public Map<Language,LanguageString> getDescription(){
187
        return this.description;
188
    }
189

    
190

    
191
    /**
192
     * Adds a translated {@link LanguageString text in a particular language}
193
     * to the {@link MultilanguageText multilanguage text} used to describe
194
     * <i>this</i> individuals association.
195
     *
196
     * @param description   the language string describing the individuals association
197
     *                      in a particular language
198
     * @see                 #getDescription()
199
     * @see                 #putDescription(Language, String)
200
     *
201
     */
202
    public void putDescription(LanguageString description){
203
        this.description.put(description.getLanguage(),description);
204
    }
205
    /**
206
     * Creates a {@link LanguageString language string} based on the given text string
207
     * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
208
     * used to describe <i>this</i> individuals association.
209
     *
210
     * @param text      the string describing the individuals association
211
     *                  in a particular language
212
     * @param language  the language in which the text string is formulated
213
     * @see             #getDescription()
214
     * @see             #putDescription(LanguageString)
215
     */
216
    public void putDescription(Language language, String text){
217
        this.description.put(language, LanguageString.NewInstance(text, language));
218
    }
219

    
220
    /**
221
     * Removes from the {@link MultilanguageText multilanguage text} used to describe
222
     * <i>this</i> individuals association the one {@link LanguageString language string}
223
     * with the given {@link Language language}.
224
     *
225
     * @param  language the language in which the language string to be removed
226
     *                  has been formulated
227
     * @see             #getDescription()
228
     */
229
    public void removeDescription(Language language){
230
        this.description.remove(language);
231
    }
232

    
233
  //*********************************** CLONE *****************************************/
234

    
235
    /**
236
     * Clones <i>this</i> external link. This is a shortcut that enables to create
237
     * a new instance that differs only slightly from <i>this</i> external link by
238
     * modifying only some of the attributes.
239
     *
240
     * @see eu.etaxonomy.cdm.model.description.DescriptionElementBase#clone()
241
     * @see java.lang.Object#clone()
242
     */
243
    @Override
244
    public Object clone() {
245

    
246
        try {
247
            ExternalLink result = (ExternalLink)super.clone();
248

    
249
            //description
250
            result.description = cloneLanguageString(getDescription());
251

    
252
            return result;
253
            //TODO do we need to clone URI?
254
            //no changes to: URI, size
255
        } catch (CloneNotSupportedException e) {
256
            logger.warn("Object does not implement cloneable");
257
            e.printStackTrace();
258
            return null;
259
        }
260
    }
261

    
262
}
(2-2/15)