hibernateweblogmanagerimpl.java

来自「这个weblogging 设计得比较精巧」· Java 代码 · 共 808 行 · 第 1/2 页

JAVA
808
字号
/* * Created on Jun 16, 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 net.sf.hibernate.expression.Junction;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.WeblogManagerImpl;import org.roller.model.Roller;import org.roller.model.RollerFactory;import org.roller.pojos.Assoc;import org.roller.pojos.CommentData;import org.roller.pojos.RefererData;import org.roller.pojos.WeblogCategoryAssoc;import org.roller.pojos.WeblogCategoryData;import org.roller.pojos.WeblogEntryData;import org.roller.pojos.WebsiteData;import org.roller.util.StringUtils;import java.util.ArrayList;import java.util.Date;import java.util.Iterator;import java.util.LinkedList;import java.util.List;/** * Hibernate queries. * @author David M Johnson */public class HibernateWeblogManagerImpl extends WeblogManagerImpl{    static final long serialVersionUID = -3730860865389981439L;        private static Log mLogger =        LogFactory.getFactory().getInstance(HibernateWeblogManagerImpl.class);    /**     * @param strategy     * @param roller     */    public HibernateWeblogManagerImpl(PersistenceStrategy strategy)    {        super(strategy);        mLogger.debug("Instantiating Weblog Manager");    }        public List getComments(String entryId, boolean nospam) throws RollerException    {        if (entryId == null)            throw new RollerException("entryId is null");        try        {            Session session = ((HibernateStrategy)mStrategy).getSession();            Criteria criteria = session.createCriteria(CommentData.class);            criteria.createAlias("weblogEntry","e");            criteria.add(Expression.eq("e.id", entryId));            if (nospam)            {                criteria.add(                    Expression.not(Expression.eq("spam", Boolean.TRUE)));            }            criteria.addOrder(Order.asc("postTime"));                        return criteria.list();        }        catch (HibernateException e)        {            throw new RollerException(e);        }    }    /*      * @see org.roller.model.WeblogManager#getNextEntry(org.roller.pojos.WeblogEntryData)     */    public List getNextPrevEntries(            WeblogEntryData current, String catName, int maxEntries, boolean next)        throws RollerException    {        if (catName != null && catName.trim().equals("/"))        {            catName = null;        }        Junction conjunction = Expression.conjunction();                conjunction.add(Expression.eq("website", current.getWebsite()));        conjunction.add(Expression.eq("publishEntry", Boolean.TRUE));                if (next)        {            conjunction.add(Expression.gt("pubTime", current.getPubTime()));        }        else        {            conjunction.add(Expression.lt("pubTime", current.getPubTime()));        }                if (catName != null)        {            WeblogCategoryData category =                 getWeblogCategoryByPath(current.getWebsite(), null, catName);            if (category != null)            {                conjunction.add(Expression.eq("category", category));            }            else            {                throw new RollerException("Cannot find category: "+catName);            }        }        try        {            Session session = ((HibernateStrategy)mStrategy).getSession();            Criteria criteria = session.createCriteria(WeblogEntryData.class);            criteria.addOrder(Order.desc("pubTime"));            criteria.add(conjunction);            criteria.setMaxResults(maxEntries);            List results = criteria.list();            return results;        }        catch (HibernateException e)        {            throw new RollerException(e);        }    }        /**     * @see org.roller.model.WeblogManager#getRootWeblogCategory(org.roller.pojos.WebsiteData)     */    public WeblogCategoryData getRootWeblogCategory(WebsiteData website)        throws RollerException    {        if (website == null)            throw new RollerException("website is null");        try        {            Session session = ((HibernateStrategy)mStrategy).getSession();            Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class);            criteria.createAlias("category","c");                        criteria.add(Expression.eq("c.website", website));            criteria.add(Expression.isNull("ancestorCategory"));            criteria.add(Expression.eq("relation", WeblogCategoryAssoc.PARENT));                        criteria.setMaxResults(1);                            List list = criteria.list();            return ((WeblogCategoryAssoc)list.get(0)).getCategory();        }        catch (HibernateException e)        {            throw new RollerException(e);        }    }        /**      * @see org.roller.model.WeblogManager#getWeblogCategories(     * org.roller.pojos.WebsiteData, boolean)     */    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);             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())            {                WeblogCategoryAssoc assoc = (WeblogCategoryAssoc) assocs.next();                cats.add(assoc.getCategory());            }            return cats;        }        catch (HibernateException e)        {            throw new RollerException(e);        }    }        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));                        return criteria.list();        }        catch (HibernateException e)        {            throw new RollerException(e);        }    }        /**      * @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,                    Boolean pinned) 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("/"))        {            catName = null;        }                            try        {            Session session = ((HibernateStrategy)mStrategy).getSession();            Criteria criteria = session.createCriteria(WeblogEntryData.class);                        if (website != null)            {                criteria.add(Expression.eq("website", website));            }            else             {                criteria.createAlias("website","w");                criteria.add(Expression.eq("w.isEnabled", Boolean.TRUE));            }                if (startDate != null)            {                criteria.add(                    Expression.ge("pubTime", startDate));            }                        if (endDate != null)            {                criteria.add(                    Expression.le("pubTime", endDate));            }                                        if (cat != null && website != null)            {                criteria.add(Expression.eq("category", cat));            }                        if (status != null && status.equals(DRAFT_ONLY))            {                criteria.add(                    Expression.eq("publishEntry", Boolean.FALSE));            }                    else if (status != null && status.equals(PUB_ONLY))            {                criteria.add(                    Expression.eq("publishEntry", Boolean.TRUE));            }            if (pinned != null)            {                criteria.add(Expression.eq("pinnedToMain", pinned));            }                                        criteria.addOrder(Order.desc("pubTime"));                        if (maxEntries != null)             {                criteria.setMaxResults(maxEntries.intValue());            }                        return criteria.list();        }        catch (HibernateException e)        {            mLogger.error(e);            throw new RollerException(e);        }    }        /**      * Use Hibernate directly because Roller's Query API does too much allocation.     */    public WeblogEntryData getWeblogEntryByAnchor(                    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        {            List list = criteria.list();            return list.size()!=0 ? (WeblogEntryData)list.get(0) : null;        }        catch (HibernateException e)        {            throw new RollerException(e);        }    }    /**     * Gets the Date of the latest Entry publish time.     *     * @param userName User name of weblog or null for all users     * @param catName Category name of posts or null for all categories     * @return Date Of last publish time     * @throws RollerException     */    public Date getWeblogLastPublishTime( String userName, String catName )        throws RollerException    {        WeblogCategoryData cat = null;        Roller mRoller = RollerFactory.getRoller();        if (userName != null)         {            WebsiteData website = mRoller.getUserManager().getWebsite(userName);            if (catName != null && website != null)            {                   cat = getWeblogCategoryByPath(website, null, catName);               if (cat == null) catName = null;            }            if (catName != null && catName.trim().equals("/"))            {                catName = null;            }        }                Session session = ((HibernateStrategy)mStrategy).getSession();        Criteria criteria = session.createCriteria(WeblogEntryData.class);        criteria.add(Expression.eq("publishEntry", Boolean.TRUE));        criteria.add(Expression.le("pubTime", new Date()));        try        {            if ( userName != null )            {                WebsiteData website = mRoller.getUserManager().getWebsite(userName);                criteria.add(Expression.eq("website", website));            }            if ( cat != null )            {                criteria.add(Expression.eq("category", cat));            }                        criteria.addOrder(Order.desc("pubTime"));            criteria.setMaxResults(1);                    List list = criteria.list();            if (list.size() > 0)            {                return ((WeblogEntryData)list.get(0)).getPubTime();            }            else            {                return null;            }        }        catch (HibernateException e)        {            throw new RollerException(e);        }    }    public void moveWeblogCategoryContents(String srcId, String destId)        throws RollerException

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?