infoweb.
java.
.
java.
.
infoweb.
.
net.
.
.
net.
.
.
net.
.
.
org.
.
.
.
org.
.
.
.
.
30 31 32 /**
33 * 构造函数
34 */
35 public BoardTreeDAOImpl
() {
36 super();
37 }
38 39 40 /**
41 * 通过ID取得版块
42 * @param id String
43 * @return Board
44 */ 45
46 public Board getBoardById
(String id
) {
47 Board board = (Board) getHibernateTemplate().load(Board.class, id);
48 return board;
49 }
50 51 52 /**
53 * 取根叶
54 * @return Iterator
55 */
56 public Iterator getRoots
() throws HibernateException
{
57 String queryString =
58 "select board from Board as board where board.parentId=‘root‘ order by board.id desc";
59 List roots = getHibernateTemplate().find(queryString);
60 return roots.iterator();
61 }
62 63 64 /**
65 * 存根叶
66 * @param board Board
67 */
68 public void setRoot
(Board board
) {
69 board.setParentId("root");
70 getHibernateTemplate().save(board);
71 }
72 73 74 /**
75 * 取子叶
76 * @param parentid String
77 * @return List
78 */
79 public Iterator getChildren
(String parentid
) {
80 /*
81 String queryString =
82 "select board as Board where board.parent_id=‘parentid‘ order by board.id desc";
83 List children = getHibernateTemplate().find(queryString);
84 return children;
85 */
86 Board parent = (Board) getHibernateTemplate().load(Board.class, parentid);
87 return parent.getChildren().iterator();
88 }
89 90 91 /**
92 * 取子叶数
93 * @param parentid String
94 * @return int
95 */ 96
97 public int getChildrenCount
(String parentid
) {
98 /*
99 String queryString =
100 "select count(*) Board where board.parent_id=‘parentid‘ order by board.id desc";
101 List children = getHibernateTemplate().find(queryString);
102 int count = ((Integer) children.iterator().next()).intValue();
103 return count;
104 */
105 Board parent = (Board) getHibernateTemplate().load(Board.class, parentid);
106 int count = parent.getChildren().size();
107 return count;
108 }
109 110 111 /**
112 * 存子叶
113 * @param parentLeaf Leaf
114 */
115 public void setChild
(Board board,
String parentid
) {
116 board.setParentId(parentid);
117 getHibernateTemplate().save(board);
118 }
119 120 121 /**
122 *
123 * 删除该叶和它的子叶
124 * @param board Board
125 */
126 public void deleteBranch
(Board board
) {
127 getHibernateTemplate().delete(board);
128 }
129 130 131 /**
132 * 根据子叶得到父叶
133 * @param child Board
134 * @return Board
135 */
136 public Board getParentByChild
(Board child
) {
137 String parentId = child.getParentId();
138 Board parent = (Board) getHibernateTemplate().load(Board.class, parentId);
139 return parent;
140 }
141 142 143 /**
144 * 通过子ID得到父叶
145 * @param id String
146 * @return Board
147 */
148 public Board getParentByChildId
(String id
) {
149 Board child = (Board) getHibernateTemplate().load(Board.class, id);
150 Board parent = (Board) getHibernateTemplate().load(Board.class,child.getParentId());
151 return parent;
152 }
153 }