Converts locales for the copied container element.<p>

@param elementResource the copied container element
@throws CmsException if something goes wrong
359207
public void adjustLocalesForElement(CmsResource elementResource) throws CmsException {

        if (m_copyMode != CopyMode.smartCopyAndChangeLocale) {
            return;
        }

        CmsFile file = m_cms.readFile(elementResource);
        Locale oldLocale = OpenCms.getLocaleManager().getDefaultLocale(m_cms, m_originalPage);
        Locale newLocale = OpenCms.getLocaleManager().getDefaultLocale(m_cms, m_targetFolder);
        CmsXmlContent content = CmsXmlContentFactory.unmarshal(m_cms, file);
        try {
            content.moveLocale(oldLocale, newLocale);
            LOG.info("Replacing locale " + oldLocale + " -> " + newLocale + " for " + elementResource.getRootPath());
            file.setContents(content.marshal());
            m_cms.writeFile(file);
        } catch (CmsXmlException e) {
            LOG.info(
                "NOT replacing locale for "
                    + elementResource.getRootPath()
                    + ": old="
                    + oldLocale
                    + ", new="
                    + newLocale
                    + ", contentLocales="
                    + content.getLocales());
        }

    }
479989
public void actionDeleteElementLocale() throws JspException {

        try {
            Locale loc = getElementLocale();
            m_page.removeLocale(loc);
            //write the modified xml content
            m_file.setContents(m_page.marshal());
            m_file = getCms().writeFile(m_file);
            List<Locale> locales = m_page.getLocales();
            if (locales.size() > 0) {
                // set first locale as new display locale
                Locale newLoc = locales.get(0);
                setParamElementlanguage(newLoc.toString());
                m_elementLocale = newLoc;
            } else {
                if (LOG.isErrorEnabled()) {
                    LOG.error(Messages.get().getBundle().key(Messages.LOG_GET_LOCALES_1, getParamResource()));
                }
            }
            initContent();
        } catch (CmsXmlException e) {
            // an error occurred while trying to delete the locale, stop action
            showErrorPage(e);
        } catch (CmsException e) {
            // should usually never happen
            if (LOG.isInfoEnabled()) {
                LOG.info(e.getLocalizedMessage(), e);
            }
        }
    }
76633
public void switchToLocale(Locale locale) throws CmsException {

        CmsObject cms = A_CmsUI.getCmsObject();
        CmsLocaleGroupService groupService = cms.getLocaleGroupService();
        CmsLocaleGroup localeGroup = groupService.readDefaultFileLocaleGroup(m_currentRoot);
        Collection<CmsResource> resources = localeGroup.getResourcesForLocale(locale);
        if (resources.isEmpty()) {
            LOG.error(
                "Can not switch to locale "
                    + locale
                    + ": no page found in locale group of "
                    + m_currentRoot.getRootPath());
        }
        CmsResource localeVariant = resources.iterator().next();
        if (!localeVariant.isFolder()) {
            CmsResource parentFolder = cms.readParentFolder(localeVariant.getStructureId());
            if (m_comparisonLocale.equals(locale)) {
                m_comparisonLocale = m_rootLocale;
                m_rootLocale = locale;
            } else {
                m_rootLocale = locale;
            }
            updateLocaleWidgets();
            initTree(parentFolder);
        } else {
            LOG.error("locale variant should not be a folder: " + localeVariant.getRootPath());
        }
    }
1165849
private Locale getLocale() throws CmsException {

        getDocument();
        List<Locale> locales = m_document.getLocales();
        if (locales.contains(Locale.ENGLISH) || locales.isEmpty()) {
            return Locale.ENGLISH;
        }
        return locales.get(0);
    }
938496
private String getContentByContainer(
        CmsContainerElementBean element,
        CmsContainer container,
        CmsFormatterConfiguration configs) {

        String content = null;

        I_CmsFormatterBean formatter = getFormatterForContainer(
            m_cms,
            element,
            container,
            m_adeConfig,
            CmsADESessionCache.getCache(m_req, m_cms));
        if (formatter != null) {
            element = element.clone(); // clone element because presets for different containers may be different
            element.initSettings(m_cms, formatter, m_locale, m_req, container.getSettingPresets());
            try {
                content = getElementContent(element, m_cms.readResource(formatter.getJspStructureId()), container);
                //                }
            } catch (Exception e) {
                LOG.error(e.getLocalizedMessage(), e);
            }
            if (content != null) {
                content = removeScriptTags(content);
            }
        }
        return content;
    }
