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

📄 cmsdbaccess.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/defaults/master/genericsql/CmsDbAccess.java,v $
* Date   : $Date: 2002/02/14 14:34:27 $
* Version: $Revision: 1.19 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package com.opencms.defaults.master.genericsql;

import java.sql.*;
import java.util.*;
import java.io.*;
import java.io.IOException;
import java.lang.reflect.*;
import com.opencms.core.CmsException;
import com.opencms.core.I_CmsConstants;
import com.opencms.boot.CmsBase;
import com.opencms.file.*;
import com.opencms.util.*;
import com.opencms.dbpool.CmsIdGenerator;
import com.opencms.defaults.master.*;
import com.opencms.boot.*;

/**
 * This class provides methods to access the database in a generic way.
 */
public class CmsDbAccess {

    public static final String C_COS_PREFIX = "/" + I_CmsConstants.C_ROOTNAME_COS;

    /** The pool to access offline ressources */
    protected String m_poolName;

    /** The pool to access the online ressources */
    protected String m_onlinePoolName;

    /** The pool to access the backup ressources */
    protected String m_backupPoolName;

    /** The query properties for this accessmodule */
    protected Properties m_queries;

    /** The root channel of the module */
    protected String m_rootChannel = "/";

    /**
     * Make this constructor private, so noone cann call the default constructor.
     */
    private CmsDbAccess() {
    }

    /**
     * Constructs a new DbAccessObject.
     * @param poolName the pool to access offline ressources.
     * @param onlinePoolName the pool to access the online ressources.
     * @param backupPoolName the pool to access the backup ressources.
     */
    public CmsDbAccess(String poolName, String onlinePoolName, String backupPoolName) {
        m_poolName = poolName;
        m_onlinePoolName = onlinePoolName;
        m_backupPoolName = backupPoolName;
        m_queries = new Properties();
        // collect all query.properties in all packages of superclasses
        loadQueries(getClass());
        combineQueries();
    }

    /**
     * Set the root channel
     * @param newRootChannel the new value for the rootChannel
     */
    public void setRootChannel(String newRootChannel) {
        m_rootChannel = newRootChannel;
    }

    /**
     * Get the root channel
     */
    public String getRootChannel() {
        return m_rootChannel;
    }

    /**
     * Inserts a new row in the database with the dataset.
     * @param cms the CmsObject to get access to cms-ressources.
     * @param content the CmsMasterContent to write to the database.
     * @param dataset the set of data for this contentdefinition.
     * @param mediaToAdd a Vector of media to add.
     */
    public void insert(CmsObject cms, CmsMasterContent content,
                       CmsMasterDataSet dataset)
        throws CmsException {
        if(isOnlineProject(cms)) {
            // this is the onlineproject - don't write into this project directly
            throw new CmsException("Can't write to the online project", CmsException.C_NO_ACCESS);
        }
        int newMasterId = CmsIdGenerator.nextId(m_poolName, "CMS_MODULE_MASTER");
        int projectId = cms.getRequestContext().currentProject().getId();
        int currentUserId = cms.getRequestContext().currentUser().getId();
        long currentTime = new java.util.Date().getTime();
        // filling some default-values for new dataset's
        dataset.m_masterId = newMasterId;
        dataset.m_userId = currentUserId;
        dataset.m_groupId = cms.getRequestContext().currentGroup().getId();
        dataset.m_projectId = projectId;
        dataset.m_lockedInProject = projectId;
        dataset.m_state = I_CmsConstants.C_STATE_NEW;
        dataset.m_lockedBy = currentUserId;
        dataset.m_lastModifiedBy = currentUserId;
        dataset.m_dateCreated = currentTime;
        dataset.m_dateLastModified = currentTime;

        PreparedStatement stmnt = null;
        Connection con = null;
        try {
            con = DriverManager.getConnection(m_poolName);
            stmnt = sqlPrepare(con, "insert_offline");
            sqlFillValues(stmnt, content.getSubId(), dataset);
            stmnt.executeUpdate();
            // after inserting the row, we have to update media and channel tables
            updateMedia(dataset.m_masterId, dataset.m_mediaToAdd, new Vector(), new Vector());
            updateChannels(cms, dataset.m_masterId, dataset.m_channelToAdd, dataset.m_channelToDelete);
        } catch(SQLException exc) {
            throw new CmsException(CmsException.C_SQL_ERROR, exc);
        } finally {
            sqlClose(con, stmnt, null);
        }
    }

