Modified: incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateWeblogManagerImpl.java URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateWeblogManagerImpl.java?rev=392618&r1=392617&r2=392618&view=diff ============================================================================== --- incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateWeblogManagerImpl.java (original) +++ incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateWeblogManagerImpl.java Sat Apr 8 15:36:25 2006 @@ -3,18 +3,16 @@ */ package org.roller.business.hibernate; +import java.text.SimpleDateFormat; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.criterion.Expression; import org.hibernate.criterion.Junction; import org.hibernate.criterion.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.WeblogManagerImpl; import org.roller.model.Roller; import org.roller.model.RollerFactory; import org.roller.pojos.Assoc; @@ -25,91 +23,404 @@ import org.roller.pojos.WeblogEntryData; import org.roller.pojos.WebsiteData; import org.roller.util.StringUtils; - import java.util.ArrayList; +import java.util.Calendar; +import java.util.Comparator; import java.util.Date; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import org.apache.commons.collections.comparators.ReverseComparator; import org.hibernate.criterion.MatchMode; +import org.roller.model.WeblogManager; +import org.roller.util.DateUtil; +import org.roller.util.Utilities; + /** - * Hibernate queries, see comments in parent interface. - * @author David M Johnson + * Hibernate implementation of the WeblogManager. */ -public class HibernateWeblogManagerImpl extends WeblogManagerImpl -{ +public class HibernateWeblogManagerImpl implements WeblogManager { + static final long serialVersionUID = -3730860865389981439L; - private static Log mLogger = - LogFactory.getFactory().getInstance(HibernateWeblogManagerImpl.class); - - public HibernateWeblogManagerImpl(PersistenceStrategy strategy) - { - super(strategy); - mLogger.debug("Instantiating Weblog Manager"); + private static Log log = LogFactory.getLog(HibernateWeblogManagerImpl.class); + + private HibernatePersistenceStrategy strategy = null; + + /* inline creation of reverse comparator, anonymous inner class */ + private Comparator reverseComparator = new ReverseComparator(); + + private SimpleDateFormat formatter = DateUtil.get8charDateFormat(); + + + public HibernateWeblogManagerImpl(HibernatePersistenceStrategy strat) { + log.debug("Instantiating Hibernate Weblog Manager"); + + this.strategy = strat; + } + + + public WeblogCategoryData createWeblogCategory() { + return new WeblogCategoryData(); + } + + + 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 void storeWeblogCategory(WeblogCategoryData cat) throws RollerException { + // TODO: new method + } + + + public WeblogCategoryAssoc createWeblogCategoryAssoc() { + return new WeblogCategoryAssoc(); + } + + + public WeblogCategoryAssoc createWeblogCategoryAssoc( + WeblogCategoryData category, + WeblogCategoryData ancestor, + String relation) throws RollerException { + return new WeblogCategoryAssoc(null, category, ancestor, relation); + } + + + // TODO: backend refactorings, transaction support + public void moveWeblogCategoryContents(String srcId, String destId) + throws RollerException { + + WeblogCategoryData srcCd = + (WeblogCategoryData) this.strategy.load( + srcId, WeblogCategoryData.class); + + WeblogCategoryData destCd = + (WeblogCategoryData) this.strategy.load( + destId, WeblogCategoryData.class); + + if (destCd.descendentOf(srcCd)) { + throw new RollerException( + "ERROR cannot move parent category into it's own child"); + } + + // get all entries in category and subcats + List results = retrieveWeblogEntries(srcCd, true); + + // Loop through entries in src cat, assign them to dest cat + Iterator iter = results.iterator(); + WebsiteData website = destCd.getWebsite(); + while (iter.hasNext()) { + WeblogEntryData entry = (WeblogEntryData) iter.next(); + entry.setCategory(destCd); + entry.setWebsite(website); + entry.save(); + } + + // Make sure website's default and bloggerapi categories + // are valid after the move + + if (srcCd.getWebsite().getDefaultCategory().getId().equals(srcId) + || srcCd.getWebsite().getDefaultCategory().descendentOf(srcCd)) { + srcCd.getWebsite().setDefaultCategory(destCd); + } + + if (srcCd.getWebsite().getBloggerCategory().getId().equals(srcId) + || srcCd.getWebsite().getBloggerCategory().descendentOf(srcCd)) { + srcCd.getWebsite().setBloggerCategory(destCd); + } } + + // TODO: rename to storeComment() for consistency + public void saveComment(CommentData comment) throws RollerException { + this.strategy.storeAndCommit(comment); + } + + + public void removeComment(String id) throws RollerException { + this.strategy.removeAndCommit(id, CommentData.class); + } + + + // TODO: refactor to use a Collection rather than array[] + public void removeComments(String[] ids) throws RollerException { + + if(ids.length < 1) { + // nothing to be done + return; + } + + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + // just go through the list and remove each id + for(int i=0; i < ids.length; i++) { + this.strategy.remove(ids[i], CommentData.class); + } + + // commit changes + this.strategy.getSession().getTransaction().commit(); + } catch (HibernateException ex) { + + try { + this.strategy.getSession().getTransaction().rollback(); + } catch(HibernateException he) { + log.error("Error doing rollback", he); + } + + strategy.release(); + + throw new RollerException(ex); + } + } + + + public void removeCommentsForEntry(String entryId) throws RollerException { + + if(entryId == null) { + throw new RollerException("cannot remove null entry"); + } + + WeblogEntryData entry = retrieveWeblogEntry(entryId); + List comments = getComments( + null, // website + entry, + null, // search String + null, // startDate + null, // endDate + null, // pending + null, // approved + null, // spam + true, // reverse chrono order (not that it matters) + 0, // offset + -1); // no limit + + this.removeComments(comments); + } + + + // convenience method for removing a list of comments + private void removeComments(List comments) throws RollerException { + + if(comments.size() < 1) { + // nothing to be done + return; + } + + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + // just go through the list and remove each commentsIT + Iterator commentsIT = comments.iterator(); + while (commentsIT.hasNext()) { + this.strategy.remove((CommentData) commentsIT.next()); + } + + // commit changes + this.strategy.getSession().getTransaction().commit(); + } catch (HibernateException ex) { + + try { + this.strategy.getSession().getTransaction().rollback(); + } catch(HibernateException he) { + log.error("Error doing rollback", he); + } + + strategy.release(); + + throw new RollerException(ex); + } + } + + + public void storeWeblogEntry(WeblogEntryData entry) throws RollerException { + // TODO: new method + } + + + public void removeWeblogEntry(String id) throws RollerException { + + if(id == null) { + throw new RollerException("cannot remove null entry"); + } + + try { + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); + + // begin transaction + this.strategy.getSession().beginTransaction(); + + WeblogEntryData entry = retrieveWeblogEntry(id); + + // remove referers + Criteria refererQuery = session.createCriteria(RefererData.class); + refererQuery.add(Expression.eq("weblogEntry", entry)); + List referers = refererQuery.list(); + for (Iterator iter = referers.iterator(); iter.hasNext();) { + RefererData referer = (RefererData) iter.next(); + this.strategy.remove(referer); + } + + // remove comments + List comments = getComments( + null, // website + entry, + null, // search String + null, // startDate + null, // endDate + null, // pending + null, // approved + null, // spam + true, // reverse chrono order (not that it matters) + 0, // offset + -1); // no limit + Iterator commentsIT = comments.iterator(); + while (commentsIT.hasNext()) { + this.strategy.remove((CommentData) commentsIT.next()); + } + + // remove entry + this.strategy.remove(id, WeblogEntryData.class); + + // commit changes + this.strategy.getSession().getTransaction().commit(); + } catch (HibernateException ex) { + + try { + this.strategy.getSession().getTransaction().rollback(); + } catch(HibernateException he) { + log.error("Error doing rollback", he); + } + + strategy.release(); + + throw new RollerException(ex); + } + } + + + public void removeWeblogEntryContents(WeblogEntryData entry) + throws RollerException { + + if(entry == null) { + throw new RollerException("cannot remove null entry"); + } + + try { + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); + + // begin transaction + this.strategy.getSession().beginTransaction(); + + // remove referers + Criteria refererQuery = session.createCriteria(RefererData.class); + refererQuery.add(Expression.eq("weblogEntry", entry)); + List referers = refererQuery.list(); + for (Iterator iter = referers.iterator(); iter.hasNext();) { + RefererData referer = (RefererData) iter.next(); + this.strategy.remove(referer); + } + + // remove comments + List comments = getComments( + null, // website + entry, + null, // search String + null, // startDate + null, // endDate + null, // pending + null, // approved + null, // spam + true, // reverse chrono order (not that it matters) + 0, // offset + -1); // no limit + Iterator commentsIT = comments.iterator(); + while (commentsIT.hasNext()) { + this.strategy.remove((CommentData) commentsIT.next()); + } + + // commit changes + this.strategy.getSession().getTransaction().commit(); + } catch (HibernateException ex) { + + try { + this.strategy.getSession().getTransaction().rollback(); + } catch(HibernateException he) { + log.error("Error doing rollback", he); + } + + strategy.release(); + + throw new RollerException(ex); + } + } + + public List getNextPrevEntries( WeblogEntryData current, String catName, int maxEntries, boolean next) - throws RollerException - { - if (catName != null && catName.trim().equals("/")) - { + throws RollerException { + if (catName != null && catName.trim().equals("/")) { catName = null; } - Junction conjunction = Expression.conjunction(); + Junction conjunction = Expression.conjunction(); conjunction.add(Expression.eq("website", current.getWebsite())); conjunction.add(Expression.eq("status", WeblogEntryData.PUBLISHED)); - if (next) - { + if (next) { conjunction.add(Expression.gt("pubTime", current.getPubTime())); - } - else - { + } else { conjunction.add(Expression.lt("pubTime", current.getPubTime())); } - if (catName != null) - { - WeblogCategoryData category = - getWeblogCategoryByPath(current.getWebsite(), null, catName); - if (category != null) - { + if (catName != null) { + WeblogCategoryData category = + getWeblogCategoryByPath(current.getWebsite(), null, catName); + if (category != null) { conjunction.add(Expression.eq("category", category)); - } - else - { + } else { throw new RollerException("Cannot find category: "+catName); } } - - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); + + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogEntryData.class); criteria.addOrder(next ? Order.asc("pubTime") : Order.desc("pubTime")); criteria.add(conjunction); criteria.setMaxResults(maxEntries); List results = criteria.list(); return results; - } - catch (HibernateException e) - { + } catch (HibernateException e) { throw new RollerException(e); } } public WeblogCategoryData getRootWeblogCategory(WebsiteData website) - throws RollerException - { + throws RollerException { if (website == null) throw new RollerException("website is null"); - - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); + + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); criteria.createAlias("category","c"); @@ -117,607 +428,485 @@ criteria.add(Expression.isNull("ancestorCategory")); criteria.add(Expression.eq("relation", WeblogCategoryAssoc.PARENT)); - criteria.setMaxResults(1); + criteria.setMaxResults(1); List list = criteria.list(); return ((WeblogCategoryAssoc)list.get(0)).getCategory(); - } - catch (HibernateException e) - { + } catch (HibernateException e) { throw new RollerException(e); } } - public List getWeblogCategories(WebsiteData website, boolean includeRoot) - throws RollerException - { + public List getWeblogCategories(WebsiteData website, boolean includeRoot) + throws RollerException { if (website == null) throw new RollerException("website is null"); if (includeRoot) return getWeblogCategories(website); - - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); - Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); + + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); + Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); criteria.createAlias("category", "c"); criteria.add(Expression.eq("c.website", website)); criteria.add(Expression.isNotNull("ancestorCategory")); criteria.add(Expression.eq("relation", "PARENT")); Iterator assocs = criteria.list().iterator(); List cats = new ArrayList(); - while (assocs.hasNext()) - { + while (assocs.hasNext()) { WeblogCategoryAssoc assoc = (WeblogCategoryAssoc) assocs.next(); cats.add(assoc.getCategory()); } return cats; - } - catch (HibernateException e) - { + } catch (HibernateException e) { throw new RollerException(e); } } - public List getWeblogCategories(WebsiteData website) throws RollerException - { + public List getWeblogCategories(WebsiteData website) throws RollerException { if (website == null) throw new RollerException("website is null"); - - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); - Criteria criteria = session.createCriteria(WeblogCategoryData.class); - criteria.add(Expression.eq("website", website)); + + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); + Criteria criteria = session.createCriteria(WeblogCategoryData.class); + criteria.add(Expression.eq("website", website)); return criteria.list(); - } - catch (HibernateException e) - { + } catch (HibernateException e) { throw new RollerException(e); } } public List getWeblogEntries( - WebsiteData website, - Date startDate, - Date endDate, - String catName, - String status, - String sortby, - Integer maxEntries) throws RollerException - { - WeblogCategoryData cat = null; - if (StringUtils.isNotEmpty(catName) && website != null) - { - cat = getWeblogCategoryByPath(website, catName); - if (cat == null) catName = null; + WebsiteData website, + Date startDate, + Date endDate, + String catName, + String status, + String sortby, + Integer maxEntries) throws RollerException { + WeblogCategoryData cat = null; + if (StringUtils.isNotEmpty(catName) && website != null) { + cat = getWeblogCategoryByPath(website, catName); + if (cat == null) catName = null; } - if (catName != null && catName.trim().equals("/")) - { + if (catName != null && catName.trim().equals("/")) { catName = null; } - - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); + + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogEntryData.class); - if (website != null) - { + if (website != null) { criteria.add(Expression.eq("website", website)); - } - else - { + } else { criteria.createAlias("website","w"); criteria.add(Expression.eq("w.enabled", Boolean.TRUE)); } - - if (startDate != null) - { + + if (startDate != null) { criteria.add( - Expression.ge("pubTime", startDate)); + Expression.ge("pubTime", startDate)); } - if (endDate != null) - { + if (endDate != null) { criteria.add( - Expression.le("pubTime", endDate)); + Expression.le("pubTime", endDate)); } - - if (cat != null && website != null) - { + + if (cat != null && website != null) { criteria.add(Expression.eq("category", cat)); } - if (status != null) - { + if (status != null) { criteria.add(Expression.eq("status", status)); - } - - if (sortby != null && sortby.equals("updateTime")) - { - criteria.addOrder(Order.desc("updateTime")); } - else - { + + if (sortby != null && sortby.equals("updateTime")) { + criteria.addOrder(Order.desc("updateTime")); + } else { criteria.addOrder(Order.desc("pubTime")); } - if (maxEntries != null) - { + if (maxEntries != null) { criteria.setMaxResults(maxEntries.intValue()); - } - return criteria.list(); - } - catch (HibernateException e) - { - mLogger.error(e); + } + return criteria.list(); + } catch (HibernateException e) { + log.error(e); throw new RollerException(e); } } - public List getWeblogEntriesPinnedToMain(Integer max) throws RollerException - { - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); + public List getWeblogEntriesPinnedToMain(Integer max) throws RollerException { + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogEntryData.class); - criteria.add(Expression.eq("pinnedToMain", Boolean.TRUE)); - criteria.addOrder(Order.desc("pubTime")); - if (max != null) - { + criteria.add(Expression.eq("pinnedToMain", Boolean.TRUE)); + criteria.addOrder(Order.desc("pubTime")); + if (max != null) { criteria.setMaxResults(max.intValue()); - } + } return criteria.list(); - } - catch (HibernateException e) - { - mLogger.error(e); + } catch (HibernateException e) { + log.error(e); throw new RollerException(e); } } public WeblogEntryData getWeblogEntryByAnchor( - WebsiteData website, String anchor) throws RollerException - { + WebsiteData website, String anchor) throws RollerException { if (website == null) throw new RollerException("Website is null"); - + if (anchor == null) throw new RollerException("Anchor is null"); - - Session session = ((HibernateStrategy)mStrategy).getSession(); - Criteria criteria = session.createCriteria(WeblogEntryData.class); - criteria.add(Expression.conjunction() - .add(Expression.eq("website",website)) - .add(Expression.eq("anchor",anchor))); - criteria.addOrder(Order.desc("pubTime")); - criteria.setMaxResults(1); - try - { + + + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); + Criteria criteria = session.createCriteria(WeblogEntryData.class); + criteria.add(Expression.conjunction() + .add(Expression.eq("website",website)) + .add(Expression.eq("anchor",anchor))); + criteria.addOrder(Order.desc("pubTime")); + criteria.setMaxResults(1); + List list = criteria.list(); return list.size()!=0 ? (WeblogEntryData)list.get(0) : null; - } - catch (HibernateException e) - { + } catch (HibernateException e) { throw new RollerException(e); } } - + public Date getWeblogLastPublishTime(WebsiteData website, String catName) - throws RollerException - { + throws RollerException { WeblogCategoryData cat = null; Roller mRoller = RollerFactory.getRoller(); - if (catName != null && website != null) - { - cat = getWeblogCategoryByPath(website, null, catName); - if (cat == null) catName = null; + if (catName != null && website != null) { + cat = getWeblogCategoryByPath(website, null, catName); + if (cat == null) catName = null; } - if (catName != null && catName.trim().equals("/")) - { + if (catName != null && catName.trim().equals("/")) { catName = null; } - Session session = ((HibernateStrategy)mStrategy).getSession(); - Criteria criteria = session.createCriteria(WeblogEntryData.class); - criteria.add(Expression.eq("status", WeblogEntryData.PUBLISHED)); - criteria.add(Expression.le("pubTime", new Date())); - - try - { - if (website != null) - { + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); + Criteria criteria = session.createCriteria(WeblogEntryData.class); + criteria.add(Expression.eq("status", WeblogEntryData.PUBLISHED)); + criteria.add(Expression.le("pubTime", new Date())); + + if (website != null) { criteria.add(Expression.eq("website", website)); } - - if ( cat != null ) - { + + if ( cat != null ) { criteria.add(Expression.eq("category", cat)); } criteria.addOrder(Order.desc("pubTime")); - criteria.setMaxResults(1); + criteria.setMaxResults(1); List list = criteria.list(); - if (list.size() > 0) - { + if (list.size() > 0) { return ((WeblogEntryData)list.get(0)).getPubTime(); - } - else - { + } else { return null; } - } - catch (HibernateException e) - { + } catch (HibernateException e) { throw new RollerException(e); } } - - public void moveWeblogCategoryContents(String srcId, String destId) - throws RollerException - { - WeblogCategoryData srcCd = - (WeblogCategoryData) mStrategy.load( - srcId, WeblogCategoryData.class); - - WeblogCategoryData destCd = - (WeblogCategoryData) mStrategy.load( - destId, WeblogCategoryData.class); - - if (destCd.descendentOf(srcCd)) - { - throw new RollerException( - "ERROR cannot move parent category into it's own child"); - } - - // get all entries in category and subcats - List results = retrieveWeblogEntries(srcCd, true); - - // Loop through entries in src cat, assign them to dest cat - Iterator iter = results.iterator(); - WebsiteData website = destCd.getWebsite(); - while (iter.hasNext()) - { - WeblogEntryData entry = (WeblogEntryData) iter.next(); - entry.setCategory(destCd); - entry.setWebsite(website); - entry.save(); - } - - // Make sure website's default and bloggerapi categories - // are valid after the move - - if (srcCd.getWebsite().getDefaultCategory().getId().equals(srcId) - || srcCd.getWebsite().getDefaultCategory().descendentOf(srcCd)) - { - srcCd.getWebsite().setDefaultCategory(destCd); - } - - if (srcCd.getWebsite().getBloggerCategory().getId().equals(srcId) - || srcCd.getWebsite().getBloggerCategory().descendentOf(srcCd)) - { - srcCd.getWebsite().setBloggerCategory(destCd); - } - } - public List retrieveWeblogEntries(WeblogCategoryData cat, boolean subcats) - throws RollerException - { - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); + + + public List retrieveWeblogEntries(WeblogCategoryData cat, boolean subcats) + throws RollerException { + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); List entries = new LinkedList(); - if (subcats) - { + if (subcats) { // Get entries in subcategories - Criteria assocsQuery = - session.createCriteria(WeblogCategoryAssoc.class); - assocsQuery.add(Expression.eq("ancestorCategory", cat)); - Iterator assocs = assocsQuery.list().iterator(); - while (assocs.hasNext()) - { + Criteria assocsQuery = + session.createCriteria(WeblogCategoryAssoc.class); + assocsQuery.add(Expression.eq("ancestorCategory", cat)); + Iterator assocs = assocsQuery.list().iterator(); + while (assocs.hasNext()) { WeblogCategoryAssoc assoc = (WeblogCategoryAssoc)assocs.next(); - Criteria entriesQuery = - session.createCriteria(WeblogEntryData.class); + Criteria entriesQuery = + session.createCriteria(WeblogEntryData.class); entriesQuery.add( - Expression.eq("category", assoc.getCategory())); + Expression.eq("category", assoc.getCategory())); Iterator entryIter = entriesQuery.list().iterator(); - while (entryIter.hasNext()) - { + while (entryIter.hasNext()) { WeblogEntryData entry = (WeblogEntryData)entryIter.next(); entries.add(entry); } - } + } } // Get entries in category - Criteria entriesQuery = - session.createCriteria(WeblogEntryData.class); - entriesQuery.add(Expression.eq("category", cat)); + Criteria entriesQuery = + session.createCriteria(WeblogEntryData.class); + entriesQuery.add(Expression.eq("category", cat)); Iterator entryIter = entriesQuery.list().iterator(); - while (entryIter.hasNext()) - { + while (entryIter.hasNext()) { WeblogEntryData entry = (WeblogEntryData)entryIter.next(); entries.add(entry); } return entries; - } - catch (HibernateException e) - { - throw new RollerException(e); - } - } - - public void removeWeblogEntryContents(WeblogEntryData entry) - throws RollerException - { - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); - - // remove referers - Criteria refererQuery = session.createCriteria(RefererData.class); - refererQuery.add(Expression.eq("weblogEntry", entry)); - List entries = refererQuery.list(); - for (Iterator iter = entries.iterator(); iter.hasNext();) - { - RefererData referer = (RefererData) iter.next(); - referer.remove(); - } - removeCommentsForEntry(entry.getId()); - } - catch (HibernateException e) - { + } catch (HibernateException e) { throw new RollerException(e); } } - - public String createAnchor(WeblogEntryData entry) throws RollerException - { - try - { + + + + public String createAnchor(WeblogEntryData entry) throws RollerException { + try { // Check for uniqueness of anchor String base = entry.createAnchorBase(); String name = base; int count = 0; - - while (true) - { - if (count > 0) - { + + while (true) { + if (count > 0) { name = base + count; } - Session session = ((HibernateStrategy)mStrategy).getSession(); + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogEntryData.class); criteria.add(Expression.eq("website", entry.getWebsite())); criteria.add(Expression.eq("anchor", name)); - + List results = criteria.list(); - if (results.size() < 1) - { + if (results.size() < 1) { break; - } - else - { + } else { count++; } } return name; - } - catch (HibernateException e) - { + } catch (HibernateException e) { throw new RollerException(e); } } - - public boolean isDuplicateWeblogCategoryName(WeblogCategoryData cat) - throws RollerException - { + + public boolean isDuplicateWeblogCategoryName(WeblogCategoryData cat) + throws RollerException { // ensure that no sibling categories share the same name - WeblogCategoryData parent = - null == cat.getId() ? (WeblogCategoryData)cat.getNewParent() : cat.getParent(); - + WeblogCategoryData parent = + null == cat.getId() ? (WeblogCategoryData)cat.getNewParent() : cat.getParent(); + if (null != parent) // don't worry about root { List sameNames; - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); criteria.createAlias("category", "c"); criteria.add(Expression.eq("c.name", cat.getName())); criteria.add(Expression.eq("ancestorCategory", parent)); criteria.add(Expression.eq("relation", Assoc.PARENT)); sameNames = criteria.list(); + } catch (HibernateException e) { + throw new RollerException(e); + } + if (sameNames.size() > 1) { + return true; } - catch (HibernateException e) - { - throw new RollerException(e); - } - if (sameNames.size() > 1) - { - return true; - } } return false; } - - public boolean isWeblogCategoryInUse(WeblogCategoryData cat) - throws RollerException - { - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); + + public boolean isWeblogCategoryInUse(WeblogCategoryData cat) + throws RollerException { + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogEntryData.class); - criteria.add(Expression.eq("category", cat)); - criteria.setMaxResults(1); + criteria.add(Expression.eq("category", cat)); + criteria.setMaxResults(1); int entryCount = criteria.list().size(); - if (entryCount > 0) - { + if (entryCount > 0) { return true; } Iterator cats = cat.getWeblogCategories().iterator(); - while (cats.hasNext()) - { + while (cats.hasNext()) { WeblogCategoryData childCat = (WeblogCategoryData)cats.next(); - if (childCat.isInUse()) - { + if (childCat.isInUse()) { return true; } } - if (cat.getWebsite().getBloggerCategory().equals(cat)) - { + if (cat.getWebsite().getBloggerCategory().equals(cat)) { return true; } - if (cat.getWebsite().getDefaultCategory().equals(cat)) - { + if (cat.getWebsite().getDefaultCategory().equals(cat)) { return true; } return false; - } - catch (HibernateException e) - { + } catch (HibernateException e) { throw new RollerException(e); } } - + public boolean isDescendentOf( WeblogCategoryData child, WeblogCategoryData ancestor) - throws RollerException - { + throws RollerException { boolean ret = false; - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); criteria.add(Expression.eq("category", child)); criteria.add(Expression.eq("ancestorCategory", ancestor)); ret = criteria.list().size() > 0; - } - catch (HibernateException e) - { - throw new RollerException(e); + } catch (HibernateException e) { + throw new RollerException(e); } return ret; } - public Assoc getWeblogCategoryParentAssoc(WeblogCategoryData cat) - throws RollerException - { - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); + public Assoc getWeblogCategoryParentAssoc(WeblogCategoryData cat) + throws RollerException { + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); criteria.add(Expression.eq("category", cat)); criteria.add(Expression.eq("relation", Assoc.PARENT)); List parents = criteria.list(); - if (parents.size() > 1) - { + if (parents.size() > 1) { throw new RollerException("ERROR: more than one parent"); - } - else if (parents.size() == 1) - { + } else if (parents.size() == 1) { return (Assoc) parents.get(0); - } - else - { + } else { return null; } - } - catch (HibernateException e) - { + } catch (HibernateException e) { throw new RollerException(e); } } - - public List getWeblogCategoryChildAssocs(WeblogCategoryData cat) - throws RollerException - { - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); + + public List getWeblogCategoryChildAssocs(WeblogCategoryData cat) + throws RollerException { + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); criteria.add(Expression.eq("ancestorCategory", cat)); criteria.add(Expression.eq("relation", Assoc.PARENT)); return criteria.list(); - } - catch (HibernateException e) - { + } catch (HibernateException e) { throw new RollerException(e); } } - - public List getAllWeblogCategoryDecscendentAssocs(WeblogCategoryData cat) - throws RollerException - { - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); + + public List getAllWeblogCategoryDecscendentAssocs(WeblogCategoryData cat) + throws RollerException { + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); criteria.add(Expression.eq("ancestorCategory", cat)); return criteria.list(); - } - catch (HibernateException e) - { + } catch (HibernateException e) { throw new RollerException(e); } } - - public List getWeblogCategoryAncestorAssocs(WeblogCategoryData cat) - throws RollerException - { - try - { - Session session = ((HibernateStrategy)mStrategy).getSession(); + + public List getWeblogCategoryAncestorAssocs(WeblogCategoryData cat) + throws RollerException { + try { + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); criteria.add(Expression.eq("category", cat)); return criteria.list(); - } - catch (HibernateException e) - { + } catch (HibernateException e) { throw new RollerException(e); } } public List getComments( - WebsiteData website, - WeblogEntryData entry, - String searchString, - Date startDate, - Date endDate, - Boolean pending, - Boolean approved, - Boolean spam, - boolean reverseChrono, - int offset, - int length - ) throws RollerException { - + WebsiteData website, + WeblogEntryData entry, + String searchString, + Date startDate, + Date endDate, + Boolean pending, + Boolean approved, + Boolean spam, + boolean reverseChrono, + int offset, + int length + ) throws RollerException { + try { - Session session = ((HibernateStrategy)mStrategy).getSession(); + // begin transaction + this.strategy.getSession().beginTransaction(); + + Session session = ((HibernatePersistenceStrategy)this.strategy).getSession(); Criteria criteria = session.createCriteria(CommentData.class); if (entry != null) { criteria.add(Expression.eq("weblogEntry", entry)); - } - else if (website != null) { + } else if (website != null) { criteria.createAlias("weblogEntry","e"); criteria.add(Expression.eq("e.website", website)); } if (searchString != null) { criteria.add(Expression.disjunction() - .add(Expression.like("url", searchString, MatchMode.ANYWHERE)) - .add(Expression.like("content", searchString, MatchMode.ANYWHERE))); + .add(Expression.like("url", searchString, MatchMode.ANYWHERE)) + .add(Expression.like("content", searchString, MatchMode.ANYWHERE))); } if (startDate != null) { @@ -741,7 +930,7 @@ } if (length != -1) { - criteria.setMaxResults(offset + length); + criteria.setMaxResults(offset + length); } if (reverseChrono) { @@ -749,7 +938,7 @@ } else { criteria.addOrder(Order.asc("postTime")); } - + List comments = criteria.list(); if (offset==0 || comments.size() < offset) { return comments; @@ -761,10 +950,251 @@ return range; } catch (HibernateException e) { - mLogger.error(e); + log.error(e); throw new RollerException(e); } } + + + public WeblogCategoryData retrieveWeblogCategory(String id) + throws RollerException { + return (WeblogCategoryData) this.strategy.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 retrieveWeblogCategoryAssoc(String id) throws RollerException { + return (WeblogCategoryAssoc)this.strategy.load(id, WeblogCategoryAssoc.class); + } + + + public CommentData retrieveComment(String id) throws RollerException { + return (CommentData) this.strategy.load(id, CommentData.class); + } + + //--------------------------------------------------- WeblogEntryData CRUD + + public WeblogEntryData retrieveWeblogEntry(String id) + throws RollerException { + return (WeblogEntryData) this.strategy.load( + id, WeblogEntryData.class); + } + + + public List getWeblogEntries( + WebsiteData website, + Date startDate, + Date endDate, + String catName, + String status, + String sortby, + int offset, + int range) throws RollerException { + List filtered = new ArrayList(); + List entries = getWeblogEntries( + website, + startDate, + endDate, + catName, + status, + sortby, + new Integer(offset + range)); + if (entries.size() < offset) { + return entries; + } + for (int i=offset; i<entries.size(); i++) { + filtered.add(entries.get(i)); + } + return filtered; + } + + /** + * Gets the Date of the latest Entry publish time, before the end of today, + * for all WeblogEntries + */ + public Date getWeblogLastPublishTime(WebsiteData website) + throws RollerException { + return getWeblogLastPublishTime(website, null); + } + + 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); + } + + 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, + null, + 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; + } + + public List getNextEntries( + WeblogEntryData current, String catName, int maxEntries) + throws RollerException { + return getNextPrevEntries(current, catName, maxEntries, true); + } + + 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(0); + } + 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; + } + + /** + * Get absolute URL to this website. + * @return Absolute URL to this website. + */ + public String getUrl(WebsiteData site, String contextUrl) { + String url = + Utilities.escapeHTML(contextUrl + "/page/" + site.getHandle()); + return url; + } + + + public void release() {} + }
Modified: incubator/roller/branches/roller-newbackend/src/org/roller/business/search/operations/RebuildWebsiteIndexOperation.java URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/src/org/roller/business/search/operations/RebuildWebsiteIndexOperation.java?rev=392618&r1=392617&r2=392618&view=diff ============================================================================== --- incubator/roller/branches/roller-newbackend/src/org/roller/business/search/operations/RebuildWebsiteIndexOperation.java (original) +++ incubator/roller/branches/roller-newbackend/src/org/roller/business/search/operations/RebuildWebsiteIndexOperation.java Sat Apr 8 15:36:25 2006 @@ -104,7 +104,6 @@ Roller roller = RollerFactory.getRoller(); try { - roller.begin(); if (writer != null) { WeblogManager weblogManager = roller.getWeblogManager();
