Project

General

Profile

« Previous | Next » 

Revision b467476c

Added by Katja Luther almost 8 years ago

#5681: move removeNullValueFromCollections to PostMergeEntityListener

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/CdmPreDataChangeListener.java
25 25
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
26 26
import eu.etaxonomy.cdm.model.common.User;
27 27
import eu.etaxonomy.cdm.model.common.VersionableEntity;
28
import eu.etaxonomy.cdm.model.description.FeatureNode;
29
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
30 28
import eu.etaxonomy.cdm.model.molecular.Amplification;
31 29
import eu.etaxonomy.cdm.model.name.NonViralName;
32 30
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
33 31
import eu.etaxonomy.cdm.model.reference.Reference;
34
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
35 32

  
36 33
/**
37 34
 * @author cmathew
......
94 91
        if(entity != null && CdmBase.class.isAssignableFrom(entity.getClass())){
95 92
            cacheDeterminationNames(entity);
96 93
            generateCaches(entity);
97
            removeNullFromCollections(entity);
94

  
98 95
        }
99 96
    }
100 97

  
101
    /**
102
     * @param entity
103
     */
104
    private static void removeNullFromCollections(Object entity) {
105
        if (entity != null){
106
            Class<?> entityClazz = entity.getClass();
107

  
108
            if (TaxonNode.class.isAssignableFrom(entityClazz)){
109
                TaxonNode node = (TaxonNode)entity;
110
                node.removeNullValueFromChildren();
111
            }else if(PolytomousKeyNode.class.isAssignableFrom(entityClazz)){
112
                PolytomousKeyNode node = (PolytomousKeyNode)entity;
113
                node.removeNullValueFromChildren();
114
            }else if(FeatureNode.class.isAssignableFrom(entityClazz)){
115
                FeatureNode node = (FeatureNode)entity;
116
                node.removeNullValueFromChildren();
117
            }
118

  
119
        }
120 98

  
121
    }
122 99

  
123 100
    private static void cacheDeterminationNames(Object entity) {
124 101
        if (entity instanceof DeterminationEvent) {
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/PostMergeEntityListener.java
21 21
import org.hibernate.event.spi.MergeEventListener;
22 22

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

  
25 29
/**
26 30
 * @author cmathew
......
46 50

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

  
50 55
    }
51 56

  
......
57 62
                event.getResult() != null && CdmBase.class.isAssignableFrom(event.getResult().getClass())) {
58 63
            CdmBase original = (CdmBase) event.getOriginal();
59 64
            CdmBase result = (CdmBase) event.getResult();
65
            removeNullFromCollections(result);
60 66
            if(original != null && Hibernate.isInitialized(original) && original.getId() == 0 &&
61 67
                    result != null && Hibernate.isInitialized(result) && result.getId() > 0) {
62 68
                original.setId(result.getId());
......
70 76

  
71 77
    }
72 78

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

  
86
            if (TaxonNode.class.isAssignableFrom(entityClazz)){
87
                TaxonNode node = (TaxonNode)entity;
88
                node.removeNullValueFromChildren();
89
            }else if(PolytomousKeyNode.class.isAssignableFrom(entityClazz)){
90
                PolytomousKeyNode node = (PolytomousKeyNode)entity;
91
                node.removeNullValueFromChildren();
92
            }else if(FeatureTree.class.isAssignableFrom(entityClazz)){
93

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

  
103
                }
104
            } else if (FeatureNode.class.isAssignableFrom(entityClazz)){
105
                FeatureNode node = (FeatureNode)entity;
106
                node.removeNullValueFromChildren();
107
            }
108

  
109
        }
110

  
111
    }
112

  
73 113
}

Also available in: Unified diff