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.file.CmsProperty;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.util.CmsStringUtil;
035import org.opencms.xml.content.CmsXmlContent;
036import org.opencms.xml.types.I_CmsXmlContentValue;
037
038import java.util.Collection;
039import java.util.Locale;
040
041import org.apache.commons.logging.Log;
042
043/**
044 * Handles editor content changes to read OpenCms resource properties and insert their values into the edited content.<p>
045 */
046public class CmsEditorChangeHandlerProperty extends A_CmsXmlContentEditorChangeHandler {
047
048    /** The logger instance. */
049    protected static final Log LOG = CmsLog.getLog(CmsEditorChangeHandlerProperty.class);
050
051    /** The property to read. */
052    private String m_propertyName;
053
054    /** The content field to manipulate. */
055    private String m_targetField;
056
057    /**
058     * @see org.opencms.xml.content.I_CmsXmlContentEditorChangeHandler#handleChange(org.opencms.file.CmsObject, org.opencms.xml.content.CmsXmlContent, java.util.Locale, java.util.Collection)
059     */
060    public CmsXmlContent handleChange(
061        CmsObject cms,
062        CmsXmlContent content,
063        Locale locale,
064        Collection<String> changedPaths) {
065
066        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_propertyName)
067            && CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_targetField)) {
068            for (String path : changedPaths) {
069                I_CmsXmlContentValue value = content.getValue(path, locale);
070                if (value != null) {
071                    String val = value.getStringValue(cms);
072                    if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(val)) {
073                        try {
074                            CmsProperty prop = cms.readPropertyObject(val, m_propertyName, false);
075
076                            if (!prop.isNullProperty()) {
077                                String target = resolveRelativePath(path, m_targetField);
078                                if (content.hasValue(target, locale)) {
079                                    content.getValue(target, locale).setStringValue(cms, prop.getValue());
080                                } else {
081                                    content.addValue(cms, target, locale, 0).setStringValue(cms, prop.getValue());
082                                }
083                            }
084                        } catch (CmsException e) {
085                            LOG.error(e.getLocalizedMessage(), e);
086                        }
087                    }
088                }
089            }
090        }
091        return content;
092    }
093
094    /**
095     * @see org.opencms.xml.content.I_CmsXmlContentEditorChangeHandler#setConfiguration(java.lang.String)
096     */
097    @Override
098    public void setConfiguration(String configuration) {
099
100        super.setConfiguration(configuration);
101        String[] temp = m_configuration.split("\\|");
102        if (temp.length == 2) {
103            m_propertyName = temp[0];
104            m_targetField = temp[1];
105        }
106    }
107}