Project

General

Profile

« Previous | Next » 

Revision de5c2087

Added by Cherian Mathew about 9 years ago

ITaxonNodeService, TaxonNodeServiceImpl : added methods using uuids
CdmDataChangeEvent : added new instance method for cdm entity session
CdmPostDataChangeObservableListener : added cdm entity session commit

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/CdmDataChangeEvent.java
1 1
/**
2 2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
3
* European Distributed Institute of Taxonomy
4 4
* http://www.e-taxonomy.eu
5
* 
5
*
6 6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7 7
* See LICENSE.TXT at the top of this package for the full license terms.
8 8
*/
......
26 26
/**
27 27
 * The CrudEvent unifies all CRUD events into one interface.
28 28
 * Crud as in Create, Retrieve, Update, Delete. This event will only hold CdmBase objects.
29
 *  
30
 * 
29
 *
30
 *
31 31
 * @author n.hoffmann
32 32
 * @created 24.03.2009
33 33
 * @version 1.0
......
35 35
public class CdmDataChangeEvent extends AbstractEvent{
36 36
	private static final long serialVersionUID = 9113025682352080372L;
37 37
	private static final Logger logger = Logger.getLogger(CdmDataChangeEvent.class);
38
	
38

  
39 39
	/**
40 40
	 * The event types currently implemented
41
	 * 
41
	 *
42 42
	 * @author n.hoffmann
43 43
	 * @created 25.03.2009
44 44
	 * @version 1.0
......
46 46
	public enum EventType {
47 47
		INSERT, LOAD, UPDATE, DELETE
48 48
	}
49
	
50
	private CdmBase entity;
51
	private EntityPersister persister;
52
	private Object[] state;
49

  
50
	private final CdmBase entity;
51
	private final EntityPersister persister;
52
	private final Object[] state;
53 53
	// for update only
54 54
	private Object[] oldState;
55
	private Serializable id;
56
	private EventType eventType;
57
	
55
	private final Serializable id;
56
	protected final EventType eventType;
57

  
58 58
	/**
59 59
	 * @param source
60 60
	 */
61 61
	private CdmDataChangeEvent(
62
			CdmBase entity, 
62
			CdmBase entity,
63 63
			Serializable id,
64 64
			Object[] state,
65 65
			Object[] oldState,
......
94 94
		}
95 95
		return oldState;
96 96
	}
97
	
97

  
98 98
	public EventType getEventType(){
99 99
		return eventType;
100 100
	}
......
102 102
	public boolean isInsert(){
103 103
		return eventType == EventType.INSERT;
104 104
	}
105
	
105

  
106 106
	public boolean isLoad(){
107 107
		return eventType == EventType.LOAD;
108 108
	}
109
	
109

  
110 110
	public boolean isUpdate(){
111 111
		return eventType == EventType.UPDATE;
112 112
	}
113
	
113

  
114 114
	public boolean isDelete(){
115 115
		return eventType == EventType.DELETE;
116 116
	}
......
126 126
			if(event instanceof PostInsertEvent){
127 127
				PostInsertEvent postEvent = (PostInsertEvent) event;
128 128
				mediationEvent = new CdmDataChangeEvent(
129
																(CdmBase)postEvent.getEntity(), 
130
																postEvent.getId(), 
131
																postEvent.getState(), 
129
																(CdmBase)postEvent.getEntity(),
130
																postEvent.getId(),
131
																postEvent.getState(),
132 132
																null,
133
																postEvent.getPersister(), 
134
																postEvent.getSession(), 
133
																postEvent.getPersister(),
134
																postEvent.getSession(),
135 135
																EventType.INSERT
136 136
																);
137 137
			}
138 138
			if(event instanceof PostLoadEvent){
139 139
				PostLoadEvent updateEvent = (PostLoadEvent) event;
140 140
				mediationEvent = new CdmDataChangeEvent(
141
																(CdmBase)updateEvent.getEntity(), 
142
																updateEvent.getId(), 
141
																(CdmBase)updateEvent.getEntity(),
142
																updateEvent.getId(),
143 143
																null,
144 144
																null,
145
																updateEvent.getPersister(), 
146
																updateEvent.getSession(), 
145
																updateEvent.getPersister(),
146
																updateEvent.getSession(),
147 147
																EventType.LOAD
148 148
																);
149 149
			}
150 150
			if(event instanceof PostUpdateEvent){
151 151
				PostUpdateEvent updateEvent = (PostUpdateEvent) event;
152 152
				mediationEvent = new CdmDataChangeEvent(
153
																(CdmBase)updateEvent.getEntity(), 
154
																updateEvent.getId(), 
155
																updateEvent.getState(), 
153
																(CdmBase)updateEvent.getEntity(),
154
																updateEvent.getId(),
155
																updateEvent.getState(),
156 156
																updateEvent.getOldState(),
157
																updateEvent.getPersister(), 
158
																updateEvent.getSession(), 
157
																updateEvent.getPersister(),
158
																updateEvent.getSession(),
159 159
																EventType.UPDATE
160 160
																);
161 161
			}
