Source code: com/RuntimeCollective/sitemap/bean/Bookmarks.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/sitemap/bean/Bookmarks.java,v 1.7 2003/09/30 15:12:59 joe Exp $
2 * $Revision: 1.7 $
3 * $Date: 2003/09/30 15:12:59 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.sitemap.bean;
31
32 import com.RuntimeCollective.sitemap.bean.SiteLocation;
33 import com.RuntimeCollective.webapps.bean.User;
34
35 import com.RuntimeCollective.webapps.RuntimeDataSource;
36 import com.RuntimeCollective.webapps.RuntimeParameters;
37
38 import java.util.Calendar;
39 import java.util.Vector;
40 import java.util.Iterator;
41 import java.sql.SQLException;
42
43 /**
44 * A collection of bookmarked content for a SitemapUser.
45 * You should not directly instantiate a Bookmarks object;
46 * just call <code>Bookmarks.getBookmarksForUser(User user)</code>.
47 *
48 * @author Joe Holmberg
49 * @version $Id: Bookmarks.java,v 1.7 2003/09/30 15:12:59 joe Exp $
50 */
51 public class Bookmarks {
52
53 /**
54 * Get a collection of bookmarks for this user
55 */
56 public static Bookmarks getBookmarksForUser(User user) throws SQLException {
57 // Check if user has bookmarks already
58 Object bookmarks = user.getAttribute("bookmarks");
59 if (bookmarks != null) {
60 return (Bookmarks)bookmarks;
61 }
62
63 // No bookmarks around? OK, get them from the database,
64 // and set them as an attribute of the user
65 Bookmarks newBookmarks = new Bookmarks(user);
66 user.setAttribute("bookmarks", newBookmarks);
67 return newBookmarks;
68 }
69
70
71 // -------------------------------------------------------
72
73
74 private Vector bookmarks;
75 private User user;
76
77 /**
78 * Make a new Bookmarks object, containing all bookmarks for this particular user
79 */
80 private Bookmarks(User user) throws SQLException {
81 bookmarks = new Vector();
82 this.user = user;
83
84 // Get all bookmarks for this user
85 String query = "SELECT sitelocation FROM sitemap_bookmark WHERE user_id = "+user.getId();
86 Object[][] results = RuntimeDataSource.queryRows(query);
87 if (results != null && results.length > 0) {
88 for (int i=0; i<results.length; i++) {
89 int siteLocId = ((Integer)results[i][0]).intValue();
90 //RuntimeParameters.logDebug(this, "Found site location: "+siteLocId);
91 SiteLocation siteLoc = (SiteLocation)RuntimeParameters.getStore().get("com.RuntimeCollective.sitemap.bean.SiteLocation", siteLocId);
92 addSorted(siteLoc);
93 }
94 } else {
95 RuntimeParameters.logDebug(this, "No bookmarks for user "+user.getId());
96 }
97 }
98
99 /*
100 * Adds a SiteLocation to this list of bookmarks.
101 * @param id the id of the SiteLocation to add
102 * @return whether the bookmark has been added. If it's there already, will return false.
103 */
104 public boolean addBookmark(int id) throws SQLException {
105 // Make a SiteLocation object from this id
106 SiteLocation sl = (SiteLocation) RuntimeParameters.getStore().get("com.RuntimeCollective.sitemap.bean.SiteLocation", id);
107 return addBookmark(sl);
108 }
109
110 /*
111 * Adds a SiteLocation to this list of bookmarks.
112 * @param sl the SiteLocation to add
113 * @return whether the bookmark has been added. If it's there already, will return false.
114 */
115 public boolean addBookmark(SiteLocation sl) throws SQLException {
116 if (sl == null) {
117 RuntimeParameters.logDebug(this, "addBookmark: given SiteLocation is null!");
118 return false;
119 }
120
121 // Check to make sure it's not there already
122 if (bookmarks.contains(sl)) {
123 // It's there already
124 //RuntimeParameters.logDebug(this, "SiteLocation "+sl.getId()+" has been bookmarked already; not bookmarking again!");
125 return false;
126 }
127
128 /* Sod this; too expensive
129 // Double-check it's not in the database
130 String query = "SELECT 1 FROM sitemap_bookmark WHERE user_id = "+user.getId()+" AND sitelocation = "+sl.getId();
131 Object[] results = RuntimeDataSource.queryRow(query);
132 if (results != null && results.length > 0) {
133 // It's there already
134 //RuntimeParameters.logDebug(this, "SiteLocation "+sl.getId()+" has been bookmarked already; not bookmarking again!");
135 return false;
136 }
137 */
138
139 // Add it to the database
140
141 // We'll use the RDS nextId, even though this isn't an EntityBean... cos it's easier that way
142 int nextId = RuntimeDataSource.nextId();
143
144 // Get today's date and time
145 String rightNow = RuntimeDataSource.toSqlString(Calendar.getInstance().getTime());
146
147 StringBuffer insert = new StringBuffer(120);
148 insert.append("INSERT INTO sitemap_bookmark (id, user_id, sitelocation, creation_date) VALUES (").append(nextId).append(", ").append(user.getId()).append(", ").append(sl.getId()).append(", ").append(rightNow).append(")");
149 RuntimeParameters.logDebug(this, "INSERTING BOOKMARK with: "+insert.toString());
150 RuntimeDataSource.update(insert.toString());
151
152 // Now add it to the internal bookmarks store, if the db add went OK
153 addSorted(sl);
154
155 return true;
156 }
157
158 /**
159 * Add the given SiteLocation to the bookmarks vector, in the right location
160 * (sort by SiteLocation name).
161 */
162 private void addSorted(SiteLocation sl) {
163 String slName = sl.getName();
164 boolean added=false;
165 for (int i=0; i<bookmarks.size(); i++) {
166 SiteLocation oneSl = (SiteLocation)bookmarks.elementAt(i);
167 String oneSlName = oneSl.getName();
168
169 if (slName.compareToIgnoreCase(oneSlName) < 0) {
170 // if (oneSlName.compareToIgnoreCase(slName) < 0) {
171 // Add it here
172 bookmarks.insertElementAt(sl, i);
173 added = true;
174 break;
175 }
176 }
177 if (!added)
178 bookmarks.add(sl);
179 }
180
181 /*
182 * Removes a SiteLocation from this list of bookmarks.
183 * @param sl the SiteLocation to remove
184 * @return whether the bookmark has been removed. If it wasn't there, will return false.
185 */
186 public boolean removeBookmark(SiteLocation sl) throws SQLException {
187 if (sl == null) {
188 RuntimeParameters.logDebug(this, "removeBookmark: given SiteLocation is null!");
189 return false;
190 }
191
192 // Check if we really have it
193 if (!bookmarks.contains(sl)) {
194 RuntimeParameters.logDebug(this, "Isn't there - can't remove!");
195 return false;
196 }
197
198 // Try removing it from the database
199 StringBuffer remove = new StringBuffer(80);
200 remove.append("DELETE FROM sitemap_bookmark WHERE user_id=").append(user.getId()).append(" AND sitelocation=").append(sl.getId());
201 RuntimeParameters.logDebug(this, "Removing sitemap_bookmark "+sl.getId());
202 RuntimeDataSource.update(remove.toString());
203
204 // Now remove it from internal bookmark store
205 bookmarks.remove(sl);
206
207 return true;
208 }
209
210 /*
211 * Removes a SiteLocation from this list of bookmarks.
212 * @param id integer id of the SiteLocation to remove
213 */
214 public boolean removeBookmark(int id) throws SQLException {
215 // Make a SiteLocation object from this id
216 SiteLocation sl = (SiteLocation) RuntimeParameters.getStore().get("com.RuntimeCollective.sitemap.bean.SiteLocation", id);
217 return removeBookmark(sl);
218 }
219
220 /**
221 * An alternative name for calling "iterator"
222 */
223 public Iterator getAllBookmarks() {
224 return iterator();
225 }
226
227 /**
228 * Return an iterator of all <b>live</b> SiteLocation bookmarks.
229 * (SiteLocations that are not currently live will still be saved as
230 * the users' bookmarks; they just won't be displayed.)
231 */
232 public Iterator iterator() {
233 Vector liveBookmarks = new Vector();
234 Iterator allBookmarks = bookmarks.iterator();
235 while (allBookmarks.hasNext()) {
236 SiteLocation sl = (SiteLocation)allBookmarks.next();
237 if (sl.isLive())
238 liveBookmarks.add(sl);
239 }
240 return liveBookmarks.iterator();
241 }
242
243 /**
244 * Return an iterator of all SiteLocation bookmarks, whether they're live or dead.
245 */
246 public Iterator iteratorIncludeDead() {
247 return bookmarks.iterator();
248 }
249 }
250