1
|
/**
|
2
|
* Copyright (C) 2015 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.persistence.hibernate;
|
10
|
|
11
|
import java.util.HashSet;
|
12
|
import java.util.Map;
|
13
|
import java.util.Set;
|
14
|
import java.util.concurrent.ConcurrentHashMap;
|
15
|
|
16
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
17
|
import org.hibernate.Hibernate;
|
18
|
import org.hibernate.HibernateException;
|
19
|
import org.hibernate.LazyInitializationException;
|
20
|
import org.hibernate.Session;
|
21
|
import org.hibernate.event.spi.MergeEvent;
|
22
|
import org.hibernate.event.spi.MergeEventListener;
|
23
|
|
24
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
25
|
import eu.etaxonomy.cdm.model.description.PolytomousKey;
|
26
|
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
|
27
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
28
|
import eu.etaxonomy.cdm.model.term.TermNode;
|
29
|
import eu.etaxonomy.cdm.model.term.TermTree;
|
30
|
|
31
|
/**
|
32
|
* @author cmathew
|
33
|
* @since 23 Sep 2015
|
34
|
*/
|
35
|
public class PostMergeEntityListener implements MergeEventListener {
|
36
|
private static final long serialVersionUID = 1565797119368313987L;
|
37
|
|
38
|
private static Map<Session, Set<CdmBase>> newEntitiesMap = new ConcurrentHashMap<>();
|
39
|
private static final Logger logger = LogManager.getLogger(PostMergeEntityListener.class);
|
40
|
|
41
|
public static void addSession(Session session) {
|
42
|
newEntitiesMap.put(session, new HashSet<>());
|
43
|
}
|
44
|
|
45
|
public static void removeSession(Session session) {
|
46
|
newEntitiesMap.remove(session);
|
47
|
}
|
48
|
|
49
|
public static Set<CdmBase> getNewEntities(Session session) {
|
50
|
return newEntitiesMap.get(session);
|
51
|
}
|
52
|
|
53
|
@Override
|
54
|
public void onMerge(MergeEvent event) throws HibernateException {
|
55
|
Object entity = event.getEntity();
|
56
|
}
|
57
|
|
58
|
@Override
|
59
|
public void onMerge(MergeEvent event, Map copiedAlready) throws HibernateException {
|
60
|
// any new entities are added to a map which is retrieved at the end of the
|
61
|
// CdmEntityDaoBase.merge(T transientObject, boolean returnTransientEntity) call
|
62
|
if(event.getOriginal() != null && CdmBase.class.isAssignableFrom(event.getOriginal().getClass()) &&
|
63
|
event.getResult() != null && CdmBase.class.isAssignableFrom(event.getResult().getClass())) {
|
64
|
CdmBase original = (CdmBase) event.getOriginal();
|
65
|
CdmBase result = (CdmBase) event.getResult();
|
66
|
removeNullFromCollections(result);
|
67
|
if(original != null && Hibernate.isInitialized(original) && original.getId() == 0 &&
|
68
|
result != null && Hibernate.isInitialized(result) && result.getId() > 0) {
|
69
|
original.setId(result.getId());
|
70
|
Set<CdmBase> newEntities = newEntitiesMap.get(event.getSession());
|
71
|
if(newEntities != null) {
|
72
|
newEntities.add(result);
|
73
|
}
|
74
|
}
|
75
|
}
|
76
|
}
|
77
|
|
78
|
private static void removeNullFromCollections(Object entity) {
|
79
|
if (entity != null){
|
80
|
Class<?> entityClazz = entity.getClass();
|
81
|
|
82
|
if (TaxonNode.class.isAssignableFrom(entityClazz)){
|
83
|
TaxonNode node = (TaxonNode)entity;
|
84
|
node.removeNullValueFromChildren();
|
85
|
} else if (PolytomousKeyNode.class.isAssignableFrom(entityClazz)){
|
86
|
PolytomousKeyNode node = (PolytomousKeyNode) entity;
|
87
|
if (node.getChildren() != null && Hibernate.isInitialized(node.getChildren()) ){
|
88
|
node.removeNullValueFromChildren();
|
89
|
for (PolytomousKeyNode childNode: node.getChildren()){
|
90
|
removeNullFromCollections(childNode);
|
91
|
}
|
92
|
}
|
93
|
}else if (PolytomousKey.class.isAssignableFrom(entityClazz)){
|
94
|
PolytomousKey key = (PolytomousKey) entity;
|
95
|
PolytomousKeyNode node = key.getRoot();
|
96
|
if (node != null && node.getChildren() != null && Hibernate.isInitialized(node.getChildren()) ){
|
97
|
node.removeNullValueFromChildren();
|
98
|
for (PolytomousKeyNode childNode: node.getChildren()){
|
99
|
removeNullFromCollections(childNode);
|
100
|
}
|
101
|
}
|
102
|
}else if(TermTree.class.isAssignableFrom(entityClazz)){
|
103
|
|
104
|
TermTree<?> tree = (TermTree<?>)entity;
|
105
|
tree.removeNullValueFromChildren();
|
106
|
try{
|
107
|
for (TermNode<?> node:tree.getRootChildren()){
|
108
|
node.removeNullValueFromChildren();
|
109
|
if (node.getChildNodes() != null){
|
110
|
for (TermNode<?> childNode: node.getChildNodes()){
|
111
|
removeNullFromCollections(childNode);
|
112
|
}
|
113
|
}
|
114
|
}
|
115
|
} catch (LazyInitializationException e) {
|
116
|
logger.warn("Cannot clean up uninitialized children without a session, skipping.");
|
117
|
}
|
118
|
} else if (TermNode.class.isAssignableFrom(entityClazz)){
|
119
|
TermNode<?> node = (TermNode<?>)entity;
|
120
|
if (Hibernate.isInitialized(node.getChildNodes())){
|
121
|
node.removeNullValueFromChildren();
|
122
|
}
|
123
|
}
|
124
|
}
|
125
|
}
|
126
|
}
|