001: package org.osbl.persistence.logic;
002:
003: import org.hibernate.*;
004: import org.osbl.persistence.model.TreeEntity;
005: import org.osbl.persistence.model.TreeNodeEntity;
006:
007: import java.util.List;
008: import java.util.StringTokenizer;
009:
010: public abstract class DefaultTreeLogic<T extends TreeEntity, N extends TreeNodeEntity>
011: implements TreeLogic<T, N> {
012: protected SessionFactory sessionFactory;
013:
014: public SessionFactory getSessionFactory() {
015: return sessionFactory;
016: }
017:
018: public void setSessionFactory(SessionFactory sessionFactory) {
019: this .sessionFactory = sessionFactory;
020: }
021:
022: protected abstract String getNodeType();
023:
024: protected abstract String getTreeType();
025:
026: public List<T> trees() {
027: Session session = sessionFactory.getCurrentSession();
028: Query query = session.createQuery("from " + getTreeType()
029: + " o");
030: return query.list();
031: }
032:
033: public T tree(String key) {
034: Session session = sessionFactory.getCurrentSession();
035: Query query = session.createQuery("from " + getTreeType()
036: + " t where t.key = :key");
037: query.setString("key", key);
038: return (T) query.uniqueResult();
039: }
040:
041: public List<N> roots() {
042: Session session = sessionFactory.getCurrentSession();
043: Query query = session.createQuery("from " + getNodeType()
044: + " n where n.path not like '/%/%/'");
045: return query.list();
046: }
047:
048: public List<N> roots(Long tree) {
049: Session session = sessionFactory.getCurrentSession();
050: Query query = session
051: .createQuery("from "
052: + getNodeType()
053: + " n where n.tree.id = :tree and n.path not like '/%/%/'");
054: query.setLong("tree", tree);
055: return query.list();
056: }
057:
058: public N byPath(String path) {
059: Session session = sessionFactory.getCurrentSession();
060: Query query = session
061: .createQuery("select n from costType n where n.path = :path");
062: query.setString("path", path);
063: return (N) query.uniqueResult();
064: }
065:
066: public List<N> children(Long tree, Long node) {
067: Session session = sessionFactory.getCurrentSession();
068: Query query = session
069: .createQuery("from "
070: + getNodeType()
071: + " n where n.tree.id = :tree and path like '%/' || :node || '/' || n.id || '/'");
072: query.setLong("tree", tree);
073: query.setLong("node", node);
074: return query.list();
075: }
076:
077: public List<N> descendants(Long tree, Long node) {
078: Session session = sessionFactory.getCurrentSession();
079: Query query = session
080: .createQuery("from "
081: + getNodeType()
082: + " n where n.tree.id = :tree and path like '%/' || :node || '/%'");
083: query.setLong("tree", tree);
084: query.setLong("node", node);
085: return query.list();
086: }
087:
088: public List<N> ancestors(Long tree, Long node) {
089: Session session = sessionFactory.getCurrentSession();
090: Query query = session
091: .createQuery("from "
092: + getNodeType()
093: + " n where n.tree.id = :tree and path like '%/' || :node || '/'");
094: query.setLong("tree", tree);
095: query.setLong("node", node);
096: return query.list();
097: }
098:
099: public List<N> all(Long tree) {
100: Session session = sessionFactory.getCurrentSession();
101: Query query = session.createQuery("from " + getNodeType()
102: + " n where n.tree.id = :tree");
103: query.setLong("tree", tree);
104: return query.list();
105: }
106:
107: public void connectNodeToNode(N parentTreeNode, N childTreeNode) {
108: childTreeNode.setTree(parentTreeNode.getTree());
109: String path = parentTreeNode.getPath() + childTreeNode.getId()
110: + "/";
111: childTreeNode.setPath(path);
112: childTreeNode.setDepth(new StringTokenizer(path, "/")
113: .countTokens());
114: System.out.println("path = " + childTreeNode.getPath());
115: }
116:
117: public void connectNodeToTree(T tree, N treeNode) {
118: treeNode.setTree(tree);
119: treeNode.setPath("/" + treeNode.getId() + "/");
120: treeNode.setDepth(1);
121: System.out.println("path = " + treeNode.getPath());
122: }
123: }
|