460524
public void init(CmsObject cms, Locale locale, String requestUri) {

        m_cms = cms;
        m_locale = locale;
        m_requestUri = requestUri;
        m_requestUriFolder = CmsResource.getFolderPath(m_requestUri);
    }
350268
private CmsResource getContainerpage(CmsObject cms) throws CmsException {

        String currentUri = cms.getRequestContext().getUri();
        CmsResource containerPage = cms.readResource(currentUri);
        if (!CmsResourceTypeXmlContainerPage.isContainerPage(containerPage)) {
            // container page is used as template
            String cntPagePath = cms.readPropertyObject(
                containerPage,
                CmsPropertyDefinition.PROPERTY_TEMPLATE_ELEMENTS,
                true).getValue("");
            try {
                containerPage = cms.readResource(cntPagePath);
            } catch (CmsException e) {
                if (!LOG.isDebugEnabled()) {
                    LOG.warn(e.getLocalizedMessage());
                }
                LOG.debug(e.getLocalizedMessage(), e);
            }
        }
        return containerPage;
    }
775774
public void setDefaultLocale(String locale) {

        try {
            m_defaultLocale = CmsLocaleManager.getLocale(locale);
        } catch (Exception e) {
            if (CmsLog.INIT.isWarnEnabled()) {
                CmsLog.INIT.warn(Messages.get().getBundle().key(Messages.INIT_NONCRIT_ERROR_0), e);
            }
        }
        if (CmsLog.INIT.isInfoEnabled()) {
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_DEFAULT_LOCALE_1, m_defaultLocale));
        }
    }
248415
private void lockLocalization(Locale l) throws CmsException {

        if (null == m_lockedBundleFiles.get(l)) {
            LockedFile lf = LockedFile.lockResource(m_cms, m_bundleFiles.get(l));
            m_lockedBundleFiles.put(l, lf);
        }

    }
