001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.xml.containerpage.mutable; 029 030import org.opencms.xml.containerpage.CmsContainerBean; 031import org.opencms.xml.containerpage.CmsContainerElementBean; 032 033import java.util.ArrayList; 034import java.util.List; 035import java.util.stream.Collectors; 036 037/** 038 * A mutable bean representing a container for use in programmaticall editing container pages. 039 * 040 */ 041public class CmsMutableContainer { 042 043 /** The mutable list of container elements. */ 044 private List<CmsContainerElementBean> m_elements = new ArrayList<>(); 045 046 /** The container name. */ 047 private String m_name; 048 049 /** The container type. */ 050 private String m_type; 051 052 /** The parent instance id. */ 053 private String m_parentInstanceId; 054 055 /** True if this is a root container. */ 056 private boolean m_isRootContainer; 057 058 /** 059 * Creates a new instance. 060 * 061 * @param name the container name 062 * @param type the container type 063 * @param parentInstanceId the parent instance id 064 * @param isRootContainer true if this is a root container 065 * @param elements the list of container elements (will be copied) 066 */ 067 public CmsMutableContainer( 068 String name, 069 String type, 070 String parentInstanceId, 071 boolean isRootContainer, 072 List<CmsContainerElementBean> elements) { 073 074 m_name = name; 075 m_type = type; 076 m_parentInstanceId = parentInstanceId; 077 m_isRootContainer = isRootContainer; 078 m_elements = elements.stream().map(elem -> elem.clone()).collect(Collectors.toList()); 079 } 080 081 /** 082 * Converts a CmsContainerBean to an instance of this class. 083 * 084 * @param container the immutable container bean 085 * @return the new instance 086 */ 087 public static CmsMutableContainer fromImmutable(CmsContainerBean container) { 088 089 return new CmsMutableContainer( 090 container.getName(), 091 container.getType(), 092 container.getParentInstanceId(), 093 container.isRootContainer(), 094 container.getElements()); 095 096 } 097 098 /** 099 * Gets the mutable list of container elements. 100 * 101 * @return the list of container elements 102 */ 103 public List<CmsContainerElementBean> elements() { 104 105 return m_elements; 106 } 107 108 /** 109 * Gets the container name. 110 * 111 * @return the container name 112 */ 113 public String getName() { 114 115 return m_name; 116 } 117 118 /** 119 * Gets the parent instance id. 120 * 121 * @return the parent instance id 122 */ 123 public String getParentInstanceId() { 124 125 return m_parentInstanceId; 126 } 127 128 /** 129 * Gets the container type 130 * 131 * @return the container type 132 */ 133 public String getType() { 134 135 return m_type; 136 } 137 138 /** 139 * Returns true if this is a root container. 140 * 141 * @return true if this is a root container 142 */ 143 public boolean isRootContainer() { 144 145 return m_isRootContainer; 146 } 147 148 /** 149 * Checks if the container matches the given user-readable name. 150 *<p> 151 * Because of nested containers, container names are not always just the values given to the cms:container tag, 152 * but can also have a prefix consisting of the parent instance id of the element which contains them. 153 * 154 * @param name the user-readable name 155 * @return true if the container matches the name 156 */ 157 public boolean matches(String name) { 158 159 return m_name.equals(name) || m_name.endsWith("-" + name); 160 } 161 162 /** 163 * Sets the container name. 164 * 165 * @param name the container name 166 */ 167 public void setName(String name) { 168 169 m_name = name; 170 } 171 172 /** 173 * Sets the parent instance id. 174 * 175 * @param parentInstanceId the parent instance id 176 */ 177 public void setParentInstanceId(String parentInstanceId) { 178 179 m_parentInstanceId = parentInstanceId; 180 } 181 182 /** 183 * Sets the 'is root container' property. 184 * 185 * @param isRootContainer true if this should be set as a root container 186 */ 187 public void setRootContainer(boolean isRootContainer) { 188 189 m_isRootContainer = isRootContainer; 190 } 191 192 /** 193 * Sets the type. 194 * 195 * @param type the type 196 */ 197 public void setType(String type) { 198 199 m_type = type; 200 } 201 202 /** 203 * Converts this bean to a CmsContainerBean. 204 * 205 * @return a new CmsContainerBean with the data from this bean 206 */ 207 public CmsContainerBean toImmutable() { 208 209 return new CmsContainerBean( 210 m_name, 211 m_type, 212 m_parentInstanceId, 213 m_isRootContainer, 214 new ArrayList<CmsContainerElementBean>(m_elements)); 215 } 216 217}