Project

General

Profile

Download (4.25 KB) Statistics
| Branch: | Tag: | Revision:
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.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;
21

    
22
import eu.etaxonomy.cdm.model.common.CdmBase;
23
import eu.etaxonomy.cdm.model.description.FeatureNode;
24
import eu.etaxonomy.cdm.model.description.FeatureTree;
25
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
26
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
27

    
28
/**
29
 * @author cmathew
30
 * @since 23 Sep 2015
31
 *
32
 */
33
public class PostMergeEntityListener implements MergeEventListener {
34
    private static final long serialVersionUID = 1565797119368313987L;
35

    
36
    private static Map<Session, Set<CdmBase>> newEntitiesMap = new ConcurrentHashMap<>();
37

    
38

    
39
    public static void addSession(Session session) {
40
        newEntitiesMap.put(session, new HashSet<>());
41
    }
42

    
43
    public static void removeSession(Session session) {
44
        newEntitiesMap.remove(session);
45
    }
46

    
47
    public static Set<CdmBase> getNewEntities(Session session) {
48
        return newEntitiesMap.get(session);
49
    }
50

    
51
    @Override
52
    public void onMerge(MergeEvent event) throws HibernateException {
53
        Object entity = event.getEntity();
54

    
55
    }
56

    
57
    @Override
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);
72
                }
73
            }
74
        }
75

    
76

    
77

    
78

    
79
    }
80

    
81

    
82

    
83
    /**
84
     * @param entity
85
     */
86
    private static void removeNullFromCollections(Object entity) {
87
        if (entity != null){
88
            Class<?> entityClazz = entity.getClass();
89

    
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);
99
                    }
100

    
101
                }
102

    
103
            }   else if(FeatureTree.class.isAssignableFrom(entityClazz)){
104

    
105
                FeatureTree<?> tree = (FeatureTree)entity;
106
                for (FeatureNode<?> node:tree.getRootChildren()){
107
                    node.removeNullValueFromChildren();
108
                    if (node.getChildNodes() != null){
109
                        for (FeatureNode childNode: node.getChildNodes()){
110
                            removeNullFromCollections(childNode);
111
                        }
112
                    }
113

    
114
                }
115
            } else if (FeatureNode.class.isAssignableFrom(entityClazz)){
116
                FeatureNode node = (FeatureNode)entity;
117
                node.removeNullValueFromChildren();
118
            }
119

    
120
        }
121

    
122
    }
123

    
124
}
(16-16/23)