⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threaddaojdo.java

📁 Chinaxp 论坛源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 2003 by Redsoft Factory Inc., * Apt 738, 68 Corporate Drive, Toronto, Ontario, Canada * All rights reserved. * * This software is the confidential and proprietary information * of Redsoft Factory Inc. ("Confidential Information").  You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Redsoft Factory. */package org.redsoft.forum.dao.jdo;import org.redsoft.forum.dao.ThreadDAO;import org.redsoft.forum.dao.Thread;import org.redsoft.forum.dao.PersistentThread;import org.redsoft.forum.dao.ThreadSequence;import org.redsoft.forum.exception.DAOException;import org.redsoft.forum.exception.CategoryNotFoundException;import org.redsoft.forum.exception.ThreadNotFoundException;import org.redsoft.forum.util.Validation;import org.apache.commons.beanutils.BeanUtils;import javax.jdo.PersistenceManagerFactory;import javax.jdo.PersistenceManager;import javax.jdo.Query;import java.util.Collection;import java.util.Iterator;import java.util.ArrayList;import java.lang.reflect.InvocationTargetException;/** * A dao impletation using liberator jdo * * @author Charles Huang * Date: 23-Feb-2004 * $Id: ThreadDAOJDO.java,v 1.5 2004/02/27 23:32:48 mustang Exp $ */public class ThreadDAOJDO implements ThreadDAO {    private PersistenceManagerFactory pmf;    public ThreadDAOJDO(PersistenceManagerFactory pmf) {        this.pmf = pmf;    }    /**     * Add a thread to database     *     * @param thread - A value object thread from client( web-tier )     * @return     * @throws DAOException     */    public Thread addThread(final org.redsoft.forum.dao.Thread thread) throws DAOException {        Validation.validateNotNull(thread);        final PersistenceManager pManager = pmf.getPersistenceManager();        final PersistentThread persistentThread = new PersistentThread();        try {            thread.setId(ThreadSequence.getInstance().getThreadSequence());            // copy all the proerties from value object thread to persistence-capable thread            BeanUtils.copyProperties(persistentThread, thread);            pManager.currentTransaction().begin();            pManager.makePersistent(persistentThread);            persistentThread.setLast_update( persistentThread.getTimeStamp() );            // This is a reply thread            if (thread.getParent_id() != -1) {                PersistentThread parentThread                        = findPersistentThread(pManager, thread.getParent_id());                parentThread.setReply(parentThread.getReply() + 1);                parentThread.setLast_update( persistentThread.getTimeStamp() );            }            pManager.currentTransaction().commit();            return thread;        } catch (IllegalAccessException e) {            throw new DAOException(e);        } catch (InvocationTargetException e) {            throw new DAOException(e);        } finally{            pManager.close();        }    }    /**     *  Remove a new thread from the DB     */    public void removeThread(org.redsoft.forum.dao.Thread thread) throws DAOException {        removeThread(thread.getId());    }    /**     * Return a persistent-capable thread given an id     *     * @param pManager     * @param id     * @return PersistentThread - A persistence-capable thread or null if can't find anything     */    private PersistentThread findPersistentThread(final PersistenceManager pManager,                                                  final long id) {        final Query query                = pManager.newQuery(PersistentThread.class, "id==" + id);        final Collection result = (Collection) query.execute();        final Iterator iterator = result.iterator();        if (iterator.hasNext()) {            return (PersistentThread) iterator.next();        } else {            return null;        }    }    /**     *  Get all the top-level threads under the given category     *     *  @param category - The category     *  @return Collection - A collection of threads under the given category     */    public Collection findByCategory(int category)            throws CategoryNotFoundException, DAOException {        return findByCategory(category, 0, Integer.MAX_VALUE);    }    /**     *  Get the top-level threads under the given category from start index     *     *  @param category - The category     *  @param startIndex - The start index for retrieveing the records     *  @param length - The number of records to be tretieved     *  @return Collection - A collection of threads under the given category     */    public Collection findByCategory(int category,                                     int startIndex,                                     int length) throws CategoryNotFoundException, DAOException {        final PersistenceManager pManager = pmf.getPersistenceManager();        final ArrayList result = new ArrayList();        final Query query                = pManager.newQuery(PersistentThread.class, "category == " + category + " && parent_id == -1 " );        query.setOrdering("last_update descending");        final Collection persistentCollection                = (Collection) query.execute();        final Iterator iterator = persistentCollection.iterator();        int index = 0;        while (iterator.hasNext() && index < startIndex + length - 1 ) {            index++;            if (index < startIndex ) {                iterator.next();                continue;            }            final PersistentThread persistentThread                    = (PersistentThread) iterator.next();            final org.redsoft.forum.dao.Thread thread = new org.redsoft.forum.dao.Thread();            try {                BeanUtils.copyProperties(thread, persistentThread);                result.add(thread);            } catch (IllegalAccessException e) {                throw new DAOException(e);            } catch (InvocationTargetException e) {                throw new DAOException(e);            }        }        pManager.close();        return result;    }    /**     *  Get a thread given a unique id     *     * @return PersistentThread - The corresponding thread     */    public org.redsoft.forum.dao.Thread findByUID(long id) throws ThreadNotFoundException, DAOException {        final PersistenceManager pManager = pmf.getPersistenceManager();        final org.redsoft.forum.dao.Thread toBeReturned = new org.redsoft.forum.dao.Thread();        try {            final PersistentThread persistentThread                    = findPersistentThread(pManager, id);            if (persistentThread != null) {                // copy all the proerties from persistence-capable thread to value object thread                BeanUtils.copyProperties(toBeReturned, persistentThread);            } else {                throw new ThreadNotFoundException(id + "");            }        } catch (IllegalAccessException e) {            throw new DAOException(e);        } catch (InvocationTargetException e) {            throw new DAOException(e);        } finally {            pManager.close();        }        return toBeReturned;    }    /**     *  Get all the children threads given a id of a parent threa     *     *  @return Collection - A collection of children threads     *     */    public Collection findByParnetID(long parentID) throws ThreadNotFoundException, DAOException {        return findByParnetID(parentID, 0, Integer.MAX_VALUE);    }    /**     *  Get all the children threads given a id of a parent threa     *     *  @param parentID - The id of a parnt thread     *  @param startIndex - The number of row that we start to get the children threads     *  @param endIndex - The number of row we end geting the children threads     *  @return Collection - A collection of children threads     *     */    public Collection findByParnetID(long parentID, int startIndex, int endIndex) throws DAOException {        final PersistenceManager pManager = pmf.getPersistenceManager();        final ArrayList result = new ArrayList();        try {            final PersistentThread parentThread = this.findPersistentThread(pManager, parentID);            Thread thread = new Thread();            BeanUtils.copyProperties(thread, parentThread);            result.add(thread);            final Query query                    = pManager.newQuery(PersistentThread.class, "parent_id == " + parentID);            query.setOrdering("timeStamp ascending");            final Collection persistentCollection                    = (Collection) query.execute();            final Iterator iterator = persistentCollection.iterator();            int index = 0;            while (iterator.hasNext() && index <= endIndex) {                index++;                if (index < startIndex) {                    iterator.next();                    continue;                }                final PersistentThread persistentThread                        = (PersistentThread) iterator.next();                thread = new Thread();                BeanUtils.copyProperties(thread, persistentThread);                result.add(thread);            }

⌨️ 快捷键说明

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