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