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

📄 accountdaojdo.java

📁 Chinaxp 论坛源代码
💻 JAVA
字号:
/* * 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 java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import javax.jdo.PersistenceManager;import javax.jdo.PersistenceManagerFactory;import javax.jdo.Query;import org.apache.commons.beanutils.BeanUtils;import org.redsoft.forum.dao.Account;import org.redsoft.forum.dao.AccountDAO;import org.redsoft.forum.dao.PersistentAccount;import org.redsoft.forum.exception.AccountAlreadyExistException;import org.redsoft.forum.exception.AccountNotFoundException;import org.redsoft.forum.exception.DAOException;import org.redsoft.forum.util.Validation;/** * A JDO version of DAO interface * * @author Charles Huang * Date: 22-Feb-2004 * $Id: AccountDAOJDO.java,v 1.5 2004/04/10 19:56:57 cinc Exp $ */public class AccountDAOJDO implements AccountDAO {    private PersistenceManagerFactory pmf;    public AccountDAOJDO(PersistenceManagerFactory pmf) {        this.pmf = pmf;    }    /**     *  Add a user account     *     * @param account - A account object that contains the user info,like userName,     *                  password,email     * @exception DAOException - Thrown if a db error happens     */    public void addAccount(Account account) throws DAOException, AccountAlreadyExistException {        final PersistenceManager pManager = pmf.getPersistenceManager();        try {            findByUserName(account.getUserName());            // if found            throw new AccountAlreadyExistException();        } catch (final AccountNotFoundException accountNotFoundException) {            // if not found, go on to add account        }        pManager.currentTransaction().begin();        final PersistentAccount persistentAccount                = new PersistentAccount(account.getUserName(),                        account.getPassword(),                        account.getEmail(),                        account.isColumnWriter() );        pManager.makePersistent(persistentAccount);        pManager.currentTransaction().commit();        // Close PersistenceManager and release the resources        pManager.close();    }    /**     *  Edit a user account     *     * @param account - A account object that contains the user info,like userName,     *                  password,email     * @exception DAOException - Thrown if a db error happens     * @exception AccountNotFoundException     */    public void updateAccount(Account account) throws DAOException, AccountNotFoundException {        Validation.validateNotNull(account);        final PersistenceManager pManager = pmf.getPersistenceManager();        final PersistentAccount persistentAccount             = findAccount( pManager, account.getUserName() );        if( persistentAccount != null ){            pManager.currentTransaction().begin();            persistentAccount.setColumnWriter( account.isColumnWriter() );            persistentAccount.setPassword( account.getPassword() );            persistentAccount.setEmail( account.getEmail() );            pManager.currentTransaction().commit();            pManager.close();        } else {            // Close PersistenceManager and release the resources            pManager.close();            throw new AccountNotFoundException();        }    }    /**     * Return a persistent account given a user name. Return null if nothing found     *     * @param pManager     * @param userName     * @return     */    private PersistentAccount findAccount( final PersistenceManager pManager,                                           final String userName ){        final Query query = pManager.newQuery(PersistentAccount.class);        query.declareParameters("String value");        query.setFilter("userName == value ");        // Execute the query, result is a collection of candidate instances        final Collection result = (Collection) query.execute( userName );        final Iterator iterator = result.iterator();        if( iterator.hasNext() ){            return (PersistentAccount)iterator.next();        }else{            return null;        }    }    /**     * Find a user given a user name.     *     * ATTENTION: The returned account is an instance associated with JDO layer, don't     * use it as a value object. To pass this object to client( presentation layer ),     * we copy the content of this Persistent account to a value object account     *     * @param userName - The user name     * @return Account - A account object that contains the user info     * @exception DAOException - Thrown if a db error happens     * @exception AccountNotFoundException     */    public Account findByUserName(String userName)            throws DAOException,            AccountNotFoundException {        final PersistenceManager pManager = pmf.getPersistenceManager();        final PersistentAccount persistentAccount            = findAccount( pManager, userName );        if( persistentAccount != null ){            // Init a transient account to be returned as a value object            final Account accountToBeReturned = new Account();            // copy properties from persistent account to a value object account            try {                BeanUtils.copyProperties( accountToBeReturned, persistentAccount);            } catch (IllegalAccessException e) {                throw new DAOException(e);            } catch (InvocationTargetException e) {                throw new DAOException(e);            } finally {                pManager.close();            }            return accountToBeReturned;        } else {            // Close PersistenceManager and release the resources            pManager.close();            throw new AccountNotFoundException();        }    }    /**     *  Remove a account given a user name     *     *  @param userName - User name     */    public void removeAccount(String userName) throws DAOException {        final PersistenceManager pManager = pmf.getPersistenceManager();        // construct a jdo query with filter: "userName==" + userName        // This will return account whose userName is the given value        final Query query = pManager.newQuery(PersistentAccount.class);        query.declareParameters("String value");        query.setFilter("userName == value ");        // Execute the query, result is a collection of candidate instances        final Collection result = (Collection) query.execute(userName);        // Get the first candidate instance        final PersistentAccount account = (PersistentAccount) result.iterator().next();        pManager.currentTransaction().begin();        pManager.deletePersistent(account);        pManager.currentTransaction().commit();        pManager.close();    }    /**     * Find all columnWriters     * @return     * @throws DAOException     */    public Collection findColumnWriters() throws DAOException {        final PersistenceManager pManager = pmf.getPersistenceManager();        // construct a jdo query with filter: "userName==" + userName        // This will return account whose userName is the given value        final Query query = pManager.newQuery(PersistentAccount.class, "this.columnWriter == true");        // Execute the query, result is a collection of candidate instances        final Collection result = (Collection) query.execute();        final ArrayList columnWriters = new ArrayList();        final Iterator iterator = result.iterator();        while( iterator.hasNext() ){            final PersistentAccount persistentAccount                = (PersistentAccount)iterator.next();            // Init a transient account to be returned as a value object            final Account accountToBeReturned = new Account();            // copy properties from persistent account to a value object account            try {                BeanUtils.copyProperties( accountToBeReturned, persistentAccount);            } catch (IllegalAccessException e) {                throw new DAOException(e);            } catch (InvocationTargetException e) {                throw new DAOException(e);            }            columnWriters.add( accountToBeReturned );        }        pManager.close();        return columnWriters;    }}

⌨️ 快捷键说明

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