162 162
			if(event instanceof PostDeleteEvent){
163 163
				PostDeleteEvent deleteEvent = (PostDeleteEvent) event;
164 164
				mediationEvent = new CdmDataChangeEvent(
165
																(CdmBase)deleteEvent.getEntity(), 
166
																deleteEvent.getId(), 
167
																deleteEvent.getDeletedState(), 
165
																(CdmBase)deleteEvent.getEntity(),
166
																deleteEvent.getId(),
167
																deleteEvent.getDeletedState(),
168 168
																null,
169
																deleteEvent.getPersister(), 
170
																deleteEvent.getSession(), 
169
																deleteEvent.getPersister(),
170
																deleteEvent.getSession(),
171 171
																EventType.DELETE
172 172
																);
173
			}	
173
			}
174 174
		}catch(ClassCastException e){
175 175
			// we are only interested in CdmBase entities, we have the try/catch block in case another entity slips through
176 176
			logger.warn("tried to instantiate event for non CdmBase entity");
177 177
		}
178
		
178

  
179 179
		return mediationEvent;
180 180
	}
181
	
181

  
182
	public static CdmDataChangeEvent NewInstance(CdmBase entity, EventType eventType) {
183
	    return new CdmDataChangeEvent(entity,
184
                null,
185
                null,
186
                null,
187
                null,
188
                null,
189
                eventType);
190
	}
191

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

  
28 28
/**
29
 * ICdmPostDataChangeObserver implementors may register for this listener and their update() will 
29
 * ICdmPostDataChangeObserver implementors may register for this listener and their update() will
30 30
 * be called after any CRUD (Create, Retrieve, Update, Delete).
31
 * 
31
 *
32 32
 * Only events whose entities are of type CdmBase will be propagated
33
 * 
33
 *
34 34
 * TODO Manage this class via Spring
35
 * 
35
 *
36 36
 * @author n.hoffmann
37 37
 * @created 24.03.2009
38 38
 * @version 1.0
39 39
 */
40
public class CdmPostDataChangeObservableListener implements 
40
public class CdmPostDataChangeObservableListener implements
41 41
		  PostDeleteEventListener
42 42
		, PostInsertEventListener
43 43
		, PostLoadEventListener
......
46 46
	private static final long serialVersionUID = -8764348096490526927L;
47 47
	@SuppressWarnings("unused")
48 48
	private static final Logger logger = Logger.getLogger(CdmPostDataChangeObservableListener.class);
49
	
49

  
50 50
	/**
51
	 * if this is set to true, observers have to be notified manually by calling 
51
	 * if this is set to true, observers have to be notified manually by calling
52 52
	 * {@link #delayedNotify()}. All events until then will be stored in {@link #changeEvents}
53 53
	 */
54 54
	private boolean delayed;
55
	
55

  
56 56
	/**
57 57
	 * Define what events will be propagated
58 58
	 */
......
60 60
					propagateInserts = true,
61 61
					propagateUpdates = true,
62 62
					propagateDeletes = true;
63
	
63

  
64 64
	/**
65 65
	 * DataChangeEvents get stored in this list for delayed propagation
66 66
	 */
67 67
	private CdmDataChangeMap changeEvents;
68
	
68

  
69 69
	/**
70
	 * Observing objects 
70
	 * Observing objects
71 71
	 */
72
	private Set<ICdmPostDataChangeObserver> observers = new HashSet<ICdmPostDataChangeObserver>();
73
	
74
	
72
	private final Set<ICdmPostDataChangeObserver> observers = new HashSet<ICdmPostDataChangeObserver>();
73

  
74

  
75 75
	/**
76 76
	 * Singleton instance
77 77
	 */
78 78
	private static CdmPostDataChangeObservableListener instance;
79
	
79

  
80 80
	/**
81 81
	 * @return the singleton CdmPostDataChangeObservableListener
82 82
	 */
