001/*
002 *  Copyright 2020 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.data;
017
018import java.util.List;
019import java.util.Map;
020import java.util.Set;
021
022import org.ametys.cms.contenttype.ContentAttributeDefinition;
023import org.ametys.cms.repository.Content;
024
025/**
026 * Collection of {@link Content} referenced by an invert relation path
027 */
028public class ReferencedContents
029{
030    private ContentAttributeDefinition _definition;
031    private Map<ContentValue, List<String>> _addedContents;
032    private Map<ContentValue, List<String>> _removedContents;
033    private Map<ContentValue, ContentValue> _thirdPartyContents;
034    
035    /**
036     * Creates an instance of {@link ReferencedContents}
037     * @param definition the definition of the attribute holding content references
038     * @param addedContents the content where to add the invert relation
039     * @param removedContents the content where to remove the invert relation
040     * @param thirdPartyContents other contents involded indirectly due to the write operation
041     */
042    public ReferencedContents(ContentAttributeDefinition definition, Map<ContentValue, List<String>> addedContents, Map<ContentValue, List<String>> removedContents, Map<ContentValue, ContentValue> thirdPartyContents)
043    {
044        _definition = definition;
045        _addedContents = addedContents;
046        _removedContents = removedContents;
047        _thirdPartyContents = thirdPartyContents;
048    }
049    
050    /**
051     * Returns the attribute definition.
052     * @return the attribute definition.
053     */
054    public ContentAttributeDefinition getDefinition()
055    {
056        return _definition;
057    }
058    
059    /**
060     * Retrieve the content where to add the invert relation
061     * @return the content where to add the invert relation
062     */
063    public Set<ContentValue> getAddedContents()
064    {
065        return _addedContents.keySet();
066    }
067    
068    /**
069     * Retrieve the content where to add the invert relation, along with associated data paths
070     * @return the content where to add the invert relation
071     */
072    public Map<ContentValue, List<String>> getAddedContentsWithPaths()
073    {
074        return _addedContents;
075    }
076    
077    /**
078     * Retrieve the content where to remove the invert relation
079     * @return the content where to remove the invert relation
080     */
081    public Set<ContentValue> getRemovedContents()
082    {
083        return _removedContents.keySet();
084    }
085    
086    /**
087     * Retrieve the content where to remove the invert relation, along with associated data paths
088     * @return the content where to remove the invert relation
089     */
090    public Map<ContentValue, List<String>> getRemovedContentsWithPaths()
091    {
092        return _removedContents;
093    }
094    
095    /**
096     * Returns the contents which values will change due the current write operation.<br>
097     * They are typically contents involved in an single-valued invert relation. The key is the source content, the value is the referenced Content.
098     * @return the involved contents
099     */
100    public Map<ContentValue, ContentValue> getThirdPartyContents()
101    {
102        return _thirdPartyContents;
103    }
104    
105    @Override
106    public String toString()
107    {
108        return _definition.toString();
109    }
110}