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

📄 cmsimportmoduledata.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                        } catch(CmsException exc) {
                            m_cms.setContextToVfs();
                            // this resource is missing - we need the root-folder
                            resources.addElement(C_ROOT);
                        }
                    }
                } catch(StringIndexOutOfBoundsException exc) {
                    // this is a resource in root-folder: ignore the excpetion
                }
            }
        } catch (Exception exc) {
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
        if (m_importZip != null){
            try{
                m_importZip.close();
            } catch (IOException exc) {
                throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
            }
        }
        if(resources.contains(C_ROOT)) {
            // we have to import root - forget the rest!
            resources.removeAllElements();
            resources.addElement(C_ROOT);
        }
        return resources;
    }

    /**
     * Imports a channel into the cms.
     * @param destination the path to the destination-file in the cms
     * @param type the resource-type of the file
     * @param user the owner of the file
     * @param group the group of the file
     * @param access the access-flags of the file
     * @param properties a hashtable with properties for this resource
     * @param writtenFilenames filenames of the files and folder which have actually been successfully written
     *       not used when null
     * @param fileCodes code of the written files (for the registry)
     *       not used when null
     */
    private void importChannel(String destination, String type, String user, String group, String access, Hashtable properties, Vector writtenFilenames, Vector fileCodes) {
        // print out the information for shell-users
        System.out.print("Importing ");
        System.out.print(destination + " ");
        boolean success = true;
        String fullname = null;
        try {
            String path = m_importPath + destination.substring(0, destination.lastIndexOf("/") + 1);
            String name = destination.substring((destination.lastIndexOf("/") + 1), destination.length());
            m_cms.setContextToCos();
            // try to read an existing channel
            CmsResource res = null;
            try{
                res = m_cms.readFolder("/"+destination+"/");
            } catch (CmsException e) {
                // the channel does not exist, so import the channel
            }
            if(res == null){
                // get a new channelid
                int newChannelId = com.opencms.dbpool.CmsIdGenerator.nextId(this.C_TABLE_CHANNELID);
                properties.put(I_CmsConstants.C_PROPERTY_CHANNELID, newChannelId+"");
                res = m_cms.importResource("", destination, type, user, group, access,
                                            properties, "", null, m_importPath);
            }
            m_cms.setContextToVfs();
            if(res != null){
                fullname = res.getAbsolutePath();
            }
            System.out.println("OK");
        } catch (Exception exc) {
            // an error while importing the file
            success = false;
            System.out.println("Error: "+exc.toString());
        } finally {
            m_cms.setContextToVfs();
        }
        byte[] digestContent = {0};
        if (success && (fullname != null)){
            if (writtenFilenames != null){
                writtenFilenames.addElement(fullname);
            }
            if (fileCodes != null){
                fileCodes.addElement(new String(digestContent));
            }
        }
    }

    /**
     * Imports a single master
     * @param subId The subid of the module
     * @param classname The name of the module class
     * @param currentElement The current element of the xml-file
     */
    private void importMaster(String subId, String classname, Element currentElement) throws CmsException{
        CmsMasterDataSet newDataset = new CmsMasterDataSet();
        Vector channelRelations = new Vector();
        Vector masterMedia = new Vector();
        // try to get the dataset
        try{
            int subIdInt = Integer.parseInt(subId);
            newDataset = getMasterDataSet(subIdInt, currentElement);
        } catch (Exception e){
            throw new CmsException("Cannot get dataset ", e);
        }
        // try to get the channelrelations
        try{
            channelRelations = getMasterChannelRelation(currentElement);
        } catch (Exception e){
            throw new CmsException("Cannot get channelrelations ", e);
        }
        // try to get the media
        try{
            masterMedia = getMasterMedia(currentElement);
        } catch (Exception e){
            throw new CmsException("Cannot get media ", e);
        }
        // add the channels and media to the dataset
        newDataset.m_channelToAdd = channelRelations;
        newDataset.m_mediaToAdd = masterMedia;
        // create the new content definition
        CmsMasterContent newMaster = getContentDefinition(classname,
                                     new Class[] {CmsObject.class, CmsMasterDataSet.class},
                                     new Object[] {m_cms, newDataset});
        try{
            int userId = newMaster.getOwner();
            int groupId = newMaster.getGroupId();
            // first insert the new master
            newMaster.importMaster(m_cms);
            // now update the master because user and group might be changed
            newMaster.chown(m_cms, userId);
            newMaster.chgrp(m_cms,groupId);
        } catch (Exception e){
            throw new CmsException("Cannot write master ", e);
        }
    }

    /**
     * Gets the dataset for the master from the xml-file
     * @param subId The subid of the module
     * @param currentElement The current element of the xml-file
     * @return CmsMasterDataSet The dataset with the imported information
     */
    private CmsMasterDataSet getMasterDataSet(int subId, Element currentElement) throws CmsException{
        String datasetfile, username, groupname, accessFlags, publicationDate, purgeDate, flags,
                feedId, feedReference, feedFilename, title;
        // get the new dataset object
        CmsMasterDataSet newDataset = new CmsMasterDataSet();

        // get the file with the dataset of the master
        datasetfile = getTextNodeValue(currentElement, CmsExportModuledata.C_EXPORT_TAG_MASTER_DATASET);
        Document datasetXml = this.getXmlFile(datasetfile);
        Element dataset = datasetXml.getDocumentElement();
        // get the information from the dataset and add it to the dataset
        // first add the subid
        newDataset.m_subId = subId;
        newDataset.m_masterId = C_UNKNOWN_ID;
        // get the id of the user or set the owner to the current user
        username = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_USER);
        int userId = m_cms.getRequestContext().currentUser().getId();
        try{
            if((username != null) && !("".equals(username.trim()))){
                userId = m_cms.readUser(username).getId();
            }
        } catch (Exception e){
        }
        newDataset.m_userId = userId;
        // get the id of the group or set the group to the current user
        groupname = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_GROUP);
        int groupId = m_cms.getRequestContext().currentGroup().getId();
        try{
            if((groupname != null) && !("".equals(groupname.trim()))){
                groupId = m_cms.readGroup(groupname).getId();
            }
        } catch (Exception e){
        }
        newDataset.m_groupId = groupId;
        // set the accessflags or the default flags
        accessFlags = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_ACCESSFLAGS);
        try{
            newDataset.m_accessFlags = Integer.parseInt(accessFlags);
        } catch (Exception e){
            newDataset.m_accessFlags = this.C_ACCESS_DEFAULT_FLAGS;
        }
        // set the publication date
        publicationDate = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_PUBLICATIONDATE);
        try{
            newDataset.m_publicationDate = convertDate(publicationDate);
        } catch (Exception e){
        }
        // set the purge date
        purgeDate = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_PURGEDATE);
        try{
            newDataset.m_purgeDate = convertDate(purgeDate);
        } catch (Exception e){
        }
        // set the flags
        flags = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_FLAGS);
        try{
            newDataset.m_flags = Integer.parseInt(flags);
        } catch (Exception e){
        }
        // set the feedid
        feedId = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_FEEDID);
        try{
            newDataset.m_feedId = Integer.parseInt(feedId);
        } catch (Exception e){
        }
        // set the feedreference
        feedReference = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_FEEDREFERENCE);
        try{
            newDataset.m_feedReference = Integer.parseInt(feedReference);
        } catch (Exception e){
        }
        // set the feedfilenam
        feedFilename = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_FEEDFILENAME);
        newDataset.m_feedFilename = feedFilename;
        // set the masters title
        title = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_TITLE);
        newDataset.m_title = title;
        // set the values of data_big
        for(int i=0; i< newDataset.m_dataBig.length; i++){
            String filename = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_DATABIG+i);
            String value = new String();
            if(filename != null && !"".equals(filename.trim())){
                // get the value from the file
                value =  new String(getFileBytes(filename));
            }
            newDataset.m_dataBig[i] = value;
        }
        // get the values of data_medium
        for(int i=0; i< newDataset.m_dataMedium.length; i++){
            String filename = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_DATAMEDIUM+i);
            String value = new String();
            if(filename != null && !"".equals(filename.trim())){
                // get the value from the file
                value =  new String(getFileBytes(filename));
            }
            newDataset.m_dataMedium[i] = value;
        }
        // get the values of data_small
        for(int i=0; i< newDataset.m_dataSmall.length; i++){
            String filename = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_DATASMALL+i);
            String value = new String();
            if(filename != null && !"".equals(filename.trim())){
                // get the value from the file
                value =  new String(getFileBytes(filename));
            }
            newDataset.m_dataSmall[i] = value;
        }
        // get the values of data_int
        for(int i=0; i< newDataset.m_dataInt.length; i++){
            String value = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_DATAINT+i);
            try{
                newDataset.m_dataInt[i] = new Integer(value).intValue();
            } catch (Exception e){
                newDataset.m_dataInt[i] = 0;
            }
        }
        // get the values of data_reference
        for(int i=0; i< newDataset.m_dataReference.length; i++){
            String value = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_DATAREFERENCE+i);
            try{
                newDataset.m_dataReference[i] = new Integer(value).intValue();
            } catch (Exception e){
                newDataset.m_dataReference[i] = 0;
            }
        }
        // get the values of data_date
        for(int i=0; i< newDataset.m_dataDate.length; i++){
            String value = getTextNodeValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_DATADATE+i);
            try{
                newDataset.m_dataDate[i] = convertDate(value);
            } catch (Exception e){
                newDataset.m_dataDate[i] = 0;
            }
        }
        return newDataset;
    }

    /**
     * Gets the channelrelations for the master from the xml-file
     * @param currentElement The current element of the xml-file
     * @return Vector The vector contains the ids of all channels of the master
     */
    private Vector getMasterChannelRelation(Element currentElement){
        Vector channelRelations = new Vector();
        // get the channelnames of the master
        NodeList channelNodes = currentElement.getElementsByTagName(CmsExportModuledata.C_EXPORT_TAG_MASTER_CHANNELNAME);

        // walk through all channelrelations
        for (int j = 0; j < channelNodes.getLength(); j++) {
            // get the name of the channel
            String channelName = ((Element)channelNodes.item(j)).getFirstChild().getNodeValue();
            // try to read the channel and get its channelid
            if ((channelName != null) && !("".equals(channelName.trim()))) {
                channelRelations.addElement(channelName);
            }
        }
        return channelRelations;

⌨️ 快捷键说明

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