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