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.ade.detailpage; 029 030import org.opencms.file.CmsFile; 031import org.opencms.file.CmsObject; 032import org.opencms.file.CmsResource; 033import org.opencms.i18n.CmsLocaleManager; 034import org.opencms.main.CmsException; 035import org.opencms.util.CmsUUID; 036import org.opencms.xml.I_CmsXmlDocument; 037import org.opencms.xml.content.CmsXmlContent; 038import org.opencms.xml.content.CmsXmlContentFactory; 039import org.opencms.xml.types.CmsXmlVfsFileValue; 040import org.opencms.xml.types.I_CmsXmlContentValue; 041 042import java.util.List; 043import java.util.Locale; 044 045/** 046 * Class for writing detail page information to an XML configuration file.<p> 047 * 048 * @since 8.0.0 049 */ 050public class CmsDetailPageConfigurationWriter { 051 052 /** The detail page node. */ 053 public static final String N_DETAIL_PAGE = "DetailPage"; 054 055 /** The name of the node containing the reference to the actual detail page. */ 056 public static final String N_PAGE = "Page"; 057 058 /** The name of the node which contains the type which the detail page renders. */ 059 public static final String N_TYPE = "Type"; 060 061 /** The CMS context. */ 062 private CmsObject m_cms; 063 064 /** The content of the configuration file. */ 065 private CmsXmlContent m_document; 066 067 /** The configuration file record. */ 068 private CmsFile m_file; 069 070 /** The configuration file resource. */ 071 private CmsResource m_resource; 072 073 /** 074 * Creates a new detail page configuration writer.<p> 075 * 076 * @param cms the current CMS context 077 * @param res the configuration file resource 078 */ 079 public CmsDetailPageConfigurationWriter(CmsObject cms, CmsResource res) { 080 081 m_cms = cms; 082 m_resource = res; 083 } 084 085 /** 086 * Writes the new detail page information to the configuration file.<p> 087 * 088 * @param infos the new detail page information 089 * @param newId the id to use for new pages 090 * 091 * @throws CmsException if something goes wrong 092 */ 093 public void updateAndSave(List<CmsDetailPageInfo> infos, CmsUUID newId) throws CmsException { 094 095 if (m_resource != null) { 096 getDocument(); 097 removeOldValues(); 098 writeDetailPageInfos(infos, newId); 099 m_document.setAutoCorrectionEnabled(true); 100 m_document.correctXmlStructure(m_cms); 101 byte[] content = m_document.marshal(); 102 m_file.setContents(content); 103 m_cms.writeFile(m_file); 104 } 105 } 106 107 /** 108 * Helper method for loading the XML content from the configuration file.<p> 109 * 110 * @return the parsed XML document 111 * 112 * @throws CmsException if something goes wrong 113 */ 114 private I_CmsXmlDocument getDocument() throws CmsException { 115 116 if (m_document == null) { 117 m_file = m_cms.readFile(m_resource); 118 CmsXmlContent content = CmsXmlContentFactory.unmarshal(m_cms, m_file); 119 m_document = content; 120 } 121 return m_document; 122 } 123 124 /** 125 * Helper method for getting the locale from which to read the configuration data.<p> 126 * 127 * @return the locale from which to read the configuration data 128 * 129 * @throws CmsException if something goes wrong 130 */ 131 private Locale getLocale() throws CmsException { 132 133 getDocument(); 134 List<Locale> locales = m_document.getLocales(); 135 if (locales.contains(Locale.ENGLISH) || locales.isEmpty()) { 136 return Locale.ENGLISH; 137 } 138 return locales.get(0); 139 } 140 141 /** 142 * Removes the old detail page information from the XML content.<p> 143 * 144 * @throws CmsException if something goes wrong 145 */ 146 private void removeOldValues() throws CmsException { 147 148 Locale locale = getLocale(); 149 I_CmsXmlContentValue value = m_document.getValue(N_DETAIL_PAGE, locale); 150 do { 151 value = m_document.getValue(N_DETAIL_PAGE, locale); 152 if (value != null) { 153 m_document.removeValue(value.getPath(), locale, 0); 154 } 155 } while (value != null); 156 } 157 158 /** 159 * Writes the detail page information to the XML content.<p> 160 * 161 * @param infos the list of detail page information bean 162 * @param newId the id to use for new pages 163 */ 164 private void writeDetailPageInfos(List<CmsDetailPageInfo> infos, CmsUUID newId) { 165 166 int i = 0; 167 for (CmsDetailPageInfo info : infos) { 168 if (info.isInherited()) { 169 continue; 170 } 171 CmsUUID id = info.getId(); 172 if (id == null) { 173 id = newId; 174 } 175 writeValue(info.getQualifiedType(), id, i); 176 i += 1; 177 } 178 } 179 180 /** 181 * Writes a single item of detail page information to the XML content.<p> 182 * 183 * @param type the type which the detail page should render 184 * @param id the page id of the detail page 185 * @param index the position at which the detail page info should be added 186 */ 187 private void writeValue(String type, CmsUUID id, int index) { 188 189 Locale locale = CmsLocaleManager.getLocale("en"); 190 // todo: check actual locale. 191 m_document.addValue(m_cms, N_DETAIL_PAGE, locale, index); 192 String typePath = N_DETAIL_PAGE + "[" + (1 + index) + "]/" + N_TYPE; 193 I_CmsXmlContentValue typeVal = m_document.getValue(typePath, locale); 194 String pagePath = N_DETAIL_PAGE + "[" + (1 + index) + "]/" + N_PAGE; 195 CmsXmlVfsFileValue pageVal = (CmsXmlVfsFileValue)m_document.getValue(pagePath, locale); 196 typeVal.setStringValue(m_cms, type); 197 pageVal.setIdValue(m_cms, id); 198 } 199}