Project

General

Profile

Download (9.28 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
package eu.etaxonomy.cdm.model.media;
10

    
11
import java.util.HashMap;
12
import java.util.Map;
13

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

    
28
import org.apache.log4j.Logger;
29
import org.hibernate.annotations.Cascade;
30
import org.hibernate.annotations.CascadeType;
31
import org.hibernate.annotations.Parameter;
32
import org.hibernate.annotations.Type;
33
import org.hibernate.envers.Audited;
34
import org.hibernate.search.annotations.FieldBridge;
35

    
36
import eu.etaxonomy.cdm.common.URI;
37
import eu.etaxonomy.cdm.hibernate.search.UriBridge;
38
import eu.etaxonomy.cdm.jaxb.MultilanguageTextAdapter;
39
import eu.etaxonomy.cdm.model.common.Language;
40
import eu.etaxonomy.cdm.model.common.LanguageString;
41
import eu.etaxonomy.cdm.model.common.MultilanguageText;
42
import eu.etaxonomy.cdm.model.common.VersionableEntity;
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
@XmlAccessorType(XmlAccessType.FIELD)
54
@XmlType(name = "ExternalLink", propOrder = {
55
    "uri",
56
    "size",
57
    "description"
58
})
59
@XmlRootElement(name = "ExternalLink")
60
@Entity
61
@Audited
62
public class ExternalLink extends VersionableEntity{
63

    
64
    private static final long serialVersionUID = -5008959652949778843L;
65

    
66
    private static final Logger logger = Logger.getLogger(ExternalLink.class);
67

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

    
80
    // the URI to link to
81
    @XmlElement(name = "URI")
82
    @FieldBridge(impl = UriBridge.class)
83
    @Type(type="uriUserType")
84
    private URI uri;
85

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

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

    
97
 // *********************************** FACTORY ******************************/
98

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

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

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

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

    
121
    public static ExternalLink NewInstance(ExternalLinkType type, URI uri, String description, Language language, Integer size){
122
        Map<Language, LanguageString> descMap = new HashMap<>();
123
        if (language == null){
124
            language = Language.UNKNOWN_LANGUAGE();
125
        }
126
        descMap.put(language, LanguageString.NewInstance(description, language));
127
        return new ExternalLink(type, uri, descMap, size);
128
    }
129

    
130
    public static ExternalLink NewWebSiteInstance(URI uri){
131
        return NewInstance(ExternalLinkType.WebSite, uri, (String)null, null);
132
    }
133

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

    
138
    public static ExternalLink NewWebSiteInstance(URI uri, String description, Language language){
139
        return NewInstance(ExternalLinkType.WebSite, uri, description, language, null);
140
    }
141

    
142
// *********************************** CONSTRUCTOR ******************************/
143

    
144
	protected ExternalLink(){
145
		super();
146
	}
147

    
148
	protected ExternalLink(ExternalLinkType type, URI uri, Map<Language,LanguageString> description, Integer size){
149
		super();
150
		if (type == null){
151
		    throw new NullPointerException("ExternalLinkType must not be null");
152
		}
153
		this.linkType = type;
154
		this.uri = uri;
155
		this.description = description;
156
		this.size = size;
157
	}
158

    
159
// ******************************* GETTER / SETTER ********************************/
160

    
161
	   public ExternalLinkType getLinkType() {
162
	        return this.linkType;
163
	    }
164
	    public void setLinkType(ExternalLinkType linkType) {
165
	        this.linkType = linkType;
166
	    }
167

    
168
	/**
169
	 * The URI of the external link
170
	 */
171
	public URI getUri() {
172
        return this.uri;
173
    }
174
    public void setUri(URI uri) {
175
        this.uri = uri;
176
    }
177

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

    
196

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

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

    
239
  //*********************************** CLONE *****************************************/
240

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

    
252
        try {
253
            ExternalLink result = (ExternalLink)super.clone();
254

    
255
            //description
256
            result.description = cloneLanguageString(getDescription());
257

    
258
            return result;
259
            //TODO do we need to clone URI?
260
            //no changes to: URI, size
261
        } catch (CloneNotSupportedException e) {
262
            logger.warn("Object does not implement cloneable");
263
            e.printStackTrace();
264
            return null;
265
        }
266
    }
267

    
268
}
(2-2/16)