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 javax.jcr.Node;
019import javax.jcr.RepositoryException;
020
021import org.apache.commons.lang3.StringUtils;
022
023import org.ametys.plugins.repository.AmetysObject;
024import org.ametys.plugins.repository.AmetysRepositoryException;
025import org.ametys.plugins.repository.RepositoryConstants;
026import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
027
028/**
029 * Class representing a thesaurus, backed by a JCR node.<br>
030 */
031public class Thesaurus extends DefaultTraversableAmetysObject<ThesaurusFactory>
032{
033    /**
034     * Creates an {@link Thesaurus}.
035     * @param node the node backing this {@link AmetysObject}
036     * @param parentPath the parentPath in the Ametys hierarchy
037     * @param factory the ThesaurusFactory which created the AmetysObject
038     */
039    public Thesaurus(Node node, String parentPath, ThesaurusFactory factory)
040    {
041        super(node, parentPath, factory);
042    }
043    
044    /**
045     * Set the label of this microthesaurus.
046     * @param label the label
047     */
048    public void setLabel (String label)
049    {
050        try
051        {
052            getNode().setProperty(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":label", label != null ? label : StringUtils.EMPTY);
053        }
054        catch (RepositoryException e)
055        {
056            throw new AmetysRepositoryException("Error while setting title property.", e);
057        }
058    }
059    
060    /**
061     * Get the label of the microthesaurus.
062     * @return The label
063     */
064    public String getLabel()
065    {        
066        try
067        {
068            if (getNode().hasProperty(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":label"))
069            {
070                return getNode().getProperty(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":label").getValue().getString();
071            }
072            else
073            {
074                return StringUtils.EMPTY;
075            }
076        }
077        catch (RepositoryException e)
078        {
079            throw new AmetysRepositoryException("Error while getting title property.", e);
080        }
081    }
082}