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.configuration;
029
030import org.opencms.ade.containerpage.shared.CmsFormatterConfig;
031import org.opencms.xml.containerpage.CmsContainerElementBean;
032
033import java.util.ArrayList;
034import java.util.Arrays;
035import java.util.Map;
036import java.util.Set;
037import java.util.stream.Collectors;
038
039/**
040 * Helper class for formatter-related functionality.
041 */
042public class CmsFormatterUtils {
043
044    /**
045     * Gets the set of all formatter keys from the settings.
046     *
047     * @param configData the sitemap configuration
048     * @param element the container element
049     *
050     * @return the set of all formatter keys
051     */
052    public static Set<String> getAllFormatterKeys(CmsADEConfigData configData, CmsContainerElementBean element) {
053
054        Set<String> result = element.getIndividualSettings().entrySet().stream().filter(
055            entry -> entry.getKey().startsWith(CmsFormatterConfig.FORMATTER_SETTINGS_KEY)).map(
056                entry -> entry.getValue()).collect(Collectors.toSet());
057        return result;
058
059    }
060
061    /**
062     * Gets the formatter key for the given container name from an element's settings.
063     *
064     * @param containerName the container name
065     * @param element the element from which to get the formatter key
066     *
067     * @return the formatter key
068     */
069    public static String getFormatterKey(String containerName, CmsContainerElementBean element) {
070
071        Map<String, String> settings = element.getSettings();
072        return getFormatterKey(containerName, settings);
073    }
074
075    /**
076     * Gets the formatter key for the given container name from an element's settings.
077     *
078     * @param containerName the container name
079     * @param settings the settings from which to get the formatter key
080     *
081     * @return the formatter key
082     */
083    public static String getFormatterKey(String containerName, Map<String, String> settings) {
084
085        String key1 = settings.get(CmsFormatterConfig.FORMATTER_SETTINGS_KEY + containerName);
086        String key2 = settings.get(CmsFormatterConfig.FORMATTER_SETTINGS_KEY);
087        for (String key : new String[] {key1, key2}) {
088            if (key != null) {
089                return key;
090            }
091        }
092        return null;
093    }
094
095    /**
096     * Gets the formatter key for the given container name from an element's settings.
097     *
098     * @param containerName the container name
099     * @param settings the settings from which to remove the formatter key
100     *
101     * @return the formatter key
102     */
103    public static String removeFormatterKey(String containerName, Map<String, String> settings) {
104
105        String result = null;
106        for (String mapKey : Arrays.asList(
107            CmsFormatterConfig.FORMATTER_SETTINGS_KEY + containerName,
108            CmsFormatterConfig.FORMATTER_SETTINGS_KEY)) {
109            if (settings.get(mapKey) != null) {
110                result = settings.remove(mapKey);
111                break;
112            }
113        }
114        for (String mapKey : new ArrayList<>(settings.keySet())) {
115            if (mapKey.startsWith(CmsFormatterConfig.FORMATTER_SETTINGS_KEY)) {
116                settings.remove(mapKey);
117            }
118        }
119        return result;
120    }
121
122}