Project

General

Profile

Download (3.18 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2007 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.List;
13

    
14
import org.hibernate.HibernateException;
15
import org.hibernate.Session;
16
import org.hibernate.event.spi.SaveOrUpdateEvent;
17
import org.hibernate.event.spi.SaveOrUpdateEventListener;
18

    
19
import eu.etaxonomy.cdm.model.common.CdmBase;
20
import eu.etaxonomy.cdm.model.common.ITreeNode;
21

    
22

    
23
@SuppressWarnings("serial")
24
public class SaveOrUpdateEntityListener implements SaveOrUpdateEventListener{
25

    
26
    @Override
27
    public void onSaveOrUpdate(SaveOrUpdateEvent event) throws HibernateException {
28
        //System.err.println("SaveOrUpdateListener" + event.getEntity().getClass());
29
        Object entity = event.getObject();
30
        saveOrUpdate(entity, event.getSession());
31
    }
32

    
33

    
34
    private void saveOrUpdate(Object entity, Session session) {
35

    
36
        //moved to CdmPreDataChangeListener
37
        if(entity != null && CdmBase.class.isAssignableFrom(entity.getClass())){
38
            if (entity instanceof ITreeNode) {
39
                ITreeNode<?> node = (ITreeNode<?>)entity;
40
                reindex(node);
41

    
42
            }
43
        }
44
    }
45

    
46
    static String sep = ITreeNode.separator;
47
    static String pref = ITreeNode.treePrefix;
48

    
49
    /**
50
     * @param event
51
     * @param node
52
     */
53
    private <T extends ITreeNode> void reindex(T node) {
54
        String oldChildIndex = node.treeIndex();
55
        ITreeNode<?> parent = node.getParent();
56
        String parentIndex = (parent == null) ? (sep + pref + node.treeId() + sep)  : parent.treeIndex();  //TODO
57
        if (node.getId() > 0){   //TODO
58
            String newChildIndex = parentIndex + node.getId() + sep;
59
            if (oldChildIndex == null || ! oldChildIndex.equals(newChildIndex)){
60
                node.setTreeIndex(newChildIndex);
61

    
62
                //TODO this is a greedy implementation, better use update by replace string
63
                //either using and improving the below code or by using native SQL
64
                //The current approach may run out of memory for large descendant sets.
65
                List<T> childNodes = node.getChildNodes();
66
                for (T child : childNodes){
67
                    if (child != null && ! child.equals(node)){  //node should not be it's own child, however just in case
68
                        reindex(child);
69
                    }
70
                }
71

    
72
                //			String className = event.getEntityName();
73
                //					String updateQuery = " UPDATE %s tn " +
74
                //							" SET tn.treeIndex = Replace(tn.treeIndex, '%s', '%s') " +
75
                //							" WHERE tn.id <> "+ node.getId()+" ";
76
                //					updateQuery = String.format(updateQuery, className, oldChildIndex, parentIndex);  //dummy
77
                //					System.out.println(updateQuery);
78
                //					EventSource session = event.getSession();
79
                //					Query query = session.createQuery(updateQuery);
80
                //					query.executeUpdate();
81
            }
82
        }
83
    }
84

    
85

    
86

    
87
}
(16-16/20)