KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > javacoding > jspider > core > storage > jdbc > FolderDAOImpl


1 package net.javacoding.jspider.core.storage.jdbc;
2
3 import net.javacoding.jspider.core.storage.spi.FolderDAOSPI;
4 import net.javacoding.jspider.core.storage.spi.StorageSPI;
5 import net.javacoding.jspider.core.model.SiteInternal;
6 import net.javacoding.jspider.core.model.FolderInternal;
7 import net.javacoding.jspider.core.logging.Log;
8 import net.javacoding.jspider.core.logging.LogFactory;
9
10 import java.sql.*;
11 import java.util.ArrayList JavaDoc;
12
13 /**
14  * $Id: FolderDAOImpl.java,v 1.2 2003/04/11 16:37:06 vanrogu Exp $
15  */

16 class FolderDAOImpl implements FolderDAOSPI {
17
18     public static final String JavaDoc ATTRIBUTE_ID = "id";
19     public static final String JavaDoc ATTRIBUTE_SITE = "site";
20     public static final String JavaDoc ATTRIBUTE_PARENT = "parent";
21     public static final String JavaDoc ATTRIBUTE_NAME = "name";
22
23     protected Log log;
24
25     protected StorageSPI storage;
26     protected DBUtil dbUtil;
27
28     public FolderDAOImpl ( StorageSPI storage, DBUtil dbUtil ) {
29         this.log = LogFactory.getLog(FolderDAOSPI.class);
30         this.storage = storage;
31         this.dbUtil = dbUtil;
32     }
33
34     public FolderInternal[] findSiteRootFolders(SiteInternal site) {
35         ArrayList JavaDoc al = new ArrayList JavaDoc ();
36
37         Statement st = null;
38         ResultSet rs = null;
39         try {
40             Connection c = dbUtil.getConnection ( );
41             st = c.createStatement();
42             rs = st.executeQuery("select * from jspider_folder where parent=0 and site=" + site.getId());
43             while ( rs.next() ) {
44                 al.add(createFolderFromRecord(rs));
45             }
46         } catch (SQLException e) {
47             log.error("SQLException", e);
48         } finally {
49             dbUtil.safeClose(rs, log);
50             dbUtil.safeClose(st, log);
51         }
52         return (FolderInternal[])al.toArray(new FolderInternal[al.size()]);
53     }
54
55     public FolderInternal[] findSubFolders(FolderInternal folder) {
56         ArrayList JavaDoc al = new ArrayList JavaDoc ( );
57
58         Statement st = null;
59         ResultSet rs = null;
60         try {
61             Connection c = dbUtil.getConnection ( );
62             st = c.createStatement();
63             rs = st.executeQuery("select * from jspider_folder where parent= " + folder.getId());
64             while ( rs.next() ) {
65                 al.add(createFolderFromRecord(rs));
66             }
67         } catch (SQLException e) {
68             log.error("SQLException", e);
69         } finally {
70             dbUtil.safeClose(rs, log);
71             dbUtil.safeClose(st, log);
72         }
73         return (FolderInternal[])al.toArray(new FolderInternal[al.size()]);
74     }
75
76     public FolderInternal findById(int folderId) {
77         FolderInternal folder = null;
78         Statement st = null;
79         ResultSet rs = null;
80         try {
81             Connection c = dbUtil.getConnection ( );
82             st = c.createStatement();
83             rs = st.executeQuery("select * from jspider_folder where id=" + folderId);
84             if ( rs.next() ) {
85                 folder = createFolderFromRecord(rs);
86             }
87         } catch (SQLException e) {
88             log.error("SQLException", e);
89         } finally {
90             dbUtil.safeClose(rs, log);
91             dbUtil.safeClose(st, log);
92         }
93         return folder;
94     }
95
96     public FolderInternal createFolder(int id, FolderInternal parent, String JavaDoc name) {
97         return createFolder ( id, parent.getSiteId(), parent.getId(), name );
98     }
99
100     public FolderInternal createFolder(int id, SiteInternal site, String JavaDoc name) {
101         return createFolder ( id, site.getId(), 0, name );
102     }
103
104     public FolderInternal createFolder ( int id, int siteId, int parentId, String JavaDoc name ) {
105         FolderInternal folder = null;
106
107         Statement st = null;
108         ResultSet rs = null;
109         try {
110             Connection c = dbUtil.getConnection ( );
111             st = c.createStatement();
112             st.executeUpdate("insert into jspider_folder ( id, parent, site, name ) values (" + id + "," + parentId + "," + siteId + ",'" + name + "')");
113         } catch (SQLException e) {
114             log.error("SQLException", e);
115         } finally {
116             dbUtil.safeClose(rs, log);
117             dbUtil.safeClose(st, log);
118         }
119         folder = findById(id);
120         return folder;
121     }
122
123
124     protected FolderInternal createFolderFromRecord ( ResultSet rs ) throws SQLException {
125         int id = rs.getInt(ATTRIBUTE_ID);
126         int parent = rs.getInt(ATTRIBUTE_PARENT);
127         int site = rs.getInt(ATTRIBUTE_SITE);
128         String JavaDoc name = rs.getString(ATTRIBUTE_NAME);
129
130         return new FolderInternal ( storage, id, parent, name, site );
131     }
132
133 }
134
Popular Tags