Project

General

Profile

« Previous | Next » 

Revision 4030ebf8

Added by Andreas Müller over 8 years ago

Rename CdmPreDataChangeObservableListener to CdmPreDataChangeListener

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/CacheStrategyGenerator.java
45 45
    }
46 46

  
47 47
    private void saveOrUpdateOrMerge(Object entity) {
48
        CdmPreDataChangeObservableListener.generateCaches(entity);
49
        //moved to CdmPreDataChangeObservableListener
50
//        if (entity != null){
51
//            Class<?> entityClazz = entity.getClass();
52
//
53
//            //non-viral-name caches
54
//            if(NonViralName.class.isAssignableFrom(entityClazz)) {
55
//                NonViralName<?> nonViralName = (NonViralName<?>)entity;
56
//                nonViralName.getAuthorshipCache();
57
//                nonViralName.getNameCache();
58
//                nonViralName.getTitleCache();
59
//                nonViralName.getFullTitleCache();
60
//                //team-or-person caches
61
//            }else if(TeamOrPersonBase.class.isAssignableFrom(entityClazz)){
62
//                TeamOrPersonBase<?> teamOrPerson = (TeamOrPersonBase<?>)entity;
63
//                String nomTitle = teamOrPerson.getNomenclaturalTitle();
64
//                if (teamOrPerson instanceof Team){
65
//                    Team team =CdmBase.deproxy(teamOrPerson, Team.class);
66
//                    team.setNomenclaturalTitle(nomTitle, team.isProtectedNomenclaturalTitleCache()); //nomTitle is not necessarily cached when it is created
67
//                }else{
68
//                    teamOrPerson.setNomenclaturalTitle(nomTitle);
69
//                }
70
//                String titleCache = teamOrPerson.getTitleCache();
71
//                if (! teamOrPerson.isProtectedTitleCache()){
72
//                    teamOrPerson.setTitleCache(titleCache, false);
73
//                }
74
//
75
//                //reference caches
76
//            }else if(Reference.class.isAssignableFrom(entityClazz)){
77
//                Reference<?> ref = (Reference<?>)entity;
78
//                ref.getAbbrevTitleCache();
79
//                ref.getTitleCache();
80
//                //title cache
81
//            }else if(IdentifiableEntity.class.isAssignableFrom(entityClazz)) {
82
//                IdentifiableEntity<?> identifiableEntity = (IdentifiableEntity)entity;
83
//                identifiableEntity.getTitleCache();
84
//            }else if(Amplification.class.isAssignableFrom(entityClazz)) {
85
//                Amplification amplification = (Amplification)entity;
86
//                amplification.updateCache();
87
//            }
88
//
89
//        }
48
        CdmPreDataChangeListener.generateCaches(entity);
90 49
    }
91 50
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/CdmListenerIntegrator.java
68 68
		eventRegistry.appendListeners(EventType.POST_UPDATE, new CdmPostDataChangeObservableListener());
69 69
		eventRegistry.appendListeners(EventType.POST_DELETE, new CdmPostDataChangeObservableListener());
70 70

  
71
		eventRegistry.appendListeners(EventType.PRE_INSERT, new CdmPreDataChangeObservableListener());
72
		eventRegistry.appendListeners(EventType.PRE_UPDATE, new CdmPreDataChangeObservableListener());
71
		eventRegistry.appendListeners(EventType.PRE_INSERT, new CdmPreDataChangeListener());
72
		eventRegistry.appendListeners(EventType.PRE_UPDATE, new CdmPreDataChangeListener());
73 73
	}
