Project

General

Profile

Download (4.07 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.HashMap;
12

    
13
import org.apache.log4j.Logger;
14
import org.hibernate.event.spi.PostInsertEvent;
15
import org.hibernate.event.spi.PostInsertEventListener;
16
import org.hibernate.event.spi.PostUpdateEvent;
17
import org.hibernate.event.spi.PostUpdateEventListener;
18

    
19
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.model.common.ICdmBase;
22
import eu.etaxonomy.cdm.model.validation.CRUDEventType;
23
import eu.etaxonomy.cdm.model.validation.EntityConstraintViolation;
24
import eu.etaxonomy.cdm.model.validation.EntityValidation;
25
import eu.etaxonomy.cdm.persistence.dao.validation.IEntityValidationCrud;
26
import eu.etaxonomy.cdm.persistence.validation.EntityValidationTaskBase;
27
import eu.etaxonomy.cdm.persistence.validation.ValidationExecutor;
28

    
29
/**
30
 * @author a.mueller
31
 \* @since 13.02.2015
32
 *
33
 */
34
@SuppressWarnings("serial")
35
public abstract class ValidationEventListenerBase implements PostInsertEventListener, PostUpdateEventListener{
36
    private static final Logger logger = Logger.getLogger(ValidationEventListenerBase.class);
37

    
38
    private ValidationExecutor validationExecutor;
39

    
40
    private final IEntityValidationCrud dao;
41

    
42
    private boolean enabled = true;
43

    
44
    protected ValidationEventListenerBase(IEntityValidationCrud dao){
45
        this.dao = dao;
46
    }
47

    
48
    public ValidationExecutor getValidationExecutor(){
49
        return validationExecutor;
50
    }
51

    
52
    public void setValidationExecutor(ValidationExecutor validationExecutor){
53
        this.validationExecutor = validationExecutor;
54
    }
55

    
56
    @Override
57
    public void onPostInsert(PostInsertEvent event){
58
        validate(event.getEntity(), CRUDEventType.INSERT);
59
    }
60

    
61

    
62
    @Override
63
    public void onPostUpdate(PostUpdateEvent event){
64
        validate(event.getEntity(), CRUDEventType.UPDATE);
65
    }
66

    
67
    /**
68
     * @return the dao
69
     */
70
    protected IEntityValidationCrud getDao() {
71
        return dao;
72
    }
73

    
74

    
75
    protected void validate(Object object, CRUDEventType trigger){
76
        if (!enabled){
77
            if(logger.isDebugEnabled()){logger.debug("Pass " + levelString() + " validator as it is disabled");}
78
            return;
79
        }
80

    
81
        if (object == null) {
82
            if(logger.isDebugEnabled()){logger.debug("Nothing to validate (entity is null)");}
83
            return;
84
        }
85

    
86

    
87
        try {
88
           if (!(object instanceof CdmBase)) {
89
                if (object.getClass() != HashMap.class) {
90
                    if(logger.isDebugEnabled()){ logger.debug(levelString() + " validation bypassed for entities of type " + object.getClass().getName());}
91
                }
92
                return;
93
            }
94
            if (object instanceof EntityValidation || object instanceof EntityConstraintViolation) {
95
                if(logger.isDebugEnabled()){ logger.debug(levelString() + " validation bypassed for entities of type " + object.getClass().getName() + ". We do not validate validation result entities themselves");}
96
                return;
97
            }
98
            //validate
99
            ICdmBase entity = HibernateProxyHelper.deproxy(object, CdmBase.class) ;
100
            EntityValidationTaskBase task = createValidationTask(entity, trigger);
101
            getValidationExecutor().execute(task);
102
        }
103
        catch (Throwable t) {
104
            logger.error("Failed applying " + levelString() + " validation to " + object.toString(), t);
105
        }
106
    }
107

    
108
    protected abstract EntityValidationTaskBase createValidationTask(ICdmBase entity, CRUDEventType trigger);
109

    
110
    protected abstract String levelString();
111

    
112
    @Deprecated  //this is in test mode, may be removed in future
113
    public void setEnabled(boolean enabled){
114
        this.enabled = enabled;
115
    }
116

    
117
}
(21-21/21)