]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/hibernate/structure/tree/TreeSRegistryDaoHibernate.java
15a7cede53ae186f5e9d475d3bc92b81c09e1cf0
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / hibernate / structure / tree / TreeSRegistryDaoHibernate.java
1 package org.argeo.slc.hibernate.structure.tree;
2
3 import java.util.List;
4
5 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import org.argeo.slc.core.structure.SimpleSElement;
11 import org.argeo.slc.core.structure.StructureElement;
12 import org.argeo.slc.core.structure.StructureRegistry;
13 import org.argeo.slc.core.structure.tree.TreeSPath;
14 import org.argeo.slc.core.structure.tree.TreeSRegistry;
15 import org.argeo.slc.dao.structure.tree.TreeSPathDao;
16 import org.argeo.slc.dao.structure.tree.TreeSRegistryDao;
17
18 /**
19 * The Hibernate implementation for tree-based structure registry.
20 *
21 * @see TreeSRegistry
22 */
23 public class TreeSRegistryDaoHibernate extends HibernateDaoSupport implements
24 TreeSRegistryDao {
25
26 private TreeSPathDao treeSPathDao;
27
28 private static Log log = LogFactory.getLog(TreeSRegistryDaoHibernate.class);
29
30 public void create(TreeSRegistry registry) {
31 getHibernateTemplate().save(registry);
32 }
33
34 public void update(TreeSRegistry registry) {
35 getHibernateTemplate().update(registry);
36 }
37
38 public TreeSRegistry getActiveTreeSRegistry() {
39 List<?> list = getHibernateTemplate().find(
40 "from TreeSRegistry where status=?",
41 TreeSRegistry.STATUS_ACTIVE);
42 if (list.size() == 0) {
43 return null;
44 } else {
45 return (TreeSRegistry) list.get(0);
46 }
47 }
48
49 public void syncPath(TreeSRegistry registry,
50 StructureRegistry<TreeSPath> localRegistry, TreeSPath path) {
51 if (path.getParent() != null) {
52 TreeSPath parent = treeSPathDao.getOrCreate(path.getParent());
53 syncPath(registry, localRegistry, parent);
54 }
55
56 if (log.isDebugEnabled())
57 log.debug("Synchronize path " + path);
58
59 if (registry.getElement(path) == null) {
60 if (localRegistry != null) {
61 registry.register(path, getElement(registry, localRegistry, path));
62 } else {
63 registry.register(path, new SimpleSElement(path.getName()));
64 }
65 update(registry);
66 } else {
67 if (localRegistry != null) {
68 StructureElement sElement = getElement(registry, localRegistry, path);
69 if (sElement != null) {
70 registry.register(path, sElement);
71 update(registry);
72 }
73 }
74 }
75
76 }
77
78 public void setTreeSPathDao(TreeSPathDao treeSPathDao) {
79 this.treeSPathDao = treeSPathDao;
80 }
81
82 protected StructureElement getElement(TreeSRegistry registry,
83 StructureRegistry<TreeSPath> localRegistry, TreeSPath path){
84 return localRegistry.getElement(path);
85 }
86 }