74 74

  
75 75

  
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/CdmPreDataChangeListener.java
1
// $Id$
2
/**
3
* Copyright (C) 2015 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.persistence.hibernate;
11

  
12
import java.util.List;
13

  
14
import org.hibernate.event.spi.PreInsertEvent;
15
import org.hibernate.event.spi.PreInsertEventListener;
16
import org.hibernate.event.spi.PreUpdateEvent;
17
import org.hibernate.event.spi.PreUpdateEventListener;
18
import org.joda.time.DateTime;
19
import org.springframework.security.core.Authentication;
20
import org.springframework.security.core.context.SecurityContextHolder;
21

  
22
import eu.etaxonomy.cdm.model.agent.Team;
23
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.model.common.ICdmBase;
26
import eu.etaxonomy.cdm.model.common.ITreeNode;
27
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
28
import eu.etaxonomy.cdm.model.common.User;
29
import eu.etaxonomy.cdm.model.common.VersionableEntity;
30
import eu.etaxonomy.cdm.model.molecular.Amplification;
31
import eu.etaxonomy.cdm.model.name.NonViralName;
32
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
33
import eu.etaxonomy.cdm.model.reference.Reference;
34

  
35
/**
36
 * @author cmathew
37
 * @date 7 Jul 2015
38
 *
39
 */
40
public class CdmPreDataChangeListener implements PreInsertEventListener, PreUpdateEventListener {
41
    private static final long serialVersionUID = -7581071903134036209L;
42

  
43
    static String sep = ITreeNode.separator;
44
    static String pref = ITreeNode.treePrefix;
45

  
46
    @Override
47
    public boolean onPreUpdate(PreUpdateEvent event) {
48
        try {
49
            Object entity = event.getEntity();
50
            if (VersionableEntity.class.isAssignableFrom(entity.getClass())) {
51
                VersionableEntity versionableEntity = (VersionableEntity)entity;
52
                versionableEntity.setUpdated(new DateTime());
53
                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
54
                if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
55
                    User user = (User)authentication.getPrincipal();
56
                    versionableEntity.setUpdatedBy(user);
57
                }
58
            }
59
            insertUpdateMerge(event.getEntity());
60
        } finally {
61
            return false;
62
        }
63
    }
64

  
65
    @Override
66
    public boolean onPreInsert(PreInsertEvent event) {
67
        try {
68
            Object entity = event.getEntity();
69
            Class<?> entityClazz = entity.getClass();
70
            if(ICdmBase.class.isAssignableFrom(entityClazz)) {
71
                ICdmBase cdmBase = (ICdmBase)entity;
72

  
73
                if (cdmBase.getCreated() == null){
74
                    cdmBase.setCreated(new DateTime());
75
                }
76
                if(cdmBase.getCreatedBy() == null) {
77
                    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
78
                    if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
79
                        User user = (User)authentication.getPrincipal();
80
                        cdmBase.setCreatedBy(user);
81
                    }
82
                }
83
            }
84
            insertUpdateMerge(entity);
85
        } finally {
86
            return false;
87
        }
88

  
89
    }
90

  
91
    //from former SaveOrUpdateOrMergeEntityListener
92
    public static void insertUpdateMerge(Object entity){
93
        if(entity != null && CdmBase.class.isAssignableFrom(entity.getClass())){
94
            generateTreeIndex(entity);
95
            cacheDeterminationNames(entity);
96
            generateCaches(entity);
97
        }
98
    }
99

  
100
    private static void cacheDeterminationNames(Object entity) {
101
        if (entity instanceof DeterminationEvent) {
102
            DeterminationEvent detEv = (DeterminationEvent)entity;
103
            if (detEv.getTaxon() != null && detEv.getTaxonName() == null && detEv.getTaxon().getName() != null){
104
                detEv.setTaxonName(detEv.getTaxon().getName());
105
            }
106
        }
107
    }
108

  
109
    private static void generateTreeIndex(Object entity) {
110
        if (entity instanceof ITreeNode) {
111
            ITreeNode<?> node = (ITreeNode<?>)entity;
112
            reindex(node);
113

  
114
        }
115
    }
