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

📄 cmsresourcetypefolder.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            }
            try{
                cms.chgrp(fullname, group);
            }catch(CmsException e){
                System.out.println("chgrp(" + group + ") failed ");
            }
            try{
                cms.chown(fullname, user);
            }catch(CmsException e){
                System.out.println("chown((" + user + ") failed ");
            }
        }

        return cmsfolder;
    }

    /**
    * Locks a given resource.
    * <br>
    * A user can lock a resource, so he is the only one who can write this
    * resource.
    *
    * @param resource the complete path to the resource to lock.
    * @param force if force is <code>true</code>, a existing locking will be overwritten.
    *
    * @exception CmsException if the user has not the rights to lock this resource.
    * It will also be thrown, if there is a existing lock and force was set to false.
    */
    public void lockResource(CmsObject cms, String resource, boolean force) throws CmsException{
        // first lock the folder in the /content/bodys/ path if it exists.
        try{
            cms.doLockResource(C_CONTENTBODYPATH  + resource.substring(1), force);
        }catch(CmsException e){
            // ignore the error. this folder doesent exist.
        }
        // now lock the folder
        cms.doLockResource(resource, force);
    }

    /**
    * Moves a file to the given destination.
    *
    * @param source the complete path of the sourcefile.
    * @param destination the complete path of the destinationfile.
    *
    * @exception CmsException if the user has not the rights to move this resource,
    * or if the file couldn't be moved.
    */
    public void moveResource(CmsObject cms, String source, String destination) throws CmsException{
        // it is a folder so we need the end /
        destination = destination +"/";
       // we have to copy the folder and all resources in the folder
        Vector allSubFolders = new Vector();
        Vector allSubFiles   = new Vector();
        getAllResources(cms, source, allSubFiles, allSubFolders);
        if(!cms.accessWrite(source)){
            throw new CmsException(source, CmsException.C_NO_ACCESS);
        }
        // first copy the folder
        cms.doCopyFolder(source, destination);
        // 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 = destination + curFolder.getAbsolutePath().substring(source.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 = 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.
    *
    * @exception 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
     *
     * @exception 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.
    *
    * @exception 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);
        String parent = ((CmsResource)cms.readFileHeader(resource)).getParent();
        if(!cms.accessWrite(resource)){
            throw new CmsException("[" + this.getClass().getName() + "]"+resource, CmsException.C_NO_ACCESS);
        }
        // first undo changes of the folder
        cms.doUndoChanges(resource);
        // undo changes in the corresponding folder in /content/bodys
        cms.doUndoChanges(C_CONTENTBODYPATH  + resource.substring(1));
        // 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.
    *
    * @exception 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 /content/bodys/ path if it exists.
        try{
            cms.doUnlockResource(C_CONTENTBODYPATH  + 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.
     * @exception 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.
     * @exception 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
     *
     * @exception 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 /content/bodys/
        String bodyFolder = C_CONTENTBODYPATH.substring(0,
                    C_CONTENTBODYPATH.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 + -