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

📄 cmsdbaccess.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            con = DriverManager.getConnection(m_poolName);
            // get the id of the parent group if nescessary
            if ((parent != null) && (!"".equals (parent))){
                parentId = readGroup (parent).getId ();
            }
            // create statement
            statement = con.prepareStatement(m_cq.get("C_GROUPS_CREATEGROUP"));
            // write new group to the database
            statement.setInt (1, nextId (C_TABLE_GROUPS));
            statement.setInt (2, parentId);
            statement.setString (3, name);
            statement.setString (4, description);
            statement.setInt (5, flags);
            statement.executeUpdate ();

            // create the user group by reading it from the database.
            // this is nescessary to get the group id which is generated in the
            // database.
            group = readGroup (name);
        } 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(con != null) {
                try {
                    con.close();
                } catch(SQLException exc) {
                    // nothing to do here
                }
            }
        }
        return group;
    }

    /**
     * Deletes all properties for a project.
     *
     * @param project The project to delete.
     *
     * @exception CmsException Throws CmsException if operation was not succesful
     */
    public void deleteProjectProperties(CmsProject project) throws CmsException {
        // get all resources of the project
        Vector resources = readResources(project);
        for (int i = 0; i < resources.size(); i++) {
            // delete the properties for each resource in project
            deleteAllProperties(project.getId(),(CmsResource) resources.elementAt(i));
        }
    }

    /**
     * Destroys this access-module
     * @exception throws CmsException if something goes wrong.
     */
    public void destroy() throws CmsException {
        try {
            ((com.opencms.dbpool.CmsDriver) DriverManager.getDriver(m_poolName)).destroy();
        } catch(SQLException exc) {
            // destroy not possible - ignoring the exception
        }

        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
            A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_INIT, "[CmsDbAccess] shutdown complete.");
        }
    }

    /**
     * Private method to init all default-resources
     */
    protected void fillDefaults() throws CmsException {
        // set the groups
        CmsGroup guests = createGroup(C_GROUP_GUEST, "the guest-group", C_FLAG_ENABLED, null);
        CmsGroup administrators = createGroup(C_GROUP_ADMIN, "the admin-group", C_FLAG_ENABLED | C_FLAG_GROUP_PROJECTMANAGER, null);
        CmsGroup users = createGroup(C_GROUP_USERS, "the users-group to access the workplace", C_FLAG_ENABLED | C_FLAG_GROUP_ROLE | C_FLAG_GROUP_PROJECTCOWORKER, C_GROUP_GUEST);
        CmsGroup projectleader = createGroup(C_GROUP_PROJECTLEADER, "the projectmanager-group", C_FLAG_ENABLED | C_FLAG_GROUP_PROJECTMANAGER | C_FLAG_GROUP_PROJECTCOWORKER | C_FLAG_GROUP_ROLE, users.getName());

        // add the users
        CmsUser guest = addUser(C_USER_GUEST, "", "the guest-user", "", "", "", 0, 0, C_FLAG_ENABLED, new Hashtable(), guests, "", "", C_USER_TYPE_SYSTEMUSER);
        CmsUser admin = addUser(C_USER_ADMIN, "admin", "the admin-user", "", "", "", 0, 0, C_FLAG_ENABLED, new Hashtable(), administrators, "", "", C_USER_TYPE_SYSTEMUSER);
        addUserToGroup(guest.getId(), guests.getId());
        addUserToGroup(admin.getId(), administrators.getId());
        writeTaskType(1, 0, "../taskforms/adhoc.asp", "Ad-Hoc", "30308", 1, 1);
        // create the online project
        CmsTask task = createTask(0, 0, 1, // standart project type,
                    admin.getId(), admin.getId(), administrators.getId(), C_PROJECT_ONLINE, new java.sql.Timestamp(new java.util.Date().getTime()), new java.sql.Timestamp(new java.util.Date().getTime()), C_TASK_PRIORITY_NORMAL);
        CmsProject online = createProject(admin, guests, projectleader, task, C_PROJECT_ONLINE, "the online-project", C_FLAG_ENABLED, C_PROJECT_TYPE_NORMAL);

        // create the root-folder for the online project
        int siteRootId = 0;
        CmsFolder rootFolder = createFolder(admin, online, C_UNKNOWN_ID, C_UNKNOWN_ID, C_ROOT, 0);
        rootFolder.setGroupId(users.getId());
        rootFolder.setState(C_STATE_UNCHANGED);
        writeFolder(online, rootFolder, false);
        rootFolder = createFolder(admin, online, rootFolder.getResourceId(), C_UNKNOWN_ID, C_DEFAULT_SITE+C_ROOT, 0);
        rootFolder.setGroupId(users.getId());
        rootFolder.setState(C_STATE_UNCHANGED);
        writeFolder(online, rootFolder, false);
        siteRootId = rootFolder.getResourceId();
        rootFolder = createFolder(admin, online, siteRootId, C_UNKNOWN_ID, C_DEFAULT_SITE+C_ROOTNAME_VFS+C_ROOT, 0);
        rootFolder.setGroupId(users.getId());
        rootFolder.setState(C_STATE_UNCHANGED);
        writeFolder(online, rootFolder, false);
        rootFolder = createFolder(admin, online, siteRootId, C_UNKNOWN_ID, C_DEFAULT_SITE+C_ROOTNAME_COS+C_ROOT, 0);
        rootFolder.setGroupId(users.getId());
        rootFolder.setState(C_STATE_UNCHANGED);
        writeFolder(online, rootFolder, false);
        // create the setup project
        task = createTask(0, 0, 1, admin.getId(), admin.getId(), administrators.getId(),
                                    "_setupProject", new java.sql.Timestamp(new java.util.Date().getTime()),
                                    new java.sql.Timestamp(new java.util.Date().getTime()),
                                    C_TASK_PRIORITY_NORMAL);

        CmsProject setup = createProject(admin, administrators, administrators, task, "_setupProject",
                                           "the project for setup", C_FLAG_ENABLED, C_PROJECT_TYPE_TEMPORARY);

        // create the root-folder for the offline project
        rootFolder = createFolder(admin, setup, C_UNKNOWN_ID, C_UNKNOWN_ID, C_ROOT, 0);
        rootFolder.setGroupId(users.getId());
        rootFolder.setState(C_STATE_UNCHANGED);
        writeFolder(setup, rootFolder, false);
        rootFolder = createFolder(admin, setup, rootFolder.getResourceId(), C_UNKNOWN_ID, C_DEFAULT_SITE+C_ROOT, 0);
        rootFolder.setGroupId(users.getId());
        rootFolder.setState(C_STATE_UNCHANGED);
        writeFolder(setup, rootFolder, false);
        siteRootId = rootFolder.getResourceId();
        rootFolder = createFolder(admin, setup, siteRootId, C_UNKNOWN_ID, C_DEFAULT_SITE+C_ROOTNAME_VFS+C_ROOT, 0);
        rootFolder.setGroupId(users.getId());
        rootFolder.setState(C_STATE_UNCHANGED);
        writeFolder(setup, rootFolder, false);
        rootFolder = createFolder(admin, setup, siteRootId, C_UNKNOWN_ID, C_DEFAULT_SITE+C_ROOTNAME_COS+C_ROOT, 0);
        rootFolder.setGroupId(users.getId());
        rootFolder.setState(C_STATE_UNCHANGED);
        writeFolder(setup, rootFolder, false);
    }

    /**
     * Finds an agent for a given role (group).
     * @param roleId The Id for the role (group).
     *
     * @return A vector with the tasks
     *
     * @exception CmsException Throws CmsException if something goes wrong.
     */
    protected int findAgent(int roleid)
        throws CmsException {
        return super.findAgent(roleid);
    }

