Revision 66aeb5aa
Added by Andreas Kohlbecker almost 12 years ago
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/CdmDeleteListener.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 |
*/ |
... | ... | |
28 | 28 |
* @version 1.0 |
29 | 29 |
*/ |
30 | 30 |
public class CdmDeleteListener implements DeleteEventListener { |
31 |
private static final long serialVersionUID = -5511287200489449838L;
|
|
32 |
protected static final Logger logger = Logger.getLogger(CdmDeleteListener.class);
|
|
31 |
private static final long serialVersionUID = -5511287200489449838L;
|
|
32 |
protected static final Logger logger = Logger.getLogger(CdmDeleteListener.class);
|
|
33 | 33 |
|
34 |
/* (non-Javadoc)
|
|
35 |
* @see org.hibernate.event.DeleteEventListener#onDelete(org.hibernate.event.DeleteEvent)
|
|
36 |
*/
|
|
37 |
public void onDelete(DeleteEvent event) throws HibernateException {
|
|
38 |
Object entity = event.getObject();
|
|
39 |
if(entity != null && RelationshipBase.class.isAssignableFrom(entity.getClass())) {
|
|
40 |
logger.info("Deleting " + entity);
|
|
41 |
deleteRelationship(event, entity);
|
|
42 |
}
|
|
43 |
}
|
|
34 |
/* (non-Javadoc)
|
|
35 |
* @see org.hibernate.event.DeleteEventListener#onDelete(org.hibernate.event.DeleteEvent)
|
|
36 |
*/
|
|
37 |
public void onDelete(DeleteEvent event) throws HibernateException {
|
|
38 |
Object entity = event.getObject();
|
|
39 |
if(entity != null && RelationshipBase.class.isAssignableFrom(entity.getClass())) {
|
|
40 |
logger.info("Deleting " + entity);
|
|
41 |
deleteRelationship(event, entity);
|
|
42 |
}
|
|
43 |
}
|
|
44 | 44 |
|
45 |
/* (non-Javadoc)
|
|
46 |
* @see org.hibernate.event.DeleteEventListener#onDelete(org.hibernate.event.DeleteEvent, java.util.Set)
|
|
47 |
*/
|
|
48 |
public void onDelete(DeleteEvent event, Set transientEntities)throws HibernateException {
|
|
49 |
Object entity = event.getObject();
|
|
50 |
if(entity != null && RelationshipBase.class.isAssignableFrom(entity.getClass())) {
|
|
51 |
logger.info("Deleting " + entity);
|
|
52 |
deleteRelationship(event, entity);
|
|
53 |
}
|
|
54 |
}
|
|
45 |
/* (non-Javadoc)
|
|
46 |
* @see org.hibernate.event.DeleteEventListener#onDelete(org.hibernate.event.DeleteEvent, java.util.Set)
|
|
47 |
*/
|
|
48 |
public void onDelete(DeleteEvent event, Set transientEntities)throws HibernateException {
|
|
49 |
Object entity = event.getObject();
|
|
50 |
if(entity != null && RelationshipBase.class.isAssignableFrom(entity.getClass())) {
|
|
51 |
logger.info("Deleting " + entity);
|
|
52 |
deleteRelationship(event, entity);
|
|
53 |
}
|
|
54 |
}
|
|
55 | 55 |
|
56 |
/**
|
|
57 |
* @param event
|
|
58 |
* @param entity
|
|
59 |
*/
|
|
60 |
private void deleteRelationship(DeleteEvent event, Object entity) {
|
|
61 |
RelationshipBase relationshipEntity = (RelationshipBase)entity;
|
|
62 |
if (relationshipEntity.isRemoved()){
|
|
63 |
Set<IRelated> deletedObjects = relationshipEntity.getDeletedObjects();
|
|
64 |
for (IRelated rel : deletedObjects){
|
|
65 |
if (rel != null){
|
|
66 |
logger.info("Updating related entity " + rel);
|
|
67 |
Object o = CdmBase.deproxy(rel, CdmBase.class);
|
|
68 |
EntityEntry entry = event.getSession().getPersistenceContext().getEntry(o);
|
|
69 |
if (entry == null){
|
|
70 |
System.out.println(); |
|
71 |
}
|
|
72 |
if (!entry.getStatus().equals(Status.DELETED)){
|
|
73 |
event.getSession().update(rel);
|
|
74 |
}
|
|
75 |
}
|
|
76 |
}
|
|
77 |
}
|
|
78 |
}
|
|
56 |
/**
|
|
57 |
* @param event
|
|
58 |
* @param entity
|
|
59 |
*/
|
|
60 |
private void deleteRelationship(DeleteEvent event, Object entity) {
|
|
61 |
RelationshipBase relationshipEntity = (RelationshipBase)entity;
|
|
62 |
if (relationshipEntity.isRemoved()){
|
|
63 |
Set<IRelated> deletedObjects = relationshipEntity.getDeletedObjects();
|
|
64 |
for (IRelated rel : deletedObjects){
|
|
65 |
if (rel != null){
|
|
66 |
logger.info("Updating related entity " + rel);
|
|
67 |
Object o = CdmBase.deproxy(rel, CdmBase.class);
|
|
68 |
EntityEntry entry = event.getSession().getPersistenceContext().getEntry(o);
|
|
69 |
if (entry == null){
|
|
70 |
// System.out.println();
|
|
71 |
}
|
|
72 |
if (!entry.getStatus().equals(Status.DELETED)){
|
|
73 |
event.getSession().update(rel);
|
|
74 |
}
|
|
75 |
}
|
|
76 |
}
|
|
77 |
}
|
|
78 |
}
|
|
79 | 79 |
} |
Also available in: Unified diff
modifying FirstDataInserter for existing tests