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

📄 cmsdbaccess.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            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();
            statement.close();
            // write the file content
            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 {
            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);
    }

    /**
     * Creates a new folder
     *
     * @param user The user who wants to create the folder.
     * @param project The project in which the resource will be used.
     * @param parentId The parentId of the folder.
     * @param fileId The fileId of the folder.
     * @param foldername The complete path to the folder in which the new folder will
     * be created.
     * @param flags The flags of this resource.
     *
     * @return The created folder.
     * @exception CmsException Throws CmsException if operation was not succesful.
     */
    public CmsFolder createFolder(CmsUser user, CmsProject project, int parentId, int fileId, String foldername, int flags) throws CmsException {

        CmsFolder oldFolder = null;
        int state = C_STATE_NEW;
        String usedPool = null;
        String usedStatement = null;

        if (foldername.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 onlineProject = getOnlineProject(project.getId());
        int onlineProject = I_CmsConstants.C_PROJECT_ONLINE_ID;
        if (project.getId() == onlineProject) {
            usedPool = m_poolNameOnline;
            usedStatement = "_ONLINE";
        } else {
            usedPool = m_poolName;
            usedStatement = "";
        }
        // Test if the folder is already there and marked as deleted.
        // If so, delete it
        // No, dont delete it, throw exception (h.riege, 04.01.01)
        try {
            oldFolder = readFolder(project.getId(), foldername);
            if (oldFolder.getState() == C_STATE_DELETED) {
                //removeFolder(oldFolder);
                //state = C_STATE_CHANGED;
                throw new CmsException("[" + this.getClass().getName() + "] ", CmsException.C_FILE_EXISTS);
            } else {
                if (oldFolder != null){
                    throw new CmsException("[" + this.getClass().getName() + "] ", CmsException.C_FILE_EXISTS);
                }
            }
        } catch (CmsException e) {
            if (e.getType() == CmsException.C_FILE_EXISTS) {
                throw e;
            }
        }
        int resourceId = nextId(m_cq.get("C_TABLE_RESOURCES"+usedStatement));
        Connection con = null;
        PreparedStatement statement = null;
        try {
            con = DriverManager.getConnection(usedPool);
            // write new resource to the database
            statement = con.prepareStatement(m_cq.get("C_RESOURCES_WRITE"+usedStatement));
            statement.setInt(1, resourceId);
            statement.setInt(2, parentId);
            statement.setString(3, foldername);
            statement.setInt(4, C_TYPE_FOLDER);
            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, C_UNKNOWN_LAUNCHER_ID);
            statement.setString(14, C_UNKNOWN_LAUNCHER);
            statement.setTimestamp(15, new Timestamp(System.currentTimeMillis()));
            statement.setTimestamp(16, new Timestamp(System.currentTimeMillis()));
            statement.setInt(17, 0);
            statement.setInt(18, user.getId());
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new CmsException("[" + this.getClass().getName() + "] " + e.getMessage(), CmsException.C_SQL_ERROR, e);
        } finally {
            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
                 }
            }
        }
        String parent = new String();
        if (!foldername.equals(C_ROOT)) {
            parent=foldername.substring(0, foldername.length()-1);
            parent=parent.substring(0, parent.lastIndexOf("/")+1);
         }
        // if this is the rootfolder or if the parentfolder is the rootfolder
        // try to create the projectresource
        if (parentId == C_UNKNOWN_ID || parent.equals(C_ROOT)){
            try {
                String rootFolder = null;
                try{
                    rootFolder = readProjectResource(project.getId(), C_ROOT);
                } catch (CmsException exc){
                }
                if (rootFolder == null){
                    createProjectResource(project.getId(), foldername);
                }
            } catch (CmsException e){
                if (e.getType() != CmsException.C_FILE_EXISTS){
                    throw e;
                }
            }
        }
        return readFolder(project.getId(), foldername);
    }

    /**
     * Creates a new folder from an existing folder object.
     *
     * @param user The user who wants to create the folder.
     * @param project The project in which the resource will be used.
     * @param onlineProject The online project of the OpenCms.
     * @param folder The folder to be written to the Cms.
     * @param parentId The parentId of the resource.
     *
     * @param foldername The complete path of the new name of this folder.
     *
     * @return The created folder.
     * @exception CmsException Throws CmsException if operation was not succesful.
     */
    public CmsFolder createFolder(CmsUser user, CmsProject project, CmsProject onlineProject, CmsFolder folder, int parentId, String foldername) throws CmsException {

        String usedPool = null;
        String usedStatement = null;

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

        CmsFolder oldFolder = null;
        int state = 0;
        int modifiedBy = user.getId();
        long dateModified = System.currentTimeMillis();
        if (project.equals(onlineProject)) {
            state = folder.getState();
            usedPool = m_poolNameOnline;
            usedStatement = "_ONLINE";
            modifiedBy = folder.getResourceLastModifiedBy();
            dateModified = folder.getDateLastModified();
        } else {
            state = C_STATE_NEW;
            usedPool = m_poolName;
            usedStatement = "";
        }

        // Test if the file is already there and marked as deleted.
        // If so, delete it
        // No, dont delete it, throw exception (h.riege, 04.01.01)
        try {
            oldFolder = readFolder(project.getId(), foldername);
            if (oldFolder.getState() == C_STATE_DELETED) {
                //removeFolder(oldFolder);
                //state = C_STATE_CHANGED;
                throw new CmsException("[" + this.getClass().getName() + "] ", CmsException.C_FILE_EXISTS);
            } else {
                if (oldFolder != null) {
                    throw new CmsException("[" + this.getClass().getName() + "] ", CmsException.C_FILE_EXISTS);
                }
            }
        } catch (CmsException e) {
            if (e.getType() == CmsException.C_FILE_EXISTS) {
                throw e;
            }
        }
        int resourceId = nextId(m_cq.get("C_TABLE_RESOURCES"+usedStatement));
        //int fileId = nextId(m_cq.get("C_TABLE_FILES"+usedStatement));
        Connection con = null;
        PreparedStatement statement = null;
        try {
            con = DriverManager.getConnection(usedPool);
            // write new resource to the database
            statement = con.prepareStatement(m_cq.get("C_RESOURCES_WRITE"+usedStatement));
            statement.setInt(1, resourceId);
            statement.setInt(2, parentId);
            statement.setString(3, foldername);
            statement.setInt(4, folder.getType());
            statement.setInt(5, folder.getFlags());
            statement.setInt(6, folder.getOwnerId());
            statement.setInt(7, folder.getGroupId());
            statement.setInt(8, project.getId());
            statement.setInt(9, C_UNKNOWN_ID);
            //statement.setInt(9, fileId);
            statement.setInt(10, folder.getAccessFlags());
            statement.setInt(11, state);
            statement.setInt(12, folder.isLockedBy());
            statement.setInt(13, folder.getLauncherType());
            statement.setString(14, folder.getLauncherClassname());
            statement.setTimestamp(15, new Timestamp(folder.getDateCreated()));
            statement.setTimestamp(16, new Timestamp(dateModified));
            statement.setInt(17, 0);
            statement.setInt(18, modifiedBy);
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new CmsException("[" + this.getClass().getName() + "] " + e.getMessage(), CmsException.C_SQL_ERROR, e);
        } finally {
            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
                }
            }
        }
        // if this is the rootfolder or if the parentfolder is the rootfolder
        // try to create the projectresource
        String parent = "/";
        if (!folder.getResourceName().equals(C_ROOT)) {
            parent=folder.getResourceName().substring(0,folder.getResourceName().length()-1);
            parent=parent.substring(0,parent.lastIndexOf("/")+1);
        }
        if (parentId == C_UNKNOWN_ID || parent.equals(C_ROOT)){
            try {
                String rootFolder = null;
                try{
                    rootFolder = readProjectResource(project.getId(), C_ROOT);
                } catch (CmsException exc){
                }
                if (rootFolder == null){
                    createProjectResource(project.getId(), foldername);
                }
            } catch (CmsException e){
                if (e.getType() != CmsException.C_FILE_EXISTS){
                    throw e;
                }
            }
        }
        return readFolder(project.getId(), foldername);
    }

    /**
     * Creates a new projectResource from an given CmsResource object.
     *
     * @param project The project in which the resource will be used.
     * @param resource The resource to be written to the Cms.
     *
     *
     * @exception CmsException Throws CmsException if operation was not succesful
     */
    public void createProjectResource(int projectId, String resourceName) throws CmsException {
        // do not create entries for online-project
        PreparedStatement statement = null;
        Connection con = null;
        try {
            String projectResource = readProjectResource(projectId, resourceName);
            throw new CmsException("[" + this.getClass().getName() + "] ", CmsException.C_FILE_EXISTS);
        } catch (CmsException e) {
            if (e.getType() == CmsException.C_FILE_EXISTS) {
                throw e;
            }
        }
        try {
            con = DriverManager.getConnection(m_poolName);
            statement = con.prepareStatement(m_cq.get("C_PROJECTRESOURCES_CREATE"));
            // write new resource to the database
            statement.setInt(1, projectId);
            statement.setString(2, resourceName);
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new CmsException("[" + this.getClass().getName() + "] " + e.getMessage(), CmsException.C_SQL_ERROR, e);
        } finally {
            if (statement != null) {
                try{

⌨️ 快捷键说明

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