001/*
002 *  Copyright 2018 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.workspaces.categories;
017
018import java.util.Map;
019
020import javax.jcr.Node;
021import javax.jcr.PathNotFoundException;
022import javax.jcr.RepositoryException;
023
024import org.ametys.cms.tag.jcr.JCRTag;
025import org.ametys.plugins.repository.AmetysObject;
026import org.ametys.plugins.repository.AmetysRepositoryException;
027import org.ametys.plugins.repository.RepositoryConstants;
028
029/**
030 * {@link JCRTag} for storing tag informations.
031 */
032public class CategoryJCR extends JCRTag
033{
034    /** Constants for color metadata. */
035    private static final String __METADATA_COLOR = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":color";
036    
037    /**
038     * Creates a {@link Category}.
039     * @param node the node backing this {@link AmetysObject}.
040     * @param parentPath the parent path in the Ametys hierarchy.
041     * @param factory the {@link CategoryFactory} which creates the AmetysObject.
042     */
043    public CategoryJCR (Node node, String parentPath, CategoryFactory factory)
044    {
045        super(node, parentPath, factory);
046    }
047    
048    /**
049     * Retrieves the color type.
050     * @return the color type.
051     * @throws AmetysRepositoryException if an error occurs.
052     */
053    public String getColor() throws AmetysRepositoryException
054    {
055        try
056        {
057            return getNode().getProperty(__METADATA_COLOR).getString();
058        }
059        catch (PathNotFoundException e)
060        {
061            return null;
062        }
063        catch (RepositoryException e)
064        {
065            throw new AmetysRepositoryException("Unable to get color property", e);
066        }
067    }
068    
069    /**
070     * Set the color type.
071     * @param color the color type.
072     * @throws AmetysRepositoryException if an error occurs.
073     */
074    public void setColor(String color) throws AmetysRepositoryException
075    {
076        try
077        {
078            getNode().setProperty(__METADATA_COLOR, color);
079        }
080        catch (RepositoryException e)
081        {
082            throw new AmetysRepositoryException("Unable to set color property", e);
083        }
084    }
085    
086    @Override
087    public Map<String, Object> toJSON()
088    {
089        Map<String, Object> infos = super.toJSON();
090        infos.put("color", getColor());
091        
092        return infos;
093    }
094}