1043826
public CmsContainerElement setElementInfo(CmsContainerElementBean elementBean, CmsContainerElement result)
    throws CmsException {

        Locale wpLocale = OpenCms.getWorkplaceManager().getWorkplaceLocale(m_cms);
        // reinitializing resource to avoid caching issues
        elementBean.initResource(m_cms);
        CmsResource resource = elementBean.getResource();
        boolean isModelGroup = elementBean.getIndividualSettings().containsKey(CmsContainerElement.MODEL_GROUP_ID);
        if (isModelGroup) {
            CmsUUID groupId = new CmsUUID(elementBean.getIndividualSettings().get(CmsContainerElement.MODEL_GROUP_ID));
            resource = m_cms.readResource(groupId, CmsResourceFilter.IGNORE_EXPIRATION);
        }

        boolean newEditorDisabled = !CmsWorkplaceEditorManager.checkAcaciaEditorAvailable(
            m_cms,
            elementBean.getResource());
        result.setNewEditorDisabled(newEditorDisabled);
        I_CmsResourceType resourceType = OpenCms.getResourceManager().getResourceType(resource);
        String typeName = resourceType.getTypeName();
        result.setHasEditHandler(
            (resourceType instanceof CmsResourceTypeXmlContent)
                && (((CmsResourceTypeXmlContent)resourceType).getEditHandler(m_cms) != null));
        result.setResourceType(typeName);
        result.setIconClasses(
            CmsIconUtil.getIconClasses(
                CmsIconUtil.getDisplayType(m_cms, resource),
                elementBean.getResource().getName(),
                false));
        CmsPermissionInfo permissionInfo;
        String title;
        String subTitle;
        if (!elementBean.isInMemoryOnly()) {
            permissionInfo = OpenCms.getADEManager().getPermissionInfo(m_cms, resource, m_page.getRootPath());
            if (CmsResourceTypeXmlContent.isXmlContent(resource)) {
                if (CmsStringUtil.isEmptyOrWhitespaceOnly(permissionInfo.getNoEditReason())
                    && elementBean.isInheritedContainer(m_cms)) {
                    String requestUri = m_cms.getRequestContext().getUri();
                    String folderPath = CmsResource.getFolderPath(requestUri);
                    String configPath = CmsStringUtil.joinPaths(
                        folderPath,
                        CmsContainerConfigurationCache.INHERITANCE_CONFIG_FILE_NAME);
                    if (m_cms.existsResource(configPath)) {
                        permissionInfo.setNoEditReason(
                            new CmsResourceUtil(m_cms, m_cms.readResource(configPath)).getNoEditReason(wpLocale, true));
                    } else {
                        if (!m_cms.getLock(folderPath).isLockableBy(m_cms.getRequestContext().getCurrentUser())) {
                            permissionInfo.setNoEditReason(
                                org.opencms.workplace.explorer.Messages.get().getBundle(wpLocale).key(
                                    org.opencms.workplace.explorer.Messages.GUI_NO_EDIT_REASON_LOCK_1,
                                    new CmsResourceUtil(m_cms, m_cms.readResource(folderPath)).getLockedByName()));
                        }
                    }
                }
            } else {
                permissionInfo.setNoEditReason(
                    Messages.get().getBundle(wpLocale).key(Messages.GUI_ELEMENT_RESOURCE_CAN_NOT_BE_EDITED_0));
            }
            CmsGallerySearchResult searchResult = CmsGallerySearch.searchById(
                m_cms,
                resource.getStructureId(),
                m_locale);
            title = searchResult.getTitle();
            if (CmsStringUtil.isEmptyOrWhitespaceOnly(title)) {
                resource.getName();
            }
            subTitle = searchResult.getUserLastModified();
            Date lastModDate = searchResult.getDateLastModified();
            if (lastModDate != null) {
                subTitle += " / " + CmsDateUtil.getDateTime(lastModDate, DateFormat.MEDIUM, wpLocale);
            }
        } else {
            permissionInfo = new CmsPermissionInfo(true, true, "");
            title = CmsWorkplaceMessages.getResourceTypeName(wpLocale, typeName);
            subTitle = CmsWorkplaceMessages.getResourceTypeDescription(wpLocale, typeName);
        }
        result.setTitle(title);
        result.setSubTitle(subTitle);
        result.setClientId(elementBean.editorHash());
        result.setSitePath(m_cms.getSitePath(resource));

        result.setCreateNew(elementBean.isCreateNew());
        CmsResourceTypeConfig typeConfig = getConfigData().getResourceType(typeName);
        if (typeConfig != null) {
            result.setCopyInModels(typeConfig.isCopyInModels());
        }
        Map<CmsUUID, CmsElementView> viewMap = OpenCms.getADEManager().getElementViews(m_cms);

        boolean isModelGroupEditing = CmsModelGroupHelper.isModelGroupResource(m_page);
        if (!isModelGroup
            && isModelGroupEditing
            && elementBean.getIndividualSettings().containsKey(CmsContainerElement.MODEL_GROUP_STATE)
            && (ModelGroupState.isModelGroup == ModelGroupState.evaluate(
                elementBean.getIndividualSettings().get(CmsContainerElement.MODEL_GROUP_STATE)))) {
            isModelGroup = true;
        }
        if (!isModelGroupEditing && isModelGroup) {
            CmsResourceTypeConfig modelGroupConfig = getConfigData().getResourceType(
                CmsResourceTypeXmlContainerPage.MODEL_GROUP_TYPE_NAME);
            if (modelGroupConfig != null) {
                CmsUUID elementView = modelGroupConfig.getElementView();
                CmsElementView viewObject = viewMap.get(elementView);
                if ((viewObject != null) && (viewObject.getParentViewId() != null)) {
                    elementView = viewObject.getParentViewId();
                }
                result.setElementView(elementView);
            }
        } else if (typeConfig != null) {
            CmsUUID elementView = typeConfig.getElementView();
            CmsElementView viewObject = viewMap.get(elementView);
            if ((viewObject != null) && (viewObject.getParentViewId() != null)) {
                elementView = viewObject.getParentViewId();
            }
            result.setElementView(elementView);
        }
        if (elementBean.isCreateNew()
            && CmsStringUtil.isEmptyOrWhitespaceOnly(permissionInfo.getNoEditReason())
            && ((typeConfig == null)
                || !typeConfig.checkCreatable(m_cms, CmsResource.getParentFolder(m_page.getRootPath())))) {
            String niceName = CmsWorkplaceMessages.getResourceTypeName(wpLocale, typeName);
            permissionInfo.setNoEditReason(
                Messages.get().getBundle(wpLocale).key(Messages.GUI_CONTAINERPAGE_TYPE_NOT_CREATABLE_1, niceName));
        }
        result.setHasSettings(hasSettings(m_cms, elementBean.getResource()));
        result.setPermissionInfo(permissionInfo);
        result.setReleasedAndNotExpired(elementBean.isReleasedAndNotExpired());
        if (elementBean.isModelGroup()) {
            String modelId = elementBean.getIndividualSettings().get(CmsContainerElement.MODEL_GROUP_ID);
            result.setModelGroupId(modelId != null ? new CmsUUID(modelId) : CmsUUID.getNullUUID());
        }
        result.setWasModelGroup(
            elementBean.getIndividualSettings().containsKey(CmsContainerElement.MODEL_GROUP_STATE)
                && (ModelGroupState.evaluate(
                    elementBean.getIndividualSettings().get(
                        CmsContainerElement.MODEL_GROUP_STATE)) == ModelGroupState.wasModelGroup));
        return result;
    }
