You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by sn...@apache.org on 2005/10/21 23:46:28 UTC
svn commit: r327589 [19/72] - in /incubator/roller/branches/roller_1.x: ./
contrib/ contrib/lib/ contrib/plugins/ contrib/plugins/src/
contrib/plugins/src/org/ contrib/plugins/src/org/roller/
contrib/plugins/src/org/roller/presentation/ contrib/plugins...
Added: incubator/roller/branches/roller_1.x/src/org/roller/business/UserManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller_1.x/src/org/roller/business/UserManagerImpl.java?rev=327589&view=auto
==============================================================================
--- incubator/roller/branches/roller_1.x/src/org/roller/business/UserManagerImpl.java (added)
+++ incubator/roller/branches/roller_1.x/src/org/roller/business/UserManagerImpl.java Fri Oct 21 14:27:36 2005
@@ -0,0 +1,421 @@
+/*
+ * Created on Aug 13, 2003
+ */
+package org.roller.business;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.roller.RollerException;
+import org.roller.model.BookmarkManager;
+import org.roller.model.Roller;
+import org.roller.model.UserManager;
+import org.roller.model.WeblogManager;
+import org.roller.pojos.BookmarkData;
+import org.roller.pojos.FolderData;
+import org.roller.pojos.WeblogTemplate;
+import org.roller.pojos.RoleData;
+import org.roller.pojos.UserCookieData;
+import org.roller.pojos.UserData;
+import org.roller.pojos.WeblogCategoryData;
+import org.roller.pojos.WebsiteData;
+import org.roller.util.RandomGUID;
+import org.roller.util.Utilities;
+
+import java.io.IOException;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import org.roller.model.RollerFactory;
+
+/**
+ * Abstract base implementation using PersistenceStrategy.
+ * @author Dave Johnson
+ * @author Lance Lavandowska
+ */
+public abstract class UserManagerImpl implements UserManager
+{
+ protected PersistenceStrategy mStrategy;
+
+ private static Log mLogger =
+ LogFactory.getFactory().getInstance(UserManagerImpl.class);
+
+ public UserManagerImpl(PersistenceStrategy strategy)
+ {
+ mStrategy = strategy;
+ }
+
+ public void release()
+ {
+ }
+
+ //--------------------------------------------------------------- Website
+
+ public WebsiteData retrieveWebsite(String id) throws RollerException
+ {
+ return (WebsiteData)mStrategy.load(id,WebsiteData.class);
+ }
+
+ /**
+ * @see org.roller.model.UserManager#storeWebsite(org.roller.pojos.WebsiteData)
+ */
+ public void storeWebsite(WebsiteData data) throws RollerException
+ {
+ mStrategy.store(data);
+ }
+
+ public void removeWebsite(String id) throws RollerException
+ {
+ mStrategy.remove(id,WebsiteData.class);
+ }
+
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ /**
+ * This method is a hotspot, it is called on every page request.
+ */
+ public WebsiteData getWebsite(String userName) throws RollerException
+ {
+ return getWebsite(userName, true);
+ }
+
+ //------------------------------------------------------------------- User
+
+ public UserData retrieveUser(String id) throws RollerException
+ {
+ return (UserData)mStrategy.load(id,UserData.class);
+ }
+
+ public void storeUser(UserData data) throws RollerException
+ {
+ mStrategy.store(data);
+ }
+
+ public void removeUser(String id) throws RollerException
+ {
+ mStrategy.remove(id,UserData.class);
+ }
+
+ //-----------------------------------------------------------------------
+
+ public UserData getUser(String userName) throws RollerException
+ {
+ return getUser(userName, true);
+ }
+
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ public UserData getUser(String userName, boolean enabledOnly) throws RollerException
+ {
+ if (userName==null )
+ throw new RollerException("userName is null");
+
+ WebsiteData website = getWebsite(userName, enabledOnly);
+ if (website != null)
+ {
+ return website.getUser();
+ }
+ return null;
+ }
+
+ //-----------------------------------------------------------------------
+
+ public List getUsers() throws RollerException
+ {
+ return getUsers(true);
+ }
+
+ //------------------------------------------------------------------------
+ /**
+ * @see org.roller.model.UserManager#retrievePage(java.lang.String)
+ */
+ public WeblogTemplate retrievePageReadOnly(String id) throws RollerException
+ {
+ // Don't hit database for templates stored on disk
+ if (id != null && id.endsWith(".vm")) return null;
+
+ // Hibernate has a read-only flag: LockMode.READ
+ return (WeblogTemplate)mStrategy.load(id,WeblogTemplate.class);
+ }
+
+ //------------------------------------------------------------------- Role
+
+ public RoleData retrieveRole(String id) throws RollerException
+ {
+ return (RoleData)mStrategy.load(id,RoleData.class);
+ }
+
+ public void storeRole(RoleData data) throws RollerException
+ {
+ mStrategy.store(data);
+ }
+
+ public void removeRole(String id) throws RollerException
+ {
+ mStrategy.remove(id,RoleData.class);
+ }
+
+ //------------------------------------------------------------------- Page
+
+ public WeblogTemplate retrievePage(String id) throws RollerException
+ {
+ // Don't hit database for templates stored on disk
+ if (id != null && id.endsWith(".vm")) return null;
+
+ return (WeblogTemplate)mStrategy.load(id,WeblogTemplate.class);
+ }
+
+ public void removePage(String id) throws RollerException
+ {
+ mStrategy.remove(id,WeblogTemplate.class);
+ }
+
+ public void removePageSafely(String id) throws RollerException
+ {
+ WeblogTemplate pd = retrievePageReadOnly(id);
+ if (pd == null) return;
+
+ WebsiteData wd = pd.getWebsite();
+ if (pd.getId() == wd.getDefaultPageId()) {
+ mLogger.error("Refusing to remove default page from website of: " + wd.getUser().getUserName());
+ throw new RollerException(new IllegalArgumentException("Page is default page of website."));
+ }
+ removePage(id);
+ }
+
+ /**
+ * @see org.roller.model.UserManager#storePage(org.roller.pojos.WeblogTemplate)
+ */
+ public void storePage(WeblogTemplate data) throws RollerException
+ {
+ mStrategy.store(data);
+ }
+
+ public String fixPageLink(WeblogTemplate data) throws RollerException
+ {
+ String link = Utilities.removeHTML(data.getName());
+ link = Utilities.removeNonAlphanumeric(link);
+
+ data.setLink(link);
+ mStrategy.store( data );
+
+ return link;
+ }
+
+ /**
+ * Add a new Roller user. Store new User, Role, Website, Category, and a
+ * "first post" WeblogEntry in the database. Reads in files from a theme
+ * directory and adds them as Pages for the User's new Website.
+ *
+ * @param ud User object representing the new user.
+ * @param themeDir Directory containing the theme for this user
+ */
+ public void addUser(UserData ud, Map pages, String theme,
+ String locale, String timezone)
+ throws RollerException
+ {
+ Roller mRoller = RollerFactory.getRoller();
+ UserManager umgr = mRoller.getUserManager();
+ WeblogManager wmgr = mRoller.getWeblogManager();
+ if ( umgr.getUser(ud.getUserName()) != null
+ || umgr.getUser(ud.getUserName().toLowerCase()) != null)
+ {
+ throw new RollerException("error.add.user.userNameInUse");
+ }
+
+ boolean adminUser = false;
+ List users = this.getUsers();
+ if (users.size() == 0)
+ {
+ // Make first user an admin
+ adminUser = true;
+ }
+
+ mStrategy.store(ud);
+
+ RoleData rd = new RoleData(null, ud, "editor");
+ mStrategy.store(rd);
+
+ //
+ // CREATE WEBSITE AND CATEGORIES FOR USER
+ //
+
+ WebsiteData website = new WebsiteData(null,
+ ud.getFullName()+"'s Weblog", // name
+ ud.getFullName()+"'s Weblog", // description
+ ud, // userId
+ "dummy", // defaultPageId
+ "dummy", // weblogDayPageId
+ Boolean.TRUE, // enableBloggerApi
+ null, // bloggerCategory
+ null, // defaultCategory
+ "editor-text.jsp", // editorPage
+ "", // ignoreWords
+ Boolean.TRUE, // allowComments
+ Boolean.FALSE, // emailComments
+ "", // emailFromAddress
+ Boolean.TRUE); // isEnabled
+ website.setEditorTheme(theme);
+ website.setLocale(locale);
+ website.setTimezone(timezone);
+ website.save();
+
+ WeblogCategoryData rootCat = wmgr.createWeblogCategory(
+ website, // websiteId
+ null, // parent
+ "root", // name
+ "root", // description
+ null ); // image
+ rootCat.save();
+
+ WeblogCategoryData generalCat = wmgr.createWeblogCategory(
+ website, // websiteId
+ rootCat,
+ "General", // name
+ "General", // description
+ null ); // image
+ generalCat.save();
+
+ WeblogCategoryData javaCat = wmgr.createWeblogCategory(
+ website, // websiteId
+ rootCat,
+ "Java", // name
+ "Java", // description
+ null ); // image
+ javaCat.save();
+
+ WeblogCategoryData musicCat = wmgr.createWeblogCategory(
+ website, // websiteId
+ rootCat,
+ "Music", // name
+ "Music", // description
+ null ); // image
+ musicCat.save();
+
+ website.setBloggerCategory(rootCat);
+ website.setDefaultCategory(rootCat);
+
+ Integer zero = new Integer(0);
+
+ BookmarkManager bmgr = mRoller.getBookmarkManager();
+
+ FolderData root = bmgr.createFolder(
+ null, "root", "root", website);
+ root.save();
+
+ FolderData blogroll = bmgr.createFolder(
+ root, "Blogroll", "Blogroll", website);
+ blogroll.save();
+
+ BookmarkData b1 = bmgr.createBookmark(
+ blogroll, "Dave Johnson", "",
+ "http://rollerweblogger.org/page/roller",
+ "http://rollerweblogger.org/rss/roller",
+ zero, zero, null);
+ b1.save();
+
+ BookmarkData b2 = bmgr.createBookmark(
+ blogroll, "Matt Raible", "",
+ "http://raibledesigns.com/page/rd",
+ "http://raibledesigns.com/rss/rd",
+ zero, zero, null);
+ b2.save();
+
+ BookmarkData b3 = bmgr.createBookmark(
+ blogroll, "Lance Lavandowska", "",
+ "http://brainopolis.dnsalias.com/roller/page/lance/",
+ "http://brainopolis.dnsalias.com/roller/rss/lance/",
+ zero, zero, null);
+ b3.save();
+
+
+ FolderData news = bmgr.createFolder(
+ root, "News", "News", website);
+ news.save();
+
+ BookmarkData b5 = bmgr.createBookmark(
+ news, "CNN", "",
+ "http://www.cnn.com",
+ "",
+ zero, zero, null);
+ b5.save();
+
+ BookmarkData b6 = bmgr.createBookmark(
+ news, "NY Times", "",
+ "http://nytimes.com",
+ "",
+ zero, zero, null);
+ b6.save();
+
+ //
+ // READ THEME FILES AND CREATE PAGES FOR USER
+ //
+ /* new registrations require choosing a theme
+ * now that themes are shared we don't save page themplates -- Allen G
+ Iterator iter = pages.keySet().iterator();
+ while ( iter.hasNext() )
+ {
+ String pageName = (String) iter.next();
+ String sb = (String)pages.get( pageName );
+
+ // Store each Velocity template as a page
+ WeblogTemplate pd = new WeblogTemplate( null,
+ website, // website
+ pageName, // name
+ pageName, // description
+ pageName, // link
+ sb, // template
+ new Date() // updateTime
+ );
+ mStrategy.store(pd);
+
+ if ( pd.getName().equals("Weblog") )
+ {
+ website.setDefaultPageId(pd.getId());
+ }
+ else if ( pd.getName().equals("_day") )
+ {
+ website.setWeblogDayPageId(pd.getId());
+ }
+ }
+ */
+
+ if (adminUser) ud.grantRole("admin");
+
+ // Save website with blogger cat id, defauld page id and day id
+ mStrategy.store(website);
+ }
+
+ /**
+ * @see org.roller.model.UserManager#createLoginCookie(java.lang.String)
+ */
+ public String createLoginCookie(String username) throws RollerException
+ {
+ UserCookieData cookie = new UserCookieData();
+ cookie.setUsername(username);
+
+ return saveLoginCookie(cookie);
+ }
+
+ /**
+ * Convenience method to set a unique cookie id and save to database
+ *
+ * @param cookie
+ * @return
+ * @throws Exception
+ */
+ protected String saveLoginCookie(UserCookieData cookie) throws RollerException
+ {
+ cookie.setCookieId(new RandomGUID().toString());
+ cookie.save();
+
+ String cookieString = null;
+ try {
+ cookieString = Utilities.encodeString(cookie.getUsername() + "|" +
+ cookie.getCookieId());
+ } catch (IOException io) {
+ mLogger.warn("Failed to encode rememberMe cookieString");
+ mLogger.warn(io.getMessage());
+ cookieString = cookie.getUsername() + "|" + cookie.getCookieId();
+ }
+ return cookieString;
+ }
+}
Added: incubator/roller/branches/roller_1.x/src/org/roller/business/WeblogManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller_1.x/src/org/roller/business/WeblogManagerImpl.java?rev=327589&view=auto
==============================================================================
--- incubator/roller/branches/roller_1.x/src/org/roller/business/WeblogManagerImpl.java (added)
+++ incubator/roller/branches/roller_1.x/src/org/roller/business/WeblogManagerImpl.java Fri Oct 21 14:27:36 2005
@@ -0,0 +1,513 @@
+/*
+ * Created on Feb 24, 2003
+ */
+package org.roller.business;
+
+import org.apache.commons.collections.comparators.ReverseComparator;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.roller.RollerException;
+import org.roller.model.Roller;
+import org.roller.model.WeblogManager;
+import org.roller.pojos.CommentData;
+import org.roller.pojos.UserData;
+import org.roller.pojos.WeblogCategoryAssoc;
+import org.roller.pojos.WeblogCategoryData;
+import org.roller.pojos.WeblogEntryData;
+import org.roller.pojos.WebsiteData;
+import org.roller.util.DateUtil;
+import org.roller.util.Utilities;
+
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import org.roller.model.RollerFactory;
+
+/**
+ * Abstract base implementation using PersistenceStrategy.
+ * @author Dave Johnson
+ * @author Lance Lavandowska
+ */
+public abstract class WeblogManagerImpl implements WeblogManager
+{
+ private static Log mLogger =
+ LogFactory.getFactory().getInstance(WeblogManagerImpl.class);
+
+ protected PersistenceStrategy mStrategy;
+
+ /* inline creation of reverse comparator, anonymous inner class */
+ private Comparator reverseComparator = new ReverseComparator();
+ /*
+ new Comparator()
+ {
+ public int compare(Object o1, Object o2)
+ {
+ return -1 * ((Date) o1).compareTo((Date)o2);
+ }
+ };
+ */
+
+ private SimpleDateFormat formatter = DateUtil.get8charDateFormat();
+
+ public abstract List getWeblogEntries(
+ WebsiteData website,
+ Date startDate,
+ Date endDate,
+ String catName,
+ String status,
+ Integer maxEntries,
+ Boolean pinned) throws RollerException;
+
+ public abstract List getNextPrevEntries(
+ WeblogEntryData current,
+ String catName,
+ int maxEntries,
+ boolean next) throws RollerException;
+
+ public WeblogManagerImpl(PersistenceStrategy strategy)
+ {
+ mStrategy = strategy;
+ }
+
+ public void release()
+ {
+ }
+
+ //------------------------------------------------ WeblogCategoryData CRUD
+
+ /**
+ * @see org.roller.model.WeblogManager#createWeblogCategory()
+ */
+ public WeblogCategoryData createWeblogCategory()
+ {
+ return new WeblogCategoryData();
+ }
+
+ /**
+ * @see org.roller.model.WeblogManager#createWeblogCategory(
+ * org.roller.pojos.WebsiteData, org.roller.pojos.WeblogCategoryData,
+ * java.lang.String, java.lang.String, java.lang.String)
+ */
+ public WeblogCategoryData createWeblogCategory(
+ WebsiteData website,
+ WeblogCategoryData parent,
+ String name,
+ String description,
+ String image) throws RollerException
+ {
+ return new WeblogCategoryData(
+ null, website, parent, name, description, image);
+ }
+
+ public WeblogCategoryData retrieveWeblogCategory(String id)
+ throws RollerException
+ {
+ return (WeblogCategoryData) mStrategy.load(
+ id,
+ WeblogCategoryData.class);
+ }
+
+ //--------------------------------------------- WeblogCategoryData Queries
+
+ public WeblogCategoryData getWeblogCategoryByPath(
+ WebsiteData website, String categoryPath) throws RollerException
+ {
+ return getWeblogCategoryByPath(website, null, categoryPath);
+ }
+
+ public String getPath(WeblogCategoryData category) throws RollerException
+ {
+ if (null == category.getParent())
+ {
+ return "/";
+ }
+ else
+ {
+ String parentPath = getPath(category.getParent());
+ parentPath = "/".equals(parentPath) ? "" : parentPath;
+ return parentPath + "/" + category.getName();
+ }
+ }
+
+ public WeblogCategoryData getWeblogCategoryByPath(
+ WebsiteData website, WeblogCategoryData category, String path)
+ throws RollerException
+ {
+ final Iterator cats;
+ final String[] pathArray = Utilities.stringToStringArray(path, "/");
+
+ if (category == null && (null == path || "".equals(path.trim())))
+ {
+ throw new RollerException("Bad arguments.");
+ }
+
+ if (path.trim().equals("/"))
+ {
+ return getRootWeblogCategory(website);
+ }
+ else if (category == null || path.trim().startsWith("/"))
+ {
+ cats = getRootWeblogCategory(website).getWeblogCategories().iterator();
+ }
+ else
+ {
+ cats = category.getWeblogCategories().iterator();
+ }
+
+ while (cats.hasNext())
+ {
+ WeblogCategoryData possibleMatch = (WeblogCategoryData)cats.next();
+ if (possibleMatch.getName().equals(pathArray[0]))
+ {
+ if (pathArray.length == 1)
+ {
+ return possibleMatch;
+ }
+ else
+ {
+ String[] subpath = new String[pathArray.length - 1];
+ System.arraycopy(pathArray, 1, subpath, 0, subpath.length);
+
+ String pathString= Utilities.stringArrayToString(subpath,"/");
+ return getWeblogCategoryByPath(website, possibleMatch, pathString);
+ }
+ }
+ }
+
+ // The category did not match and neither did any sub-categories
+ return null;
+ }
+
+ //----------------------------------------------- WeblogCategoryAssoc CRUD
+
+ public WeblogCategoryAssoc createWeblogCategoryAssoc()
+ {
+ return new WeblogCategoryAssoc();
+ }
+
+ public WeblogCategoryAssoc createWeblogCategoryAssoc(
+ WeblogCategoryData category,
+ WeblogCategoryData ancestor,
+ String relation) throws RollerException
+ {
+ return new WeblogCategoryAssoc(null, category, ancestor, relation);
+ }
+
+ public WeblogCategoryAssoc retrieveWeblogCategoryAssoc(String id) throws RollerException
+ {
+ return (WeblogCategoryAssoc)mStrategy.load(id, WeblogCategoryAssoc.class);
+ }
+
+ //------------------------------------------------------- CommentData CRUD
+
+ public void removeComment(String id) throws RollerException
+ {
+ mStrategy.remove(id, CommentData.class);
+ }
+
+ public void removeComments(String[] ids) throws RollerException
+ {
+ for (int i = 0; i < ids.length; i++)
+ {
+ removeComment(ids[i]);
+ }
+ }
+
+ public void removeCommentsForEntry(String entryId) throws RollerException
+ {
+ List comments = getComments(entryId, false); // get all Comments
+ Iterator it = comments.iterator();
+ while (it.hasNext())
+ {
+ removeComment( ((CommentData)it.next()).getId() );
+ }
+ }
+
+ //---------------------------------------------------- CommentData Queries
+
+ public CommentData retrieveComment(String id) throws RollerException
+ {
+ return (CommentData) mStrategy.load(id, CommentData.class);
+ }
+
+ public List getComments(String entryId) throws RollerException
+ {
+ return getComments(entryId, true);
+ }
+
+ //--------------------------------------------------- WeblogEntryData CRUD
+
+ public WeblogEntryData retrieveWeblogEntry(String id)
+ throws RollerException
+ {
+ return (WeblogEntryData) mStrategy.load(
+ id, WeblogEntryData.class);
+ }
+
+ public void removeWeblogEntry(String id) throws RollerException
+ {
+ Roller mRoller = RollerFactory.getRoller();
+ mRoller.getRefererManager().removeReferersForEntry(id);
+ removeCommentsForEntry( id );
+ mStrategy.remove(id, WeblogEntryData.class);
+ }
+
+ //------------------------------------------------ WeblogEntryData Queries
+
+ /**
+ * Gets the Date of the latest Entry publish time, before the end of today,
+ * for all WeblogEntries
+ *
+ * @param userName
+ * @return Date
+ * @throws RollerException
+ */
+ public Date getWeblogLastPublishTime(String userName)
+ throws RollerException
+ {
+ return getWeblogLastPublishTime(userName, null);
+ }
+
+ //--------------------------------------------------------- Implementation
+
+ /**
+ * Get weblog entries.
+ * @see org.roller.model.WeblogManager#getWeblogEntries(
+ * java.lang.String,
+ * java.util.Date,
+ * java.util.Date,
+ * java.lang.String,
+ * java.lang.String,
+ * java.lang.Integer)
+ */
+ public List getWeblogEntries(
+ WebsiteData website,
+ Date startDate,
+ Date endDate,
+ String catName,
+ String status,
+ Integer maxEntries) throws RollerException
+ {
+ return getWeblogEntries(
+ website,
+ startDate,
+ endDate,
+ catName,
+ status,
+ maxEntries,
+ null);
+ }
+
+ /**
+ * Get webloog entries in range specified by offset and length.
+ * @see org.roller.model.WeblogManager#getWeblogEntries(
+ * java.lang.String,
+ * java.util.Date,
+ * java.util.Date,
+ * java.lang.String,
+ * java.lang.String,
+ * int offset,
+ * int length)
+ */
+ public List getWeblogEntries(
+ WebsiteData website,
+ Date startDate,
+ Date endDate,
+ String catName,
+ String status,
+ int offset,
+ int range) throws RollerException
+ {
+ List filtered = new ArrayList();
+ List entries = getWeblogEntries(
+ website,
+ startDate,
+ endDate,
+ catName,
+ status,
+ new Integer(offset + range),
+ null);
+ if (entries.size() < offset)
+ {
+ return filtered;
+ }
+ for (int i=offset; i<entries.size(); i++)
+ {
+ filtered.add(entries.get(i));
+ }
+ return filtered;
+ }
+
+ /**
+ * @see org.roller.model.WeblogManager#getWeblogEntryDayMap(
+ * java.lang.String,
+ * java.util.Date,
+ * java.util.Date,
+ * java.lang.String,
+ * java.lang.String,
+ * java.lang.Integer)
+ */
+ public Map getWeblogEntryObjectMap(
+ WebsiteData website,
+ Date startDate,
+ Date endDate,
+ String catName,
+ String status,
+ Integer maxEntries) throws RollerException
+ {
+ return getWeblogEntryMap(
+ website,
+ startDate,
+ endDate,
+ catName,
+ status,
+ maxEntries,
+ false);
+ }
+
+ /**
+ * @see org.roller.model.WeblogManager#getWeblogEntryDayMap(
+ * java.lang.String,
+ * java.util.Date,
+ * java.util.Date,
+ * java.lang.String,
+ * java.lang.String,
+ * java.lang.Integer)
+ */
+ public Map getWeblogEntryStringMap(
+ WebsiteData website,
+ Date startDate,
+ Date endDate,
+ String catName,
+ String status,
+ Integer maxEntries) throws RollerException
+ {
+ return getWeblogEntryMap(
+ website,
+ startDate,
+ endDate,
+ catName,
+ status,
+ maxEntries,
+ true);
+ }
+
+ private Map getWeblogEntryMap(
+ WebsiteData website,
+ Date startDate,
+ Date endDate,
+ String catName,
+ String status,
+ Integer maxEntries,
+ boolean stringsOnly) throws RollerException
+ {
+ TreeMap map = new TreeMap(reverseComparator);
+
+ List entries = getWeblogEntries(
+ website,
+ startDate,
+ endDate,
+ catName,
+ status,
+ maxEntries);
+
+ Calendar cal = Calendar.getInstance();
+ if (website != null)
+ {
+ cal.setTimeZone(website.getTimeZoneInstance());
+ }
+
+ for (Iterator wbItr = entries.iterator(); wbItr.hasNext();)
+ {
+ WeblogEntryData entry = (WeblogEntryData) wbItr.next();
+ Date sDate = DateUtil.getNoonOfDay(entry.getPubTime(), cal);
+ if (stringsOnly)
+ {
+ if (map.get(sDate) == null)
+ map.put(sDate, formatter.format(sDate));
+ }
+ else
+ {
+ List dayEntries = (List) map.get(sDate);
+ if (dayEntries == null)
+ {
+ dayEntries = new ArrayList();
+ map.put(sDate, dayEntries);
+ }
+ dayEntries.add(entry);
+ }
+ }
+ return map;
+ }
+
+ /*
+ * @see org.roller.model.WeblogManager#getNextEntry(org.roller.pojos.WeblogEntryData)
+ */
+ public List getNextEntries(
+ WeblogEntryData current, String catName, int maxEntries)
+ throws RollerException
+ {
+ return getNextPrevEntries(current, catName, maxEntries, true);
+ }
+
+ /*
+ * @see org.roller.model.WeblogManager#getPreviousEntry(org.roller.pojos.WeblogEntryData)
+ */
+ public List getPreviousEntries(
+ WeblogEntryData current, String catName, int maxEntries)
+ throws RollerException
+ {
+ return getNextPrevEntries(current, catName, maxEntries, false);
+ }
+
+ public WeblogEntryData getNextEntry(WeblogEntryData current, String catName)
+ throws RollerException
+ {
+ WeblogEntryData entry = null;
+ List entryList = getNextEntries(current, catName, 1);
+ if (entryList != null && entryList.size() > 0)
+ {
+ entry = (WeblogEntryData)entryList.get(entryList.size()-1);
+ }
+ return entry;
+ }
+
+ public WeblogEntryData getPreviousEntry(WeblogEntryData current, String catName)
+ throws RollerException
+ {
+ WeblogEntryData entry = null;
+ List entryList = getPreviousEntries(current, catName, 1);
+ if (entryList != null && entryList.size() > 0)
+ {
+ entry = (WeblogEntryData)entryList.get(0);
+ }
+ return entry;
+ }
+
+ /**
+ * @see org.roller.model.WeblogManager#getWeblogEntriesPinnedToMain(int)
+ */
+ public List getWeblogEntriesPinnedToMain(Integer max) throws RollerException
+ {
+ return getWeblogEntries(
+ null, null, new Date(), null, null, max, Boolean.TRUE);
+ }
+
+ /**
+ * Get absolute URL to this website.
+ * @return Absolute URL to this website.
+ */
+ public String getUrl(UserData user, String contextUrl)
+ {
+ String url =
+ Utilities.escapeHTML(contextUrl + "/page/" + user.getUserName());
+ return url;
+ }
+
+}
Added: incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernateAutoPingManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernateAutoPingManagerImpl.java?rev=327589&view=auto
==============================================================================
--- incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernateAutoPingManagerImpl.java (added)
+++ incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernateAutoPingManagerImpl.java Fri Oct 21 14:27:36 2005
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2005
+ * Anil R. Gangolli. All rights reserved.
+ *
+ * Distributed with the Roller Weblogger Project under the terms of the Roller Software
+ * License
+ */
+
+package org.roller.business.hibernate;
+
+import net.sf.hibernate.Criteria;
+import net.sf.hibernate.HibernateException;
+import net.sf.hibernate.Session;
+import net.sf.hibernate.expression.Expression;
+import org.roller.RollerException;
+import org.roller.business.PersistenceStrategy;
+import org.roller.business.AutoPingManagerImpl;
+import org.roller.pojos.AutoPingData;
+import org.roller.pojos.PingTargetData;
+import org.roller.pojos.WeblogEntryData;
+import org.roller.pojos.WebsiteData;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+public class HibernateAutoPingManagerImpl extends AutoPingManagerImpl
+{
+ static final long serialVersionUID = 5420615676256979199L;
+
+ public HibernateAutoPingManagerImpl(PersistenceStrategy persistenceStrategy)
+ {
+ super(persistenceStrategy);
+ }
+
+ public void removeAutoPing(PingTargetData pingTarget, WebsiteData website) throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+ Criteria criteria = session.createCriteria(AutoPingData.class);
+ // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
+ // website.
+ criteria.add(Expression.eq("pingTarget", pingTarget));
+ criteria.add(Expression.eq("website", website));
+ List matches = criteria.list();
+ // This should have at most one element, but we remove them all regardless.
+ for (Iterator i = matches.iterator(); i.hasNext(); ) {
+ ((AutoPingData) i.next()).remove();
+ }
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ public List getAutoPingsByWebsite(WebsiteData website) throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+ Criteria criteria = session.createCriteria(AutoPingData.class);
+ // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
+ // website.
+ criteria.add(Expression.eq("website", website));
+ return criteria.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ public List getAutoPingsByTarget(PingTargetData pingTarget) throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+ Criteria criteria = session.createCriteria(AutoPingData.class);
+ // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
+ // website.
+ criteria.add(Expression.eq("pingTarget", pingTarget));
+ return criteria.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ public void removeAllAutoPings() throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+ Criteria criteria = session.createCriteria(AutoPingData.class);
+ List allAutoPings = criteria.list();
+ removeAutoPings(allAutoPings);
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ public List getCategoryRestrictions(AutoPingData autoPing) throws RollerException
+ {
+ return Collections.EMPTY_LIST;
+ }
+
+ public void setCategoryRestrictions(AutoPingData autoPing, Collection newCategories)
+ {
+ // NOT YET IMPLEMENTED
+ return;
+ }
+
+ public List getApplicableAutoPings(WeblogEntryData changedWeblogEntry) throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+ Criteria criteria = session.createCriteria(AutoPingData.class);
+ // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
+ // website.
+ criteria.add(Expression.eq("website", changedWeblogEntry.getWebsite()));
+ return criteria.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+}
Added: incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernateBookmarkManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernateBookmarkManagerImpl.java?rev=327589&view=auto
==============================================================================
--- incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernateBookmarkManagerImpl.java (added)
+++ incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernateBookmarkManagerImpl.java Fri Oct 21 14:27:36 2005
@@ -0,0 +1,326 @@
+/*
+ * Created on Jun 18, 2004
+ */
+package org.roller.business.hibernate;
+import net.sf.hibernate.Criteria;
+import net.sf.hibernate.HibernateException;
+import net.sf.hibernate.Session;
+import net.sf.hibernate.expression.Expression;
+import org.roller.RollerException;
+import org.roller.business.BookmarkManagerImpl;
+import org.roller.business.PersistenceStrategy;
+import org.roller.pojos.Assoc;
+import org.roller.pojos.BookmarkData;
+import org.roller.pojos.FolderAssoc;
+import org.roller.pojos.FolderData;
+import org.roller.pojos.WebsiteData;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Hibernate queries.
+ *
+ * @author David M Johnson
+ */
+public class HibernateBookmarkManagerImpl extends BookmarkManagerImpl
+{
+ static final long serialVersionUID = 5286654557062382772L;
+
+ private static Log mLogger =
+ LogFactory.getFactory().getInstance(HibernateBookmarkManagerImpl.class);
+
+ /**
+ * @param pstrategy
+ * @param roller
+ */
+ public HibernateBookmarkManagerImpl(PersistenceStrategy pstrategy)
+ {
+ super(pstrategy);
+ mLogger.debug("Instantiating Bookmark Manager");
+ }
+
+ /**
+ * @see org.roller.model.BookmarkManager#retrieveBookmarks(
+ * org.roller.pojos.FolderData, boolean)
+ */
+ public List retrieveBookmarks(FolderData folder, boolean subfolders)
+ throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy) mStrategy).getSession();
+ List bookmarks = new LinkedList();
+ if (subfolders)
+ {
+ // get bookmarks in subfolders
+ Criteria assocsQuery = session
+ .createCriteria(FolderAssoc.class);
+ assocsQuery.add(Expression.eq("ancestorFolder", folder));
+ Iterator assocs = assocsQuery.list().iterator();
+ while (assocs.hasNext())
+ {
+ FolderAssoc assoc = (FolderAssoc) assocs.next();
+ Criteria bookmarksQuery = session
+ .createCriteria(BookmarkData.class);
+ bookmarksQuery.add(Expression.eq("folder", assoc
+ .getFolder()));
+ Iterator bookmarkIter = bookmarksQuery.list().iterator();
+ while (bookmarkIter.hasNext())
+ {
+ BookmarkData entry = (BookmarkData) bookmarkIter.next();
+ bookmarks.add(entry);
+ }
+ }
+ }
+ // get bookmarks in folder
+ Criteria bookmarksQuery = session
+ .createCriteria(BookmarkData.class);
+ bookmarksQuery.add(Expression.eq("folder", folder));
+ Iterator bookmarkIter = bookmarksQuery.list().iterator();
+ while (bookmarkIter.hasNext())
+ {
+ BookmarkData bookmark = (BookmarkData) bookmarkIter.next();
+ bookmarks.add(bookmark);
+ }
+ return bookmarks;
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ public FolderData getRootFolder(WebsiteData website) throws RollerException
+ {
+ if (website == null)
+ throw new RollerException("website is null");
+ try
+ {
+ Session session = ((HibernateStrategy) mStrategy).getSession();
+ Criteria criteria = session.createCriteria(FolderAssoc.class);
+ criteria.createAlias("folder", "f");
+ criteria.add(Expression.eq("f.website", website));
+ criteria.add(Expression.isNull("ancestorFolder"));
+ criteria.add(Expression.eq("relation", FolderAssoc.PARENT));
+ List results = criteria.list();
+ if (results.size() > 1)
+ {
+ // Should not have more than one root
+ throw new RollerException(
+ "More than one root folder found for website "
+ + website.getId());
+ }
+ else if (results.size() == 1)
+ {
+ // Return root
+ return ((FolderAssoc) results.get(0)).getFolder();
+ }
+ return null;
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ public List getAllFolders(WebsiteData website) throws RollerException
+ {
+ if (website == null)
+ throw new RollerException("Website is null");
+
+ try
+ {
+ Session session = ((HibernateStrategy) mStrategy).getSession();
+ Criteria criteria = session.createCriteria(FolderData.class);
+ criteria.add(Expression.eq("website", website));
+ return criteria.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+
+ }
+
+ /**
+ * @see org.roller.model.BookmarkManager#isDuplicateFolderName(org.roller.pojos.FolderData)
+ */
+ public boolean isDuplicateFolderName(FolderData folder) throws RollerException
+ {
+ // ensure that no sibling folders share the same name
+ boolean isNewFolder = (folder.getId() == null);
+ FolderData parent =
+ isNewFolder ? (FolderData)folder.getNewParent() : folder.getParent();
+
+ if (null != parent)
+ {
+ List sameNames;
+ try
+ {
+ Session session = ((HibernateStrategy) mStrategy).getSession();
+ Criteria criteria = session.createCriteria(FolderAssoc.class);
+ criteria.createAlias("folder", "f");
+ criteria.add(Expression.eq("f.name", folder.getName()));
+ criteria.add(Expression.eq("ancestorFolder", parent));
+ criteria.add(Expression.eq("relation", Assoc.PARENT));
+ sameNames = criteria.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ // If we got some matches
+ if (sameNames.size() > 0)
+ {
+ // if we're saving a new folder, any matches are dups
+ if (isNewFolder) return true;
+ // otherwise it's a dup it isn't the same one (one match with the same id).
+ if (!(sameNames.size() == 1 && folder.getId().equals(((FolderAssoc)sameNames.get(0)).getFolder().getId())))
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * @see org.roller.model.BookmarkManager#getFolderParentAssoc(
+ * org.roller.pojos.FolderData)
+ */
+ public Assoc getFolderParentAssoc(FolderData folder) throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy)mStrategy).getSession();
+ Criteria criteria = session.createCriteria(FolderAssoc.class);
+ criteria.add(Expression.eq("folder", folder));
+ criteria.add(Expression.eq("relation", Assoc.PARENT));
+ List parents = criteria.list();
+ if (parents.size() > 1)
+ {
+ throw new RollerException("ERROR: more than one parent");
+ }
+ else if (parents.size() == 1)
+ {
+ return (Assoc) parents.get(0);
+ }
+ else
+ {
+ return null;
+ }
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ /**
+ * @see org.roller.model.BookmarkManager#getFolderChildAssocs(
+ * org.roller.pojos.FolderData)
+ */
+ public List getFolderChildAssocs(FolderData folder) throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy)mStrategy).getSession();
+ Criteria criteria = session.createCriteria(FolderAssoc.class);
+ criteria.add(Expression.eq("ancestorFolder", folder));
+ criteria.add(Expression.eq("relation", Assoc.PARENT));
+ return criteria.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ /**
+ * @see org.roller.model.BookmarkManager#getAllFolderDecscendentAssocs(
+ * org.roller.pojos.FolderData)
+ */
+ public List getAllFolderDecscendentAssocs(FolderData folder) throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy)mStrategy).getSession();
+ Criteria criteria = session.createCriteria(FolderAssoc.class);
+ criteria.add(Expression.eq("ancestorFolder", folder));
+ return criteria.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ /**
+ * @see org.roller.model.BookmarkManager#getFolderAncestorAssocs(
+ * org.roller.pojos.FolderData)
+ */
+ public List getFolderAncestorAssocs(FolderData folder) throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy)mStrategy).getSession();
+ Criteria criteria = session.createCriteria(FolderAssoc.class);
+ criteria.add(Expression.eq("folder", folder));
+ return criteria.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ /**
+ * @see org.roller.model.BookmarkManager#isFolderInUse(org.roller.pojos.FolderData)
+ */
+ public boolean isFolderInUse(FolderData folder) throws RollerException
+ {
+ try
+ {
+ // We consider a folder to be "in use" if it contains any bookmarks or has
+ // any children.
+
+ // We first determine the number of bookmark entries.
+ // NOTE: This seems to be an attempt to optimize, rather than just use getBookmarks(),
+ // but I'm not sure that this optimization is really worthwhile, and it ignores
+ // caching in the case that the (lazy) getBookmarks has been done already. --agangolli
+ // TODO: condider changing to just use getBookmarks().size()
+ Session session = ((HibernateStrategy)mStrategy).getSession();
+ Criteria criteria = session.createCriteria(BookmarkData.class);
+ criteria.add(Expression.eq("folder", folder));
+ criteria.setMaxResults(1);
+ int entryCount = criteria.list().size();
+
+ // Return true if we have bookmarks or (, failing that, then checking) if we have children
+ return (entryCount > 0 || folder.getFolders().size() > 0);
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ public boolean isDescendentOf(FolderData child, FolderData ancestor)
+ throws RollerException
+ {
+ boolean ret = false;
+ try
+ {
+ Session session = ((HibernateStrategy)mStrategy).getSession();
+ Criteria criteria = session.createCriteria(FolderAssoc.class);
+ criteria.add(Expression.eq("folder", child));
+ criteria.add(Expression.eq("ancestorFolder", ancestor));
+ ret = criteria.list().size() > 0;
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ return ret;
+ }
+}
Added: incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernateConfigManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernateConfigManagerImpl.java?rev=327589&view=auto
==============================================================================
--- incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernateConfigManagerImpl.java (added)
+++ incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernateConfigManagerImpl.java Fri Oct 21 14:27:36 2005
@@ -0,0 +1,67 @@
+/*
+ * Created on Jun 18, 2004
+ */
+package org.roller.business.hibernate;
+
+import net.sf.hibernate.Criteria;
+import net.sf.hibernate.HibernateException;
+import net.sf.hibernate.Session;
+
+import org.roller.RollerException;
+import org.roller.business.ConfigManagerImpl;
+import org.roller.business.PersistenceStrategy;
+import org.roller.pojos.RollerConfigData;
+
+import java.util.List;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @author David M Johnson
+ */
+public class HibernateConfigManagerImpl extends ConfigManagerImpl
+{
+ static final long serialVersionUID = -3674252864091781177L;
+
+ private static Log mLogger =
+ LogFactory.getFactory().getInstance(HibernateConfigManagerImpl.class);
+
+ /**
+ * @param strategy
+ * @param roller
+ */
+ public HibernateConfigManagerImpl(PersistenceStrategy strategy)
+ {
+ super(strategy);
+ mLogger.debug("Instantiating Config Manager");
+ }
+
+ /**
+ * Fetch all RollerConfigs and return the first one, if any.
+ * Note: there should only be one!
+ * @see org.roller.model.ConfigManager#getRollerConfig()
+ */
+ public RollerConfigData getRollerConfig() throws RollerException
+ {
+ mLogger.error("Someone is trying to use the old config!!\n"+
+ "This configuration mechanism has been deprecated\n"+
+ "You should see this message only once when you first upgrade\n"+
+ "your installation to roller 1.2\n\n"+
+ "If you continue to see this message please shoot us an email\n"+
+ "at roller-development@lists.sourceforge.net with some output\n"+
+ "from your log files.\n");
+ try
+ {
+ Session session = ((HibernateStrategy)mStrategy).getSession();
+ Criteria criteria = session.createCriteria(RollerConfigData.class);
+ criteria.setMaxResults(1);
+ List list = criteria.list();
+ return list.size()!=0 ? (RollerConfigData)list.get(0) : null;
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+}
Added: incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePersistenceSession.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePersistenceSession.java?rev=327589&view=auto
==============================================================================
--- incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePersistenceSession.java (added)
+++ incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePersistenceSession.java Fri Oct 21 14:27:36 2005
@@ -0,0 +1,38 @@
+package org.roller.business.hibernate;
+
+import net.sf.hibernate.Session;
+
+import org.roller.model.PersistenceSession;
+import org.roller.pojos.UserData;
+
+/**
+ * @author David M Johnson
+ */
+public class HibernatePersistenceSession implements PersistenceSession
+{
+ private Session session = null;
+ private UserData user = null;
+ public HibernatePersistenceSession(UserData user, Session session)
+ {
+ this.user = user;
+ this.session = session;
+ }
+ public Object getSessionObject()
+ {
+ return session;
+ }
+ public void setSessionObject(Object newSession)
+ {
+ this.session = (Session)session;
+ }
+ public UserData getUser()
+ {
+ return user;
+ }
+ public void setUser(UserData user)
+ {
+ this.user = user;
+ }
+}
+
+
Added: incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePingQueueManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePingQueueManagerImpl.java?rev=327589&view=auto
==============================================================================
--- incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePingQueueManagerImpl.java (added)
+++ incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePingQueueManagerImpl.java Fri Oct 21 14:27:36 2005
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2005
+ * Anil R. Gangolli. All rights reserved.
+ *
+ * Distributed with the Roller Weblogger Project under the terms of the Roller Software
+ * License
+ */
+
+package org.roller.business.hibernate;
+
+import net.sf.hibernate.Criteria;
+import net.sf.hibernate.HibernateException;
+import net.sf.hibernate.Session;
+import net.sf.hibernate.expression.Expression;
+import net.sf.hibernate.expression.Order;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.roller.RollerException;
+import org.roller.business.PersistenceStrategy;
+import org.roller.business.PingQueueManagerImpl;
+import org.roller.pojos.AutoPingData;
+import org.roller.pojos.PingQueueEntryData;
+import org.roller.pojos.WebsiteData;
+import org.roller.pojos.PingTargetData;
+
+import java.sql.Timestamp;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Collection;
+
+public class HibernatePingQueueManagerImpl extends PingQueueManagerImpl
+{
+ static final long serialVersionUID = -7660638707453106615L;
+
+ private static Log logger = LogFactory.getLog(HibernatePingQueueManagerImpl.class);
+
+ public HibernatePingQueueManagerImpl(PersistenceStrategy persistenceStrategy)
+ {
+ super(persistenceStrategy);
+ }
+
+ public void addQueueEntry(AutoPingData autoPing) throws RollerException
+ {
+ if (logger.isDebugEnabled()) logger.debug("Creating new ping queue entry for auto ping configuration: " + autoPing);
+
+ // First check if there is an existing ping queue entry for the same target and website
+ if (isAlreadyQueued(autoPing))
+ {
+ if (logger.isDebugEnabled()) logger.debug("A ping queue entry is already present for this ping target and website: " + autoPing);
+ return;
+ }
+
+ Timestamp now = new Timestamp(System.currentTimeMillis());
+ PingQueueEntryData pingQueueEntry =
+ new PingQueueEntryData(null, now, autoPing.getPingTarget(), autoPing.getWebsite(), 0);
+ storeQueueEntry(pingQueueEntry);
+ }
+
+ public void dropQueue() throws RollerException
+ {
+ logger.info("NOTICE Dropping all ping queue entries.");
+ List queueEntries = getAllQueueEntries();
+ removeEntries(queueEntries);
+ }
+
+ public List getAllQueueEntries() throws RollerException
+ {
+ Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+ Criteria criteria = session.createCriteria(PingQueueEntryData.class);
+ criteria.addOrder(Order.asc("entryTime"));
+ try
+ {
+ return criteria.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException("ERROR retrieving queue entries.", e);
+ }
+ }
+
+ public void removeQueueEntriesByPingTarget(PingTargetData pingTarget) throws RollerException {
+ try
+ {
+ if (logger.isDebugEnabled()) logger.debug("Removing all ping queue entries for ping target " + pingTarget);
+ Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+ Criteria criteria = session.createCriteria(PingQueueEntryData.class);
+ criteria.add(Expression.eq("pingTarget", pingTarget));
+ List queueEntries = criteria.list();
+ removeEntries(queueEntries);
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException("ERROR removing queue entries for ping target " + pingTarget, e);
+ }
+ }
+
+ public void removeQueueEntriesByWebsite(WebsiteData website) throws RollerException {
+ try
+ {
+ if (logger.isDebugEnabled()) logger.debug("Removing all ping queue entries for website " + website);
+ Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+ Criteria criteria = session.createCriteria(PingQueueEntryData.class);
+ criteria.add(Expression.eq("website", website));
+ List queueEntries = criteria.list();
+ removeEntries(queueEntries);
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException("ERROR removing queue entries for website " + website, e);
+ }
+ }
+
+ // private helper to determine if an has already been queued for the same website and ping target.
+ private boolean isAlreadyQueued(AutoPingData autoPing) throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+ Criteria criteria = session.createCriteria(PingQueueEntryData.class);
+ criteria.add(Expression.eq("pingTarget", autoPing.getPingTarget()));
+ criteria.add(Expression.eq("website", autoPing.getWebsite()));
+ return !criteria.list().isEmpty();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException("ERROR determining if preexisting queue entry is present.",e);
+ }
+ }
+
+ // Private helper to remove a collection of queue entries
+ private void removeEntries(Collection queueEntries) throws RollerException {
+ for (Iterator i = queueEntries.iterator(); i.hasNext();)
+ {
+ PingQueueEntryData pqe = (PingQueueEntryData) i.next();
+ pqe.remove();
+ }
+ }
+}
Added: incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePingTargetManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePingTargetManagerImpl.java?rev=327589&view=auto
==============================================================================
--- incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePingTargetManagerImpl.java (added)
+++ incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePingTargetManagerImpl.java Fri Oct 21 14:27:36 2005
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2005
+ * Anil R. Gangolli. All rights reserved.
+ *
+ * Distributed with the Roller Weblogger Project under the terms of the Roller Software
+ * License
+ */
+
+package org.roller.business.hibernate;
+
+import net.sf.hibernate.Criteria;
+import net.sf.hibernate.HibernateException;
+import net.sf.hibernate.Session;
+import net.sf.hibernate.expression.Expression;
+import net.sf.hibernate.expression.Order;
+import org.roller.RollerException;
+import org.roller.business.PersistenceStrategy;
+import org.roller.business.PingTargetManagerImpl;
+import org.roller.pojos.PingTargetData;
+import org.roller.pojos.WebsiteData;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Collection;
+
+public class HibernatePingTargetManagerImpl extends PingTargetManagerImpl
+{
+ static final long serialVersionUID = 121008492583382718L;
+
+ public HibernatePingTargetManagerImpl(PersistenceStrategy persistenceStrategy)
+ {
+ super(persistenceStrategy);
+ }
+
+ /**
+ * @see org.roller.model.PingTargetManager#getCommonPingTargets()
+ */
+ public List getCommonPingTargets() throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+ Criteria criteria = session.createCriteria(PingTargetData.class);
+ criteria.add(Expression.isNull("website"));
+ criteria.addOrder(Order.asc("name"));
+ return criteria.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+
+ }
+
+ /**
+ * @see org.roller.model.PingTargetManager#getCustomPingTargets(org.roller.pojos.WebsiteData)
+ */
+ public List getCustomPingTargets(WebsiteData website) throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+ Criteria criteria = session.createCriteria(PingTargetData.class);
+ criteria.add(Expression.eq("website", website));
+ criteria.addOrder(Order.asc("name"));
+ return criteria.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ /**
+ * @see org.roller.model.PingTargetManager#removeCustomPingTargets(org.roller.pojos.WebsiteData)
+ */
+ public void removeCustomPingTargets(WebsiteData website) throws RollerException
+ {
+ List customTargets = getCustomPingTargets(website);
+ removeTargets(customTargets);
+ }
+
+ /**
+ * @see org.roller.model.PingTargetManager#removeAllCustomPingTargets()
+ */
+ public void removeAllCustomPingTargets() throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+ Criteria criteria = session.createCriteria(PingTargetData.class);
+ criteria.add(Expression.isNotNull("website"));
+ List allCustomTargets = criteria.list();
+ removeTargets(allCustomTargets);
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ // Private helper to remove a collection of targets.
+ private void removeTargets(Collection customTargets)
+ throws RollerException
+ {
+ for (Iterator i = customTargets.iterator(); i.hasNext();)
+ {
+ PingTargetData pt = (PingTargetData) i.next();
+ pt.remove();
+ }
+ }
+
+}
Added: incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePlanetManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePlanetManagerImpl.java?rev=327589&view=auto
==============================================================================
--- incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePlanetManagerImpl.java (added)
+++ incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePlanetManagerImpl.java Fri Oct 21 14:27:36 2005
@@ -0,0 +1,376 @@
+/*
+ * Copyright 2005 Sun Microsystems, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.roller.business.hibernate;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import net.sf.hibernate.Criteria;
+import net.sf.hibernate.HibernateException;
+import net.sf.hibernate.Query;
+import net.sf.hibernate.Session;
+import net.sf.hibernate.expression.Expression;
+import net.sf.hibernate.expression.Order;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.roller.RollerException;
+import org.roller.business.PersistenceStrategy;
+import org.roller.business.PlanetManagerImpl;
+import org.roller.model.Roller;
+import org.roller.pojos.PlanetConfigData;
+import org.roller.pojos.PlanetEntryData;
+import org.roller.pojos.PlanetGroupData;
+import org.roller.pojos.PlanetGroupSubscriptionAssoc;
+import org.roller.pojos.PlanetSubscriptionData;
+
+
+/**
+ * Manages Planet Roller objects and entry aggregations in a database.
+ * @author Dave Johnson
+ */
+public class HibernatePlanetManagerImpl extends PlanetManagerImpl
+{
+ protected Map lastUpdatedByGroup = new HashMap();
+ protected static final String NO_GROUP = "zzz_nogroup_zzz";
+ private static Log logger =
+ LogFactory.getFactory().getInstance(HibernatePlanetManagerImpl.class);
+
+ public HibernatePlanetManagerImpl(
+ PersistenceStrategy strategy, Roller roller)
+ {
+ super(strategy, roller);
+ }
+
+ public void saveConfiguration(PlanetConfigData config)
+ throws RollerException
+ {
+ config.save();
+ }
+
+ public void saveGroup(PlanetGroupData group) throws RollerException
+ {
+ Iterator assocs = group.getGroupSubscriptionAssocs().iterator();
+ while (assocs.hasNext())
+ {
+ PlanetGroupSubscriptionAssoc assoc =
+ (PlanetGroupSubscriptionAssoc)assocs.next();
+ assoc.save();
+ }
+ group.save();
+ }
+
+ public void saveEntry(PlanetEntryData entry) throws RollerException
+ {
+ entry.save();
+ }
+
+ public void saveSubscription(PlanetSubscriptionData sub)
+ throws RollerException
+ {
+ PlanetSubscriptionData existing = getSubscription(sub.getFeedUrl());
+ if (existing == null || (existing.getId().equals(sub.getId())))
+ {
+ sub.save();
+ }
+ else
+ {
+ throw new RollerException("ERROR: duplicate feed URLs not allowed");
+ }
+ }
+
+ public PlanetConfigData getConfiguration() throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy)strategy).getSession();
+ Criteria criteria = session.createCriteria(PlanetConfigData.class);
+ criteria.setMaxResults(1);
+ List list = criteria.list();
+ return list.size()!=0 ? (PlanetConfigData)list.get(0) : null;
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ public List getGroups() throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy)strategy).getSession();
+ Criteria criteria = session.createCriteria(PlanetGroupData.class);
+ return criteria.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ public List getGroupHandles() throws RollerException
+ {
+ List handles = new ArrayList();
+ Iterator list = getGroups().iterator();
+ while (list.hasNext())
+ {
+ PlanetGroupData group = (PlanetGroupData)list.next();
+ handles.add(group.getHandle());
+ }
+ return handles;
+ }
+
+ public PlanetSubscriptionData getSubscription(String feedUrl)
+ throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy)strategy).getSession();
+ Criteria criteria =
+ session.createCriteria(PlanetSubscriptionData.class);
+ criteria.setMaxResults(1);
+ criteria.add(Expression.eq("feedUrl", feedUrl));
+ List list = criteria.list();
+ return list.size()!=0 ? (PlanetSubscriptionData)list.get(0) : null;
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ public PlanetSubscriptionData getSubscriptionById(String id)
+ throws RollerException
+ {
+ return (PlanetSubscriptionData)
+ strategy.load(id, PlanetSubscriptionData.class);
+ }
+
+ public PlanetGroupData getGroup(String handle) throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy)strategy).getSession();
+ Criteria criteria = session.createCriteria(PlanetGroupData.class);
+ criteria.setMaxResults(1);
+ criteria.add(Expression.eq("handle", handle));
+ List list = criteria.list();
+ return list.size()!=0 ? (PlanetGroupData)list.get(0) : null;
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+ public PlanetGroupData getGroupById(String id) throws RollerException
+ {
+ return (PlanetGroupData)
+ strategy.load(id, PlanetGroupData.class);
+ }
+
+ public synchronized List getAggregation(int maxEntries) throws RollerException
+ {
+ return getAggregation(null, maxEntries);
+ }
+
+ public synchronized List getAggregation(PlanetGroupData group, int maxEntries)
+ throws RollerException
+ {
+ List ret = null;
+ try
+ {
+ String groupHandle = (group == null) ? NO_GROUP : group.getHandle();
+ ret = (List)aggregationsByGroup.get(groupHandle);
+ if (ret == null)
+ {
+ long startTime = System.currentTimeMillis();
+ Session session =
+ ((HibernateStrategy)strategy).getSession();
+ if (group != null)
+ {
+ Query query = session.createQuery(
+ "select entry from org.roller.pojos.PlanetEntryData entry "
+ +"join entry.subscription.groupSubscriptionAssocs assoc "
+ +"where assoc.group=:group order by entry.published desc");
+ query.setEntity("group", group);
+ query.setMaxResults(maxEntries);
+ ret = query.list();
+ }
+ else
+ {
+ Query query = session.createQuery(
+ "select entry from org.roller.pojos.PlanetEntryData entry "
+ +"join entry.subscription.groupSubscriptionAssocs assoc "
+ +"where "
+ +"assoc.group.handle='external' or assoc.group.handle='all'"
+ +" order by entry.published desc");
+ query.setMaxResults(maxEntries);
+ ret = query.list();
+ }
+ Date retLastUpdated = null;
+ if (ret.size() > 0)
+ {
+ PlanetEntryData entry = (PlanetEntryData)ret.get(0);
+ retLastUpdated = entry.getPublished();
+ }
+ else
+ {
+ retLastUpdated = new Date();
+ }
+ aggregationsByGroup.put(groupHandle, ret);
+ lastUpdatedByGroup.put(groupHandle, retLastUpdated);
+
+ long endTime = System.currentTimeMillis();
+ logger.info("Generated aggregation in "
+ +((endTime-startTime)/1000.0)+" seconds");
+ }
+ }
+ catch (Exception e)
+ {
+ logger.error("ERROR: building aggregation for: "+group, e);
+ throw new RollerException(e);
+ }
+ return ret;
+ }
+
+ public void deleteEntry(PlanetEntryData entry) throws RollerException
+ {
+ entry.remove();
+ }
+
+ public void deleteGroup(PlanetGroupData group) throws RollerException
+ {
+ group.remove();
+ }
+
+ public void deleteSubscription(PlanetSubscriptionData sub)
+ throws RollerException
+ {
+ sub.remove();
+ }
+
+ public Iterator getAllSubscriptions()
+ {
+ try
+ {
+ Session session = ((HibernateStrategy)strategy).getSession();
+ Criteria criteria =
+ session.createCriteria(PlanetSubscriptionData.class);
+ criteria.addOrder(Order.asc("feedUrl"));
+ List list = criteria.list();
+ return list.iterator();
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(
+ "ERROR fetching subscription collection", e);
+ }
+ }
+
+ public int getSubscriptionCount() throws RollerException
+ {
+ try
+ {
+ Session session = ((HibernateStrategy)strategy).getSession();
+ Integer count = (Integer)session.createQuery(
+ "select count(*) from org.roller.pojos.PlanetSubscriptionData").uniqueResult();
+ return count.intValue();
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(
+ "ERROR fetching subscription count", e);
+ }
+ }
+
+ public synchronized List getTopSubscriptions(int max) throws RollerException
+ {
+ String groupHandle = NO_GROUP;
+ List ret = (List)topSubscriptionsByGroup.get(groupHandle);
+ if (ret == null)
+ {
+ try
+ {
+ Session session = ((HibernateStrategy)strategy).getSession();
+ Criteria criteria =
+ session.createCriteria(PlanetSubscriptionData.class);
+ criteria.setMaxResults(max);
+ criteria.addOrder(Order.desc("inboundblogs"));
+ ret = criteria.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ topSubscriptionsByGroup.put(groupHandle, ret);
+ }
+ return ret;
+ }
+
+ public synchronized List getTopSubscriptions(
+ PlanetGroupData group, int max) throws RollerException
+ {
+ String groupHandle = (group == null) ? NO_GROUP : group.getHandle();
+ List ret = (List)topSubscriptionsByGroup.get(groupHandle);
+ if (ret == null)
+ {
+ try
+ {
+ Session session = ((HibernateStrategy)strategy).getSession();
+ Query query = session.createQuery(
+ "select sub from org.roller.pojos.PlanetSubscriptionData sub "
+ +"join sub.groupSubscriptionAssocs assoc "
+ +"where "
+ +"assoc.group.handle=:groupHandle "
+ +"order by sub.inboundblogs desc");
+ query.setString("groupHandle", group.getHandle());
+ query.setMaxResults(max);
+ ret = query.list();
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ topSubscriptionsByGroup.put(groupHandle, ret);
+ }
+ return ret;
+ }
+
+ public synchronized void clearCachedAggregations()
+ {
+ aggregationsByGroup.purge();
+ topSubscriptionsByGroup.purge();
+ lastUpdatedByGroup.clear();
+ }
+
+ public Date getLastUpdated()
+ {
+ return (Date)lastUpdatedByGroup.get(NO_GROUP);
+ }
+
+ public Date getLastUpdated(PlanetGroupData group)
+ {
+ return (Date)lastUpdatedByGroup.get(group);
+ }
+}
+
Added: incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePropertiesManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePropertiesManagerImpl.java?rev=327589&view=auto
==============================================================================
--- incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePropertiesManagerImpl.java (added)
+++ incubator/roller/branches/roller_1.x/src/org/roller/business/hibernate/HibernatePropertiesManagerImpl.java Fri Oct 21 14:27:36 2005
@@ -0,0 +1,92 @@
+/*
+ * HibernatePropertiesManagerImpl.java
+ *
+ * Created on April 21, 2005, 10:40 AM
+ */
+
+package org.roller.business.hibernate;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import net.sf.hibernate.Criteria;
+import net.sf.hibernate.HibernateException;
+import net.sf.hibernate.Session;
+import net.sf.hibernate.expression.Expression;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.roller.RollerException;
+import org.roller.business.PersistenceStrategy;
+import org.roller.business.PropertiesManagerImpl;
+import org.roller.pojos.RollerPropertyData;
+
+/**
+ * A hibernate specific implementation of the properties manager.
+ *
+ * @author Allen Gilliland
+ */
+public class HibernatePropertiesManagerImpl extends PropertiesManagerImpl {
+
+ static final long serialVersionUID = -4326713177137796936L;
+
+ private static Log mLogger =
+ LogFactory.getFactory().getInstance(HibernatePropertiesManagerImpl.class);
+
+
+ /** Creates a new instance of HibernatePropertiesManagerImpl */
+ public HibernatePropertiesManagerImpl(PersistenceStrategy strategy) {
+ super(strategy);
+ mLogger.debug("Instantiating Hibernate Properties Manager");
+ }
+
+
+ /** Retrieve a single property by name */
+ public RollerPropertyData getProperty(String name) throws RollerException {
+ try
+ {
+ Session session = ((HibernateStrategy) mStrategy).getSession();
+ Criteria criteria = session.createCriteria(RollerPropertyData.class);
+ criteria.add(Expression.eq("name", name));
+ criteria.setMaxResults(1);
+ List list = criteria.list();
+ return (list.size()!= 0) ? (RollerPropertyData)list.get(0) : null;
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+ }
+
+
+ /** Retrieve all properties */
+ public Map getProperties() throws RollerException {
+
+ HashMap props = new HashMap();
+
+ try
+ {
+ Session session = ((HibernateStrategy) mStrategy).getSession();
+ Criteria criteria = session.createCriteria(RollerPropertyData.class);
+ List list = criteria.list();
+
+ // for convenience sake we are going to put the list of props
+ // into a map for users to access it. The value element of the
+ // hash still needs to be the RollerPropertyData object so that
+ // we can save the elements again after they have been updated
+ RollerPropertyData prop = null;
+ Iterator it = list.iterator();
+ while(it.hasNext()) {
+ prop = (RollerPropertyData) it.next();
+ props.put(prop.getName(), prop);
+ }
+ }
+ catch (HibernateException e)
+ {
+ throw new RollerException(e);
+ }
+
+ return props;
+ }
+
+}