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

📄 cmsresourcetypepage.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    * <B>Security:</B>
    * Access is granted, if:
    * <ul>
    * <li>the user has access to the project</li>
    * <li>the user is owner of the resource or is admin</li>
    * <li>the resource is locked by the callingUser</li>
    * </ul>
    *
    * @param filename the complete path to the resource.
    * @param newType the name of the new resourcetype for this resource.
    *
    * @throws CmsException if operation was not successful.
    */
    public void chtype(CmsObject cms, String filename, String newType) throws CmsException{
        CmsFile file = cms.readFile(filename);
        // check if the current user has the right to change the group of the
        // resource. Only the owner of a file and the admin are allowed to do this.
        if ((cms.getRequestContext().currentUser().equals(cms.readOwner(file))) ||
            (cms.userInGroup(cms.getRequestContext().currentUser().getName(), C_GROUP_ADMIN))){
            cms.doChtype(filename, newType);
            //check if the file type name is page
            String bodyPath = checkBodyPath(cms, (CmsFile)file);
            if (bodyPath != null){
                cms.doChtype(bodyPath, newType);
            }
        }
    }


    /**
    * Copies a Resource.
    *
    * @param source the complete path of the sourcefile.
    * @param destination the complete path of the destinationfolder.
    * @param keepFlags <code>true</code> if the copy should keep the source file's flags,
    *        <code>false</code> if the copy should get the user's default flags.
    *
    * @throws CmsException if the file couldn't be copied, or the user
    * has not the appropriate rights to copy the file.
    */
    public void copyResource(CmsObject cms, String source, String destination, boolean keepFlags) throws CmsException{
        // Read and parse the source page file
        CmsFile file = cms.readFile(source);
        CmsXmlControlFile hXml=new CmsXmlControlFile(cms, file);

        // Check the path of the body file.
        // Don't use the checkBodyPath method here to avaoid overhead.
        String bodyPath=(C_VFS_PATH_BODIES.substring(0, C_VFS_PATH_BODIES.lastIndexOf("/")))+(source);
        String bodyXml=cms.getRequestContext().getDirectoryTranslator().translateResource(C_DEFVFS + hXml.getElementTemplate("body"));        

        if ((C_DEFAULT_SITE + C_ROOTNAME_VFS + bodyPath).equals(bodyXml)){

            // Evaluate some path information
            String destinationFolder = destination.substring(0,destination.lastIndexOf("/")+1);
            checkFolders(cms, destinationFolder);
            String newbodyPath=(C_VFS_PATH_BODIES.substring(0, C_VFS_PATH_BODIES.lastIndexOf("/")))+ destination;

            // we don't want to use the changeContent method here
            // to avoid overhead by copying, readig, parsing, setting XML and writing again.
            // Instead, we re-use the already parsed XML content of the source
            hXml.setElementTemplate("body", newbodyPath);
            cms.doCopyFile(source, destination);
            CmsFile newPageFile = cms.readFile(destination);
            newPageFile.setContents(hXml.getXmlText().getBytes());
            cms.writeFile(newPageFile);

            // Now the new page file is created. Copy the body file
            cms.doCopyFile(bodyPath, newbodyPath);
            // linkmanagement: copy the links of the page
            cms.createLinkEntrys(newPageFile.getResourceId(), cms.readLinkEntrys(file.getResourceId()));
        } else {
            // The body part of the source was not found at
            // the default place. Leave it there, don't make
            // a copy and simply make a copy of the page file.
            // So the new page links to the old body.
            cms.doCopyFile(source, destination);
        }
        // set access flags, if neccessary
        if(!keepFlags) {
            setDefaultFlags(cms, destination);
        }
    }

    /**
    * Copies a resource from the online project to a new, specified project.
    * <br>
    * Copying a resource will copy the file header or folder into the specified
    * offline project and set its state to UNCHANGED.
    *
    * @param resource the name of the resource.
    * @throws CmsException if operation was not successful.
    */
    public void copyResourceToProject(CmsObject cms, String resourceName) throws CmsException {
        //String resourceName = linkManager.getResourceName(resourceId);
        CmsFile file = cms.readFile(resourceName, true);
        cms.doCopyResourceToProject(resourceName);
        //check if the file type name is page
        String bodyPath = checkBodyPath(cms, (CmsFile)file);
        if (bodyPath != null){
            cms.doCopyResourceToProject(bodyPath);
        }
    }

    /**
     * Creates a new resource
     *
     * @param cms The CmsObject
     * @param folder The name of the parent folder
     * @param name The name of the file
     * @param properties The properties of the file
     * @param contents The file content
     * @param parameter an object (e.g. a HashMap) holding parameters (e.g. key/value coded) to create the new resource
     *
     * @throws CmsException if operation was not successful.
     */
    public CmsResource createResource(CmsObject cms, String newPageName, Map properties, byte[] contents, Object parameter) throws CmsException{

        String folderName = newPageName.substring(0, newPageName.lastIndexOf(C_FOLDER_SEPARATOR, newPageName.length())+1);
        String pageName = newPageName.substring(folderName.length(), newPageName.length());

        // Scan for mastertemplates
        Vector allMasterTemplates = cms.getFilesInFolder(C_VFS_PATH_DEFAULT_TEMPLATES);

        // Select the first mastertemplate as default
        String masterTemplate = "";
        if(allMasterTemplates.size() > 0) {
            masterTemplate = ((CmsFile)allMasterTemplates.elementAt(0)).getAbsolutePath();
        }

        // Evaluate the absolute path to the new body file
        String bodyFolder =(C_VFS_PATH_BODIES.substring(0, C_VFS_PATH_BODIES.lastIndexOf("/"))) + folderName;

        // Create the new page file
        CmsFile file = cms.doCreateFile(newPageName, "".getBytes(), m_resourceTypeName, properties);
        cms.doLockResource(newPageName, true);
        CmsXmlControlFile pageXml = new CmsXmlControlFile(cms, file);
        pageXml.setTemplateClass(C_CLASSNAME);
        pageXml.setMasterTemplate(masterTemplate);
        pageXml.setElementClass("body", C_CLASSNAME);
        pageXml.setElementTemplate("body", bodyFolder + pageName);
        pageXml.write();

        // Check, if the body path exists and create missing folders, if neccessary
        checkFolders(cms, folderName);

        // Create the new body file
        CmsFile bodyFile = cms.doCreateFile(bodyFolder + pageName, (getDefaultBodyStart() + new String(contents) + getDefaultBodyEnd()).getBytes(), I_CmsConstants.C_TYPE_PLAIN_NAME, new Hashtable());
        cms.doLockResource(bodyFolder + pageName, true);
        int flags = bodyFile.getAccessFlags();
        if ((flags & C_ACCESS_INTERNAL_READ) ==0 ) {
            flags += C_ACCESS_INTERNAL_READ;
        }
        cms.chmod(bodyFile.getAbsolutePath(), flags);
        // linkmanagement: create the links of the new page (for the case that the content was not empty
        if(contents.length > 1){
            CmsPageLinks linkObject = cms.getPageLinks(newPageName);
            cms.createLinkEntrys(linkObject.getResourceId(), linkObject.getLinkTargets());
        }
        return file;
    }

    public CmsResource createResourceForTemplate(CmsObject cms, String newPageName, Hashtable properties, byte[] contents, String masterTemplate) throws CmsException{
        CmsFile resource = (CmsFile)this.createResource(cms, newPageName, properties, contents, null);
        CmsXmlControlFile pageXml = new CmsXmlControlFile(cms, resource);
        pageXml.setMasterTemplate(masterTemplate);
        pageXml.write();
        return resource;
    }

    /**
    * Deletes a resource.
    *
    * @param filename the complete path of the file.
    *
    * @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 filename) throws CmsException{
        CmsFile file = cms.readFile(filename);
        cms.doDeleteFile(filename);
        // linkmanagement: delete the links on the page
        cms.deleteLinkEntrys(file.getResourceId());
        String bodyPath = checkBodyPath(cms, (CmsFile)file);
        if (bodyPath != null){
            try{
                cms.doDeleteFile(bodyPath);
            } catch (CmsException e){
                if(e.getType() != CmsException.C_NOT_FOUND){
                    throw e;
                }
            }
        }

        // The page file contains XML.
        // So there could be some data in the parser's cache.
        // Clear it!
        String currentProject = cms.getRequestContext().currentProject().getName();
        CmsXmlControlFile.clearFileCache(currentProject + ":" + filename);
    }

    /**
    * Undeletes a resource.
    *
    * @param filename the complete path of the file.
    *
    * @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 filename) throws CmsException{
        CmsFile file = cms.readFile(filename, true);
        cms.doUndeleteFile(filename);
        String bodyPath = checkBodyPath(cms, (CmsFile)file);
        if (bodyPath != null){
            try{
                cms.doUndeleteFile(bodyPath);
            } catch (CmsException e){
                if(e.getType() != CmsException.C_NOT_FOUND){
                    throw e;
                }
            }
        }

        // The page file contains XML.
        // So there could be some data in the parser's cache.
        // Clear it!
        String currentProject = cms.getRequestContext().currentProject().getName();
        CmsXmlControlFile.clearFileCache(currentProject + ":" + filename);

        // linkmanagement: create the links of the restored page
        CmsPageLinks linkObject = cms.getPageLinks(file.getAbsolutePath());
        cms.createLinkEntrys(linkObject.getResourceId(), linkObject.getLinkTargets());
    }

    /**
     * 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 in a page-file (control-file)
        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;
        
        boolean changed = true;
        int resourceType = cms.getResourceType(type).getResourceType();
		int launcherType = cms.getResourceType(type).getLauncherType();
		if((launcherStartClass == null) || ("".equals(launcherStartClass))){
			launcherStartClass = cms.getResourceType(type).getLauncherClass();
        }
        // 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 {
            importedResource = cms.doImportResource(destination, resourceType ,properties, launcherType, 
                                             launcherStartClass, resowner.getName(), resgroup.getName(), Integer.parseInt(access), lastmodified, content);
            if(importedResource != null){
                changed = false;
            }
        } catch (CmsException e) {
            // an exception is thrown if the resource already exists
        }
        if(changed){
        	// if the resource already exists it must be updated
            lockResource(cms,destination, true);
            cms.doWriteResource(destination,properties,resowner.getName(), resgroup.getName(),Integer.parseInt(access),resourceType,content);
            importedResource = cms.readFileHeader(destination);
        }

        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.
    *

⌨️ 快捷键说明

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