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.List;
019import java.util.Optional;
020
021import org.ametys.plugins.repository.data.DataComment;
022import org.ametys.plugins.repository.data.external.ExternalizableDataProvider.ExternalizableDataStatus;
023
024/**
025 * Wrapper for a synchronizable value
026 * Contains the value itself and indicates if it is the external value
027 */
028public class SynchronizableValue
029{
030    private Object _localValue;
031    private Object _externalValue;
032    private ExternalizableDataStatus _externalizableDataStatus;
033    private List<DataComment> _comments;
034    private Mode _mode = Mode.REPLACE;
035    
036    /**
037     * The value write mode.
038     */
039    public static enum Mode
040    {
041        /** The value will replace existing values */
042        REPLACE,
043        /** The value will be appended to existing values */
044        APPEND,
045        /** The value will be removed if found among existing values */
046        REMOVE
047    }
048    
049    /**
050     * Constructor.
051     * @param localValue the actual local value.
052     */
053    public SynchronizableValue(Object localValue)
054    {
055        this(localValue, ExternalizableDataStatus.LOCAL);
056    }
057
058    /**
059     * Constructor
060     * @param value the actual value
061     * @param valueStatus the status of the given value (not the status of the object itself)
062     */
063    public SynchronizableValue(Object value, ExternalizableDataStatus valueStatus)
064    {
065        if (ExternalizableDataStatus.EXTERNAL.equals(valueStatus))
066        {
067            _externalValue = value;
068            _localValue = new UntouchedValue();
069        }
070        else
071        {
072            _localValue = value;
073            _externalValue = new UntouchedValue();
074        }
075    }
076    
077    /**
078     * Retrieves the synchronizable local value
079     * @return the synchronizable local value
080     */
081    public Object getLocalValue()
082    {
083        return _localValue;
084    }
085    
086    /**
087     * Sets the synchronizable local value
088     * @param localValue the synchronizable local value to set
089     */
090    public void setLocalValue(Object localValue)
091    {
092        _localValue = localValue;
093    }
094    
095    /**
096     * Retrieves the external value
097     * @return the external value
098     */
099    public Object getExternalValue()
100    {
101        return _externalValue;
102    }
103    
104    /**
105     * Sets the external value
106     * @param externalValue the external value to set
107     */
108    public void setExternalValue(Object externalValue)
109    {
110        _externalValue = externalValue;
111    }
112    
113    /**
114     * Retrieves the value of the given status
115     * @param status the status
116     * @return the value of the given status
117     */
118    public Object getValue(Optional<ExternalizableDataStatus> status)
119    {
120        if (status.isPresent() && ExternalizableDataStatus.EXTERNAL.equals(status.get()))
121        {
122            return getExternalValue();
123        }
124        else
125        {
126            return getLocalValue();
127        }
128    }
129    
130    /**
131     * Retrieves the externalizable status of the value
132     * If <code>null</code>, the status of the value won't be updated
133     * @return the externalizable status of the value
134     */
135    public ExternalizableDataStatus getExternalizableStatus()
136    {
137        return _externalizableDataStatus;
138    }
139    
140    /**
141     * Sets the externalizable status of the value
142     * If not set, the status of the value won't be updated
143     * @param externalizableDataStatus the status to set
144     */
145    public void setExternalizableStatus(ExternalizableDataStatus externalizableDataStatus)
146    {
147        _externalizableDataStatus = externalizableDataStatus;
148    }
149    
150    /**
151     * Retrieves the comments associated with the value
152     * @return the comments associated with the value
153     */
154    public List<DataComment> getComments()
155    {
156        return _comments;
157    }
158    
159    /**
160     * Set the comments associated to the value
161     * @param comments the comment
162     */
163    public void setComments(List<DataComment> comments)
164    {
165        _comments = comments;
166    }
167    
168    /**
169     * Retrieves the write mode for the value
170     * @return the write mode for the value
171     */
172    public Mode getMode()
173    {
174        return _mode;
175    }
176    
177    /**
178     * Set the write mode for the value
179     * @param mode the write mode
180     */
181    public void setMode(Mode mode)
182    {
183        _mode = mode;
184    }
185}