KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > UserManagerImpl


1 /*
2  * Created on Aug 13, 2003
3  */

4 package org.roller.business;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.roller.RollerException;
9 import org.roller.model.BookmarkManager;
10 import org.roller.model.Roller;
11 import org.roller.model.UserManager;
12 import org.roller.model.WeblogManager;
13 import org.roller.pojos.BookmarkData;
14 import org.roller.pojos.FolderData;
15 import org.roller.pojos.PageData;
16 import org.roller.pojos.RoleData;
17 import org.roller.pojos.UserCookieData;
18 import org.roller.pojos.UserData;
19 import org.roller.pojos.WeblogCategoryData;
20 import org.roller.pojos.WebsiteData;
21 import org.roller.util.RandomGUID;
22 import org.roller.util.Utilities;
23
24 import java.io.IOException JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import org.roller.model.RollerFactory;
30
31 /**
32  * Abstract base implementation using PersistenceStrategy.
33  * @author Dave Johnson
34  * @author Lance Lavandowska
35  */

36 public abstract class UserManagerImpl implements UserManager
37 {
38     protected PersistenceStrategy mStrategy;
39     
40     private static Log mLogger =
41         LogFactory.getFactory().getInstance(UserManagerImpl.class);
42     
43     public UserManagerImpl(PersistenceStrategy strategy)
44     {
45         mStrategy = strategy;
46     }
47
48     public void release()
49     {
50     }
51             
52     //--------------------------------------------------------------- Website
53

54     public WebsiteData retrieveWebsite(String JavaDoc id) throws RollerException
55     {
56         return (WebsiteData)mStrategy.load(id,WebsiteData.class);
57     }
58
59     /**
60      * @see org.roller.model.UserManager#storeWebsite(org.roller.pojos.WebsiteData)
61      */

62     public void storeWebsite(WebsiteData data) throws RollerException
63     {
64         mStrategy.store(data);
65     }
66
67     public void removeWebsite(String JavaDoc id) throws RollerException
68     {
69         mStrategy.remove(id,WebsiteData.class);
70     }
71
72     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73
/**
74      * This method is a hotspot, it is called on every page request.
75      */

76     public WebsiteData getWebsite(String JavaDoc userName) throws RollerException
77     {
78         return getWebsite(userName, true);
79     }
80
81     //------------------------------------------------------------------- User
82

83     public UserData retrieveUser(String JavaDoc id) throws RollerException
84     {
85         return (UserData)mStrategy.load(id,UserData.class);
86     }
87
88     public void storeUser(UserData data) throws RollerException
89     {
90         mStrategy.store(data);
91     }
92
93     public void removeUser(String JavaDoc id) throws RollerException
94     {
95         mStrategy.remove(id,UserData.class);
96     }
97
98     //-----------------------------------------------------------------------
99

100     public UserData getUser(String JavaDoc userName) throws RollerException
101     {
102         return getUser(userName, true);
103     }
104
105     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
106
public UserData getUser(String JavaDoc userName, boolean enabledOnly) throws RollerException
107     {
108         if (userName==null )
109             throw new RollerException("userName is null");
110         
111         WebsiteData website = getWebsite(userName, enabledOnly);
112         if (website != null)
113         {
114             return website.getUser();
115         }
116         return null;
117     }
118
119     //-----------------------------------------------------------------------
120

121     public List JavaDoc getUsers() throws RollerException
122     {
123         return getUsers(true);
124     }
125
126     //------------------------------------------------------------------------
127
/**
128      * @see org.roller.model.UserManager#retrievePage(java.lang.String)
129      */

130     public PageData retrievePageReadOnly(String JavaDoc id) throws RollerException
131     {
132         // Don't hit database for templates stored on disk
133
if (id != null && id.endsWith(".vm")) return null;
134
135         // Hibernate has a read-only flag: LockMode.READ
136
return (PageData)mStrategy.load(id,PageData.class);
137     }
138
139     //------------------------------------------------------------------- Role
140

141     public RoleData retrieveRole(String JavaDoc id) throws RollerException
142     {
143         return (RoleData)mStrategy.load(id,RoleData.class);
144     }
145
146     public void storeRole(RoleData data) throws RollerException
147     {
148         mStrategy.store(data);
149     }
150
151     public void removeRole(String JavaDoc id) throws RollerException
152     {
153         mStrategy.remove(id,RoleData.class);
154     }
155
156     //------------------------------------------------------------------- Page
157

158     public PageData retrievePage(String JavaDoc id) throws RollerException
159     {
160         // Don't hit database for templates stored on disk
161
if (id != null && id.endsWith(".vm")) return null;
162
163         return (PageData)mStrategy.load(id,PageData.class);
164     }
165  
166     public void removePage(String JavaDoc id) throws RollerException
167     {
168         mStrategy.remove(id,PageData.class);
169     }
170
171     public void removePageSafely(String JavaDoc id) throws RollerException
172     {
173         PageData pd = retrievePageReadOnly(id);
174         if (pd == null) return;
175
176         WebsiteData wd = pd.getWebsite();
177         if (pd.getId() == wd.getDefaultPageId()) {
178             mLogger.error("Refusing to remove default page from website of: " + wd.getUser().getUserName());
179             throw new RollerException(new IllegalArgumentException JavaDoc("Page is default page of website."));
180         }
181         removePage(id);
182     }
183
184     /**
185      * @see org.roller.model.UserManager#storePage(org.roller.pojos.PageData)
186      */

187     public void storePage(PageData data) throws RollerException
188     {
189         mStrategy.store(data);
190     }
191     
192     public String JavaDoc fixPageLink(PageData data) throws RollerException
193     {
194         String JavaDoc link = Utilities.removeHTML(data.getName());
195         link = Utilities.removeNonAlphanumeric(link);
196
197         data.setLink(link);
198         mStrategy.store( data );
199
200         return link;
201     }
202
203     /**
204      * Add a new Roller user. Store new User, Role, Website, Category, and a
205      * "first post" WeblogEntry in the database. Reads in files from a theme
206      * directory and adds them as Pages for the User's new Website.
207      *
208      * @param ud User object representing the new user.
209      * @param themeDir Directory containing the theme for this user
210      */

211     public void addUser(UserData ud, Map JavaDoc pages, String JavaDoc theme,
212                         String JavaDoc locale, String JavaDoc timezone)
213         throws RollerException
214     {
215         Roller mRoller = RollerFactory.getRoller();
216         UserManager umgr = mRoller.getUserManager();
217         WeblogManager wmgr = mRoller.getWeblogManager();
218         if ( umgr.getUser(ud.getUserName()) != null
219              || umgr.getUser(ud.getUserName().toLowerCase()) != null)
220         {
221             throw new RollerException("error.add.user.userNameInUse");
222         }
223         
224         boolean adminUser = false;
225         List JavaDoc users = this.getUsers();
226         if (users.size() == 0)
227         {
228             // Make first user an admin
229
adminUser = true;
230         }
231         
232         mStrategy.store(ud);
233         
234         RoleData rd = new RoleData(null, ud, "editor");
235         mStrategy.store(rd);
236         
237         //
238
// CREATE WEBSITE AND CATEGORIES FOR USER
239
//
240

241         WebsiteData website = new WebsiteData(null,
242             ud.getFullName()+"'s Weblog", // name
243
ud.getFullName()+"'s Weblog", // description
244
ud, // userId
245
"dummy", // defaultPageId
246
"dummy", // weblogDayPageId
247
Boolean.TRUE, // enableBloggerApi
248
null, // bloggerCategory
249
null, // defaultCategory
250
"editor-text.jsp", // editorPage
251
"", // ignoreWords
252
Boolean.TRUE, // allowComments
253
Boolean.FALSE, // emailComments
254
"", // emailFromAddress
255
Boolean.TRUE); // isEnabled
256
website.setEditorTheme(theme);
257         website.setLocale(locale);
258         website.setTimezone(timezone);
259         website.save();
260
261         WeblogCategoryData rootCat = wmgr.createWeblogCategory(
262             website, // websiteId
263
null, // parent
264
"root", // name
265
"root", // description
266
null ); // image
267
rootCat.save();
268         
269         WeblogCategoryData generalCat = wmgr.createWeblogCategory(
270             website, // websiteId
271
rootCat,
272             "General", // name
273
"General", // description
274
null ); // image
275
generalCat.save();
276             
277         WeblogCategoryData javaCat = wmgr.createWeblogCategory(
278             website, // websiteId
279
rootCat,
280             "Java", // name
281
"Java", // description
282
null ); // image
283
javaCat.save();
284             
285         WeblogCategoryData musicCat = wmgr.createWeblogCategory(
286             website, // websiteId
287
rootCat,
288             "Music", // name
289
"Music", // description
290
null ); // image
291
musicCat.save();
292         
293         website.setBloggerCategory(rootCat);
294         website.setDefaultCategory(rootCat);
295         
296         Integer JavaDoc zero = new Integer JavaDoc(0);
297         
298         BookmarkManager bmgr = mRoller.getBookmarkManager();
299                     
300         FolderData root = bmgr.createFolder(
301             null, "root", "root", website);
302         root.save();
303
304         FolderData blogroll = bmgr.createFolder(
305             root, "Blogroll", "Blogroll", website);
306         blogroll.save();
307
308         BookmarkData b1 = bmgr.createBookmark(
309             blogroll, "Dave Johnson", "",
310             "http://rollerweblogger.org/page/roller",
311             "http://rollerweblogger.org/rss/roller",
312             zero, zero, null);
313         b1.save();
314
315         BookmarkData b2 = bmgr.createBookmark(
316             blogroll, "Matt Raible", "",
317             "http://raibledesigns.com/page/rd",
318             "http://raibledesigns.com/rss/rd",
319             zero, zero, null);
320         b2.save();
321
322         BookmarkData b3 = bmgr.createBookmark(
323             blogroll, "Lance Lavandowska", "",
324             "http://brainopolis.dnsalias.com/roller/page/lance/",
325             "http://brainopolis.dnsalias.com/roller/rss/lance/",
326             zero, zero, null);
327         b3.save();
328         
329         
330         FolderData news = bmgr.createFolder(
331             root, "News", "News", website);
332         news.save();
333
334         BookmarkData b5 = bmgr.createBookmark(
335             news, "CNN", "",
336             "http://www.cnn.com",
337             "",
338             zero, zero, null);
339         b5.save();
340
341         BookmarkData b6 = bmgr.createBookmark(
342             news, "NY Times", "",
343            "http://nytimes.com",
344            "",
345             zero, zero, null);
346         b6.save();
347
348         //
349
// READ THEME FILES AND CREATE PAGES FOR USER
350
//
351
Iterator JavaDoc iter = pages.keySet().iterator();
352         while ( iter.hasNext() )
353         {
354             String JavaDoc pageName = (String JavaDoc) iter.next();
355             String JavaDoc sb = (String JavaDoc)pages.get( pageName );
356               
357             // Store each Velocity template as a page
358
PageData pd = new PageData( null,
359                 website, // website
360
pageName, // name
361
pageName, // description
362
pageName, // link
363
sb, // template
364
new Date JavaDoc() // updateTime
365
);
366             mStrategy.store(pd);
367             
368             if ( pd.getName().equals("Weblog") )
369             {
370                 website.setDefaultPageId(pd.getId());
371             }
372             else if ( pd.getName().equals("_day") )
373             {
374                 website.setWeblogDayPageId(pd.getId());
375             }
376         }
377         
378         if (adminUser) ud.grantRole("admin");
379         
380         // Save website with blogger cat id, defauld page id and day id
381
mStrategy.store(website);
382     }
383     
384     /**
385      * @see org.roller.model.UserManager#createLoginCookie(java.lang.String)
386      */

387     public String JavaDoc createLoginCookie(String JavaDoc username) throws RollerException
388     {
389         UserCookieData cookie = new UserCookieData();
390         cookie.setUsername(username);
391
392         return saveLoginCookie(cookie);
393     }
394
395     /**
396      * Convenience method to set a unique cookie id and save to database
397      *
398      * @param cookie
399      * @return
400      * @throws Exception
401      */

402     protected String JavaDoc saveLoginCookie(UserCookieData cookie) throws RollerException
403     {
404         cookie.setCookieId(new RandomGUID().toString());
405         cookie.save();
406
407         String JavaDoc cookieString = null;
408         try {
409             cookieString = Utilities.encodeString(cookie.getUsername() + "|" +
410                            cookie.getCookieId());
411         } catch (IOException JavaDoc io) {
412             mLogger.warn("Failed to encode rememberMe cookieString");
413             mLogger.warn(io.getMessage());
414             cookieString = cookie.getUsername() + "|" + cookie.getCookieId();
415         }
416         return cookieString;
417     }
418 }
419
Popular Tags