Project

General

Profile

Download (4.08 KB) Statistics
| Branch: | Tag: | Revision:
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.HashMap;
13

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

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

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

    
39
    private ValidationExecutor validationExecutor;
40

    
41
    private final IEntityValidationCrud dao;
42

    
43
    private boolean enabled = true;
44

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

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

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

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

    
62

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

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

    
75

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

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

    
87

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

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

    
111
    protected abstract String levelString();
112

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

    
118
}
(18-18/18)