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

📄 cmsmergepages.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        String defaultLocale = CmsLocaleManager.getDefaultLocale().toString();
        String locale1 = m_cms.readPropertyObject(getParamFolder1(), "locale", true).getValue(defaultLocale);
        String locale2 = m_cms.readPropertyObject(getParamFolder2(), "locale", true).getValue(defaultLocale);
        m_report.println(
            Messages.get().container(Messages.RPT_CREATE_EXTERNAL_LINK_0, getParamFolder1(), locale1),
            I_CmsReport.FORMAT_NOTE);
        m_report.println(
            Messages.get().container(Messages.RPT_CREATE_EXTERNAL_LINK_0, getParamFolder2(), locale2),
            I_CmsReport.FORMAT_NOTE);

        // collect all resources in folder 1
        collectFolder(getParamFolder1(), getParamFolder2(), 1);
        // collect all resources in folder 2
        collectFolder(getParamFolder2(), getParamFolder1(), 2);

        // report the results of the collection
        m_report.println(Messages.get().container(Messages.RPT_SCANNING_RESULTS_0), I_CmsReport.FORMAT_HEADLINE);

        m_report.println(Messages.get().container(Messages.RPT_FOLDER1_EXCLUSIVE_0), I_CmsReport.FORMAT_HEADLINE);
        reportList(m_folder1Exclusive, false);
        m_report.println(Messages.get().container(Messages.RPT_FOLDER2_EXCLUSIVE_0), I_CmsReport.FORMAT_HEADLINE);
        reportList(m_folder2Exclusive, false);
        m_report.println(Messages.get().container(Messages.RPT_FOLDERS_SIBLING_0), I_CmsReport.FORMAT_HEADLINE);
        reportList(m_foldersSibling, false);
        m_report.println(Messages.get().container(Messages.RPT_FOLDERS_EQUALNAMES_0), I_CmsReport.FORMAT_HEADLINE);
        reportList(m_foldersEqualnames, true);
        m_report.println(Messages.get().container(Messages.RPT_FOLDERS_DIFFERENTTYPES_0), I_CmsReport.FORMAT_HEADLINE);
        reportList(m_foldersDifferenttypes, false);

    }

    /**
     * Gets the name of a resource in the other merge folder.<p>
     * 
     * @param resName the complete path of a resource
     * @param sourceMergeFolder the path to the source merge folder
     * @param targetMergefolder the path to the target merge folder
     * @return the name of a resource in the other merge folder
     */
    private String getResourceNameInOtherFolder(String resName, String sourceMergeFolder, String targetMergefolder) {

        // get the resourcename of the resouce to test without the source merge folder
        String resourcename = resName.substring(sourceMergeFolder.length());
        // get the complete path of the resource in the other merge folder
        return targetMergefolder + resourcename;
    }

    /**
     * Merges those pages in the two merge folders that have the same name and are no siblings of each other yet.<p>
     * @throws CmsException if something goes wrong
     */
    private void mergePages() throws CmsException {

        int size = m_foldersEqualnames.size();
        if (size > 0) {

            m_report.println(
                Messages.get().container(Messages.RPT_MERGE_PAGES_BEGIN_1, String.valueOf(size)),
                I_CmsReport.FORMAT_HEADLINE);
            String defaultLocale = CmsLocaleManager.getDefaultLocale().toString();
            String locale2 = m_cms.readPropertyObject(getParamFolder2(), "locale", true).getValue(defaultLocale);

            // lock the source and the target folder
            m_report.print(Messages.get().container(Messages.RPT_LOCK_FOLDER_0), I_CmsReport.FORMAT_NOTE);
            m_report.print(org.opencms.report.Messages.get().container(
                org.opencms.report.Messages.RPT_ARGUMENT_1,
                getParamFolder1()));
            m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
            m_cms.lockResource(getParamFolder1());
            m_report.println(
                org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                I_CmsReport.FORMAT_OK);

            m_report.print(Messages.get().container(Messages.RPT_LOCK_FOLDER_0), I_CmsReport.FORMAT_NOTE);
            m_report.print(org.opencms.report.Messages.get().container(
                org.opencms.report.Messages.RPT_ARGUMENT_1,
                getParamFolder2()));
            m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
            m_cms.lockResource(getParamFolder2());
            m_report.println(
                org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                I_CmsReport.FORMAT_OK);

            // now loop through all collected resources
            int count = 1;
            Iterator i = m_foldersEqualnames.iterator();
            while (i.hasNext()) {
                String resFolder1Name = (String)i.next();
                try {
                    String resFolder2Name = getResourceNameInOtherFolder(
                        resFolder1Name,
                        getParamFolder1(),
                        getParamFolder2());
                    m_report.print(org.opencms.report.Messages.get().container(
                        org.opencms.report.Messages.RPT_SUCCESSION_2,
                        String.valueOf(count++),
                        String.valueOf(size)), I_CmsReport.FORMAT_NOTE);
                    m_report.print(Messages.get().container(Messages.RPT_PROCESS_0), I_CmsReport.FORMAT_NOTE);
                    m_report.print(org.opencms.report.Messages.get().container(
                        org.opencms.report.Messages.RPT_ARGUMENT_1,
                        resFolder1Name));
                    m_report.print(Messages.get().container(Messages.RPT_DOUBLE_ARROW_0), I_CmsReport.FORMAT_NOTE);
                    m_report.print(org.opencms.report.Messages.get().container(
                        org.opencms.report.Messages.RPT_ARGUMENT_1,
                        resFolder2Name));
                    m_report.println(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));

                    // get the content of the resource in folder1  
                    String locale = m_cms.readPropertyObject(resFolder1Name, "locale", true).getValue(defaultLocale);
                    m_report.print(
                        Messages.get().container(Messages.RPT_READ_CONTENT_2, resFolder1Name, locale),
                        I_CmsReport.FORMAT_NOTE);
                    CmsResource resFolder1 = m_cms.readResource(resFolder1Name, CmsResourceFilter.IGNORE_EXPIRATION);
                    CmsFile fileFolder1 = CmsFile.upgrade(resFolder1, m_cms);
                    CmsXmlPage pageFolder1 = CmsXmlPageFactory.unmarshal(m_cms, fileFolder1);
                    m_report.println(
                        org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                        I_CmsReport.FORMAT_OK);

                    // get the content of the resource in folder2
                    locale = m_cms.readPropertyObject(resFolder2Name, "locale", true).getValue(defaultLocale);
                    m_report.print(
                        Messages.get().container(Messages.RPT_READ_CONTENT_2, resFolder2Name, locale),
                        I_CmsReport.FORMAT_NOTE);
                    CmsResource resFolder2 = m_cms.readResource(resFolder2Name, CmsResourceFilter.IGNORE_EXPIRATION);
                    CmsFile fileFolder2 = CmsFile.upgrade(resFolder2, m_cms);
                    CmsXmlPage pageFolder2 = CmsXmlPageFactory.unmarshal(m_cms, fileFolder2);
                    m_report.println(
                        org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                        I_CmsReport.FORMAT_OK);

                    // now get all the text elements from the resource in folder 2 which match the the locale of folder 2
                    Locale loc = new Locale(locale2);
                    List textElements2 = pageFolder2.getNames(loc);
                    Iterator j = textElements2.iterator();
                    while (j.hasNext()) {
                        String textElementName = (String)j.next();
                        m_report.print(
                            Messages.get().container(Messages.RPT_PROCESS_TEXT_ELEM_1, textElementName),
                            I_CmsReport.FORMAT_NOTE);
                        // get the text element from the resource in folder 2...
                        String textElement = pageFolder2.getValue(textElementName, loc).getStringValue(m_cms);
                        // and set it in the resource in folder 1...
                        // WARNING: An existing content will be overwritten!
                        if (!pageFolder1.hasValue(textElementName, loc)) {
                            pageFolder1.addValue(textElementName, loc);
                        }
                        pageFolder1.setStringValue(m_cms, textElementName, loc, textElement);
                        m_report.println(org.opencms.report.Messages.get().container(
                            org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK);
                    }
                    // the resource in folder 1 now has all text elements in both locales, so update it in the vfs  

                    m_report.print(
                        Messages.get().container(Messages.RPT_WRITE_CONTENT_1, resFolder1Name),
                        I_CmsReport.FORMAT_NOTE);
                    fileFolder1.setContents(pageFolder1.marshal());
                    m_cms.writeFile(fileFolder1);
                    m_report.println(
                        org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                        I_CmsReport.FORMAT_OK);

                    // save all properties from the resource in folder2
                    m_report.print(
                        Messages.get().container(Messages.RPT_READ_PROPERTIES_1, resFolder2Name),
                        I_CmsReport.FORMAT_NOTE);
                    List properties = m_cms.readPropertyObjects(resFolder2Name, false);
                    m_report.println(
                        org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                        I_CmsReport.FORMAT_OK);

                    // the next thing to do is to delete the old resource in folder 2
                    m_report.print(
                        Messages.get().container(Messages.RPT_DELETE_PAGE_1, resFolder2Name),
                        I_CmsReport.FORMAT_NOTE);
                    m_cms.deleteResource(resFolder2Name, CmsResource.DELETE_PRESERVE_SIBLINGS);
                    m_report.println(
                        org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                        I_CmsReport.FORMAT_OK);

                    // copy a sibling of the resource from folder 1 to folder 2 
                    m_report.print(
                        Messages.get().container(Messages.RPT_COPY_2, resFolder1Name, resFolder2Name),
                        I_CmsReport.FORMAT_NOTE);
                    m_cms.copyResource(resFolder1Name, resFolder2Name, CmsResource.COPY_AS_SIBLING);
                    m_report.println(
                        org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                        I_CmsReport.FORMAT_OK);

                    // restore the properties at the sibling in folder 2
                    m_report.print(
                        Messages.get().container(Messages.RPT_RESORE_PROPERTIES_1, resFolder2Name),
                        I_CmsReport.FORMAT_NOTE);
                    m_cms.writePropertyObjects(resFolder2Name, properties);
                    m_report.println(
                        org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                        I_CmsReport.FORMAT_OK);

                    resFolder1 = null;
                    resFolder2 = null;
                    fileFolder1 = null;
                    fileFolder2 = null;
                    pageFolder1 = null;
                    pageFolder2 = null;

                } catch (CmsException e) {
                    m_report.println(e);
                }

            }
            // lock the source and the target folder
            m_report.print(
                Messages.get().container(Messages.RPT_UNLOCK_1, getParamFolder1()),
                I_CmsReport.FORMAT_NOTE);
            m_cms.unlockResource(getParamFolder1());
            m_report.println(
                org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                I_CmsReport.FORMAT_OK);

            m_report.print(
                Messages.get().container(Messages.RPT_UNLOCK_1, getParamFolder2()),
                I_CmsReport.FORMAT_NOTE);
            m_cms.unlockResource(getParamFolder2());
            m_report.println(
                org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                I_CmsReport.FORMAT_OK);

            m_report.println(Messages.get().container(Messages.RPT_MERGE_PAGES_END_0), I_CmsReport.FORMAT_HEADLINE);
        }
    }

    /**
     * Creates a report list of all resources in one of the collected lists.<p>
     * 
     * @param collected the list to create the output from
     * @param doReport flag to enable detailed report
     */
    private void reportList(List collected, boolean doReport) {

        int size = collected.size();
        // now loop through all collected resources
        m_report.println(
            Messages.get().container(Messages.RPT_NUM_PAGES_1, new Integer(size)),
            I_CmsReport.FORMAT_HEADLINE);
        if (doReport) {
            int count = 1;

            Iterator i = collected.iterator();
            while (i.hasNext()) {
                String resName = (String)i.next();
                m_report.print(org.opencms.report.Messages.get().container(
                    org.opencms.report.Messages.RPT_SUCCESSION_2,
                    String.valueOf(count++),
                    String.valueOf(size)), I_CmsReport.FORMAT_NOTE);
                m_report.println(Messages.get().container(Messages.RPT_PROCESS_1, resName), I_CmsReport.FORMAT_NOTE);
            }
        }
        m_report.println(Messages.get().container(Messages.RPT_MERGE_PAGES_END_0), I_CmsReport.FORMAT_HEADLINE);
    }
}

⌨️ 快捷键说明

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