org.opencms.ade.containerpage.inherited.CmsInheritedContainerState Maven / Gradle / Ivy
Show all versions of opencms-test Show documentation
/*
* File : $Source$
* Date : $Date$
* Version: $Revision$
*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (C) 2002 - 2011 Alkacon Software (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.ade.containerpage.inherited;
import org.opencms.ade.containerpage.shared.CmsInheritanceInfo;
import org.opencms.file.CmsResource;
import org.opencms.xml.containerpage.CmsContainerElementBean;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.Lists;
/**
* The state of an inherited container at a given point in the VFS tree.
*/
public class CmsInheritedContainerState {
/** The list of configuration beans for each tree level, from top to bottom. */
private List m_parentConfigurations = new ArrayList();
/**
* Default constructor.
*/
public CmsInheritedContainerState() {
}
/**
* Reads the configurations for a root path and its parents from a cache instance and adds them to this state.
*
* @param cache the cache instance
* @param rootPath the root path
* @param name the name of the container configuration
*/
public void addConfigurations(CmsContainerConfigurationCache cache, String rootPath, String name) {
String currentPath = rootPath;
List configurations = new ArrayList();
CmsContainerConfigurationCacheState state = cache.getState();
while (currentPath != null) {
CmsContainerConfiguration configuration = state.getContainerConfiguration(currentPath, name);
if (configuration == null) {
configuration = CmsContainerConfiguration.emptyConfiguration();
}
configuration.setPath(currentPath);
configurations.add(configuration);
currentPath = CmsResource.getParentFolder(currentPath);
}
Collections.reverse(configurations);
for (CmsContainerConfiguration configuration : configurations) {
if (configuration != null) {
addConfiguration(configuration);
}
}
}
/**
* Gets a list of container element beans which represent the state of the inherited container.
*
* The container element beans returned will have additional information available via the getInheritanceInfo method.
*
* @param includeHidden if true, hidden elements will be included in the result list
*
* @return the elements for this container state
*/
public List getElements(boolean includeHidden) {
if (m_parentConfigurations.isEmpty()) {
return Collections.emptyList();
}
Map elementsByKey = new HashMap();
for (CmsContainerConfiguration bean : m_parentConfigurations) {
elementsByKey.putAll(bean.getNewElements());
}
List result = new ArrayList();
CmsContainerConfiguration lastElement = m_parentConfigurations.get(m_parentConfigurations.size() - 1);
Set newKeys = new HashSet();
for (Map.Entry entry : lastElement.getNewElements().entrySet()) {
String key = entry.getKey();
newKeys.add(key);
}
Set keysUsed = new HashSet();
Map pathsByKey = new HashMap();
// STEP 1: Get first defined ordering
List ordering = null;
for (CmsContainerConfiguration configuration : Lists.reverse(m_parentConfigurations)) {
if (configuration.getOrdering() != null) {
ordering = configuration.getOrdering();
break;
}
}
if (ordering == null) {
ordering = new ArrayList();
}
// STEP 2: Get elements which are referenced by the ordering
for (String key : ordering) {
CmsContainerElementBean element = elementsByKey.get(key);
if ((element != null) && !keysUsed.contains(key)) {
CmsContainerElementBean elementToAdd = CmsContainerElementBean.cloneWithSettings(
element,
element.getIndividualSettings());
CmsInheritanceInfo info = new CmsInheritanceInfo();
info.setKey(key);
elementToAdd.setInheritanceInfo(info);
result.add(elementToAdd);
keysUsed.add(key);
}
}
// STEP 3: Add 'new' elements from parents; also fill pathsByKey
for (int i = 0; i < (m_parentConfigurations.size()); i++) {
CmsContainerConfiguration currentConfig = m_parentConfigurations.get(i);
for (Map.Entry entry : currentConfig.getNewElementsInOrder().entrySet()) {
String key = entry.getKey();
pathsByKey.put(key, currentConfig.getPath());
if (!keysUsed.contains(key)) {
CmsContainerElementBean elementToAdd = CmsContainerElementBean.cloneWithSettings(
entry.getValue(),
entry.getValue().getIndividualSettings());
CmsInheritanceInfo info = new CmsInheritanceInfo();
info.setKey(key);
elementToAdd.setInheritanceInfo(info);
result.add(elementToAdd);
}
}
}
// STEP 4: Determine visibility and new-ness
for (CmsContainerElementBean resultElement : result) {
CmsInheritanceInfo info = resultElement.getInheritanceInfo();
String key = info.getKey();
List visibilities = getVisibilities(key);
computeVisibility(visibilities, info);
info.setIsNew(newKeys.contains(info.getKey()));
}
List resultWithoutHidden = new ArrayList();
List hiddenElements = new ArrayList();
for (CmsContainerElementBean resultElement : result) {
CmsInheritanceInfo info = resultElement.getInheritanceInfo();
if (!info.isVisible()) {
hiddenElements.add(resultElement);
} else {
resultWithoutHidden.add(resultElement);
}
}
result = resultWithoutHidden;
if (includeHidden) {
result.addAll(hiddenElements);
}
for (CmsContainerElementBean elementBean : result) {
CmsInheritanceInfo info = elementBean.getInheritanceInfo();
String path = pathsByKey.get(info.getKey());
info.setPath(path);
}
return result;
}
/**
* Gets the keys of new elements.
*
* @return a set containing the keys of the new elements
*/
public Set getNewElementKeys() {
Set result = new HashSet();
for (CmsContainerConfiguration configuration : m_parentConfigurations) {
result.addAll(configuration.getNewElements().keySet());
}
return result;
}
/**
* Checks whether an element with the given key is actually defined in this inherited container state.
*
* @param key the key for which the check should be performed
*
* @return true if an element with the key has been defined in this state
*/
public boolean hasElementWithKey(String key) {
for (CmsContainerConfiguration configuration : m_parentConfigurations) {
if (configuration.getNewElements().containsKey(key)) {
return true;
}
}
return false;
}
/**
* Adds a configuration bean for a new tree level.
*
* @param configuration the configuration bean
*/
protected void addConfiguration(CmsContainerConfiguration configuration) {
m_parentConfigurations.add(configuration);
}
/**
* Gets the list of visibilities for a given key in all the tree levels.
*
* @param key the key for which the visibilities should be returned
*
* @return the list of visibilities, from top to bottom
*/
protected List getVisibilities(String key) {
List result = new ArrayList();
for (CmsContainerConfiguration config : m_parentConfigurations) {
result.add(config.getVisibility().get(key));
}
return result;
}
/**
* Computes the visibility for an element.
*
* @param visibilities the visibilities for the element in the sequence of parent configurations.
*
* @param info the object in which the visibility should be stored
*/
void computeVisibility(List visibilities, CmsInheritanceInfo info) {
boolean visible = true;
boolean inherited = true;
boolean parentVisible = true;
for (Boolean visibility : visibilities) {
parentVisible = visible;
if (visibility == Boolean.TRUE) {
visible = true;
inherited = false;
} else if (visibility == Boolean.FALSE) {
visible = false;
inherited = false;
} else {
inherited = true;
}
}
info.setVisible(visible);
info.setVisibilityInherited(inherited);
info.setParentVisible(parentVisible);
}
}