83 83
	public static CdmPostDataChangeObservableListener getDefault(){
84
		if(instance == null){			
84
		if(instance == null){
85 85
			instance = new CdmPostDataChangeObservableListener();
86 86
			// TODO set these properties via Spring
87 87
			// get the delayed version by default
......
91 91
		}
92 92
		return instance;
93 93
	}
94
	
94

  
95 95
	/**
96 96
	 * Register for updates
97
	 * 
97
	 *
98 98
	 * @param observer
99 99
	 */
100 100
	public void register(ICdmPostDataChangeObserver observer){
101 101
		getDefault().observers.add(observer);
102 102
	}
103
	
103

  
104 104
	/**
105 105
	 * Remove observer from notify queue
106 106
	 * @param observer
......
108 108
	public void unregister(ICdmPostDataChangeObserver observer){
109 109
		getDefault().observers.remove(observer);
110 110
	}
111
	
111

  
112 112
	public void delayedNotify(){
113 113
		if(delayed && changeEvents.size() > 0){
114
			Set<ICdmPostDataChangeObserver> modificationSaveObservers 
114
			Set<ICdmPostDataChangeObserver> modificationSaveObservers
115 115
						= new HashSet<ICdmPostDataChangeObserver>(observers);
116 116
			for( ICdmPostDataChangeObserver observer : modificationSaveObservers){
117 117
				observer.update(changeEvents);
......
119 119
			changeEvents.clear();
120 120
		}
121 121
	}
122
	
122

  
123 123
	/**
124 124
	 * Propagates the event to all registered objects.
125
	 * 
125
	 *
126 126
	 * @param event
127 127
	 */
128
	private void notifyObservers(CdmDataChangeEvent event){
128
	public void notifyObservers(CdmDataChangeEvent event){
129 129
		for( ICdmPostDataChangeObserver observer : observers){
130 130
			if(delayed){
131 131
				// store event for delayed propagation
......
138 138
			}
139 139
		}
140 140
	}
141
	
141

  
142
	public void fireNotification(CdmDataChangeEvent event) {
143
	    for( ICdmPostDataChangeObserver observer : observers){
144
	        // propagate event directly
145
	        CdmDataChangeMap tmpMap = new CdmDataChangeMap();
146
	        tmpMap.add(event.getEventType(), event);
147
	        observer.update(tmpMap);
148

  
149
	    }
150
	}
151

  
142 152
	@Override
143 153
	public void onPostInsert(PostInsertEvent event) {
144 154
		if(propagateInserts && event.getEntity() instanceof CdmBase){
145 155
			getDefault().notifyObservers(CdmDataChangeEvent.NewInstance(event));
146 156
		}
147 157
	}
148
	
158

  
149 159
	@Override
150 160
	public void onPostLoad(PostLoadEvent event) {
151 161
		if(propagateLoads && event.getEntity() instanceof CdmBase){
......
239 249
	public void setPropagateDeletes(boolean propagateDeletes) {
240 250
		this.propagateDeletes = propagateDeletes;
241 251
	}
242
	
252

  
243 253
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/ITaxonNodeService.java
10 10

  
11 11
package eu.etaxonomy.cdm.api.service;
12 12

  
13
import java.util.Collection;
13 14
import java.util.List;
14 15
import java.util.Set;
16
import java.util.UUID;
15 17

  
16 18
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
17 19
import eu.etaxonomy.cdm.model.reference.Reference;
......
53 55
	 */
54 56
	public Synonym makeTaxonNodeASynonymOfAnotherTaxonNode(TaxonNode oldTaxonNode, TaxonNode newAcceptedTaxonNode, SynonymRelationshipType synonymRelationshipType, Reference citation, String citationMicroReference) ;
55 57

  
58
	public Synonym makeTaxonNodeASynonymOfAnotherTaxonNode(UUID oldTaxonNodeUuid,
59
	        UUID newAcceptedTaxonNodeUUID,
60
	        SynonymRelationshipType synonymRelationshipType,
61
	        Reference citation,
62
	        String citationMicroReference) ;
63

  
56 64
	/**
57 65
	 * deletes the given taxon nodes
58 66
	 *
......
61 69
	 * @return
62 70
	 *
63 71
	 */
64
	DeleteResult deleteTaxonNodes(Set<ITaxonTreeNode> nodes,
72
	public DeleteResult deleteTaxonNodes(Set<ITaxonTreeNode> nodes,
65 73
			TaxonDeletionConfigurator config) ;
74
    /**
75
     * @param nodeUuids
76
     * @param config
77
     * @return
78
     */
79
    public DeleteResult deleteTaxonNodes(Collection<UUID> nodeUuids, TaxonDeletionConfigurator config);
80

  
66 81
	/**
67 82
	 * deletes the given taxon node the configurator defines whether the children will be deleted too or not
68 83
	 *
......
80 95
	 * @param end  - limit of how many rows are to be pulled from the database, i.e. 1000 rows.
81 96
	 * @return filtered List of TaxonNode according to the classification provided
82 97
	 */
98

  
99
    /**
100
     * @param nodeUuid
101
     * @param config
102
     * @return
103
     */
104
    public DeleteResult deleteTaxonNode(UUID nodeUuid, TaxonDeletionConfigurator config);
105

  
83 106
	public List<TaxonNode> listAllNodesForClassification(Classification classification, Integer start, Integer end);
84 107

  
85 108
	/**
......
95 118

  
96 119

  
97 120

  
121

  
122

  
123

  
124

  
98 125
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/TaxonNodeServiceImpl.java
11 11
package eu.etaxonomy.cdm.api.service;
12 12

  
13 13
import java.util.ArrayList;
14
import java.util.Collection;
14 15
import java.util.Collections;
15 16
import java.util.Comparator;
16 17
import java.util.HashSet;
......
100 101
     */
101 102
    @Override
102 103
    @Transactional(readOnly = false)
103
    public Synonym makeTaxonNodeASynonymOfAnotherTaxonNode(TaxonNode oldTaxonNode, TaxonNode newAcceptedTaxonNode, SynonymRelationshipType synonymRelationshipType, Reference citation, String citationMicroReference)  {
104
    public Synonym makeTaxonNodeASynonymOfAnotherTaxonNode(TaxonNode oldTaxonNode,
105
            TaxonNode newAcceptedTaxonNode,
106
            SynonymRelationshipType synonymRelationshipType,
107
            Reference citation,
108
            String citationMicroReference)  {
104 109

  
105 110
        // TODO at the moment this method only moves synonym-, concept relations and descriptions to the new accepted taxon
106 111
        // in a future version we also want to move cdm data like annotations, marker, so., but we will need a policy for that
......
239 244
        return synonmyRelationship.getSynonym();
240 245
    }
241 246

  
247

  
248
    /* (non-Javadoc)
249
     * @see eu.etaxonomy.cdm.api.service.ITaxonNodeService#makeTaxonNodeASynonymOfAnotherTaxonNode(java.util.UUID, java.util.UUID, java.util.UUID, java.util.UUID, java.lang.String)
250
     */
251
    @Override
252
    @Transactional(readOnly = false)
253
    public Synonym makeTaxonNodeASynonymOfAnotherTaxonNode(UUID oldTaxonNodeUuid,
254
            UUID newAcceptedTaxonNodeUUID,
255
            SynonymRelationshipType synonymRelationshipType,
256
            Reference citation,
257
            String citationMicroReference) {
258

  
259
        return makeTaxonNodeASynonymOfAnotherTaxonNode(dao.load(oldTaxonNodeUuid),
260
                dao.load(newAcceptedTaxonNodeUUID),
261
                synonymRelationshipType,
262
                citation,
263
                citationMicroReference);
264
    }
265

  
242 266
    /* (non-Javadoc)
243 267
     * @see eu.etaxonomy.cdm.api.service.ITaxonNodeService#deleteTaxonNodes(java.util.List)
244 268
     */
245 269
    @Override
246 270
    @Transactional(readOnly = false)
247 271
    public DeleteResult deleteTaxonNodes(Set<ITaxonTreeNode> nodes, TaxonDeletionConfigurator config) {
272

  
248 273
        if (config == null){
249 274
        	config = new TaxonDeletionConfigurator();
250 275
        }
......
356 381
        return result;
357 382

  
358 383
    }
384

  
385
    @Override
386
    @Transactional(readOnly = false)
387
    public DeleteResult deleteTaxonNodes(Collection<UUID> nodeUuids, TaxonDeletionConfigurator config) {
388
        Set<ITaxonTreeNode> nodes = new HashSet<ITaxonTreeNode>();
389
        for(UUID nodeUuid : nodeUuids) {
390
            nodes.add(dao.load(nodeUuid));
391
        }
392
        return deleteTaxonNodes(nodes, config);
393
    }
394

  
359 395
    /* (non-Javadoc)
360 396
     * @see eu.etaxonomy.cdm.api.service.ITaxonNodeService#deleteTaxonNode(java.util.List)
361 397
     */
362 398
    @Override
363 399
    @Transactional(readOnly = false)
364 400
    public DeleteResult deleteTaxonNode(TaxonNode node, TaxonDeletionConfigurator config) {
365
        //node = dao.merge(node);
401

  
366 402
    	Taxon taxon = (Taxon)HibernateProxyHelper.deproxy(node.getTaxon());
367 403
    	if (config == null){
368 404
    		config = new TaxonDeletionConfigurator();
......
389 425

  
390 426
    }
391 427

  
428
    @Override
429
    @Transactional(readOnly = false)
430
    public DeleteResult deleteTaxonNode(UUID nodeUuid, TaxonDeletionConfigurator config) {
431
        DeleteResult dr = deleteTaxonNode(dao.load(nodeUuid), config);
432
        return dr;
433
    }
392 434
    /* (non-Javadoc)
393 435
     * @see eu.etaxonomy.cdm.api.service.ITaxonNodeService#listAllNodesForClassification(eu.etaxonomy.cdm.model.taxon.Classification, int, int)
394 436
     */
......
407 449

  
408 450

  
409 451

  
452

  
410 453
}

Also available in: Unified diff