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.ArrayList;
019import java.util.HashSet;
020import java.util.List;
021import java.util.Set;
022
023import javax.jcr.Node;
024import javax.jcr.PathNotFoundException;
025import javax.jcr.RepositoryException;
026import javax.jcr.Value;
027
028import org.ametys.plugins.repository.AmetysObject;
029import org.ametys.plugins.repository.AmetysRepositoryException;
030import org.ametys.plugins.repository.RepositoryConstants;
031import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
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 __PROPERTY_LABEL = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":label";
039    private static final String __PROPERTY_MICROTHESAURUS = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":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        try
059        {
060            getNode().setProperty(__PROPERTY_LABEL, label);
061        }
062        catch (RepositoryException e)
063        {
064            throw new AmetysRepositoryException("Error setting the label property.", e);
065        }
066    }
067    
068    /**
069     * Get the label of the microthesaurus.
070     * @return The label
071     */
072    public String getLabel()
073    {        
074        try
075        {
076            return getNode().getProperty(__PROPERTY_LABEL).getString();
077        }
078        catch (PathNotFoundException e)
079        {
080            return null;
081        }
082        catch (RepositoryException e)
083        {
084            throw new AmetysRepositoryException("Error getting the label property.", e);
085        }
086    }
087    
088    /**
089     * Add a microthesaurus to this thesaurus
090     * @param microthesaurus the microthesaurus to add
091     */
092    public void addMicrothesaurus (String microthesaurus)
093    {
094        try
095        {
096            Set<String> microthesaurii = new HashSet<>();
097            
098            if (getNode().hasProperty(__PROPERTY_MICROTHESAURUS))
099            {
100                Value[] values = getNode().getProperty(__PROPERTY_MICROTHESAURUS).getValues();
101                for (Value value : values)
102                {
103                    microthesaurii.add(value.getString());
104                }
105            }
106            
107            microthesaurii.add(microthesaurus);
108            
109            getNode().setProperty(__PROPERTY_MICROTHESAURUS, microthesaurii.toArray(new String[microthesaurii.size()]));
110        }
111        catch (RepositoryException e)
112        {
113            throw new AmetysRepositoryException("Error while adding a microthesaurus.", e);
114        }
115    }
116    
117    /**
118     * Remove a microthesaurus from this thesaurus
119     * @param microthesaurus the microthesaurus to add
120     */
121    public void removeMicrothesaurus (String microthesaurus)
122    {
123        try
124        {
125            Set<String> microthesaurii = new HashSet<>();
126            
127            if (getNode().hasProperty(__PROPERTY_MICROTHESAURUS))
128            {
129                Value[] values = getNode().getProperty(__PROPERTY_MICROTHESAURUS).getValues();
130                for (Value value : values)
131                {
132                    microthesaurii.add(value.getString());
133                }
134            }
135            
136            microthesaurii.remove(microthesaurus);
137            
138            getNode().setProperty(__PROPERTY_MICROTHESAURUS, microthesaurii.toArray(new String[microthesaurii.size()]));
139        }
140        catch (RepositoryException e)
141        {
142            throw new AmetysRepositoryException("Error while removing a microthesaurus.", e);
143        }
144    }
145    
146    /**
147     * Set the microthesaurii of this thesaurus.
148     * @param microthesaurii the id of microthesaurii to add
149     */
150    public void setMicrothesaurus (List<String> microthesaurii)
151    {
152        try
153        {
154            getNode().setProperty(__PROPERTY_MICROTHESAURUS, microthesaurii.toArray(new String[microthesaurii.size()]));
155        }
156        catch (RepositoryException e)
157        {
158            throw new AmetysRepositoryException("Error while setting the microthesaurii.", e);
159        }
160    }
161    
162    /**
163     * Get the microthesaurii array of the thesaurus.
164     * @return The array of microthesaurus
165     */
166    public List<String> getMicrothesaurii()
167    {        
168        try
169        {
170            List<String> microthesaurii = new ArrayList<>();
171            
172            if (getNode().hasProperty(__PROPERTY_MICROTHESAURUS))
173            {
174                Value[] values = getNode().getProperty(__PROPERTY_MICROTHESAURUS).getValues();
175                for (Value value : values)
176                {
177                    microthesaurii.add(value.getString());
178                }
179            }
180            
181            return microthesaurii;
182        }
183        catch (RepositoryException e)
184        {
185            throw new AmetysRepositoryException("Error while getting the microthesaurii.", e);
186        }
187    }
188}