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

📄 cmsresourcetypefolder.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            }
        }
        // now move the files
        for (int i=0; i<allSubFiles.size(); i++){
            CmsFile curFile = (CmsFile)allSubFiles.elementAt(i);
            if(curFile.getState() != C_STATE_DELETED){
                String curDest = destination + curFile.getAbsolutePath().substring(source.length());
                cms.moveResource(curFile.getAbsolutePath(), curDest);
            }
        }
        // finaly remove the original folders
        deleteResource(cms, source);
    }

    /**
    * Renames the file to the new name.
    *
    * @param oldname the complete path to the file which will be renamed.
    * @param newname the new name of the file.
    *
    * @throws CmsException if the user has not the rights
    * to rename the file, or if the file couldn't be renamed.
    */
    public void renameResource(CmsObject cms, String oldname, String newname) throws CmsException{
        // first of all check the new name
        validResourcename(newname.replace('/','\n'));
       // we have to copy the folder and all resources in the folder
        Vector allSubFolders = new Vector();
        Vector allSubFiles   = new Vector();
        getAllResources(cms, oldname, allSubFiles, allSubFolders);
        String parent = ((CmsResource)cms.readFileHeader(oldname)).getParent();
        if(!cms.accessWrite(oldname)){
            throw new CmsException(oldname, CmsException.C_NO_ACCESS);
        }
        if(!newname.endsWith("/")){
            newname = newname+"/";
        }
        // first copy the folder
        cms.doCopyFolder(oldname, parent + newname);
        // now copy the subfolders
        for (int i=0; i<allSubFolders.size(); i++){
            CmsFolder curFolder = (CmsFolder) allSubFolders.elementAt(i);
            if(curFolder.getState() != C_STATE_DELETED){
                String curDestination = parent + newname
                                        + curFolder.getAbsolutePath().substring(oldname.length());
                cms.doCopyFolder(curFolder.getAbsolutePath(), curDestination );
            }
        }
        // now move the files
        for (int i=0; i<allSubFiles.size(); i++){
            CmsFile curFile = (CmsFile)allSubFiles.elementAt(i);
            if(curFile.getState() != C_STATE_DELETED){
                String curDest = parent + newname
                                + curFile.getAbsolutePath().substring(oldname.length());
                cms.moveResource(curFile.getAbsolutePath(), curDest);
            }
        }
        // finaly remove the original folders
        deleteResource(cms, oldname);

    }

    /**
     * Restores a file in the current project with a version in the backup
     *
     * @param cms The CmsObject
     * @param versionId The version id of the resource
     * @param filename The name of the file to restore
     *
     * @throws CmsException  Throws CmsException if operation was not succesful.
     */
    public void restoreResource(CmsObject cms, int versionId, String filename) throws CmsException{
        throw new CmsException("[" + this.getClass().getName() + "] Cannot restore folders.",CmsException.C_ACCESS_DENIED);
    }

    /**
    * Undo changes in a resource.
    * <br>
    *
    * @param resource the complete path to the resource to be restored.
    *
    * @throws CmsException if the user has not the rights
    * to write this resource.
    */
    public void undoChanges(CmsObject cms, String resource) throws CmsException{
        // we have to undo changes of the folder and all resources in the folder
//        Vector allSubFolders = new Vector();
//        Vector allSubFiles   = new Vector();
//        getAllResources(cms, resource, allSubFiles, allSubFolders);
        
        if(!cms.accessWrite(resource)){
            throw new CmsException("[" + this.getClass().getName() + "]"+resource, CmsException.C_NO_ACCESS);
        }
        // first undo changes of the folder
        cms.doUndoChanges(resource);
        
        // check if there is a corrosponding body folder
        String bodyPath = C_VFS_PATH_BODIES  + resource.substring(1);
        boolean hasBodyFolder = false;
        try {
            cms.readFileHeader(bodyPath);
            hasBodyFolder = true;
        } catch (CmsException e) {
            // body folder not found or no access, so ignore it
        }
        // undo changes in the corresponding folder in C_VFS_PATH_BODIES          
        if (hasBodyFolder) {
            cms.doUndoChanges(bodyPath);
        }
        
        // TODO: Implement optional "recurse" function 
        // now undo changes of the subfolders
//        for (int i=0; i<allSubFolders.size(); i++){
//            CmsFolder curFolder = (CmsFolder) allSubFolders.elementAt(i);
//            if(curFolder.getState() != C_STATE_NEW){
//                if(curFolder.getState() == C_STATE_DELETED){
//                    undeleteResource(cms, curFolder.getAbsolutePath());
//                    lockResource(cms, curFolder.getAbsolutePath(), true);
//                }
//                undoChanges(cms, curFolder.getAbsolutePath());
//            } else {
//                // if it is a new folder then delete the folder
//                try{
//                    deleteResource(cms, curFolder.getAbsolutePath());
//                } catch (CmsException ex){
//                    // do not throw exception when resource not exists
//                    if(ex.getType() != CmsException.C_NOT_FOUND){
//                        throw ex;
//                    }
//                }
//            }
//        }
//        // now undo changes in the files
//        for (int i=0; i<allSubFiles.size(); i++){
//            CmsFile curFile = (CmsFile)allSubFiles.elementAt(i);
//            if(curFile.getState() != C_STATE_NEW){
//                if(curFile.getState() == C_STATE_DELETED){
//                    cms.undeleteResource(curFile.getAbsolutePath());
//                    cms.lockResource(curFile.getAbsolutePath(), true);
//                }
//                cms.undoChanges(curFile.getAbsolutePath());
//            } else {
//                // if it is a new file then delete the file
//                try{
//                    cms.deleteResource(curFile.getAbsolutePath());
//                } catch (CmsException ex){
//                    // do not throw exception when resource not exists
//                    if(ex.getType() != CmsException.C_NOT_FOUND){
//                        throw ex;
//                    }
//                }
//            }
//        }
    }

    /**
    * Unlocks a resource.
    * <br>
    * A user can unlock a resource, so other users may lock this file.
    *
    * @param resource the complete path to the resource to be unlocked.
    *
    * @throws CmsException if the user has not the rights
    * to unlock this resource.
    */
    public void unlockResource(CmsObject cms, String resource) throws CmsException{
        // first unlock the folder in the C_VFS_PATH_BODIES path if it exists.
        try{
            cms.doUnlockResource(C_VFS_PATH_BODIES  + resource.substring(1));
        }catch(CmsException e){
            // ignore the error. this folder doesent exist.
        }
        // now unlock the folder
        cms.doUnlockResource(resource);
    }
    /**
     * Set the access flags of the copied Folder to the default values.
     * @param cms The CmsObject.
     * @param foldername The name of the folder.
     * @throws Throws CmsException if something goes wrong.
     */
    private void setDefaultFlags(CmsObject cms, String foldername)
        throws CmsException {

        Hashtable startSettings=null;
        Integer accessFlags=null;
        startSettings=(Hashtable)cms.getRequestContext().currentUser().getAdditionalInfo(C_ADDITIONAL_INFO_STARTSETTINGS);
        if (startSettings != null) {
            accessFlags=(Integer)startSettings.get(C_START_ACCESSFLAGS);
        }
        if (accessFlags == null) {
            accessFlags = new Integer(C_ACCESS_DEFAULT_FLAGS);
        }
        chmod(cms, foldername, accessFlags.intValue(), false);
    }
    /**
     * Gets all resources - files and subfolders - of a given folder.
     * @param cms The CmsObject.
     * @param rootFolder The name of the given folder.
     * @param allFiles Vector containing all files found so far. All files of this folder
     * will be added here as well.
     * @param allolders Vector containing all folders found so far. All subfolders of this folder
     * will be added here as well.
     * @throws Throws CmsException if something goes wrong.
     */

    private void getAllResources(CmsObject cms, String rootFolder, Vector allFiles,
                    Vector allFolders) throws CmsException {
        Vector folders = new Vector();
        Vector files = new Vector();

        // get files and folders of this rootFolder
        folders = cms.getSubFolders(rootFolder, true);
        files = cms.getFilesInFolder(rootFolder, true);

        //copy the values into the allFiles and allFolders Vectors
        for(int i = 0;i < folders.size();i++) {
            allFolders.addElement((CmsFolder)folders.elementAt(i));
            getAllResources(cms, ((CmsFolder)folders.elementAt(i)).getAbsolutePath(),
                allFiles, allFolders);
        }
        for(int i = 0;i < files.size();i++) {
            allFiles.addElement((CmsFile)files.elementAt(i));
        }
    }

    /**
     * Checks if there are at least one character in the resourcename
     *
     * @param resourcename String to check
     *
     * @throws throws a exception, if the check fails.
     */
    protected void validResourcename( String resourcename )
        throws CmsException {
        if (resourcename == null) {
            throw new CmsException("[" + this.getClass().getName() + "] " + resourcename,
                CmsException.C_BAD_NAME);
        }

        int l = resourcename.trim().length();

        if (l == 0) {
            throw new CmsException("[" + this.getClass().getName() + "] " + resourcename,
                CmsException.C_BAD_NAME);
        }
    }

    /**
     * Changes the project-id of the resource to the new project
     * for publishing the resource directly
     *
     * @param newProjectId The Id of the new project
     * @param resourcename The name of the resource to change
     */
    public void changeLockedInProject(CmsObject cms, int newProjectId, String resourcename)
        throws CmsException{
        // we have to change the folder and all resources in the folder
        Vector allSubFolders = new Vector();
        Vector allSubFiles   = new Vector();
        getAllResources(cms, resourcename, allSubFiles, allSubFolders);
        // first change all the files
        for (int i=0; i<allSubFiles.size(); i++){
            CmsFile curFile = (CmsFile)allSubFiles.elementAt(i);
            if(curFile.getState() != C_STATE_UNCHANGED){
                cms.changeLockedInProject(newProjectId, curFile.getAbsolutePath());
            }
        }
        // now all the subfolders
        for (int i=0; i<allSubFolders.size(); i++){
            CmsFolder curFolder = (CmsFolder) allSubFolders.elementAt(i);
            if(curFolder.getState() != C_STATE_UNCHANGED){
                changeLockedInProject(cms, newProjectId, curFolder.getAbsolutePath());
            }
        }
        // finally the folder
        cms.doChangeLockedInProject(newProjectId, resourcename);
        // change the corresponding folder in C_VFS_PATH_BODIES
        String bodyFolder = C_VFS_PATH_BODIES.substring(0,
                    C_VFS_PATH_BODIES.lastIndexOf("/")) + resourcename;
        try {
            cms.readFolder(bodyFolder,true);
            changeLockedInProject(cms, newProjectId, bodyFolder);
        }
        catch(CmsException ex) {
            // no folder is there, so do nothing
        }
    }
}

⌨️ 快捷键说明

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