Project

General

Profile

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

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

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

    
136

    
137
// *********************************** CONSTRUCTOR ******************************/
138

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

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

    
154
// ******************************* GETTER / SETTER ********************************/
155

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

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

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

    
191

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

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

    
234
  //*********************************** CLONE *****************************************/
235

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

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

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

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

    
263
}
(2-2/15)