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

📄 accountdbutils.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//    \______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////                 . . . i n   j a h i a   w e   t r u s t . . .//package org.jahia.security.accounts;import java.util.Date;import java.lang.StringBuffer;import java.sql.Connection;import java.sql.Statement;import java.sql.ResultSet;import java.sql.SQLException;import org.jahia.services.sites.SitesDBInterface;import org.jahia.security.accounts.AccountsDBInterface;import org.jahia.exceptions.database.JahiaDatabaseException;import org.jahia.security.accounts.AccountNotFoundException;import org.jahia.registries.ServicesRegistry;import org.jahia.security.accounts.Account;import org.jahia.security.accounts.AccountList;import org.jahia.utils.JahiaConsole;/** * This class is used inside the account service and should not be used directly * for any account opertion. This class implements the singelton design pattern. * * @author  Fulco Houkes * @version 1.0 */class AccountDBUtils implements AccountsDBInterface, SitesDBInterface{    /** reference to the unique class instance */    private static AccountDBUtils mInstance = null;    /** null database date string */    private static final String NULL_STRING = "-null-";    //-------------------------------------------------------------------------    // Foux     17 Apr. 2001    /** Default constructor     */    private AccountDBUtils () {    }    //-------------------------------------------------------------------------    // Foux     17 Apr. 2001    /** Return the unique instance of this class.     *     * @return     *      Return the unique instance of this class.     */    static public AccountDBUtils getInstance () {        if (mInstance == null) {            mInstance = new AccountDBUtils();        }        return mInstance;    }    //-------------------------------------------------------------------------    // Foux     17 Apr. 2001    /** Delete the specified user account from the database.     *     * @param   account     *      Reference to the account object to be deleted.     *     * @return     *      Return <code>true</code> if the specified user account could be     *      deleted successfully from the database.     *      Return <code>false</code> if the speficied account is     *      <code>null</code> or on any other error.     *     * @exception   JahiaDatabaseException     *      Throws this exception on any database failure.     */    public synchronized boolean deleteAccount (Account account)        throws  JahiaDatabaseException    {        if (account == null) {            return false;        }        // Get the database connection.        Connection connection = getDBConnection();        if (connection == null) {            throw new JahiaDatabaseException (                    "Account deletion process could not get a connection.",                    JahiaDatabaseException.CRITICAL);        }        boolean result = false;        Statement statement = null;        StringBuffer query = new StringBuffer ("");        try {            // composes the query            query.append ("DELETE FROM ");            query.append (JAHIA_ACCOUNTS);            query.append (" WHERE ");            query.append (FIELD_ACCOUNT_USER_KEY);            query.append ("='");            query.append (account.getUserKey());            query.append ("' AND ");            query.append (FIELD_ACCOUNT_SITE_KEY);            query.append ("='");            query.append (account.getSiteKey());            query.append ("'");            // executes the query            statement = connection.createStatement();            if (statement != null) {                statement.execute (query.toString());                result = true;            }        } // catches an exception in the query        catch (SQLException ex) {            throw new JahiaDatabaseException ("Cannot remove the account",                    query.toString(), ex, JahiaDatabaseException.ERROR);        }        finally {            query = null;            closeStatement (statement);            closeDBConnection (connection);        }        return result;    }    //-------------------------------------------------------------------------    // Foux     17 Apr. 2001    /** Insert the specified account into the database. The new inserted account     *  object receives automaticaly a unique ID.     *     * @param   account     *      Reference to the account object to be inserted.     *     * @return     *      Return <code>true</code> if the specified user account could be     *      inserted successfully in the database. Retrun false on any error.     *      Return <code>false</code> if the speficied account is     *      <code>null</code> or on any other error.     *     * @exception   JahiaDatabaseException     *      Throws this exception on any database failure.     */    public synchronized boolean insertAccount (Account account)        throws  JahiaDatabaseException    {        if (account == null) {            return false;        }        // Get the database connection.        Connection connection = getDBConnection();        if (connection == null) {            throw new JahiaDatabaseException (                    "Account deletion process could not get a connection.",                    JahiaDatabaseException.CRITICAL);        }        // by default the result is set to false. It will become true only if        // the account could be inserted into the database.        boolean result = false;        // create the query string        StringBuffer query = new StringBuffer ("INSERT INTO ");        query.append (JAHIA_ACCOUNTS);        query.append (" (");        query.append (FIELD_ACCOUNT_USER_KEY);        query.append (",");        query.append (FIELD_ACCOUNT_SITE_KEY);        query.append (",");        query.append (FIELD_CREATION_DATE_ACCOUNTS);        query.append (",");        query.append (FIELD_EXPIRATION_DATE_ACCOUNTS);        query.append (",");        query.append (FIELD_PWD_EXPIRATION_DATE_ACCOUNTS);        query.append (",");        query.append (FIELD_LAST_LOGIN_DATE_ACCOUNTS);        query.append (",");        query.append (FIELD_ACTIVATED_ACCOUNTS);        query.append (") VALUES ('");        // add the account values to the query string        query.append (account.getUserKey());        query.append ("','");        query.append (account.getSiteKey());        query.append ("',");        query.append ("'");        query.append (processDate (account.getCreationDate()));        query.append ("',");        query.append ("'");        query.append (processDate (account.getExpirationDate()));        query.append ("',");        query.append ("'");        query.append (processDate (account.getPasswordExpirationDate()));        query.append ("',");        query.append ("'");        query.append (processDate (account.getLastLoginDate()));        query.append ("',");        query.append (account.isActivated() ? "1" : "0");        // end the query string        query.append (")");        Statement statement = null;        try {            statement = connection.createStatement();            if (statement != null) {                statement.execute (query.toString());                result = true;            }        }        catch (SQLException ex) {            throw new JahiaDatabaseException ("Cannot insert the account",                    query.toString(), ex, JahiaDatabaseException.ERROR);        }        finally {            query = null;            closeStatement (statement);            closeDBConnection (connection);        }        return result;    }    //-------------------------------------------------------------------------    // Foux     17 Apr. 2001    /**     * Load the requested user account from the database.     *     * @param   accountID     *      Account unique identification number.     *     * @return     *      Return the reference the account if the requested account ID     *      could be found and successfully loaded from the database.     *     * @exception   JahiaDatabaseException     *      Throws this exception on any database failure.     */    public Account loadAccount (String userKey, String siteKey)        throws  JahiaDatabaseException    {        // get the DB connection        Connection connection = getDBConnection();        if (connection == null) {            throw new JahiaDatabaseException (                    "Account load process could not get a connection.",                    JahiaDatabaseException.CRITICAL);        }        Statement statement = null;        StringBuffer query = new StringBuffer ("");        Account account = null;        try {            query.append ("SELECT ");            query.append (FIELD_CREATION_DATE_ACCOUNTS);            query.append (",");            query.append (FIELD_EXPIRATION_DATE_ACCOUNTS);            query.append (",");            query.append (FIELD_PWD_EXPIRATION_DATE_ACCOUNTS);            query.append (",");            query.append (FIELD_LAST_LOGIN_DATE_ACCOUNTS);            query.append (",");            query.append (FIELD_ACTIVATED_ACCOUNTS);            query.append (" FROM ");            query.append (JAHIA_ACCOUNTS);            query.append (" WHERE ");            query.append (FIELD_ACCOUNT_USER_KEY);            query.append ("='");            query.append (account.getUserKey());            query.append ("' AND ");            query.append (FIELD_ACCOUNT_SITE_KEY);            query.append ("='");            query.append (account.getSiteKey());            query.append ("'");            statement = connection.createStatement();            if (statement != null) {                ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery (statement, query.toString());                if (rs != null) {                    while (rs.next()) {                        Date creationDate =                                extractDate (rs, FIELD_CREATION_DATE_ACCOUNTS);                        Date expirationDate =                                extractDate (rs, FIELD_EXPIRATION_DATE_ACCOUNTS);                        Date pwdExpirationDate =                                extractDate (rs, FIELD_PWD_EXPIRATION_DATE_ACCOUNTS);                        Date lastLoginDate =                                extractDate (rs, FIELD_LAST_LOGIN_DATE_ACCOUNTS);                        int activated = rs.getInt (FIELD_ACTIVATED_ACCOUNTS);                        account = new Account (userKey, siteKey, creationDate,                                expirationDate, pwdExpirationDate,                                lastLoginDate, (activated==1));                    }                    rs = null;                }            }        } catch (SQLException ex) {            throw new JahiaDatabaseException ("Cannot load the account for user ["+                userKey + "] on site [" + siteKey + "]", query.toString(), ex,                    JahiaDatabaseException.ERROR);        }        finally {            query = null;            closeDBConnection (connection);            closeStatement (statement);        }        return account;    }    //-------------------------------------------------------------------------    // Foux     17 Apr. 2001    /** Update to the account in the database. Only the expiration date, the     *  last login date and the activation data will be updated, all the other     *  data will not be updated, because they can't change.     *     * @param   account     *      Reference to the account object to be updated.     *     * @return     *      Return <code>true</code> if the specified user account could be     *      updated successfully in the database. Retrun false on any error.     *      Return <code>false</code> if the speficied account is     *      <code>null</code> or on any other error.     *     * @exception   JahiaDatabaseException     *      Throws this exception on any database failure.     */    public synchronized boolean updateAccount (Account account)        throws  JahiaDatabaseException    {        if (account == null) {            return false;        }        // Get the database connection.        Connection connection = getDBConnection();        if (connection == null) {            throw new JahiaDatabaseException (                    "Account deletion process could not get a connection.",                    JahiaDatabaseException.CRITICAL);        }        // by default the result is set to false. It will become true only if        // the account could be inserted into the database.        boolean result = false;        // create the query string        StringBuffer query = new StringBuffer ("UPDATE ");        query.append (JAHIA_ACCOUNTS);        query.append (" SET ");        query.append (FIELD_EXPIRATION_DATE_ACCOUNTS);        query.append ("='");        query.append (processDate(account.getExpirationDate()));        query.append ("',");        query.append (FIELD_PWD_EXPIRATION_DATE_ACCOUNTS);        query.append ("='");        query.append (processDate(account.getPasswordExpirationDate()));        query.append ("',");        query.append (FIELD_LAST_LOGIN_DATE_ACCOUNTS);        query.append ("='");        query.append (processDate(account.getLastLoginDate()));        query.append ("',");        if (account.isActivated()) {

⌨️ 快捷键说明

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