116

  
117
    public static void generateCaches(Object entity){
118
        if (entity != null){
119
            Class<?> entityClazz = entity.getClass();
120

  
121
            //non-viral-name caches
122
            if(NonViralName.class.isAssignableFrom(entityClazz)) {
123
                NonViralName<?> nonViralName = (NonViralName<?>)entity;
124
                nonViralName.getAuthorshipCache();
125
                nonViralName.getNameCache();
126
                nonViralName.getTitleCache();
127
                nonViralName.getFullTitleCache();
128
                //team-or-person caches
129
            }else if(TeamOrPersonBase.class.isAssignableFrom(entityClazz)){
130
                TeamOrPersonBase<?> teamOrPerson = (TeamOrPersonBase<?>)entity;
131
                String nomTitle = teamOrPerson.getNomenclaturalTitle();
132
                if (teamOrPerson instanceof Team){
133
                    Team team =CdmBase.deproxy(teamOrPerson, Team.class);
134
                    team.setNomenclaturalTitle(nomTitle, team.isProtectedNomenclaturalTitleCache()); //nomTitle is not necessarily cached when it is created
135
                }else{
136
                    teamOrPerson.setNomenclaturalTitle(nomTitle);
137
                }
138
                String titleCache = teamOrPerson.getTitleCache();
139
                if (! teamOrPerson.isProtectedTitleCache()){
140
                    teamOrPerson.setTitleCache(titleCache, false);
141
                }
142

  
143
                //reference caches
144
            }else if(Reference.class.isAssignableFrom(entityClazz)){
145
                Reference<?> ref = (Reference<?>)entity;
146
                ref.getAbbrevTitleCache();
147
                ref.getTitleCache();
148
                //title cache
149
            }else if(IdentifiableEntity.class.isAssignableFrom(entityClazz)) {
150
                IdentifiableEntity<?> identifiableEntity = (IdentifiableEntity)entity;
151
                identifiableEntity.getTitleCache();
152
            }else if(Amplification.class.isAssignableFrom(entityClazz)) {
153
                Amplification amplification = (Amplification)entity;
154
                amplification.updateCache();
155
            }
156

  
157
        }
158
    }
159

  
160
    /**
161
     * @param event
162
     * @param node
163
     */
164
    private static <T extends ITreeNode> void reindex(T node) {
165
        String oldChildIndex = node.treeIndex();
166
        ITreeNode<?> parent = node.getParent();
167
        String parentIndex = (parent == null) ? (sep + pref + node.treeId() + sep)  : parent.treeIndex();  //TODO
168
        if (node.getId() > 0){   //TODO
169
            String newChildIndex = parentIndex + node.getId() + sep;
170
            if (oldChildIndex == null || ! oldChildIndex.equals(newChildIndex)){
171
                node.setTreeIndex(newChildIndex);
172

  
173
                //TODO this is a greedy implementation, better use update by replace string
174
                //either using and improving the below code or by using native SQL
175
                //The current approach may run out of memory for large descendant sets.
176
                List<T> childNodes = node.getChildNodes();
177
                for (T child : childNodes){
178
                    if (child != null && ! child.equals(node)){  //node should not be it's own child, however just in case
179
                        reindex(child);
180
                    }
181
                }
182

  
183
                //          String className = event.getEntityName();
184
                //                  String updateQuery = " UPDATE %s tn " +
185
                //                          " SET tn.treeIndex = Replace(tn.treeIndex, '%s', '%s') " +
186
                //                          " WHERE tn.id <> "+ node.getId()+" ";
187
                //                  updateQuery = String.format(updateQuery, className, oldChildIndex, parentIndex);  //dummy
188
                //                  System.out.println(updateQuery);
189
                //                  EventSource session = event.getSession();
190
                //                  Query query = session.createQuery(updateQuery);
191
                //                  query.executeUpdate();
192
            }
193
        }
194
    }
195

  
196
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/CdmPreDataChangeObservableListener.java
1
// $Id$
2
/**
3
* Copyright (C) 2015 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.persistence.hibernate;
11

  
12
import java.util.List;
13

  
14
import org.hibernate.event.spi.PreInsertEvent;
15
import org.hibernate.event.spi.PreInsertEventListener;
16
import org.hibernate.event.spi.PreUpdateEvent;
17
import org.hibernate.event.spi.PreUpdateEventListener;
18
import org.joda.time.DateTime;
19
import org.springframework.security.core.Authentication;
20
import org.springframework.security.core.context.SecurityContextHolder;
21

  
22
import eu.etaxonomy.cdm.model.agent.Team;
23
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.model.common.ICdmBase;
26
import eu.etaxonomy.cdm.model.common.ITreeNode;
27
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
28
import eu.etaxonomy.cdm.model.common.User;
29
import eu.etaxonomy.cdm.model.common.VersionableEntity;
30
import eu.etaxonomy.cdm.model.molecular.Amplification;
31
import eu.etaxonomy.cdm.model.name.NonViralName;
32
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
33
import eu.etaxonomy.cdm.model.reference.Reference;
34

  
35
/**
36
 * @author cmathew
37
 * @date 7 Jul 2015
38
 *
39
 */
