001/*
002 *  Copyright 2021 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.duplicate.contents;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.avalon.framework.configuration.Configuration;
025import org.apache.avalon.framework.configuration.ConfigurationException;
026import org.apache.commons.lang3.tuple.Pair;
027
028/**
029 * Represents the global configuration of content duplicate detection configuration
030 */
031public class DuplicateContentConfiguration
032{
033    private Map<String, DuplicateContentTypeConfiguration> _contentTypes;
034    private List<Pair<String, List<Object>>> _errorList = new ArrayList<>();
035    private List<Pair<String, List<Object>>> _warnList = new ArrayList<>();
036
037    /**
038     * Create a new DuplicateContentConfiguration
039     * @param configuration the configuration used to create the DuplicateContentConfiguration
040     * @param duplicateContentsManager the duplicate contents manager, only use this parameter to get linked components
041     * @throws ConfigurationException if the configuration is not valid.
042     */
043    public DuplicateContentConfiguration(Configuration configuration, DuplicateContentsManager duplicateContentsManager) throws ConfigurationException
044    {
045        _contentTypes = new HashMap<>();
046        Configuration[] contentTypes = configuration.getChildren("content-type");
047        if (contentTypes.length == 0)
048        {
049            _warnList.add(Pair.of("Missing duplicate-contents.xml file inside WEB-INF/param folder", List.of()));
050        }
051        for (Configuration config : contentTypes)
052        {
053            DuplicateContentTypeConfiguration duplicateContentTypeConfiguration = new DuplicateContentTypeConfiguration(config, duplicateContentsManager);
054            if (!duplicateContentTypeConfiguration.getAttributeList().isEmpty())
055            {
056                _contentTypes.put(duplicateContentTypeConfiguration.getContentTypeId(), duplicateContentTypeConfiguration);
057            }
058            _errorList.addAll(duplicateContentTypeConfiguration.getErrors());
059            _warnList.addAll(duplicateContentTypeConfiguration.getWarns());
060        }
061    }
062
063    /**
064     * Get the content type configuration map
065     * @return the content type configuration map
066     */
067    public Map<String, DuplicateContentTypeConfiguration> getContentTypes()
068    {
069        return _contentTypes;
070    }
071
072    /**
073     * Set the content type configuration map
074     * @param contentTypes the content type configuration map
075     */
076    public void setContentTypes(Map<String, DuplicateContentTypeConfiguration> contentTypes)
077    {
078        this._contentTypes = contentTypes;
079    }
080
081    /**
082     * Get the list of duplicate content types
083     * @return the list of duplicate content types
084     */
085    public Set<String> getDuplicatesContentTypes()
086    {
087        return _contentTypes.keySet();
088    }
089
090    /**
091     * Get the DuplicateContentTypeConfiguration related to a given content type
092     * @param duplicateContentTypeId the id of the content type
093     * @return the DuplicateContentTypeConfiguration 
094     */
095    public DuplicateContentTypeConfiguration get(String duplicateContentTypeId)
096    {
097        return _contentTypes.get(duplicateContentTypeId);
098    }
099
100    /**
101     * get the list of errors
102     * @return the error list
103     */
104    public List<Pair<String, List<Object>>> getErrors()
105    {
106        return _errorList;
107    }
108
109    /**
110     * get the list of warns
111     * @return the warn list
112     */
113    public List<Pair<String, List<Object>>> getWarns()
114    {
115        return _warnList;
116    }
117}