1
|
/**
|
2
|
* Copyright (C) 2017 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.ref;
|
10
|
|
11
|
import java.util.UUID;
|
12
|
|
13
|
import org.apache.commons.lang3.builder.HashCodeBuilder;
|
14
|
|
15
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
16
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
17
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
18
|
|
19
|
/**
|
20
|
* @author a.kohlbecker
|
21
|
* @since Jun 12, 2017
|
22
|
*/
|
23
|
public class TypedEntityReference<T extends CdmBase> extends EntityReference {
|
24
|
|
25
|
private static final long serialVersionUID = -4619590272174606288L;
|
26
|
|
27
|
public static <T extends CdmBase> TypedEntityReference<T> fromEntity(T entity) {
|
28
|
return TypedEntityReference.fromEntity(entity, true);
|
29
|
}
|
30
|
|
31
|
private Class<T> type;
|
32
|
|
33
|
/**
|
34
|
* @deprecated use factory method instead, should only be used by in DTO sub-class constructors (TODO: to be made protected once no longer used publicly)
|
35
|
*/
|
36
|
@Deprecated
|
37
|
public TypedEntityReference(Class<T> type, UUID uuid, String label) {
|
38
|
super(uuid, label);
|
39
|
this.type = type;
|
40
|
}
|
41
|
|
42
|
/**
|
43
|
* @deprecated use factory method instead, should only be used by in DTO sub-class constructors (TODO: to be made protected once no longer used publicly)
|
44
|
*/
|
45
|
@Deprecated
|
46
|
protected TypedEntityReference(T entity) {
|
47
|
this.type = (Class<T>) entity.getClass();
|
48
|
this.uuid = entity.getUuid();
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* @deprecated use factory method instead, should only be used by in DTO sub-class constructors (TODO; to be made protected once no longer used publicly)
|
53
|
*/
|
54
|
@Deprecated
|
55
|
public TypedEntityReference(Class<T> type, UUID uuid) {
|
56
|
super(uuid, null);
|
57
|
this.type = type;
|
58
|
}
|
59
|
|
60
|
public static <T extends CdmBase> TypedEntityReference<T> fromEntity(T entity, boolean withLabel) {
|
61
|
if(entity == null) {
|
62
|
return null;
|
63
|
}
|
64
|
entity = HibernateProxyHelper.deproxy(entity);
|
65
|
if(withLabel && IdentifiableEntity.class.isAssignableFrom(entity.getClass())) {
|
66
|
return new TypedEntityReference<>((Class<T>)entity.getClass(), entity.getUuid(), ((IdentifiableEntity)entity).getTitleCache());
|
67
|
} else {
|
68
|
return new TypedEntityReference<>((Class<T>)entity.getClass(), entity.getUuid());
|
69
|
}
|
70
|
}
|
71
|
|
72
|
public static <T extends CdmBase> TypedEntityReference<T> fromEntityWithLabel(T entity, String explicitLabel) {
|
73
|
if(entity == null) {
|
74
|
return null;
|
75
|
}
|
76
|
entity = HibernateProxyHelper.deproxy(entity);
|
77
|
return new TypedEntityReference<>((Class<T>)entity.getClass(), entity.getUuid(), explicitLabel);
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Casts the <code>TypedEntityReference</code> to the <code>subType</code> if possible.
|
82
|
*
|
83
|
* @throws ClassCastException
|
84
|
* If the {@link #type} is not a super type of <code>subType</code>.
|
85
|
*/
|
86
|
public <S extends CdmBase> TypedEntityReference<S> castTo(Class<S> subType){
|
87
|
if(!type.isAssignableFrom(subType)) {
|
88
|
throw new ClassCastException("Cannot cast " + type.getName() + " to " + subType.getName());
|
89
|
}
|
90
|
return new TypedEntityReference<>(subType, getUuid());
|
91
|
}
|
92
|
|
93
|
public Class<T> getType() {
|
94
|
return type;
|
95
|
}
|
96
|
public void setType(Class<T> type) {
|
97
|
this.type = type;
|
98
|
}
|
99
|
|
100
|
@Override
|
101
|
public int hashCode() {
|
102
|
return new HashCodeBuilder(17, 31)
|
103
|
.append(uuid)
|
104
|
.appendSuper(type.hashCode())
|
105
|
.hashCode();
|
106
|
}
|
107
|
|
108
|
@SuppressWarnings("rawtypes")
|
109
|
@Override
|
110
|
public boolean equals(Object obj) {
|
111
|
try {
|
112
|
TypedEntityReference other = (TypedEntityReference) obj;
|
113
|
return uuid.equals(other.uuid) && type.equals(other.type);
|
114
|
|
115
|
} catch (Exception e) {
|
116
|
return false;
|
117
|
}
|
118
|
}
|
119
|
|
120
|
@Override
|
121
|
public String toString(){
|
122
|
return type.getSimpleName() + "#" + uuid;
|
123
|
}
|
124
|
}
|