/**
 * retrieve the correct instance of the queries holder.
 * This method should be overloaded if other query strings should be used.
 */
protected com.opencms.file.genericSql.CmsQueries getQueries()
{
    return new com.opencms.file.mySql.CmsQueries();
}

/**
 * Reads a file from the Cms.<BR/>
 *
 * @param projectId The Id of the project in which the resource will be used.
 * @param onlineProjectId The online projectId of the OpenCms.
 * @param filename The complete name of the new file (including pathinformation).
 *
 * @return file The read file.
 *
 * @exception CmsException Throws CmsException if operation was not succesful
 */
public CmsFile readFile(int projectId, int onlineProjectId, String filename) throws CmsException {
    CmsFile file = null;
    PreparedStatement statement = null;
    ResultSet res = null;
    Connection con = null;
    String usedPool;
    String usedStatement;
    if (projectId == onlineProjectId){
        usedPool = m_poolNameOnline;
        usedStatement = "_ONLINE";
    } else {
        usedPool = m_poolName;
        usedStatement = "";
    }
    try {
        con = DriverManager.getConnection(usedPool);
        statement = con.prepareStatement(m_cq.get("C_FILES_READ"+usedStatement));
        statement.setString(1, filename);
        statement.setInt(2, projectId);
        res = statement.executeQuery();
        if (res.next()) {
            int resId = res.getInt(m_cq.get("C_RESOURCES_RESOURCE_ID"));
            int parentId = res.getInt(m_cq.get("C_RESOURCES_PARENT_ID"));
            int resType = res.getInt(m_cq.get("C_RESOURCES_RESOURCE_TYPE"));
            int resFlags = res.getInt(m_cq.get("C_RESOURCES_RESOURCE_FLAGS"));
            int userId = res.getInt(m_cq.get("C_RESOURCES_USER_ID"));
            int groupId = res.getInt(m_cq.get("C_RESOURCES_GROUP_ID"));
            int fileId = res.getInt(m_cq.get("C_RESOURCES_FILE_ID"));
            int accessFlags = res.getInt(m_cq.get("C_RESOURCES_ACCESS_FLAGS"));
            int state = res.getInt(m_cq.get("C_RESOURCES_STATE"));
            int lockedBy = res.getInt(m_cq.get("C_RESOURCES_LOCKED_BY"));
            int launcherType = res.getInt(m_cq.get("C_RESOURCES_LAUNCHER_TYPE"));
            String launcherClass = res.getString(m_cq.get("C_RESOURCES_LAUNCHER_CLASSNAME"));
            long created = SqlHelper.getTimestamp(res, m_cq.get("C_RESOURCES_DATE_CREATED")).getTime();
            long modified = SqlHelper.getTimestamp(res, m_cq.get("C_RESOURCES_DATE_LASTMODIFIED")).getTime();
            int modifiedBy = res.getInt(m_cq.get("C_RESOURCES_LASTMODIFIED_BY"));
            int resSize = res.getInt(m_cq.get("C_RESOURCES_SIZE"));
            byte[] content = res.getBytes(m_cq.get("C_RESOURCES_FILE_CONTENT"));
            int resProjectId = res.getInt(m_cq.get("C_RESOURCES_PROJECT_ID"));
            int lockedInProject = res.getInt("LOCKED_IN_PROJECT");
            file = new CmsFile(resId, parentId, fileId, filename, resType, resFlags, userId,
                               groupId, resProjectId, accessFlags, state, lockedBy, launcherType,
                               launcherClass, created, modified, modifiedBy, content, resSize, lockedInProject);
            // check if this resource is marked as deleted
            if (file.getState() == C_STATE_DELETED) {
                throw new CmsException("["+this.getClass().getName()+"] "+file.getAbsolutePath(),CmsException.C_RESOURCE_DELETED);
            }
        } else {
            throw new CmsException("[" + this.getClass().getName() + "] " + filename, CmsException.C_NOT_FOUND);
        }
    } catch (SQLException e) {
        throw new CmsException("[" + this.getClass().getName() + "] " + e.getMessage(), CmsException.C_SQL_ERROR, e);
    } catch (CmsException ex) {
        throw ex;
    } catch (Exception exc) {
        throw new CmsException("readFile " + exc.getMessage(), CmsException.C_UNKNOWN_EXCEPTION, exc);
    } finally {
            // close all db-resources
            if(res != null) {
                 try {
                     res.close();
                 } catch(SQLException exc) {
                     // nothing to do here
                 }
            }
            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 file;
}

/**
 * Reads a file from the Cms.<BR/>
 *
 * @param projectId The Id of the project in which the resource will be used.
 * @param onlineProjectId The online projectId of the OpenCms.
 * @param filename The complete name of the new file (including pathinformation).
 *
 * @return file The read file.
 *
 * @exception CmsException Throws CmsException if operation was not succesful
 */
public CmsFile readFile(int projectId, int onlineProjectId, String filename, boolean includeDeleted) throws CmsException {
    CmsFile file = null;
    PreparedStatement statement = null;
    ResultSet res = null;
    Connection con = null;
    String usedPool;
    String usedStatement;
    if (projectId == onlineProjectId){
        usedPool = m_poolNameOnline;
        usedStatement = "_ONLINE";
    } else {
        usedPool = m_poolName;
        usedStatement = "";
    }
    try {
        con = DriverManager.getConnection(usedPool);
        statement = con.prepareStatement(m_cq.get("C_FILES_READ"+usedStatement));
        statement.setString(1, filename);
        statement.setInt(2, projectId);
        res = statement.executeQuery();
        if (res.next()) {
            int resId = res.getInt(m_cq.get("C_RESOURCES_RESOURCE_ID"));
            int parentId = res.getInt(m_cq.get("C_RESOURCES_PARENT_ID"));
            int resType = res.getInt(m_cq.get("C_RESOURCES_RESOURCE_TYPE"));
            int resFlags = res.getInt(m_cq.get("C_RESOURCES_RESOURCE_FLAGS"));
            int userId = res.getInt(m_cq.get("C_RESOURCES_USER_ID"));
            int groupId = res.getInt(m_cq.get("C_RESOURCES_GROUP_ID"));
            int fileId = res.getInt(m_cq.get("C_RESOURCES_FILE_ID"));
            int accessFlags = res.getInt(m_cq.get("C_RESOURCES_ACCESS_FLAGS"));
            int state = res.getInt(m_cq.get("C_RESOURCES_STATE"));
            int lockedBy = res.getInt(m_cq.get("C_RESOURCES_LOCKED_BY"));
            int launcherType = res.getInt(m_cq.get("C_RESOURCES_LAUNCHER_TYPE"));
            String launcherClass = res.getString(m_cq.get("C_RESOURCES_LAUNCHER_CLASSNAME"));
            long created = SqlHelper.getTimestamp(res, m_cq.get("C_RESOURCES_DATE_CREATED")).getTime();
            long modified = SqlHelper.getTimestamp(res, m_cq.get("C_RESOURCES_DATE_LASTMODIFIED")).getTime();
            int modifiedBy = res.getInt(m_cq.get("C_RESOURCES_LASTMODIFIED_BY"));
            int resSize = res.getInt(m_cq.get("C_RESOURCES_SIZE"));
            byte[] content = res.getBytes(m_cq.get("C_RESOURCES_FILE_CONTENT"));
            int resProjectId = res.getInt(m_cq.get("C_RESOURCES_PROJECT_ID"));
            int lockedInProject = res.getInt("LOCKED_IN_PROJECT");
            file = new CmsFile(resId, parentId, fileId, filename, resType, resFlags, userId,
                               groupId, resProjectId, accessFlags, state, lockedBy, launcherType,
                               launcherClass, created, modified, modifiedBy, content, resSize, lockedInProject);
            // check if this resource is marked as deleted
            if (file.getState() == C_STATE_DELETED &&!includeDeleted) {
                throw new CmsException("["+this.getClass().getName()+"] "+file.getAbsolutePath(),CmsException.C_RESOURCE_DELETED);
            }
        } else {
            throw new CmsException("[" + this.getClass().getName() + "] " + filename, CmsException.C_NOT_FOUND);
        }
    } catch (SQLException e) {
        throw new CmsException("[" + this.getClass().getName() + "] " + e.getMessage(), CmsException.C_SQL_ERROR, e);
    } catch (CmsException ex) {
        throw ex;
    } catch (Exception exc) {
        throw new CmsException("readFile " + exc.getMessage(), CmsException.C_UNKNOWN_EXCEPTION, exc);
    } finally {
            // close all db-resources
            if(res != null) {
                 try {
                     res.close();
                 } catch(SQLException exc) {
                     // nothing to do here
                 }
            }
            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
                 }
            }
    }

⌨️ 快捷键说明

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