Project

General

Profile

« Previous | Next » 

Revision 27e202aa

Added by Cherian Mathew over 8 years ago

#5299 Add new entity listener and event firing

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/aspectj/PropertyChangeAspect.aj
18 18
	
19 19
//	pointcut execAdder(CdmBase cb): target(cb) && execution(void CdmBase+.add*(..) );  //once implemented we may want to remove addToSetWithChangeEvent and remove... from CdmBase
20 20
	
21
	/**
22
     * @param cb
23
     * Around aspect that will be weaven into the original setter methods of the CdmBase derived classes
24
     */
25
    after() returning(CdmBase cb): call(CdmBase+.new(..)) { 
26
        //logger.warn(" new instance aop " + cb.getClass().getName());
27
        cb.fireOnCreateEvent(cb);
28
    }
29
    
21 30
	pointcut execSetter(CdmBase cb): target(cb) && execution(void CdmBase+.set*(..) );
22 31
//	/** *********** OLD ***********************/
23 32
//	pointcut callSetter( CdmBase b ) : call( * CdmBase+.set*(..) ) && target( b );
24 33

  
34

  
35

  
25 36
	/**
26 37
	 * @param cb
27 38
	 * Around aspect that will be weaven into the original setter methods of the CdmBase derived classes
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/NewEntityListener.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.model;
11

  
12
import eu.etaxonomy.cdm.model.common.CdmBase;
13

  
14
/**
15
 * @author cmathew
16
 * @date 30 Sep 2015
17
 *
18
 */
19
public interface NewEntityListener {
20

  
21
    public void onCreate(CdmBase cdmBase);
22

  
23
}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/CdmBase.java
57 57
import eu.etaxonomy.cdm.hibernate.search.UuidBridge;
58 58
import eu.etaxonomy.cdm.jaxb.DateTimeAdapter;
59 59
import eu.etaxonomy.cdm.jaxb.UUIDAdapter;
60
import eu.etaxonomy.cdm.model.NewEntityListener;
60 61
import eu.etaxonomy.cdm.strategy.match.Match;
61 62
import eu.etaxonomy.cdm.strategy.match.MatchMode;
62 63

  
......
91 92
    @XmlTransient
92 93
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
93 94

  
95
    @Transient
96
    @XmlTransient
97
    private static NewEntityListener newEntityListener;
98

  
94 99
    //@XmlAttribute(name = "id", required = true)
95 100
    @XmlTransient
96 101
    @Id
......
146 151
        this.created = new DateTime().withMillisOfSecond(0);
147 152
    }
148 153

  
154
    public static void setNewEntityListener(NewEntityListener nel) {
155
        newEntityListener = nel;
156
    }
157

  
158
    public static void fireOnCreateEvent(CdmBase cdmBase) {
159
        if(newEntityListener != null) {
160
            newEntityListener.onCreate(cdmBase);
161
        }
162
    }
163

  
149 164
    /**
150 165
     * see {@link PropertyChangeSupport#addPropertyChangeListener(PropertyChangeListener)}
151 166
     * @param listener
cdmlib-model/src/test/java/eu/etaxonomy/cdm/aspectj/NewEntityListenerTest.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.aspectj;
11

  
12
import org.apache.log4j.Logger;
13
import org.junit.Assert;
14
import org.junit.Test;
15

  
16
import eu.etaxonomy.cdm.model.NewEntityListener;
17
import eu.etaxonomy.cdm.model.common.Annotation;
18
import eu.etaxonomy.cdm.model.common.CdmBase;
19
import eu.etaxonomy.cdm.model.name.NonViralName;
20
import eu.etaxonomy.cdm.model.name.Rank;
21

  
22
/**
23
 * @author cmathew
24
 * @date 30 Sep 2015
25
 *
26
 */
27
public class NewEntityListenerTest implements NewEntityListener {
28
    static Logger logger = Logger.getLogger(NewEntityListenerTest.class);
29
    private Object lastPropValue;
30

  
31
    /* (non-Javadoc)
32
     * @see eu.etaxonomy.cdm.model.NewEntityListener#onCreate(eu.etaxonomy.cdm.model.common.CdmBase)
33
     */
34
    @Override
35
    public void onCreate(CdmBase cdmBase) {
36
        logger.warn("New Entity " + cdmBase + " created");
37
        lastPropValue = cdmBase;
38
    }
39

  
40
    @Test
41
    public void testPropertyChange() {
42
        NonViralName.setNewEntityListener(this);
43
        NonViralName b = NonViralName.NewInstance(Rank.SPECIES());
44
        Annotation newAnnotation = Annotation.NewDefaultLanguageInstance("test");
45
        b.addAnnotation(newAnnotation);
46
        Assert.assertEquals(newAnnotation, lastPropValue);
47
    }
48

  
49
}

Also available in: Unified diff