    /**
     * Inserts a new row in the database with the copied dataset.
     * @param cms the CmsObject to get access to cms-ressources.
     * @param content the CmsMasterContent to write to the database.
     * @param dataset the set of data for this contentdefinition.
     * @param mediaToAdd a Vector of media to add.
     * @param channelToAdd a Vector of channels to add.
     * @return int The id of the new content definition
     */
    public int copy(CmsObject cms, CmsMasterContent content,
                       CmsMasterDataSet dataset, Vector mediaToAdd, Vector channelToAdd)
        throws CmsException {
        if(isOnlineProject(cms)) {
            // this is the onlineproject - don't write into this project directly
            throw new CmsException("Can't write to the online project", CmsException.C_NO_ACCESS);
        }
        if(dataset.m_versionId != I_CmsConstants.C_UNKNOWN_ID) {
            // this is not the online row - it was read from history
            // don't write it!
            throw new CmsException("Can't update a cd with a backup cd ", CmsException.C_NO_ACCESS);
        }
        if(!content.isWriteable()) {
            // no write access
            throw new CmsException("Not writeable", CmsException.C_NO_ACCESS);
        }
        int newMasterId = CmsIdGenerator.nextId(m_poolName, "CMS_MODULE_MASTER");
        int projectId = cms.getRequestContext().currentProject().getId();
        int currentUserId = cms.getRequestContext().currentUser().getId();
        long currentTime = new java.util.Date().getTime();
        // filling some default-values for new dataset's
        dataset.m_masterId = newMasterId;
        dataset.m_projectId = projectId;
        dataset.m_lockedInProject = projectId;
        dataset.m_state = I_CmsConstants.C_STATE_NEW;
        dataset.m_lockedBy = currentUserId;
        dataset.m_lastModifiedBy = currentUserId;
        dataset.m_dateCreated = currentTime;
        dataset.m_dateLastModified = currentTime;

        PreparedStatement stmnt = null;
        Connection con = null;
        try {
            con = DriverManager.getConnection(m_poolName);
            stmnt = sqlPrepare(con, "insert_offline");
            sqlFillValues(stmnt, content.getSubId(), dataset);
            stmnt.executeUpdate();
            // after inserting the row, we have to update media and channel tables
            updateMedia(dataset.m_masterId, mediaToAdd, new Vector(), new Vector());
            updateChannels(cms, dataset.m_masterId, channelToAdd, new Vector());
        } catch(SQLException exc) {
            throw new CmsException(CmsException.C_SQL_ERROR, exc);
        } finally {
            sqlClose(con, stmnt, null);
        }
        return newMasterId;
    }

    /**
     * Updates the lockstate in the database.
     * @param cms the CmsObject to get access to cms-ressources.
     * @param content the CmsMasterContent to write to the database.
     * @param dataset the set of data for this contentdefinition.
     */
    public void writeLockstate(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset)
        throws CmsException {
        if(isOnlineProject(cms)) {
            // this is the onlineproject - don't write into this project directly
            throw new CmsException("Can't lock in the online project", CmsException.C_NO_ACCESS);
        }
        if(!content.isWriteable()) {
            // no write access
            throw new CmsException("Not writeable", CmsException.C_NO_ACCESS);
        }
        if(dataset.m_lockedBy <= -1) {
            // unlock the cd
            dataset.m_lockedBy = -1;
        } else {
            // lock the ressource into the current project
            dataset.m_lockedInProject = cms.getRequestContext().currentProject().getId();
        }

        PreparedStatement stmnt = null;
        Connection con = null;
        try {
            con = DriverManager.getConnection(m_poolName);
            stmnt = sqlPrepare(con, "update_lockstate_offline");
            stmnt.setInt(1, dataset.m_lockedBy);
            stmnt.setInt(2, dataset.m_lockedInProject);
            stmnt.setInt(3, dataset.m_masterId);
            stmnt.setInt(4, content.getSubId());
            stmnt.executeUpdate();
        } catch(SQLException exc) {
            throw new CmsException(CmsException.C_SQL_ERROR, exc);
        } finally {
            sqlClose(con, stmnt, null);
        }
    }

    /**
     * @param cms the CmsObject to get access to cms-ressources.
     * @param content the CmsMasterContent to write to the database.
     * @param dataset the set of data for this contentdefinition.
     */
    public void write(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset)
        throws CmsException {
        if(isOnlineProject(cms)) {
            // this is the onlineproject - don't write into this project directly
            throw new CmsException("Can't write to the online project", CmsException.C_NO_ACCESS);
        }
        if(dataset.m_versionId != I_CmsConstants.C_UNKNOWN_ID) {
            // this is not the online row - it was read from history
            // don't write it!
            throw new CmsException("Can't update a cd with a backup cd ", CmsException.C_NO_ACCESS);
        }
        // read the lockstate
        readLockstate(dataset, content.getSubId());
        if((dataset.m_lockedBy != cms.getRequestContext().currentUser().getId())) {
            // is not locked by this user
            throw new CmsException("Not locked by this user", CmsException.C_NO_ACCESS);
        }
        if(dataset.m_lockedInProject != dataset.m_projectId) {
            // not locked in this project
            throw new CmsException("Not locked in this project", CmsException.C_NO_ACCESS);
        }
        if(!content.isWriteable()) {
            // no write access
            throw new CmsException("Not writeable", CmsException.C_NO_ACCESS);
        }

        long currentTime = new java.util.Date().getTime();
        int currentUserId = cms.getRequestContext().currentUser().getId();
        // updateing some values for updated dataset
        if(dataset.m_state != I_CmsConstants.C_STATE_NEW) {
            // if the state is not new then set the state to changed
            dataset.m_state = I_CmsConstants.C_STATE_CHANGED;
        }
        dataset.m_lastModifiedBy = currentUserId;
        dataset.m_dateLastModified = currentTime;

        PreparedStatement stmnt = null;
        Connection con = null;
        try {
            con = DriverManager.getConnection(m_poolName);
            stmnt = sqlPrepare(con, "update_offline");
            int rowcounter = sqlFillValues(stmnt, content.getSubId(), dataset);
            stmnt.setInt(rowcounter++, dataset.m_masterId);
            stmnt.setInt(rowcounter++, content.getSubId());
            stmnt.executeUpdate();
            // after inserting the row, we have to update media and channel tables
            updateMedia(dataset.m_masterId, dataset.m_mediaToAdd, dataset.m_mediaToUpdate, dataset.m_mediaToDelete);
            updateChannels(cms, dataset.m_masterId, dataset.m_channelToAdd, dataset.m_channelToDelete);
        } catch(SQLException exc) {
            throw new CmsException(CmsException.C_SQL_ERROR, exc);
        } finally {
            sqlClose(con, stmnt, null);

⌨️ 快捷键说明

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