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.contenteditor; 029 030import org.opencms.file.CmsObject; 031import org.opencms.search.galleries.CmsGalleryNameMacroResolver; 032import org.opencms.util.CmsStringUtil; 033import org.opencms.xml.content.CmsXmlContent; 034import org.opencms.xml.types.I_CmsXmlContentValue; 035 036import java.util.ArrayList; 037import java.util.Collection; 038import java.util.List; 039import java.util.Locale; 040 041/** 042 * A change handler that reacts to changes in a field by setting another empty field to a default value. 043 * 044 * <p>This is useful because the default value used may contain macros, which are expanded every time the 045 * change handler is called, rather than only during the initial editor load. 046 * 047 * <p>The change handler's configuration consists of two strings separated by a pipe symbol: 048 * 049 * <p>path|default 050 * 051 * <p>path is the path of the field relative to the changed field which should be filled, while default is 052 * the default value to write to the field (possibly containing macros). 053 */ 054public class CmsDelayedDefaultChangeHandler extends A_CmsXmlContentEditorChangeHandler { 055 056 /** The relative path of the target field. */ 057 private String m_relPath; 058 059 /** The default value to set. */ 060 private String m_default; 061 062 /** 063 * @see org.opencms.xml.content.I_CmsXmlContentEditorChangeHandler#handleChange(org.opencms.file.CmsObject, org.opencms.xml.content.CmsXmlContent, java.util.Locale, java.util.Collection) 064 */ 065 public CmsXmlContent handleChange( 066 CmsObject cms, 067 CmsXmlContent content, 068 Locale locale, 069 Collection<String> changedPaths) { 070 071 List<String> paths = new ArrayList<String>(changedPaths); 072 for (String changedPath : paths) { 073 String fieldPath = resolveRelativePath(changedPath, m_relPath); 074 I_CmsXmlContentValue value = content.getValue(fieldPath, locale); 075 if ((value != null) && CmsStringUtil.isEmptyOrWhitespaceOnly(value.getStringValue(cms))) { 076 CmsGalleryNameMacroResolver resolver = new CmsGalleryNameMacroResolver(cms, content, locale); 077 String newVal = resolver.resolveMacros(m_default); 078 value.setStringValue(cms, newVal); 079 } 080 } 081 return content; 082 } 083 084 /** 085 * @see org.opencms.ade.contenteditor.A_CmsXmlContentEditorChangeHandler#setConfiguration(java.lang.String) 086 */ 087 @Override 088 public void setConfiguration(String configuration) { 089 090 super.setConfiguration(configuration); 091 int pipePos = configuration.indexOf("|"); 092 if ((pipePos < 0) || (pipePos == (configuration.length() - 1))) { 093 throw new RuntimeException("Invalid configuration for " + getClass().getName()); 094 } 095 m_relPath = configuration.substring(0, pipePos); 096 m_default = configuration.substring(pipePos + 1, configuration.length()); 097 } 098 099}