40
public class CdmPreDataChangeObservableListener implements PreInsertEventListener, PreUpdateEventListener {
41
    private static final long serialVersionUID = -7581071903134036209L;
42

  
43
    static String sep = ITreeNode.separator;
44
    static String pref = ITreeNode.treePrefix;
45

  
46
    @Override
47
    public boolean onPreUpdate(PreUpdateEvent event) {
48
        try {
49
            Object entity = event.getEntity();
50
            if (VersionableEntity.class.isAssignableFrom(entity.getClass())) {
51
                VersionableEntity versionableEntity = (VersionableEntity)entity;
52
                versionableEntity.setUpdated(new DateTime());
53
                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
54
                if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
55
                    User user = (User)authentication.getPrincipal();
56
                    versionableEntity.setUpdatedBy(user);
57
                }
58
            }
59
            insertUpdateMerge(event.getEntity());
60
        } finally {
61
            return false;
62
        }
63
    }
64

  
65
    @Override
66
    public boolean onPreInsert(PreInsertEvent event) {
67
        try {
68
            Object entity = event.getEntity();
69
            Class<?> entityClazz = entity.getClass();
70
            if(ICdmBase.class.isAssignableFrom(entityClazz)) {
71
                ICdmBase cdmBase = (ICdmBase)entity;
72

  
73
                if (cdmBase.getCreated() == null){
74
                    cdmBase.setCreated(new DateTime());
75
                }
76
                if(cdmBase.getCreatedBy() == null) {
77
                    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
78
                    if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
79
                        User user = (User)authentication.getPrincipal();
80
                        cdmBase.setCreatedBy(user);
81
                    }
82
                }
83
            }
84
            insertUpdateMerge(entity);
85
        } finally {
86
            return false;
87
        }
88

  
89
    }
90

  
91
    //from former SaveOrUpdateOrMergeEntityListener
92
    public static void insertUpdateMerge(Object entity){
93
        if(entity != null && CdmBase.class.isAssignableFrom(entity.getClass())){
94
            generateTreeIndex(entity);
95
            cacheDeterminationNames(entity);
96
            generateCaches(entity);
97
        }
98
    }
99

  
100
    private static void cacheDeterminationNames(Object entity) {
101
        if (entity instanceof DeterminationEvent) {
102
            DeterminationEvent detEv = (DeterminationEvent)entity;
103
            if (detEv.getTaxon() != null && detEv.getTaxonName() == null && detEv.getTaxon().getName() != null){
104
                detEv.setTaxonName(detEv.getTaxon().getName());
105
            }
106
        }
107
    }
108

  
109
    private static void generateTreeIndex(Object entity) {
110
        if (entity instanceof ITreeNode) {
111
            ITreeNode<?> node = (ITreeNode<?>)entity;
112
            reindex(node);
113

  
114
        }
115
    }
