import org.argeo.slc.core.structure.tree.TreeSRegistry;\r
import org.argeo.slc.dao.structure.tree.TreeSPathDao;\r
import org.argeo.slc.dao.structure.tree.TreeSRegistryDao;\r
+import org.hibernate.Session;\r
\r
/**\r
* The Hibernate implementation for tree-based structure registry.\r
getHibernateTemplate().save(registry);\r
}\r
\r
- public void update(TreeSRegistry registry) {\r
- getHibernateTemplate().update(registry);\r
+ public void update(TreeSRegistry registryArg) {\r
+ Session session = getSession();\r
+ session.beginTransaction();\r
+ TreeSRegistry registry = (TreeSRegistry)session.merge(registryArg);\r
+ session.update(registry);\r
+ session.getTransaction().commit();\r
}\r
\r
public TreeSRegistry getActiveTreeSRegistry() {\r
}\r
}\r
\r
- public void syncPath(TreeSRegistry registry,\r
- StructureRegistry<TreeSPath> localRegistry, TreeSPath path) {\r
+ public void syncPath(TreeSRegistry registryArg,\r
+ StructureRegistry<TreeSPath> localRegistry, TreeSPath pathArg) {\r
+ Session session = getSession();\r
+ session.beginTransaction();\r
+ TreeSRegistry registry = (TreeSRegistry)session.merge(registryArg);\r
+ TreeSPath path = (TreeSPath)session.merge(pathArg);\r
+ if (log.isTraceEnabled())\r
+ log.trace("Session#" + session.hashCode() + " " + session);\r
+ syncPathImpl(registry, localRegistry, path, session);\r
+ session.update(registry);\r
+ session.getTransaction().commit();\r
+ }\r
+\r
+ private void syncPathImpl(TreeSRegistry registry,\r
+ StructureRegistry<TreeSPath> localRegistry, TreeSPath path,\r
+ Session session) {\r
if (path.getParent() != null) {\r
TreeSPath parent = treeSPathDao.getOrCreate(path.getParent());\r
- syncPath(registry, localRegistry, parent);\r
+ syncPathImpl(registry, localRegistry, parent, session);\r
}\r
\r
if (log.isTraceEnabled())\r
log.trace("Synchronize path " + path);\r
- \r
+\r
if (registry.getElement(path) == null) {\r
- if (localRegistry != null) {\r
- registry.register(path, getElement(registry, localRegistry, path));\r
- } else {\r
- registry.register(path, new SimpleSElement(path.getName()));\r
- }\r
- update(registry);\r
+ final StructureElement element = getElement(registry,\r
+ localRegistry, path, session);\r
+ StructureElement elementPersisted = (StructureElement) session\r
+ .merge(element);\r
+ registry.register(path, elementPersisted);\r
+ // update(registry);\r
+\r
+ if (log.isTraceEnabled())\r
+ log.trace("No element in persisted structure for " + path\r
+ + ", merged to " + elementPersisted);\r
} else {\r
if (localRegistry != null) {\r
- StructureElement sElement = getElement(registry, localRegistry, path);\r
- if (sElement != null) {\r
- registry.register(path, sElement);\r
- update(registry);\r
+ StructureElement element = getElement(registry, localRegistry,\r
+ path, session);\r
+\r
+ if (element != null) {\r
+ StructureElement elementPersisted = (StructureElement) session\r
+ .merge(element);\r
+ registry.register(path, elementPersisted);\r
+ // update(registry);\r
+ if (log.isTraceEnabled())\r
+ log\r
+ .trace("Update existing element in persisted structure for "\r
+ + path\r
+ + ", merged to "\r
+ + elementPersisted);\r
}\r
}\r
}\r
}\r
\r
protected StructureElement getElement(TreeSRegistry registry,\r
- StructureRegistry<TreeSPath> localRegistry, TreeSPath path){\r
- return localRegistry.getElement(path);\r
+ StructureRegistry<TreeSPath> localRegistry, TreeSPath path,\r
+ Session session) {\r
+ StructureElement element;\r
+ if (localRegistry != null) {\r
+ element = localRegistry.getElement(path);\r
+ if (session.getSessionFactory()\r
+ .getClassMetadata(element.getClass()) == null) {\r
+ if (log.isTraceEnabled())\r
+ log.trace("Replace non-hibernate element " + element\r
+ + " by a simple element.");\r
+ element = new SimpleSElement(element.getLabel());\r
+ }\r
+ } else {\r
+ element = new SimpleSElement(path.getName());\r
+ }\r
+ return element;\r
}\r
}\r