Project

General

Profile

Download (4.12 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.common;
11

    
12

    
13
import javax.persistence.Column;
14
import javax.persistence.Entity;
15
import javax.persistence.FetchType;
16
import javax.persistence.JoinColumn;
17
import javax.persistence.Lob;
18
import javax.persistence.ManyToOne;
19
import javax.xml.bind.annotation.XmlAccessType;
20
import javax.xml.bind.annotation.XmlAccessorType;
21
import javax.xml.bind.annotation.XmlElement;
22
import javax.xml.bind.annotation.XmlIDREF;
23
import javax.xml.bind.annotation.XmlSchemaType;
24
import javax.xml.bind.annotation.XmlType;
25

    
26
import org.apache.commons.lang.StringUtils;
27
import org.apache.log4j.Logger;
28
import org.hibernate.annotations.Any;
29
import org.hibernate.envers.Audited;
30
import org.hibernate.envers.NotAudited;
31

    
32
/**
33
 * This class aims to make available more "attributes" for identifiable entities
34
 * in a flexible way. Application developers (and even users) can define their own
35
 * "attributes" as an ExtensionType and add data to Identifiable instances via
36
 * Extension instances.
37
 * @author m.doering
38
 * @version 1.0
39
 * @created 08-Nov-2007 13:06:23
40
 */
41
@XmlAccessorType(XmlAccessType.FIELD)
42
@XmlType(name = "Extension", propOrder = {
43
    "value",
44
    "type",
45
    "extendedObj"
46
})
47
@Entity
48
@Audited
49
public class Extension extends VersionableEntity implements Cloneable {
50
	private static final long serialVersionUID = -857207737641432202L;
51
	@SuppressWarnings("unused")
52
	private static final  Logger logger = Logger.getLogger(Extension.class);
53
	
54
    @XmlElement(name = "Value")
55
	@Lob
56
    private String value;
57
	
58
    @XmlElement(name = "ExtensionType")
59
    @XmlIDREF
60
    @XmlSchemaType(name = "IDREF")
61
    @ManyToOne(fetch = FetchType.LAZY)
62
    private ExtensionType type;
63
	
64
    @XmlElement(name = "ExtendedObject")
65
    @XmlIDREF
66
    @XmlSchemaType(name = "IDREF")
67
    @Any(metaDef = "CdmBase",
68
	    	 metaColumn=@Column(name = "extendedObj_type"),
69
	    	 fetch = FetchType.LAZY,
70
	    	 optional = false)
71
	@JoinColumn(name = "extendedObj_id")
72
	@NotAudited
73
	private IdentifiableEntity extendedObj;
74
	
75
	public static Extension NewInstance(){
76
		return new Extension();
77
	}
78
	
79
	/**
80
	 * Creates a new extension and adds it to the extended object.
81
	 * @param extendedObject
82
	 * @param value
83
	 * @param extensionType
84
	 * @return
85
	 */
86
	public static Extension NewInstance(IdentifiableEntity<?> extendedObject, String value, ExtensionType extensionType){
87
		Extension extension = new Extension();
88
		extension.setValue(value);
89
		extension.setType(extensionType);
90
		extendedObject.addExtension(extension);
91
		return extension;
92
	}
93
	
94
	/**
95
	 * TODO should not be private but throws error in persistence/io test
96
	 * Constructor
97
	 */
98
	protected Extension(){
99
	}
100
	
101
	public IdentifiableEntity getExtendedObj() {
102
		return extendedObj;
103
	}
104
	protected void setExtendedObj(IdentifiableEntity extendedObj) {
105
		this.extendedObj = extendedObj;
106
	}
107

    
108
	
109
	public ExtensionType getType(){
110
		return this.type;
111
	}
112

    
113
	/**
114
	 * 
115
	 * @param type    type
116
	 */
117
	public void setType(ExtensionType type){
118
		this.type = type;
119
	}
120

    
121
	public String getValue(){
122
		return this.value;
123
	}
124

    
125
	/**
126
	 * 
127
	 * @param value    value
128
	 */
129
	public void setValue(String value){
130
		this.value = value;
131
	}
132

    
133
//***************************** TO STRING ***********************************
134
	
135
	@Override
136
	public String toString() {
137
		if (StringUtils.isNotBlank(this.value)){
138
			return "Ext.: " + this.value;
139
		}else{
140
			return super.toString();
141
		}
142
	}
143
	
144
	
145
//****************** CLONE ************************************************/
146
	 
147
	@Override
148
	public Object clone() throws CloneNotSupportedException{
149
		Extension result = (Extension)super.clone();	
150
		//no changes to: type, value
151
		return result;
152
	}
153
	
154
	/**
155
	 * Clones this extension and sets the clone's extended object to 'extendedObject'
156
	 * @see java.lang.Object#clone()
157
	 */
158
	public Extension clone(IdentifiableEntity extendedObject) throws CloneNotSupportedException{
159
		Extension result = (Extension)clone();
160
		result.setExtendedObj(extendedObject);
161
		return result;
162
	}
163

    
164
}
(11-11/72)