Extension bidirectional
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / Extension.java
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.ManyToOne;
18 import javax.xml.bind.annotation.XmlAccessType;
19 import javax.xml.bind.annotation.XmlAccessorType;
20 import javax.xml.bind.annotation.XmlElement;
21 import javax.xml.bind.annotation.XmlIDREF;
22 import javax.xml.bind.annotation.XmlSchemaType;
23 import javax.xml.bind.annotation.XmlType;
24
25 import org.apache.log4j.Logger;
26 import org.hibernate.annotations.Any;
27 import org.hibernate.envers.Audited;
28 import org.hibernate.envers.NotAudited;
29
30 /**
31 * This class aims to make available more "attributes" for identifiable entities
32 * in a flexible way. Application developers (and even users) can define their own
33 * "attributes" as an ExtensionType and add data to Identifiable instances via
34 * Extension instances.
35 * @author m.doering
36 * @version 1.0
37 * @created 08-Nov-2007 13:06:23
38 */
39 @XmlAccessorType(XmlAccessType.FIELD)
40 @XmlType(name = "Extension", propOrder = {
41 "value",
42 "type",
43 "extendedObj"
44 })
45 @Entity
46 @Audited
47 public class Extension extends VersionableEntity implements Cloneable {
48 private static final long serialVersionUID = -857207737641432202L;
49 @SuppressWarnings("unused")
50 private static final Logger logger = Logger.getLogger(Extension.class);
51
52 @XmlElement(name = "Value")
53 private String value;
54
55 @XmlElement(name = "ExtensionType")
56 @XmlIDREF
57 @XmlSchemaType(name = "IDREF")
58 @ManyToOne(fetch = FetchType.LAZY)
59 private ExtensionType type;
60
61 @XmlElement(name = "ExtendedObject")
62 @XmlIDREF
63 @XmlSchemaType(name = "IDREF")
64 @Any(metaDef = "CdmBase",
65 metaColumn=@Column(name = "extendedObj_type"),
66 fetch = FetchType.LAZY,
67 optional = false)
68 @JoinColumn(name = "extendedObj_id")
69 @NotAudited
70 private IdentifiableEntity extendedObj;
71
72 public static Extension NewInstance(){
73 return new Extension();
74 }
75
76 public static Extension NewInstance(IdentifiableEntity<?> extendedObject, String value, ExtensionType extensionType){
77 Extension extension = new Extension();
78 extension.setValue(value);
79 extension.setType(extensionType);
80 extendedObject.addExtension(extension);
81 return extension;
82 }
83
84 /**
85 * TODO should not be private but throws error in persistence/io test
86 * Constructor
87 */
88 protected Extension(){
89 }
90
91 public IdentifiableEntity getExtendedObj() {
92 return extendedObj;
93 }
94 //for hibernate use only
95 protected void setExtendedObj(IdentifiableEntity extendedObj) {
96 this.extendedObj = extendedObj;
97 }
98
99
100 public ExtensionType getType(){
101 return this.type;
102 }
103
104 /**
105 *
106 * @param type type
107 */
108 public void setType(ExtensionType type){
109 this.type = type;
110 }
111
112 public String getValue(){
113 return this.value;
114 }
115
116 /**
117 *
118 * @param value value
119 */
120 public void setValue(String value){
121 this.value = value;
122 }
123
124 //****************** CLONE ************************************************/
125
126 /* (non-Javadoc)
127 * @see java.lang.Object#clone()
128 */
129 @Override
130 public Object clone() throws CloneNotSupportedException{
131 Extension result = (Extension)super.clone();
132 //no changes to: type, value
133 return result;
134 }
135
136 /**
137 * Clones this extension and sets the clone's extended object to 'extendedObject'
138 * @see java.lang.Object#clone()
139 */
140 public Extension clone(IdentifiableEntity extendedObject) throws CloneNotSupportedException{
141 Extension result = (Extension)clone();
142 result.setExtendedObj(extendedObject);
143 return result;
144 }
145
146 }