001/*
002 *  Copyright 2013 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.thesaurus;
017
018import java.util.List;
019import java.util.stream.Collectors;
020import java.util.stream.Stream;
021
022import javax.jcr.Node;
023
024import org.ametys.plugins.repository.AmetysObject;
025import org.ametys.plugins.repository.RepositoryConstants;
026import org.ametys.plugins.repository.data.holder.ModifiableDataHolder;
027import org.ametys.plugins.repository.data.holder.impl.DefaultModifiableDataHolder;
028import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
029import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
030import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
031import org.ametys.runtime.model.type.ModelItemTypeConstants;
032
033/**
034 * Class representing a thesaurus, backed by a JCR node.<br>
035 */
036public class Thesaurus extends DefaultTraversableAmetysObject<ThesaurusFactory>
037{
038    private static final String __METADATA_LABEL = "label";
039    private static final String __METADATA_MICROTHESAURUS = "microthesaurus";
040    
041    /**
042     * Creates an {@link Thesaurus}.
043     * @param node the node backing this {@link AmetysObject}
044     * @param parentPath the parentPath in the Ametys hierarchy
045     * @param factory the ThesaurusFactory which created the AmetysObject
046     */
047    public Thesaurus(Node node, String parentPath, ThesaurusFactory factory)
048    {
049        super(node, parentPath, factory);
050    }
051    
052    /**
053     * Set the label of this microthesaurus.
054     * @param label the label
055     */
056    public void setLabel(String label)
057    {
058        setValue(__METADATA_LABEL, label, ModelItemTypeConstants.STRING_TYPE_ID);
059    }
060    
061    /**
062     * Get the label of the microthesaurus.
063     * @return The label
064     */
065    public String getLabel()
066    {
067        return getValueOfType(__METADATA_LABEL, ModelItemTypeConstants.STRING_TYPE_ID);
068    }
069    
070    /**
071     * Add a microthesaurus to this thesaurus
072     * @param microthesaurus the microthesaurus to add
073     */
074    public void addMicrothesaurus(String microthesaurus)
075    {
076        List<String> microthesaurii = getMicrothesaurii();
077        if (!microthesaurii.contains(microthesaurus))
078        {
079            microthesaurii.add(microthesaurus);
080        }
081        setMicrothesaurii(microthesaurii);
082    }
083    
084    /**
085     * Remove a microthesaurus from this thesaurus
086     * @param microthesaurus the microthesaurus to add
087     */
088    public void removeMicrothesaurus(String microthesaurus)
089    {
090        List<String> microthesaurii = getMicrothesaurii();
091        microthesaurii.remove(microthesaurus);
092        setMicrothesaurii(microthesaurii);
093    }
094    
095    /**
096     * Set the microthesaurii of this thesaurus.
097     * @param microthesaurii the id of microthesaurii to add
098     */
099    public void setMicrothesaurii(List<String> microthesaurii)
100    {
101        setValue(__METADATA_MICROTHESAURUS, microthesaurii.toArray(String[]::new), ModelItemTypeConstants.STRING_TYPE_ID);
102    }
103    
104    /**
105     * Get the microthesaurii array of the thesaurus.
106     * @return The array of microthesaurus
107     */
108    public List<String> getMicrothesaurii()
109    {
110        return Stream.of(getValueOfTypeOrDefault(__METADATA_MICROTHESAURUS, ModelItemTypeConstants.STRING_TYPE_ID, new String[0])).collect(Collectors.toList());
111    }
112
113    @Override
114    protected ModifiableDataHolder getDataHolder()
115    {
116        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode(), RepositoryConstants.NAMESPACE_PREFIX_INTERNAL);
117        return new DefaultModifiableDataHolder(_getFactory().getModelItemTypeExtensionPoint(), repositoryData);
118    }
119}