KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > treeservice > ss > ContentNodeSupplierJDBC


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.treeservice.ss;
25
26 import java.sql.DriverManager JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.List JavaDoc;
32
33 import org.infoglue.cms.controllers.kernel.impl.simple.ContentControllerProxy;
34 import org.infoglue.cms.entities.content.ContentVO;
35 import org.infoglue.cms.exception.ConstraintException;
36 import org.infoglue.cms.exception.SystemException;
37
38 import com.frovi.ss.Tree.BaseNode;
39 import com.frovi.ss.Tree.BaseNodeSupplier;
40
41 /**
42  * ContentNodeSupplier.java
43  * Created on 2002-sep-30
44  * @author Stefan Sik, ss@frovi.com
45  * ss
46  */

47 public class ContentNodeSupplierJDBC extends BaseNodeSupplier
48 {
49
50     private ArrayList JavaDoc cacheLeafs;
51     private java.sql.Connection JavaDoc conn;
52     
53     
54     public ContentNodeSupplierJDBC(Integer JavaDoc repositoryId, String JavaDoc userName) throws Exception JavaDoc, SystemException
55     {
56         Class.forName("com.mysql.jdbc.Driver").newInstance();
57         conn = DriverManager.getConnection("jdbc:mysql://localhost/frovi_cms_dev?user=frovi_cms&password=pass123");
58     
59         Statement JavaDoc stmt = null;
60         stmt = conn.createStatement();
61         ResultSet JavaDoc rs = stmt.executeQuery("select * from cmContent where parentContentId is null and repositoryId=" + repositoryId);
62         BaseNode rootNode = new ContentNodeImpl();
63         if (rs.next())
64         {
65             rootNode.setChildren(true);
66             rootNode.setId(new Integer JavaDoc(rs.getInt("contentId")));
67             rootNode.setTitle(rs.getString("name"));
68             rootNode.setContainer(rs.getBoolean("isBranch"));
69             setRootNode(rootNode);
70         }
71         else
72         {
73             ContentVO vo =null;
74             try
75             {
76                 vo = ContentControllerProxy.getController().getRootContentVO(repositoryId, userName);
77                 rootNode.setChildren(true);
78                 rootNode.setId(vo.getId());
79                 rootNode.setTitle(vo.getName());
80                 rootNode.setContainer(vo.getIsBranch().booleanValue());
81                 setRootNode(rootNode);
82             }
83             catch (ConstraintException e)
84             {
85             }
86             catch (SystemException e)
87             {
88             }
89         }
90             
91     }
92     /**
93      * @see com.frovi.ss.Tree.BaseNodeSupplier#hasChildren()
94      */

95     public boolean hasChildren()
96     {
97         return true;
98     }
99     
100     /**
101      * @see com.frovi.ss.Tree.BaseNodeSupplier#hasChildren(Integer)
102      */

103     public boolean hasChildren(Integer JavaDoc nodeId)
104     {
105         boolean res = false;
106         ArrayList JavaDoc ret = new ArrayList JavaDoc();
107         List JavaDoc l = null;
108         Statement JavaDoc stmt = null;
109         
110         
111         try {
112             stmt = conn.createStatement();
113             ResultSet JavaDoc rs;
114             rs = stmt.executeQuery("select count(contentId) as cnt from cmContent where parentContentId="+ nodeId);
115             if(rs.next())
116                 res = rs.getInt("cnt") > 0;
117         }
118         catch(Exception JavaDoc e)
119         {
120             e.printStackTrace();
121         }
122         return res;
123     }
124
125
126     /**
127      * @see com.frovi.ss.Tree.INodeSupplier#getChildContainerNodes(Integer)
128      */

129     public Collection JavaDoc getChildContainerNodes(Integer JavaDoc parentNode)
130     {
131         ArrayList JavaDoc ret = new ArrayList JavaDoc();
132         cacheLeafs = new ArrayList JavaDoc();
133         
134         List JavaDoc l = null;
135         ResultSet JavaDoc rs = null;
136         Statement JavaDoc stmt = null;
137
138         
139         try {
140             stmt = conn.createStatement();
141             rs = stmt.executeQuery("select * from cmContent where parentContentId=" + parentNode);
142             while (rs.next())
143             {
144                 BaseNode node = new ContentNodeImpl();
145                 node.setId(new Integer JavaDoc(rs.getInt("contentId")));
146                 node.setTitle(rs.getString("name"));
147                 node.setContainer(rs.getBoolean("isBranch"));
148                 
149                 if (node.isContainer())
150                 {
151                     ret.add(node);
152                 }
153                 else
154                 {
155                     cacheLeafs.add(node);
156                 }
157                 
158             }
159         }
160         catch(Exception JavaDoc e)
161         {
162             e.printStackTrace();
163         }
164         return ret;
165     }
166
167     /**
168      * @see com.frovi.ss.Tree.INodeSupplier#getChildLeafNodes(Integer)
169      */

170     public Collection JavaDoc getChildLeafNodes(Integer JavaDoc parentNode)
171     {
172         if (cacheLeafs == null)
173             getChildContainerNodes(parentNode);
174         return cacheLeafs;
175     }
176
177 }
178
Popular Tags