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

📄 cmsresourcetypefolder.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                cms.doCopyResourceToProject(contentFolder.getAbsolutePath());
            }
        } catch(CmsException e){
            // cannot read the folder in C_VFS_PATH_BODIES so do nothing
        }
    }

    /**
    * Creates a new resource.<br>
    *
    * @param folder the complete path to the folder in which the file will be created.
    * @param filename the name of the new resource.
    * @param contents the contents of the new file.
    * @param type the resourcetype of the new file.
    *
    * @return file a <code>CmsFile</code> object representing the newly created file.
    *
    * @throws CmsException or if the resourcetype is set to folder. The CmsException is also thrown, if the
    * filename is not valid or if the user has not the appropriate rights to create a new file.
    */
    public CmsResource createResource(CmsObject cms, String newFolderName, Map properties, byte[] contents, Object parameter) throws CmsException{
        if (! newFolderName.endsWith(C_FOLDER_SEPARATOR)) newFolderName += C_FOLDER_SEPARATOR;
        CmsFolder res = cms.doCreateFolder(newFolderName, properties);        
        cms.lockResource(newFolderName);
        res.setLocked(cms.getRequestContext().currentUser().getId());
        return res;
    }


    /**
    * Deletes a resource.
    *
    * @param folder the complete path of the folder.
    *
    * @throws CmsException if the file couldn't be deleted, or if the user
    * has not the appropriate rights to delete the file.
    */
    public void deleteResource(CmsObject cms, String folder) throws CmsException{

        Vector allSubFolders = new Vector();
        Vector allSubFiles   = new Vector();
        getAllResources(cms, folder, allSubFiles, allSubFolders);
        // first delete all the files
        for (int i=0; i<allSubFiles.size(); i++){
            CmsFile curFile = (CmsFile)allSubFiles.elementAt(i);
            if(curFile.getState() != C_STATE_DELETED){
                try{
                    cms.deleteResource(curFile.getAbsolutePath());
                }catch(CmsException e){
                    if(e.getType() != CmsException.C_RESOURCE_DELETED){
                        throw e;
                    }
                }
            }
        }
        // now all the empty subfolders
        for (int i=allSubFolders.size() - 1; i > -1; i--){
            CmsFolder curFolder = (CmsFolder) allSubFolders.elementAt(i);
            if(curFolder.getState() != C_STATE_DELETED){
                cms.doDeleteFolder(curFolder.getAbsolutePath());
            }
        }

        // finaly the folder
        cms.doDeleteFolder(folder);
        // delete the corresponding folder in C_VFS_PATH_BODIES
        String bodyFolder = C_VFS_PATH_BODIES.substring(0,
                    C_VFS_PATH_BODIES.lastIndexOf("/")) + folder;
        try {
            cms.readFolder(bodyFolder);
            cms.deleteResource(bodyFolder);
        }
        catch(CmsException ex) {
            // no folder is there, so do nothing
        }
    }

    /**
    * Undeletes a resource.
    *
    * @param folder the complete path of the folder.
    *
    * @throws CmsException if the file couldn't be undeleted, or if the user
    * has not the appropriate rights to undelete the file.
    */
    public void undeleteResource(CmsObject cms, String folder) throws CmsException{

       // we have to undelete the folder and all resources in the folder
        Vector allSubFolders = new Vector();
        Vector allSubFiles   = new Vector();
        getAllResources(cms, folder, allSubFiles, allSubFolders);
        // first undelete all the files
        for (int i=0; i<allSubFiles.size(); i++){
            CmsFile curFile = (CmsFile)allSubFiles.elementAt(i);
            if(curFile.getState() == C_STATE_DELETED){
                cms.undeleteResource(curFile.getAbsolutePath());
            }
        }
        // now all the empty subfolders
        for (int i=0; i<allSubFolders.size(); i++){
            CmsFolder curFolder = (CmsFolder) allSubFolders.elementAt(i);
            if(curFolder.getState() == C_STATE_DELETED){
                cms.doUndeleteFolder(curFolder.getAbsolutePath());
            }
        }
        // finally the folder
        cms.doUndeleteFolder(folder);
        // undelete the corresponding folder in C_VFS_PATH_BODIES
        String bodyFolder = C_VFS_PATH_BODIES.substring(0,
                    C_VFS_PATH_BODIES.lastIndexOf("/")) + folder;
        try {
            cms.readFolder(bodyFolder,true);
            cms.undeleteResource(bodyFolder);
        }
        catch(CmsException ex) {
            // no folder is there, so do nothing
        }
    }

    /**
     * Does the Linkmanagement when a resource will be exported.
     * When a resource has to be exported, the ID磗 inside the
     * Linkmanagement-Tags have to be changed to the corresponding URL磗
     *
     * @param file is the file that has to be changed
     */
    public CmsFile exportResource(CmsObject cms, CmsFile file) throws CmsException {
        // nothing to do here, because there couldn磘 be any Linkmanagement-Tags inside a folder-resource
        return file;
    }

    /**
     * Imports a resource.
     *
     * @param cms The current CmsObject.
     * @param source The sourcepath of the resource to import.
     * @param destination The destinationpath of the resource to import.
     * @param type The type of the resource to import.
     * @param user The name of the owner of the resource.
     * @param group The name of the group of the resource.
     * @param access The access flags of the resource.
     * @param properties A Hashtable with the properties of the resource.
     * The key is the name of the propertydefinition, the value is the propertyvalue.
     * @param launcherStartClass The name of the launcher startclass.
     * @param content The filecontent if the resource is of type file
     * @param importPath The name of the import path
     * 
     * @return CmsResource The imported resource.
     * 
     * @throws Throws CmsException if the resource could not be imported
     * 
     */
    public CmsResource importResource(CmsObject cms, String source, String destination, String type,
                                       String user, String group, String access, long lastmodified, 
                                       Map properties, String launcherStartClass, byte[] content, String importPath) 
                       throws CmsException {
        CmsResource importedResource = null;
        destination = importPath + destination;
        if (! destination.endsWith(C_FOLDER_SEPARATOR)) destination += C_FOLDER_SEPARATOR;

        boolean changed = true;
        // try to read the new owner and group
        CmsUser resowner = null;
        CmsGroup resgroup = null;
        try{
        	resowner = cms.readUser(user);
        } catch (CmsException e){
        	if (DEBUG>0) System.err.println("[" + this.getClass().getName() + ".importResource/1] User " + user + " not found");
            if(I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_CRITICAL)) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_CRITICAL, "[" + this.getClass().getName() + ".importResource/1] User " + user + " not found");
            }            
        	resowner = cms.getRequestContext().currentUser();	
        }
        try{
        	resgroup = cms.readGroup(group);
        } catch (CmsException e){
            if (DEBUG>0) System.err.println("[" + this.getClass().getName() + ".importResource/2] Group " + group + " not found");
            if(I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_CRITICAL)) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_CRITICAL, "[" + this.getClass().getName() + ".importResource/2] Group " + group + " not found");
            }             
        	resgroup = cms.getRequestContext().currentGroup();	
        } 
        // try to create the resource		
        try {
            importedResource = cms.doImportResource(destination, C_TYPE_FOLDER, properties, C_UNKNOWN_LAUNCHER_ID, 
                                             C_UNKNOWN_LAUNCHER, resowner.getName(), resgroup.getName(), Integer.parseInt(access), lastmodified, new byte[0]);
            if(importedResource != null){
                changed = false;
            }
        } catch (CmsException e) {
            // an exception is thrown if the folder already exists
        }
        if(changed){
        	changed = false;
            //the resource exists, check if properties has to be updated
            importedResource = cms.readFolder(destination);
            Map oldProperties = cms.readProperties(importedResource.getAbsolutePath());
            if(oldProperties == null){
                oldProperties = new HashMap();
            }
            if(properties == null){
                properties = new Hashtable();
            }
            if (properties.size() > 0) {
                // if no properties are to be imported we do not need to check the old properties
                if (oldProperties.size() != properties.size()) {
                    changed = true;
                } else {
                    // check each of the properties
                    Iterator i = properties.keySet().iterator();
                    while (i.hasNext()) {
                        String curKey = (String) i.next();
                        String value = (String) properties.get(curKey);
                        String oldValue = (String) oldProperties.get(curKey);
                        if ((oldValue == null) || !(value.trim().equals(oldValue.trim()))) {
                            changed = true;
                            break;
                        }
                    }
                }
            }
            // check changes of the owner, group and access
            if(importedResource.getAccessFlags() != Integer.parseInt(access) ||
                   importedResource.getOwnerId() != resowner.getId() ||
                   importedResource.getGroupId() != resgroup.getId()){
                changed = true;            
            }
            // check changes of the resourcetype
            if(importedResource.getType() != cms.getResourceType(type).getResourceType()){
            	changed = true;
            }
            // update the folder if something has changed
            if(changed){
            	lockResource(cms,importedResource.getAbsolutePath(), true);
            	cms.doWriteResource(importedResource.getAbsolutePath(),properties,resowner.getName(), resgroup.getName(),Integer.parseInt(access),C_TYPE_FOLDER,new byte[0]);
            }
        }

        return importedResource;
    }

    /**
    * 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.
    *
    * @throws 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 C_VFS_PATH_BODIES path if it exists.
        try{
            cms.doLockResource(C_VFS_PATH_BODIES  + 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.
    *
    * @throws 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 );

⌨️ 快捷键说明

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