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.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.lock.CmsLockUtil; 033import org.opencms.main.CmsException; 034import org.opencms.xml.containerpage.CmsContainerElementBean; 035import org.opencms.xml.containerpage.CmsContainerPageBean; 036import org.opencms.xml.containerpage.CmsXmlContainerPage; 037import org.opencms.xml.containerpage.CmsXmlContainerPageFactory; 038 039/** 040 * 'Wrapper' around XML container page used for programmatic editing operations on container pages. 041 * <p> 042 * Uses mutable helper classes for the container page and its containers. 043 */ 044public class CmsContainerPageWrapper { 045 046 /** The mutable bean containing the container page data. */ 047 private CmsMutableContainerPage m_page; 048 049 /** The underlying XML container page. */ 050 private CmsXmlContainerPage m_xml; 051 052 /** The CMS context. */ 053 private CmsObject m_cms; 054 055 /** 056 * Creates a new instance by reading the container page from a file. 057 * 058 * @param cms the CMS context 059 * @param res the resource 060 * @throws CmsException if something goes wrong 061 */ 062 public CmsContainerPageWrapper(CmsObject cms, CmsResource res) 063 throws CmsException { 064 065 m_cms = cms; 066 m_xml = CmsXmlContainerPageFactory.unmarshal(cms, cms.readFile(res)); 067 m_page = CmsMutableContainerPage.fromImmutable(m_xml.getContainerPage(cms)); 068 } 069 070 /** 071 * Creates a new instance from an existing XML container page object. 072 * 073 * @param cms the CMS context 074 * @param xml the XML container page object 075 */ 076 public CmsContainerPageWrapper(CmsObject cms, CmsXmlContainerPage xml) { 077 078 m_cms = cms; 079 m_xml = xml; 080 m_page = CmsMutableContainerPage.fromImmutable(m_xml.getContainerPage(cms)); 081 } 082 083 /** 084 * Adds an element to the given container (the first container with the given container suffix is used). 085 * 086 * @param containerName the container name or suffix 087 * @param element the element to add 088 * @return false if there was no container to add the element to, true otherwise 089 */ 090 public boolean addElementToContainer(String containerName, CmsContainerElementBean element) { 091 092 CmsMutableContainer container = page().firstContainer(containerName); 093 if (container == null) { 094 return false; 095 } 096 container.elements().add(element); 097 return true; 098 } 099 100 /** 101 * Marshals the page data without writing it to the VFS. 102 * 103 * @return the marshalled page data 104 * @throws CmsException if something goes wrong 105 */ 106 public byte[] marshal() throws CmsException { 107 108 m_xml.writeContainerPage(m_cms, m_page.toImmutable()); 109 return m_xml.marshal(); 110 111 } 112 113 /** 114 * Gets the mutable page bean instance. 115 * 116 * @return the mutable page bean 117 */ 118 public CmsMutableContainerPage page() { 119 120 return m_page; 121 } 122 123 /** 124 * Saves the page data to the VFS, using the same resource from which this object was created. 125 * 126 * @throws CmsException if something goes wrong 127 */ 128 public void saveToVfs() throws CmsException { 129 130 CmsContainerPageBean immutablePage = page().toImmutable(); 131 try (AutoCloseable c = CmsLockUtil.withLockedResources(m_cms, m_xml.getFile())) { 132 m_xml.save(m_cms, immutablePage); 133 } catch (Exception e) { 134 if (e instanceof CmsException) { 135 throw (CmsException)e; 136 } else { 137 throw new RuntimeException(e); 138 } 139 } 140 } 141 142}