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
|
private Class<T> type;
|
28
|
|
29
|
/**
|
30
|
* @deprecated use factory method instead (TODO: to be made protected once no longer used publicly)
|
31
|
*/
|
32
|
@Deprecated
|
33
|
public TypedEntityReference(Class<T> type, UUID uuid, String label) {
|
34
|
super(uuid, label);
|
35
|
this.type = type;
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* @deprecated use factory method instead (TODO; to be made protected once no longer used publicly)
|
40
|
*/
|
41
|
@Deprecated
|
42
|
public TypedEntityReference(Class<T> type, UUID uuid) {
|
43
|
super(uuid, null);
|
44
|
this.type = type;
|
45
|
}
|
46
|
|
47
|
public static <T extends CdmBase> TypedEntityReference<T> fromEntity(T entity, boolean withLabel) {
|
48
|
if(entity == null) {
|
49
|
return null;
|
50
|
}
|
51
|
entity = HibernateProxyHelper.deproxy(entity);
|
52
|
if(withLabel && IdentifiableEntity.class.isAssignableFrom(entity.getClass())) {
|
53
|
return new TypedEntityReference<T>((Class<T>)entity.getClass(), entity.getUuid(), ((IdentifiableEntity)entity).getTitleCache());
|
54
|
} else {
|
55
|
return new TypedEntityReference<T>((Class<T>)entity.getClass(), entity.getUuid());
|
56
|
}
|
57
|
}
|
58
|
|
59
|
public static <T extends CdmBase> TypedEntityReference<T> fromEntity(T entity) {
|
60
|
return TypedEntityReference.fromEntity(entity, true);
|
61
|
}
|
62
|
|
63
|
public Class<T> getType() {
|
64
|
return type;
|
65
|
}
|
66
|
public void setType(Class<T> type) {
|
67
|
this.type = type;
|
68
|
}
|
69
|
|
70
|
@Override
|
71
|
public int hashCode() {
|
72
|
return new HashCodeBuilder(17, 31)
|
73
|
.append(uuid)
|
74
|
.appendSuper(type.hashCode())
|
75
|
.hashCode();
|
76
|
}
|
77
|
|
78
|
@SuppressWarnings("rawtypes")
|
79
|
@Override
|
80
|
public boolean equals(Object obj) {
|
81
|
try {
|
82
|
TypedEntityReference other = (TypedEntityReference) obj;
|
83
|
return uuid.equals(other.uuid) && type.equals(other.type);
|
84
|
|
85
|
} catch (Exception e) {
|
86
|
return false;
|
87
|
}
|
88
|
}
|
89
|
|
90
|
@Override
|
91
|
public String toString(){
|
92
|
return type.getSimpleName() + "#" + uuid;
|
93
|
}
|
94
|
}
|