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

📄 cmsdbaccess.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/file/mySql/CmsDbAccess.java,v $
* Date   : $Date: 2001/11/14 10:09:13 $
* Version: $Revision: 1.70 $
*
* 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.file.mySql;


import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
import java.security.*;
import java.io.*;
import source.org.apache.java.io.*;
import source.org.apache.java.util.*;
import com.opencms.core.*;
import com.opencms.file.*;
import com.opencms.file.utils.*;
import com.opencms.util.*;



/**
 * This is the generic access module to load and store resources from and into
 * the database.
 *
 * @author Andreas Schouten
 * @author Michael Emmerich
 * @author Hanjo Riege
 * @author Anders Fugmann
 * @version $Revision: 1.70 $ $Date: 2001/11/14 10:09:13 $ *
 */
public class CmsDbAccess extends com.opencms.file.genericSql.CmsDbAccess implements I_CmsConstants, I_CmsLogChannels {
    /**
     * Instanciates the access-module and sets up all required modules and connections.
     * @param config The OpenCms configuration.
     * @exception CmsException Throws CmsException if something goes wrong.
     */
    public CmsDbAccess(Configurations config)
        throws CmsException {

        super(config);
    }
    /**
     * Adds a user to the database.
     *
     * @param name username
     * @param password user-password
     * @param description user-description
     * @param firstname user-firstname
     * @param lastname user-lastname
     * @param email user-email
     * @param lastlogin user-lastlogin
     * @param lastused user-lastused
     * @param flags user-flags
     * @param additionalInfos user-additional-infos
     * @param defaultGroup user-defaultGroup
     * @param address user-defauladdress
     * @param section user-section
     * @param type user-type
     *
     * @return the created user.
     * @exception thorws CmsException if something goes wrong.
     */
    public CmsUser addUser(String name, String password, String description,
                          String firstname, String lastname, String email,
                          long lastlogin, long lastused, int flags, Hashtable additionalInfos,
                          CmsGroup defaultGroup, String address, String section, int type)
        throws CmsException {
        int id = nextId(C_TABLE_USERS);
        byte[] value=null;
        PreparedStatement statement = null;
        Connection con = null;

        try {
            con = DriverManager.getConnection(m_poolName);
            // serialize the hashtable
            ByteArrayOutputStream bout= new ByteArrayOutputStream();
            ObjectOutputStream oout=new ObjectOutputStream(bout);
            oout.writeObject(additionalInfos);
            oout.close();
            value=bout.toByteArray();

            // write data to database
            statement = con.prepareStatement(m_cq.get("C_USERS_ADD"));

            statement.setInt(1,id);
            statement.setString(2,name);
            // crypt the password with MD5
            statement.setString(3, digest(password));
            statement.setString(4, digest(""));
            statement.setString(5,description);
            statement.setString(6,firstname);
            statement.setString(7,lastname);
            statement.setString(8,email);
            statement.setTimestamp(9, new Timestamp(lastlogin));
            statement.setTimestamp(10, new Timestamp(lastused));
            statement.setInt(11,flags);
            statement.setBytes(12,value);
            statement.setInt(13,defaultGroup.getId());
            statement.setString(14,address);
            statement.setString(15,section);
            statement.setInt(16,type);
            statement.executeUpdate();
         }
        catch (SQLException e){
            throw new CmsException("["+this.getClass().getName()+"]"+e.getMessage(),CmsException.C_SQL_ERROR, e);
        }
        catch (IOException e){
            throw new CmsException("[CmsAccessUserInfoMySql/addUserInformation(id,object)]:"+CmsException. C_SERIALIZATION, e);
        } finally {
            // close all db-resources
            if(statement != null) {
                 try {
                     statement.close();
                 } catch(SQLException exc) {
                     // nothing to do here
                 }
            }
            if(con != null) {
                 try {
                     con.close();
                 } catch(SQLException exc) {
                     // nothing to do here
                 }
            }
        }
        return readUser(id);
    }
    /**
     * Deletes all files in CMS_FILES without fileHeader in CMS_RESOURCES
     *
     *
     */
    protected void clearFilesTable()
      throws CmsException{
        PreparedStatement statementSearch = null;
        PreparedStatement statementDestroy = null;
        ResultSet res = null;
        Connection con = null;

        try{
            con = DriverManager.getConnection(m_poolName);
            statementSearch = con.prepareStatement(m_cq.get("C_RESOURCES_GET_LOST_ID"));
            res = statementSearch.executeQuery();
            // delete the lost fileId's
            statementDestroy = con.prepareStatement(m_cq.get("C_FILE_DELETE"));
            while (res.next() ){
                statementDestroy.setInt(1,res.getInt(m_cq.get("C_FILE_ID")));
                statementDestroy.executeUpdate();
                statementDestroy.clearParameters();
            }
        } catch (SQLException e){
            throw new CmsException("["+this.getClass().getName()+"] "+e.getMessage(),CmsException.C_SQL_ERROR, e);
        }finally {
            // close all db-resources
            if(res != null) {
                 try {
                     res.close();
                 } catch(SQLException exc) {
                     // nothing to do here
                 }
            }
            if(statementDestroy != null) {
                 try {
                     statementDestroy.close();
                 } catch(SQLException exc) {
                     // nothing to do here
                 }
            }
            if(statementSearch != null) {
                 try {
                     statementSearch.close();
                 } catch(SQLException exc) {
                     // nothing to do here
                 }
            }
            if(con != null) {
                 try {
                     con.close();
                 } catch(SQLException exc) {
                     // nothing to do here
                 }
            }
        }
    }