116

  
117
    public static void generateCaches(Object entity){
118
        if (entity != null){
119
            Class<?> entityClazz = entity.getClass();
120

  
121
            //non-viral-name caches
122
            if(NonViralName.class.isAssignableFrom(entityClazz)) {
123
                NonViralName<?> nonViralName = (NonViralName<?>)entity;
124
                nonViralName.getAuthorshipCache();
125
                nonViralName.getNameCache();
126
                nonViralName.getTitleCache();
127
                nonViralName.getFullTitleCache();
128
                //team-or-person caches
129
            }else if(TeamOrPersonBase.class.isAssignableFrom(entityClazz)){
130
                TeamOrPersonBase<?> teamOrPerson = (TeamOrPersonBase<?>)entity;
131
                String nomTitle = teamOrPerson.getNomenclaturalTitle();
132
                if (teamOrPerson instanceof Team){
133
                    Team team =CdmBase.deproxy(teamOrPerson, Team.class);
134
                    team.setNomenclaturalTitle(nomTitle, team.isProtectedNomenclaturalTitleCache()); //nomTitle is not necessarily cached when it is created
135
                }else{
136
                    teamOrPerson.setNomenclaturalTitle(nomTitle);
137
                }
138
                String titleCache = teamOrPerson.getTitleCache();
139
                if (! teamOrPerson.isProtectedTitleCache()){
140
                    teamOrPerson.setTitleCache(titleCache, false);
141
                }
142

  
143
                //reference caches
144
            }else if(Reference.class.isAssignableFrom(entityClazz)){
145
                Reference<?> ref = (Reference<?>)entity;
146
                ref.getAbbrevTitleCache();
147
                ref.getTitleCache();
148
                //title cache
149
            }else if(IdentifiableEntity.class.isAssignableFrom(entityClazz)) {
150
                IdentifiableEntity<?> identifiableEntity = (IdentifiableEntity)entity;
151
                identifiableEntity.getTitleCache();
152
            }else if(Amplification.class.isAssignableFrom(entityClazz)) {
153
                Amplification amplification = (Amplification)entity;
154
                amplification.updateCache();
155
            }
156

  
157
        }
158
    }
159

  
160
    /**
161
     * @param event
162
     * @param node
163
     */
164
    private static <T extends ITreeNode> void reindex(T node) {
165
        String oldChildIndex = node.treeIndex();
166
        ITreeNode<?> parent = node.getParent();
167
        String parentIndex = (parent == null) ? (sep + pref + node.treeId() + sep)  : parent.treeIndex();  //TODO
168
        if (node.getId() > 0){   //TODO
169
            String newChildIndex = parentIndex + node.getId() + sep;
170
            if (oldChildIndex == null || ! oldChildIndex.equals(newChildIndex)){
171
                node.setTreeIndex(newChildIndex);
172

  
173
                //TODO this is a greedy implementation, better use update by replace string
174
                //either using and improving the below code or by using native SQL
175
                //The current approach may run out of memory for large descendant sets.
176
                List<T> childNodes = node.getChildNodes();
177
                for (T child : childNodes){
178
                    if (child != null && ! child.equals(node)){  //node should not be it's own child, however just in case
179
                        reindex(child);
180
                    }
181
                }
182

  
183
                //          String className = event.getEntityName();
184
                //                  String updateQuery = " UPDATE %s tn " +
185
                //                          " SET tn.treeIndex = Replace(tn.treeIndex, '%s', '%s') " +
186
                //                          " WHERE tn.id <> "+ node.getId()+" ";
187
                //                  updateQuery = String.format(updateQuery, className, oldChildIndex, parentIndex);  //dummy
188
                //                  System.out.println(updateQuery);
189
                //                  EventSource session = event.getSession();
190
                //                  Query query = session.createQuery(updateQuery);
191
                //                  query.executeUpdate();
192
            }
193
        }
194
    }
195

  
196
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/CdmSecurityHibernateInterceptor.java
65 65
//        exculdeMap.put(TaxonNameBase.class, new HashSet<String>());
66 66

  
67 67
        Set<String> defaultExculdes = new HashSet<String>();
68
        defaultExculdes.add("createdBy");  //created by is changed by CdmPreDataChangeObservableListener after save. This is handled as a change and therefore throws a security exception during first insert if only CREATE rights exist
68
        defaultExculdes.add("createdBy");  //created by is changed by CdmPreDataChangeListener after save. This is handled as a change and therefore throws a security exception during first insert if only CREATE rights exist
69 69
        defaultExculdes.add("created");  // same behavior was not yet observed for "created", but to be on the save side we also exclude "created"
70 70

  
71 71
        for ( CdmBaseType type: CdmBaseType.values()){
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/SaveOrUpdateorMergeEntityListener.java
45 45

  
46 46
    private void saveOrUpdateOrMerge(Object entity, Session session) {
47 47

  
48
        //moved to CdmPreDataChangeObservableListener
48
        //moved to CdmPreDataChangeListener
49 49
//        if(entity != null && CdmBase.class.isAssignableFrom(entity.getClass())){
50 50
//
51 51
//            if (entity instanceof ITreeNode) {

Also available in: Unified diff