Project

General

Profile

Download (3.17 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9
package eu.etaxonomy.cdm.persistence.hibernate;
10

    
11
import java.util.List;
12

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

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

    
21

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

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

    
32

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

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

    
41
            }
42
        }
43
    }
44

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

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

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

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

    
84

    
85

    
86
}
(16-16/20)