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.main.CmsLog;
032import org.opencms.xml.content.CmsXmlContent;
033
034import java.util.ArrayList;
035import java.util.List;
036import java.util.Locale;
037
038import org.apache.commons.logging.Log;
039
040/**
041 * Class used to keep track of optional dynamic category fields for a content.
042 */
043public class CmsDynamicCategoryFieldList {
044
045    /** The logger instance for this class. */
046    private static final Log LOG = CmsLog.getLog(CmsDynamicCategoryFieldList.class);
047
048    /** The list of field paths. */
049    private List<String> m_paths = new ArrayList<>();
050
051    /**
052     * Creates a new instance.
053     */
054    public CmsDynamicCategoryFieldList() {
055
056        // do nothing
057    }
058
059    /**
060     * Adds a path.
061     *
062     * @param path the path to add
063     */
064    public void add(String path) {
065
066        m_paths.add(path);
067    }
068
069    /**
070     * Tries to add the collected fields to all locales of the given content.
071     *
072     * @param cms the CMS context
073     * @param content the content which the fields should be added to
074     */
075    public void ensureFields(CmsObject cms, CmsXmlContent content) {
076        for (Locale locale: content.getLocales()) {
077            ensureFields(cms, content, locale);
078        }
079    }
080
081    /**
082     * Tries to add the collected fields to one locale of the given content.
083     *
084     * @param cms the CMS context
085     * @param content the content to add the fields to
086     * @param locale the locale
087     */
088    public void ensureFields(CmsObject cms, CmsXmlContent content, Locale locale) {
089
090        for (String path : m_paths) {
091            if (!content.hasValue(path, locale)) {
092                try {
093                    content.addValue(cms, path, locale, 0);
094                } catch (Exception e) {
095                    LOG.error(e.getLocalizedMessage(), e);
096                }
097            }
098        }
099    }
100
101}