001/*
002 *  Copyright 2014 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 */
016
017package org.ametys.cms.content.references;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.commons.lang3.StringUtils;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
027import org.ametys.plugins.repository.data.holder.group.ModelAwareRepeater;
028import org.ametys.plugins.repository.data.holder.group.ModelAwareRepeaterEntry;
029import org.ametys.plugins.repository.data.type.ModelItemTypeConstants;
030import org.ametys.runtime.model.ModelItem;
031import org.ametys.runtime.model.type.ModelItemType;
032import org.ametys.runtime.plugin.component.AbstractLogEnabled;
033
034/**
035 * The outgoing references extractor is a class that analyzes a content to extract the list of outgoing references in the metadata.
036 * Theses references should be stored on the content to allow research or post-processing later (example: consistency analyzer).
037 */
038public class OutgoingReferencesExtractor extends AbstractLogEnabled implements Component
039{
040    /** Avalon role */
041    public static final String ROLE = OutgoingReferencesExtractor.class.getName();
042
043    /**
044     * This method analyzes the content to return the list of outgoing references grouped by data path.
045     * @param content The content to analyze
046     * @return A Map where keys are data path and values are outgoing references for this data. Can not be null.
047     */
048    public Map<String, OutgoingReferences> getOutgoingReferences(Content content)
049    {
050        return _getOutgoingReferences(content, StringUtils.EMPTY);
051    }
052
053    /**
054     * Analyze a data holder to extract the outgoing references
055     * @param dataHolder the data holder to analyze
056     * @param dataPathPrefix the data path of the current data holder
057     * @return the outgoing references, indexed by data path
058     */
059    protected Map<String, OutgoingReferences> _getOutgoingReferences(ModelAwareDataHolder dataHolder, String dataPathPrefix)
060    {
061        Map<String, OutgoingReferences> outgoingReferencesByPath = new HashMap<>();
062        
063        for (String dataName : dataHolder.getDataNames())
064        {
065            ModelItem definition = dataHolder.getDefinition(dataName);
066            ModelItemType type = definition.getType();
067
068            String dataPath = StringUtils.isNotEmpty(dataPathPrefix) ? dataPathPrefix + ModelItem.ITEM_PATH_SEPARATOR + dataName : dataName;
069
070            if (type instanceof Referencer referencer)
071            {
072                Object value = dataHolder.getValue(dataName);
073                OutgoingReferences outgoingReferences = referencer.getOutgoingReferences(value);
074                if (!outgoingReferences.isEmpty())
075                {
076                    outgoingReferencesByPath.put(dataPath, outgoingReferences);
077                }
078            }
079            else if (ModelItemTypeConstants.REPEATER_TYPE_ID.equals(type.getId()))
080            {
081                ModelAwareRepeater repeater = dataHolder.getRepeater(dataName);
082                for (ModelAwareRepeaterEntry entry : repeater.getEntries())
083                {
084                    Map<String, OutgoingReferences> entryOutgoingReferences = _getOutgoingReferences(entry, dataPath + '[' + entry.getPosition() + ']');
085                    outgoingReferencesByPath.putAll(entryOutgoingReferences);
086                }
087            }
088            else if (ModelItemTypeConstants.COMPOSITE_TYPE_ID.equals(type.getId()))
089            {
090                Map<String, OutgoingReferences> compOutgoingReferences = _getOutgoingReferences(dataHolder.getComposite(dataName), dataPath);
091                outgoingReferencesByPath.putAll(compOutgoingReferences);
092            }
093        }
094
095        return outgoingReferencesByPath;
096    }
097}