1 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 ; 25 import java.util.Date ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import org.roller.model.RollerFactory; 30 31 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 54 public WebsiteData retrieveWebsite(String id) throws RollerException 55 { 56 return (WebsiteData)mStrategy.load(id,WebsiteData.class); 57 } 58 59 62 public void storeWebsite(WebsiteData data) throws RollerException 63 { 64 mStrategy.store(data); 65 } 66 67 public void removeWebsite(String id) throws RollerException 68 { 69 mStrategy.remove(id,WebsiteData.class); 70 } 71 72 76 public WebsiteData getWebsite(String userName) throws RollerException 77 { 78 return getWebsite(userName, true); 79 } 80 81 83 public UserData retrieveUser(String 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 id) throws RollerException 94 { 95 mStrategy.remove(id,UserData.class); 96 } 97 98 100 public UserData getUser(String userName) throws RollerException 101 { 102 return getUser(userName, true); 103 } 104 105 public UserData getUser(String 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 121 public List getUsers() throws RollerException 122 { 123 return getUsers(true); 124 } 125 126 130 public PageData retrievePageReadOnly(String id) throws RollerException 131 { 132 if (id != null && id.endsWith(".vm")) return null; 134 135 return (PageData)mStrategy.load(id,PageData.class); 137 } 138 139 141 public RoleData retrieveRole(String 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 id) throws RollerException 152 { 153 mStrategy.remove(id,RoleData.class); 154 } 155 156 158 public PageData retrievePage(String id) throws RollerException 159 { 160 if (id != null && id.endsWith(".vm")) return null; 162 163 return (PageData)mStrategy.load(id,PageData.class); 164 } 165 166 public void removePage(String id) throws RollerException 167 { 168 mStrategy.remove(id,PageData.class); 169 } 170 171 public void removePageSafely(String 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 ("Page is default page of website.")); 180 } 181 removePage(id); 182 } 183 184 187 public void storePage(PageData data) throws RollerException 188 { 189 mStrategy.store(data); 190 } 191 192 public String fixPageLink(PageData data) throws RollerException 193 { 194 String 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 211 public void addUser(UserData ud, Map pages, String theme, 212 String locale, String 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 users = this.getUsers(); 226 if (users.size() == 0) 227 { 228 adminUser = true; 230 } 231 232 mStrategy.store(ud); 233 234 RoleData rd = new RoleData(null, ud, "editor"); 235 mStrategy.store(rd); 236 237 241 WebsiteData website = new WebsiteData(null, 242 ud.getFullName()+"'s Weblog", ud.getFullName()+"'s Weblog", ud, "dummy", "dummy", Boolean.TRUE, null, null, "editor-text.jsp", "", Boolean.TRUE, Boolean.FALSE, "", Boolean.TRUE); website.setEditorTheme(theme); 257 website.setLocale(locale); 258 website.setTimezone(timezone); 259 website.save(); 260 261 WeblogCategoryData rootCat = wmgr.createWeblogCategory( 262 website, null, "root", "root", null ); rootCat.save(); 268 269 WeblogCategoryData generalCat = wmgr.createWeblogCategory( 270 website, rootCat, 272 "General", "General", null ); generalCat.save(); 276 277 WeblogCategoryData javaCat = wmgr.createWeblogCategory( 278 website, rootCat, 280 "Java", "Java", null ); javaCat.save(); 284 285 WeblogCategoryData musicCat = wmgr.createWeblogCategory( 286 website, rootCat, 288 "Music", "Music", null ); musicCat.save(); 292 293 website.setBloggerCategory(rootCat); 294 website.setDefaultCategory(rootCat); 295 296 Integer zero = new Integer (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 Iterator iter = pages.keySet().iterator(); 352 while ( iter.hasNext() ) 353 { 354 String pageName = (String ) iter.next(); 355 String sb = (String )pages.get( pageName ); 356 357 PageData pd = new PageData( null, 359 website, pageName, pageName, pageName, sb, new Date () ); 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 mStrategy.store(website); 382 } 383 384 387 public String createLoginCookie(String username) throws RollerException 388 { 389 UserCookieData cookie = new UserCookieData(); 390 cookie.setUsername(username); 391 392 return saveLoginCookie(cookie); 393 } 394 395 402 protected String saveLoginCookie(UserCookieData cookie) throws RollerException 403 { 404 cookie.setCookieId(new RandomGUID().toString()); 405 cookie.save(); 406 407 String cookieString = null; 408 try { 409 cookieString = Utilities.encodeString(cookie.getUsername() + "|" + 410 cookie.getCookieId()); 411 } catch (IOException 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 |