590283
protected void init() throws CmsException {

        m_cms = OpenCms.initCmsObject(m_cms);
        // we want to use autocorrection when writing XML contents back
        //m_cms.getRequestContext().setAttribute(CmsXmlContent.AUTO_CORRECTION_ATTRIBUTE, Boolean.TRUE);
        m_cms.getRequestContext().setSiteRoot("");
        List<CmsPair<CmsResource, CmsResource>> allMatchingResources = Lists.newArrayList();
        for (CmsPair<String, String> pair : m_sourceTargetPairs) {
            List<CmsPair<CmsResource, CmsResource>> matchingResources = getMatchingResources(
                pair.getFirst(),
                pair.getSecond());
            allMatchingResources.addAll(matchingResources);
        }
        for (CmsPair<CmsResource, CmsResource> resPair : allMatchingResources) {
            CmsResource source = resPair.getFirst();
            CmsResource target = resPair.getSecond();
            m_translationsById.put(source.getStructureId(), target);
            m_translationsByPath.put(source.getRootPath(), target);
        }
    }
399895
public CmsContainerPageBean getPage(Object page, Object locale) {

        CmsResource pageResource = null;
        CmsContainerPageBean result = null;
        if (m_cms != null) {
            try {
                pageResource = CmsJspElFunctions.convertRawResource(m_cms, page);
                Locale l = CmsJspElFunctions.convertLocale(locale);
                result = getPage(pageResource);
                if (result != null) {
                    CmsADEConfigData adeConfig = OpenCms.getADEManager().lookupConfiguration(
                        m_cms,
                        pageResource.getRootPath());
                    for (CmsContainerBean container : result.getContainers().values()) {
                        for (CmsContainerElementBean element : container.getElements()) {
                            boolean isGroupContainer = element.isGroupContainer(m_cms);
                            boolean isInheritedContainer = element.isInheritedContainer(m_cms);
                            I_CmsFormatterBean formatterConfig = null;
                            if (!isGroupContainer && !isInheritedContainer) {
                                element.initResource(m_cms);
                                // ensure that the formatter configuration id is added to the element settings, so it will be persisted on save
                                formatterConfig = CmsJspTagContainer.getFormatterConfigurationForElement(
                                    m_cms,
                                    element,
                                    adeConfig,
                                    container.getName(),
                                    "",
                                    0);
                                if (formatterConfig != null) {
                                    element.initSettings(m_cms, formatterConfig, l, m_request, null);
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                LOG.warn(e.getLocalizedMessage(), e);
            }

        }
        return result;
    }
760146
private void saveInheritanceGroup(CmsResource resource, CmsInheritanceContainer inheritanceContainer)
    throws CmsException {

        CmsObject cms = getCmsObject();
        CmsFile file = cms.readFile(resource);
        CmsXmlContent document = CmsXmlContentFactory.unmarshal(cms, file);

        for (Locale docLocale : document.getLocales()) {
            document.removeLocale(docLocale);
        }
        Locale locale = Locale.ENGLISH;
        document.addLocale(cms, locale);
        document.getValue("Title", locale).setStringValue(cms, inheritanceContainer.getTitle());
        document.getValue("Description", locale).setStringValue(cms, inheritanceContainer.getDescription());
        document.getValue("ConfigName", locale).setStringValue(cms, inheritanceContainer.getName());
        byte[] content = document.marshal();
        file.setContents(content);
        cms.writeFile(file);
    }
1224824
private List<CmsElementComparison> getElements(I_CmsXmlDocument xmlPage) {

        List<CmsElementComparison> elements = new ArrayList<CmsElementComparison>();
        Iterator<Locale> locales = xmlPage.getLocales().iterator();
        while (locales.hasNext()) {
            Locale locale = locales.next();
            Iterator<String> elementNames = xmlPage.getNames(locale).iterator();
            while (elementNames.hasNext()) {
                String elementName = elementNames.next();
                elements.add(new CmsElementComparison(locale, elementName));
            }
        }
        return elements;
    }
936772
public void init(CmsObject cms, Locale locale, I_CmsXmlDocument content, CmsResource resource) {

        m_cms = cms;
        m_requestedLocale = locale;
        m_content = content;
        m_resource = resource;
    }
1291907
public CmsLocaleGroup readLocaleGroup(CmsResource resource) throws CmsException {

        if (resource.isFolder()) {
            CmsResource defaultFile = m_cms.readDefaultFile(resource, CmsResourceFilter.IGNORE_EXPIRATION);
            if (defaultFile != null) {
                resource = defaultFile;
            }
        }

        List<CmsRelation> relations = m_cms.readRelations(
            CmsRelationFilter.ALL.filterType(CmsRelationType.LOCALE_VARIANT).filterStructureId(
                resource.getStructureId()));
        List<CmsRelation> out = Lists.newArrayList();
        List<CmsRelation> in = Lists.newArrayList();
        for (CmsRelation rel : relations) {
            if (rel.getSourceId().equals(resource.getStructureId())) {
                out.add(rel);
            } else {
                in.add(rel);
            }
        }
        CmsResource primaryResource = null;
        List<CmsResource> secondaryResources = Lists.newArrayList();
        if ((out.size() == 0) && (in.size() == 0)) {
            primaryResource = resource;
        } else if ((out.size() == 0) && (in.size() > 0)) {
            primaryResource = resource;
            // resource is the primary variant
            for (CmsRelation relation : in) {
                CmsResource source = relation.getSource(m_cms, CmsResourceFilter.ALL);
                secondaryResources.add(source);
            }
        } else if ((out.size() == 1) && (in.size() == 0)) {

            CmsResource target = out.get(0).getTarget(m_cms, CmsResourceFilter.ALL);
            primaryResource = target;
            CmsRelationFilter filter = CmsRelationFilter.TARGETS.filterType(
                CmsRelationType.LOCALE_VARIANT).filterStructureId(target.getStructureId());
            List<CmsRelation> relationsToTarget = m_cms.readRelations(filter);
            for (CmsRelation targetRelation : relationsToTarget) {
                CmsResource secondaryResource = targetRelation.getSource(m_cms, CmsResourceFilter.ALL);
                secondaryResources.add(secondaryResource);
            }
        } else {
            throw new IllegalStateException(
                "illegal locale variant relations for resource with id="
                    + resource.getStructureId()
                    + ", path="
                    + resource.getRootPath());
        }
        return new CmsLocaleGroup(m_cms, primaryResource, secondaryResources);
    }
132397
private void setXmlContentFromFile(
        CmsResource cmsResource,
        CmsFile cmsFile,
        CmsObject cmsObject,
        String xmlContent,
        String encodingType,
        I_CmsReport report) {

        try {
            byte[] fileContent = xmlContent.getBytes(encodingType);
            cmsFile.setContents(fileContent);
            // write into file
            cmsObject.writeFile(cmsFile);
            // unlock resource
            try {
                cmsObject.unlockResource(cmsObject.getSitePath(cmsResource));
            } catch (CmsException e) {
                m_errorTransform += 1;
                report.println(
                    Messages.get().container(Messages.RPT_CONVERTXML_UNLOCK_FILE_1, cmsObject.getSitePath(cmsResource)),
                    I_CmsReport.FORMAT_ERROR);
                if (LOG.isErrorEnabled()) {
                    LOG.error(e.getMessageContainer(), e);
                }
            }
        } catch (Exception e) {
            m_errorTransform += 1;
            String reportContent = "<br/>";
            reportContent = reportContent + CmsEncoder.escapeXml(xmlContent);
            reportContent = reportContent.replaceAll("\r\n", "<br/>");
            report.println(
                Messages.get().container(Messages.RPT_CONVERTXML_WRITE_ERROR_1, reportContent),
                I_CmsReport.FORMAT_ERROR);
            if (LOG.isErrorEnabled()) {
                LOG.error(e.toString());
            }
        }
    }