2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.persistence
.hibernate
;
11 import java
.util
.HashSet
;
14 import java
.util
.concurrent
.ConcurrentHashMap
;
16 import org
.hibernate
.Hibernate
;
17 import org
.hibernate
.HibernateException
;
18 import org
.hibernate
.Session
;
19 import org
.hibernate
.event
.spi
.MergeEvent
;
20 import org
.hibernate
.event
.spi
.MergeEventListener
;
22 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
23 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
24 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
25 import eu
.etaxonomy
.cdm
.model
.term
.TermTree
;
26 import eu
.etaxonomy
.cdm
.model
.term
.TermNode
;
33 public class PostMergeEntityListener
implements MergeEventListener
{
34 private static final long serialVersionUID
= 1565797119368313987L;
36 private static Map
<Session
, Set
<CdmBase
>> newEntitiesMap
= new ConcurrentHashMap
<>();
39 public static void addSession(Session session
) {
40 newEntitiesMap
.put(session
, new HashSet
<>());
43 public static void removeSession(Session session
) {
44 newEntitiesMap
.remove(session
);
47 public static Set
<CdmBase
> getNewEntities(Session session
) {
48 return newEntitiesMap
.get(session
);
52 public void onMerge(MergeEvent event
) throws HibernateException
{
53 Object entity
= event
.getEntity();
58 public void onMerge(MergeEvent event
, Map copiedAlready
) throws HibernateException
{
59 // any new entities are added to a map which is retrieved at the end of the
60 // CdmEntityDaoBase.merge(T transientObject, boolean returnTransientEntity) call
61 if(event
.getOriginal() != null && CdmBase
.class.isAssignableFrom(event
.getOriginal().getClass()) &&
62 event
.getResult() != null && CdmBase
.class.isAssignableFrom(event
.getResult().getClass())) {
63 CdmBase original
= (CdmBase
) event
.getOriginal();
64 CdmBase result
= (CdmBase
) event
.getResult();
65 removeNullFromCollections(result
);
66 if(original
!= null && Hibernate
.isInitialized(original
) && original
.getId() == 0 &&
67 result
!= null && Hibernate
.isInitialized(result
) && result
.getId() > 0) {
68 original
.setId(result
.getId());
69 Set
<CdmBase
> newEntities
= newEntitiesMap
.get(event
.getSession());
70 if(newEntities
!= null) {
71 newEntities
.add(result
);
86 private static void removeNullFromCollections(Object entity
) {
88 Class
<?
> entityClazz
= entity
.getClass();
90 if (TaxonNode
.class.isAssignableFrom(entityClazz
)){
91 TaxonNode node
= (TaxonNode
)entity
;
92 node
.removeNullValueFromChildren();
93 } else if (PolytomousKeyNode
.class.isAssignableFrom(entityClazz
)){
94 PolytomousKeyNode node
= (PolytomousKeyNode
) entity
;
95 if (node
.getChildren() != null && Hibernate
.isInitialized(node
.getChildren()) ){
96 node
.removeNullValueFromChildren();
97 for (PolytomousKeyNode childNode
: node
.getChildren()){
98 removeNullFromCollections(childNode
);
103 } else if(TermTree
.class.isAssignableFrom(entityClazz
)){
105 TermTree
<?
> tree
= (TermTree
)entity
;
106 for (TermNode
<?
> node
:tree
.getRootChildren()){
107 node
.removeNullValueFromChildren();
108 if (node
.getChildNodes() != null){
109 for (TermNode childNode
: node
.getChildNodes()){
110 removeNullFromCollections(childNode
);
115 } else if (TermNode
.class.isAssignableFrom(entityClazz
)){
116 TermNode node
= (TermNode
)entity
;
117 node
.removeNullValueFromChildren();