public CmsContainerPageBean prepareforModelGroupContent(
        Map<String, CmsContainerElementBean> elements,
        List<String> foundGroups,
        CmsContainerPageBean page,
        boolean alwaysCopy,
        Locale locale)
    throws CmsException {

        for (Entry<String, CmsContainerElementBean> entry : elements.entrySet()) {
            CmsContainerElementBean element = entry.getValue();
            CmsContainerPageBean modelPage = null;
            String modelInstanceId = null;
            boolean foundInstance = false;
            if (CmsModelGroupHelper.isModelGroupResource(element.getResource())) {
                modelPage = getContainerPageBean(element.getResource());
                CmsContainerElementBean baseElement = getModelBaseElement(modelPage, element.getResource());
                if (baseElement == null) {
                    break;
                }
                String baseInstanceId = baseElement.getInstanceId();
                String originalInstanceId = element.getInstanceId();
                element = getModelReplacementElement(element, baseElement, true);
                List<CmsContainerBean> modelContainers = readModelContainers(
                    baseInstanceId,
                    originalInstanceId,
                    modelPage,
                    baseElement.isCopyModel());
                if (!m_isEditingModelGroups && baseElement.isCopyModel()) {
                    modelContainers = createNewElementsForModelGroup(m_cms, modelContainers, locale);
                }
                modelContainers.addAll(page.getContainers().values());
                page = new CmsContainerPageBean(modelContainers);
                // update the entry element value, as the settings will have changed
                entry.setValue(element);
                if (m_sessionCache != null) {
                    // also update the session cache
                    m_sessionCache.setCacheContainerElement(element.editorHash(), element);
                }
            } else {
                // here we need to make sure to remove the source container page setting and to set a new element instance id

                Map<String, String> settings = new HashMap<String, String>(element.getIndividualSettings());
                String source = settings.get(CmsContainerpageService.SOURCE_CONTAINERPAGE_ID_SETTING);
                settings.remove(CmsContainerpageService.SOURCE_CONTAINERPAGE_ID_SETTING);
                // TODO: Make sure source id is available for second call

                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(source)) {
                    try {
                        CmsUUID sourceId = new CmsUUID(source);
                        CmsResource sourcePage = m_cms.readResource(sourceId);
                        if (CmsResourceTypeXmlContainerPage.isContainerPage(sourcePage)) {
                            CmsXmlContainerPage xmlCnt = CmsXmlContainerPageFactory.unmarshal(
                                m_cms,
                                m_cms.readFile(sourcePage));
                            modelPage = xmlCnt.getContainerPage(m_cms);
                            modelInstanceId = element.getInstanceId();
                        }

                        settings.remove(CmsContainerElement.ELEMENT_INSTANCE_ID);

                        boolean copyRoot = false;
                        if (alwaysCopy && (modelInstanceId != null) && (modelPage != null)) {
                            for (CmsContainerElementBean el : modelPage.getElements()) {
                                if (modelInstanceId.equals(el.getInstanceId())) {
                                    copyRoot = el.isCreateNew();
                                    break;
                                }
                            }
                        }

                        if (copyRoot) {
                            CmsObject cloneCms = OpenCms.initCmsObject(m_cms);
                            cloneCms.getRequestContext().setLocale(locale);
                            String typeName = OpenCms.getResourceManager().getResourceType(
                                element.getResource()).getTypeName();
                            CmsResourceTypeConfig typeConfig = m_configData.getResourceType(typeName);
                            if (typeConfig == null) {
                                throw new IllegalArgumentException(
                                    "Can not copy template model element '"
                                        + element.getResource().getRootPath()
                                        + "' because the resource type '"
                                        + typeName
                                        + "' is not available in this sitemap.");
                            }
                            CmsResource newResource = typeConfig.createNewElement(
                                cloneCms,
                                element.getResource(),
                                m_configData.getBasePath());

                            element = new CmsContainerElementBean(
                                newResource.getStructureId(),
                                element.getFormatterId(),
                                settings,
                                false);
                        } else {
                            element = CmsContainerElementBean.cloneWithSettings(element, settings);
                        }
                        if (modelPage != null) {
                            Map<String, List<CmsContainerBean>> containerByParent = new HashMap<String, List<CmsContainerBean>>();

                            for (CmsContainerBean container : modelPage.getContainers().values()) {
                                if (container.getParentInstanceId() != null) {
                                    if (!containerByParent.containsKey(container.getParentInstanceId())) {
                                        containerByParent.put(
                                            container.getParentInstanceId(),
                                            new ArrayList<CmsContainerBean>());
                                    }
                                    containerByParent.get(container.getParentInstanceId()).add(container);
                                }
                                if (!foundInstance) {
                                    for (CmsContainerElementBean child : container.getElements()) {
                                        if (modelInstanceId == null) {
                                            if (child.getId().equals(element.getId())) {
                                                modelInstanceId = child.getInstanceId();
                                                foundInstance = true;
                                                // we also want to keep the settings of the model group
                                                Map<String, String> setting = new HashMap<String, String>(
                                                    child.getIndividualSettings());
                                                setting.remove(CmsContainerElement.ELEMENT_INSTANCE_ID);
                                                element = CmsContainerElementBean.cloneWithSettings(element, setting);
                                                break;
                                            }
                                        } else {
                                            if (modelInstanceId.equals(child.getInstanceId())) {
                                                foundInstance = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            if (foundInstance && containerByParent.containsKey(modelInstanceId)) {
                                List<CmsContainerBean> modelContainers = collectModelStructure(
                                    modelInstanceId,
                                    element.getInstanceId(),
                                    containerByParent,
                                    true);
                                if (alwaysCopy) {
                                    modelContainers = createNewElementsForModelGroup(m_cms, modelContainers, locale);
                                }
                                foundGroups.add(element.editorHash());
                                modelContainers.addAll(page.getContainers().values());
                                page = new CmsContainerPageBean(modelContainers);
                            }
                        }

                        // update the entry element value, as the settings will have changed
                        entry.setValue(element);
                        if (m_sessionCache != null) {
                            // also update the session cache
                            m_sessionCache.setCacheContainerElement(element.editorHash(), element);
                        }
                    } catch (Exception e) {
                        LOG.info(e.getLocalizedMessage(), e);
                    }

                }
            }

        }
        return page;
    }
public CmsContainerPageBean prepareforModelGroupContent ( Map < String , CmsContainerElementBean > elements , List < String > foundGroups , CmsContainerPageBean page , boolean alwaysCopy , Locale locale ) throws CmsException { for ( Entry < String , CmsContainerElementBean > entry : elements . entrySet ( ) ) { CmsContainerElementBean element = entry . getValue ( ) ; CmsContainerPageBean modelPage = null ; String modelInstanceId = null ; boolean foundInstance = false ; if ( CmsModelGroupHelper . isModelGroupResource ( element . getResource ( ) ) ) { modelPage = getContainerPageBean ( element . getResource ( ) ) ; CmsContainerElementBean baseElement = getModelBaseElement ( modelPage , element . getResource ( ) ) ; if ( baseElement == null ) { break ; } String baseInstanceId = baseElement . getInstanceId ( ) ; String originalInstanceId = element . getInstanceId ( ) ; element = getModelReplacementElement ( element , baseElement , true ) ; List < CmsContainerBean > modelContainers = readModelContainers ( baseInstanceId , originalInstanceId , modelPage , baseElement . isCopyModel ( ) ) ; if ( ! m_isEditingModelGroups && baseElement . isCopyModel ( ) ) { modelContainers = createNewElementsForModelGroup ( m_cms , modelContainers , locale ) ; } modelContainers . addAll ( page . getContainers ( ) . values ( ) ) ; page = new CmsContainerPageBean ( modelContainers ) ; entry . setValue ( element ) ; if ( m_sessionCache != null ) { m_sessionCache . setCacheContainerElement ( element . editorHash ( ) , element ) ; } } else { Map < String , String > settings = new HashMap < String , String > ( element . getIndividualSettings ( ) ) ; String source = settings . get ( CmsContainerpageService . SOURCE_CONTAINERPAGE_ID_SETTING ) ; settings . remove ( CmsContainerpageService . SOURCE_CONTAINERPAGE_ID_SETTING ) ; if ( CmsStringUtil . isNotEmptyOrWhitespaceOnly ( source ) ) { try { CmsUUID sourceId = new CmsUUID ( source ) ; CmsResource sourcePage = m_cms . readResource ( sourceId ) ; if ( CmsResourceTypeXmlContainerPage . isContainerPage ( sourcePage ) ) { CmsXmlContainerPage xmlCnt = CmsXmlContainerPageFactory . unmarshal ( m_cms , m_cms . readFile ( sourcePage ) ) ; modelPage = xmlCnt . getContainerPage ( m_cms ) ; modelInstanceId = element . getInstanceId ( ) ; } settings . remove ( CmsContainerElement . ELEMENT_INSTANCE_ID ) ; boolean copyRoot = false ; if ( alwaysCopy && ( modelInstanceId != null ) && ( modelPage != null ) ) { for ( CmsContainerElementBean el : modelPage . getElements ( ) ) { if ( modelInstanceId . equals ( el . getInstanceId ( ) ) ) { copyRoot = el . isCreateNew ( ) ; break ; } } } if ( copyRoot ) { CmsObject cloneCms = OpenCms . initCmsObject ( m_cms ) ; cloneCms . getRequestContext ( ) . setLocale ( locale ) ; String typeName = OpenCms . getResourceManager ( ) . getResourceType ( element . getResource ( ) ) . getTypeName ( ) ; CmsResourceTypeConfig typeConfig = m_configData . getResourceType ( typeName ) ; if ( typeConfig == null ) { throw new IllegalArgumentException ( "Can not copy template model element '" + element . getResource ( ) . getRootPath ( ) + "' because the resource type '" + typeName + "' is not available in this sitemap." ) ; } CmsResource newResource = typeConfig . createNewElement ( cloneCms , element . getResource ( ) , m_configData . getBasePath ( ) ) ; element = new CmsContainerElementBean ( newResource . getStructureId ( ) , element . getFormatterId ( ) , settings , false ) ; } else { element = CmsContainerElementBean . cloneWithSettings ( element , settings ) ; } if ( modelPage != null ) { Map < String , List < CmsContainerBean > > containerByParent = new HashMap < String , List < CmsContainerBean > > ( ) ; for ( CmsContainerBean container : modelPage . getContainers ( ) . values ( ) ) { if ( container . getParentInstanceId ( ) != null ) { if ( ! containerByParent . containsKey ( container . getParentInstanceId ( ) ) ) { containerByParent . put ( container . getParentInstanceId ( ) , new ArrayList < CmsContainerBean > ( ) ) ; } containerByParent . get ( container . getParentInstanceId ( ) ) . add ( container ) ; } if ( ! foundInstance ) { for ( CmsContainerElementBean child : container . getElements ( ) ) { if ( modelInstanceId == null ) { if ( child . getId ( ) . equals ( element . getId ( ) ) ) { modelInstanceId = child . getInstanceId ( ) ; foundInstance = true ; Map < String , String > setting = new HashMap < String , String > ( child . getIndividualSettings ( ) ) ; setting . remove ( CmsContainerElement . ELEMENT_INSTANCE_ID ) ; element = CmsContainerElementBean . cloneWithSettings ( element , setting ) ; break ; } } else { if ( modelInstanceId . equals ( child . getInstanceId ( ) ) ) { foundInstance = true ; break ; } } } } } if ( foundInstance && containerByParent . containsKey ( modelInstanceId ) ) { List < CmsContainerBean > modelContainers = collectModelStructure ( modelInstanceId , element . getInstanceId ( ) , containerByParent , true ) ; if ( alwaysCopy ) { modelContainers = createNewElementsForModelGroup ( m_cms , modelContainers , locale ) ; } foundGroups . add ( element . editorHash ( ) ) ; modelContainers . addAll ( page . getContainers ( ) . values ( ) ) ; page = new CmsContainerPageBean ( modelContainers ) ; } } entry . setValue ( element ) ; if ( m_sessionCache != null ) { m_sessionCache . setCacheContainerElement ( element . editorHash ( ) , element ) ; } } catch ( Exception e ) { LOG . info ( e . getLocalizedMessage ( ) , e ) ; } } } } return page ; }
Adds the model group elements to the page.<p>

@param elements the requested elements
@param foundGroups list to add the found group element client ids to
@param page the page
@param alwaysCopy <code>true</code> to create element copies in case of non model groups and createNew is set
@param locale the content locale

@return the adjusted page

@throws CmsException in case something goes wrong