    /**
     * Creates a new file with the given content and resourcetype.
     *
     * @param user The user who wants to create the file.
     * @param project The project in which the resource will be used.
     * @param onlineProject The online project of the OpenCms.
     * @param filename The complete name of the new file (including pathinformation).
     * @param flags The flags of this resource.
     * @param parentId The parentId of the resource.
     * @param contents The contents of the new file.
     * @param resourceType The resourceType of the new file.
     *
     * @return file The created file.
     *
     * @exception CmsException Throws CmsException if operation was not succesful
     */
    public CmsFile createFile(CmsUser user,
                               CmsProject project,
                               CmsProject onlineProject,
                               String filename, int flags,int parentId,
                               byte[] contents, I_CmsResourceType resourceType)

        throws CmsException {
        if (filename.length() > C_MAX_LENGTH_RESOURCE_NAME){
            throw new CmsException("["+this.getClass().getName()+"] "+"Resourcename too long(>"+C_MAX_LENGTH_RESOURCE_NAME+") ",CmsException.C_BAD_NAME);
        }

        int state= C_STATE_NEW;
        // Test if the file is already there and marked as deleted.
        // If so, delete it
        try {
            readFileHeader(project.getId(),filename);
            throw new CmsException("["+this.getClass().getName()+"] ",CmsException.C_FILE_EXISTS);
        } catch (CmsException e) {
            // if the file is maked as deleted remove it!
            if (e.getType()==CmsException.C_RESOURCE_DELETED) {
                removeFile(project.getId(),filename);
                state=C_STATE_CHANGED;
                //throw new CmsException("["+this.getClass().getName()+"] ",CmsException.C_FILE_EXISTS);
            }
            if (e.getType()==CmsException.C_FILE_EXISTS) {
                throw e;
            }
        }

        String usedPool;
        String usedStatement;
        if (project.getId() == onlineProject.getId()) {
            usedPool = m_poolNameOnline;
            usedStatement = "_ONLINE";
        } else {
            usedPool = m_poolName;
            usedStatement = "";
        }
        int resourceId = nextId(m_cq.get("C_TABLE_RESOURCES"+usedStatement));
        int fileId = nextId(m_cq.get("C_TABLE_FILES"+usedStatement));

        PreparedStatement statement = null;
        PreparedStatement statementFileWrite = null;
        Connection con = null;

        try {
            con = DriverManager.getConnection(usedPool);
            statement = con.prepareStatement(m_cq.get("C_RESOURCES_WRITE"+usedStatement));
            // write new resource to the database
            statement.setInt(1,resourceId);
            statement.setInt(2,parentId);
            statement.setString(3, filename);
            statement.setInt(4,resourceType.getResourceType());
            statement.setInt(5,flags);
            statement.setInt(6,user.getId());
            statement.setInt(7,user.getDefaultGroupId());
            statement.setInt(8,project.getId());
            statement.setInt(9,fileId);
            statement.setInt(10,C_ACCESS_DEFAULT_FLAGS);
            statement.setInt(11,state);
            statement.setInt(12,C_UNKNOWN_ID);
            statement.setInt(13,resourceType.getLauncherType());
            statement.setString(14,resourceType.getLauncherClass());
            statement.setTimestamp(15,new Timestamp(System.currentTimeMillis()));
            statement.setTimestamp(16,new Timestamp(System.currentTimeMillis()));
            statement.setInt(17,contents.length);
            statement.setInt(18,user.getId());
            statement.executeUpdate();

            statementFileWrite = con.prepareStatement(m_cq.get("C_FILES_WRITE"+usedStatement));
            statementFileWrite.setInt(1,fileId);
            statementFileWrite.setBytes(2,contents);
            statementFileWrite.executeUpdate();
        } catch (SQLException e){
            throw new CmsException("["+this.getClass().getName()+"] "+e.getMessage(),CmsException.C_SQL_ERROR, e);
        }finally {
            // close all db-resources
            if(statement != null) {
                try {
                    statement.close();
                } catch(SQLException exc) {
                    // nothing to do here
                }
            }
            if(statementFileWrite != null) {
                try {
                    statementFileWrite.close();
                } catch(SQLException exc) {
                    // nothing to do here
                }
            }
            if(con != null) {
                try {
                    con.close();
                } catch(SQLException exc) {
                    // nothing to do here
                }
            }
        }
        return readFile(project.getId(),onlineProject.getId(),filename);
    }

    /**
     * Add a new group to the Cms.<BR/>
     *
     * Only the admin can do this.<P/>
     *
     * @param name The name of the new group.
     * @param description The description for the new group.
     * @int flags The flags for the new group.
     * @param name The name of the parent group (or null).
     *
     * @return Group
     *
     * @exception CmsException Throws CmsException if operation was not succesfull.
     */
  public CmsGroup createGroup (String name, String description, int flags,
                   String parent) throws CmsException
  {
        int parentId = C_UNKNOWN_ID;
        CmsGroup group = null;
        PreparedStatement statement = null;
        Connection con = null;
        try {

⌨️ 快捷键说明

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