Project

General

Profile

Download (4.11 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
 * @date 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<Session, Set<CdmBase>>();
37

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

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

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

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

    
54
    }
55

    
56
    @Override
57
    public void onMerge(MergeEvent event, Map copiedAlready) throws HibernateException {
58
        // any new entities are added to a map which is retrieved at the end of the
59
        // CdmEntityDaoBase.merge(T transientObject, boolean returnTransientEntity) call
60
        if(event.getOriginal() != null && CdmBase.class.isAssignableFrom(event.getOriginal().getClass()) &&
61
                event.getResult() != null && CdmBase.class.isAssignableFrom(event.getResult().getClass())) {
62
            CdmBase original = (CdmBase) event.getOriginal();
63
            CdmBase result = (CdmBase) event.getResult();
64
            removeNullFromCollections(result);
65
            if(original != null && Hibernate.isInitialized(original) && original.getId() == 0 &&
66
                    result != null && Hibernate.isInitialized(result) && result.getId() > 0) {
67
                original.setId(result.getId());
68
                Set<CdmBase> newEntities = newEntitiesMap.get(event.getSession());
69
                if(newEntities != null) {
70
                    newEntities.add(result);
71
                }
72
            }
73
        }
74

    
75

    
76
    }
77

    
78
    /**
79
     * @param entity
80
     */
81
    private static void removeNullFromCollections(Object entity) {
82
        if (entity != null){
83
            Class<?> entityClazz = entity.getClass();
84

    
85
            if (TaxonNode.class.isAssignableFrom(entityClazz)){
86
                TaxonNode node = (TaxonNode)entity;
87
                node.removeNullValueFromChildren();
88
            } else if (PolytomousKeyNode.class.isAssignableFrom(entityClazz)){
89
                PolytomousKeyNode node = (PolytomousKeyNode) entity;
90
                if (node.getChildren() != null && Hibernate.isInitialized(node.getChildren()) ){
91
                    node.removeNullValueFromChildren();
92

    
93
                }
94

    
95
            }   else if(FeatureTree.class.isAssignableFrom(entityClazz)){
96

    
97
                FeatureTree tree = (FeatureTree)entity;
98
                for (FeatureNode node:tree.getRootChildren()){
99
                    node.removeNullValueFromChildren();
100
                    if (node.getChildNodes() != null){
101
                        for (FeatureNode childNode: node.getChildNodes()){
102
                            removeNullFromCollections(childNode);
103
                        }
104
                    }
105

    
106
                }
107
            } else if (FeatureNode.class.isAssignableFrom(entityClazz)){
108
                FeatureNode node = (FeatureNode)entity;
109
                node.removeNullValueFromChildren();
110
            }
111

    
112
        }
113

    
114
    }
115

    
116
}
(14-14/20)