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.migration.jcr.data.repository;
017
018import java.util.Set;
019
020import javax.jcr.Node;
021
022import org.ametys.plugins.repository.AmetysRepositoryException;
023import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
024import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
025import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
026
027/**
028 * Object representing the versions root element.
029 */
030public class VersionsAmetysObject extends DefaultTraversableAmetysObject<VersionsFactory>
031{
032    /** Metadata name for storing a Node's lockToken */
033    public static final String KNOWN_PLUGINS = "knownPlugins";
034    
035    /** Metadata name set to true on the creation of the repository */
036    public static final String NEW_REPOSITORY = "newrepository";
037
038    /** The repository data */
039    protected ModifiableRepositoryData _repositoryData;
040
041    /**
042     * Constructor.
043     * @param node the JCR Node.
044     * @param parentPath the parent path
045     * @param factory the corresponding factory.
046     */
047    public VersionsAmetysObject(Node node, String parentPath, VersionsFactory factory)
048    {
049        super(node, parentPath, factory);
050        _repositoryData = new JCRRepositoryData(getNode());
051    }
052    
053    /**
054     * Get all the known plugins.
055     * @return the known plugins
056     */
057    public Set<String> getKnownPlugins()
058    {
059        if (_repositoryData.hasValue(KNOWN_PLUGINS))
060        {
061            return Set.of(_repositoryData.getStrings(KNOWN_PLUGINS));
062        }
063        
064        return Set.of();
065    }
066    
067    /**
068     * Set the known plugins.
069     * @param knownPlugins the known plugins
070     * @throws AmetysRepositoryException if an error occurs
071     */
072    public void setKnownPlugins(Set<String> knownPlugins) throws AmetysRepositoryException
073    {
074        _repositoryData.setValues(KNOWN_PLUGINS, knownPlugins.toArray(String[]::new));
075    }
076    
077    /**
078     * Get true if this is the first launch of this repository
079     * @return true if this is the first launch of this repository
080     */
081    public Boolean isNewRepository()
082    {
083        if (_repositoryData.hasValue(NEW_REPOSITORY))
084        {
085            return _repositoryData.getBoolean(NEW_REPOSITORY);
086        }
087        
088        return false;
089    }
090    
091    /**
092     * Set the information thas this repository is not considered new anymore
093     * @throws AmetysRepositoryException if an error occurs
094     */
095    public void setNotNewRepository() throws AmetysRepositoryException
096    {
097        _repositoryData.setValue(NEW_REPOSITORY, false);
098    }
099}