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.model;
017
018import java.util.HashSet;
019import java.util.Set;
020
021import org.ametys.runtime.model.ModelHelper;
022import org.ametys.runtime.model.type.DataContext;
023
024/**
025 * Object that gives some context for repository data manipulation
026 */
027public class RepositoryDataContext extends DataContext
028{
029    private Set<String> _externalizableData = new HashSet<>();
030    
031    /**
032     * Creates a new instance of a {@link RepositoryDataContext}
033     */
034    protected RepositoryDataContext()
035    {
036        // Empty constructor
037    }
038    
039    /**
040     * Creates a new instance of a {@link RepositoryDataContext}
041     * @return the created instance
042     */
043    public static RepositoryDataContext newInstance()
044    {
045        return new RepositoryDataContext();
046    }
047    
048    /**
049     * Creates a new instance of a {@link RepositoryDataContext}, with the current context values
050     * @return the created instance
051     */
052    @Override
053    public RepositoryDataContext cloneContext()
054    {
055        RepositoryDataContext clone = new RepositoryDataContext();
056        _cloneContextData(clone);
057        return clone;
058    }
059    
060    @Override
061    protected void _cloneContextData(DataContext context)
062    {
063        super._cloneContextData(context);
064        if (context instanceof RepositoryDataContext)
065        {
066            ((RepositoryDataContext) context).withExternalizableData(getExternalizableData());
067        }
068        else
069        {
070            throw new IllegalArgumentException("Unable to clone the data context '" + context + "' as a repository data context");
071        }
072    }
073    
074    /**
075     * Retrieves the externalizable data
076     * @return the externalizable data
077     */
078    public Set<String> getExternalizableData()
079    {
080        return _externalizableData;
081    }
082    
083    /**
084     * Check if the current data is externalizable
085     * @return <code>true</code> if the data is externalizable, <code>false</code> otherwise
086     */
087    public boolean isDataExternalizable()
088    {
089        String dataPath = getDataPath();
090        String definitionPath = ModelHelper.getDefinitionPathFromDataPath(dataPath);
091        return _externalizableData.contains(definitionPath);
092    }
093    
094    /**
095     * Sets the externalizable data
096     * @param externalizableData the externalizable data to set
097     * @return the current {@link RepositoryDataContext}
098     */
099    public RepositoryDataContext withExternalizableData(Set<String> externalizableData)
100    {
101        _externalizableData = externalizableData;
102        return this;
103    }
104}