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.Collection;
019import java.util.Optional;
020
021import org.ametys.plugins.repository.data.external.ExternalizableDataProvider.ExternalizableDataStatus;
022import org.ametys.plugins.repository.data.holder.values.SynchronizationContext;
023
024/**
025 * Object that gives some context for content attributes synchronization
026 */
027public class ContentSynchronizationContext extends SynchronizationContext
028{
029    private boolean _enableInvertRelations = true;
030    private Optional<Collection<ReferencedContents>> _referencedContents = Optional.empty();
031    
032    /**
033     * Creates a new instance of a {@link ContentSynchronizationContext}
034     */
035    protected ContentSynchronizationContext()
036    {
037        // Empty constructor
038    }
039    
040    /**
041     * Creates a new instance of a {@link ContentSynchronizationContext}
042     * @return the created instance
043     */
044    public static ContentSynchronizationContext newInstance()
045    {
046        return new ContentSynchronizationContext();
047    }
048    
049    /**
050     * Determines if the invert relations of the content attributes have to be synchronized
051     * @return <code>true</code> if the invert relations of the content attributes have to be synchronized, <code>false</code> otherwise.
052     */
053    public boolean invertRelationsEnabled()
054    {
055        return _enableInvertRelations;
056    }
057    
058    /**
059     * Set to <code>false</code> to avoid to synchronize the invert relations of the content attributes (default to <code>true</code>)
060     * @param enableInvertRelations <code>true</code> to synchronized the invert relations of the content attributes, <code>false</code> otherwise.
061     * @return the current {@link ContentSynchronizationContext}
062     */
063    public ContentSynchronizationContext withInvertRelations(boolean enableInvertRelations)
064    {
065        _enableInvertRelations = enableInvertRelations;
066        return this;
067    }
068    
069    /**
070     * Retrieves the referenced contents to modify for invert relations
071     * @return the referenced contents to modify for invert relations
072     */
073    public Optional<Collection<ReferencedContents>> getReferencedContents()
074    {
075        return _referencedContents;
076    }
077    
078    /**
079     * Sets the referenced contents to modify for invert relations
080     * @param referencedContents the {@link ReferencedContents}s to set
081     * @return the current {@link ContentSynchronizationContext}
082     */
083    public ContentSynchronizationContext withReferencedContents(Collection<ReferencedContents> referencedContents)
084    {
085        _referencedContents = Optional.ofNullable(referencedContents);
086        return this;
087    }
088    
089    @Override
090    public ContentSynchronizationContext withDefaultFromModel(boolean useDefaultFromModel)
091    {
092        return (ContentSynchronizationContext) super.withDefaultFromModel(useDefaultFromModel);
093    }
094    
095    @Override
096    public ContentSynchronizationContext withStatus(ExternalizableDataStatus externalizableDataStatus)
097    {
098        return (ContentSynchronizationContext) super.withStatus(externalizableDataStatus);
099    }
100}