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.plugins.repository.data.holder.values;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.ametys.plugins.repository.data.external.ExternalizableDataProvider.ExternalizableDataStatus;
022
023/**
024 * Object that gives some context for values synchronization
025 */
026public class SynchronizationContext
027{
028    private boolean _useDefaultFromModel;
029    private ExternalizableDataStatus _externalizableDataStatus = ExternalizableDataStatus.LOCAL;
030    private Map<String, Object> _externalizableDataContext = new HashMap<>();
031    private boolean _forceStatusIfNotPresent = true;
032
033    /**
034     * Creates a new instance of a {@link SynchronizationContext}
035     */
036    protected SynchronizationContext()
037    {
038        // Empty constructor
039    }
040    
041    /**
042     * Creates a new instance of a {@link SynchronizationContext}
043     * @return the created instance
044     */
045    public static SynchronizationContext newInstance()
046    {
047        return new SynchronizationContext();
048    }
049
050    /**
051     * Checks if synchronization has to use the default value from the model
052     * @return <code>true</code> to use the default value from the model, <code>false</code> otherwise
053     */
054    public boolean useDefaultFromModel()
055    {
056        return _useDefaultFromModel;
057    }
058    
059    /**
060     * Set to <code>true</code> to use the default value from the model (default to <code>false</code>) 
061     * @param useDefaultFromModel <code>true</code> to use the default value from the model, <code>false</code> otherwise
062     * @return the current {@link SynchronizationContext}
063     */
064    public SynchronizationContext withDefaultFromModel(boolean useDefaultFromModel)
065    {
066        _useDefaultFromModel = useDefaultFromModel;
067        return this;
068    }
069
070    /**
071     * Determines which values (locals or externals) have to be synchronized
072     * @return the status of the value to synchronize
073     */
074    public ExternalizableDataStatus getStatusToSynchronize()
075    {
076        return _externalizableDataStatus;
077    }
078    
079    /**
080     * Sets the status to determine which values (locals or externals) have to be synchronized (default to local)
081     * @param externalizableDataStatus the status to determine which values have to be synchronized
082     * @return the current {@link SynchronizationContext}
083     */
084    public SynchronizationContext withStatus(ExternalizableDataStatus externalizableDataStatus)
085    {
086        _externalizableDataStatus = externalizableDataStatus;
087        return this;
088    }
089    
090    /**
091     * Retrieves the context {@link Map} that is used to determine if a data is externalizable
092     * @return the context {@link Map}
093     */
094    public Map<String, Object> getExternalizableDataContext()
095    {
096        return _externalizableDataContext;
097    }
098    
099    /**
100     * Add an entry in the context {@link Map} that is used to determine if a data is externalizable
101     * @param entryKey the key of the entry
102     * @param entryValue the value of the entry
103     * @return the current {@link SynchronizationContext}
104     */
105    public SynchronizationContext withExternalizableDataContextEntry(String entryKey, Object entryValue)
106    {
107        _externalizableDataContext.put(entryKey, entryValue);
108        return this;
109    }
110    
111    /**
112     * Determines if the status has to be forced if there is no status yet
113     * This case can happen if a data is newly externalizable
114     * @return <code>true</code> if the status has to be forced, <code>false</code> otherwise
115     */
116    public boolean forceStatusIfNotPresent()
117    {
118        return _forceStatusIfNotPresent;
119    }
120    
121    /**
122     * Set to <code>false</code> if the status has not to be forced (default to <code>true</code>)
123     * @param forceStatusIfNotPresent <code>true</code> to force the status if it is not yet present, <code>false</code> otherwise
124     * @return the current {@link SynchronizationContext}
125     */
126    public SynchronizationContext withStatusForcedIfNotPresent(boolean forceStatusIfNotPresent)
127    {
128        _forceStatusIfNotPresent = forceStatusIfNotPresent;
129        return this;
130    }
131}