001/*
002 *  Copyright 2010 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.cms.tag.jcr;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import javax.jcr.Node;
024import javax.jcr.PathNotFoundException;
025import javax.jcr.RepositoryException;
026
027import org.ametys.cms.tag.CMSTag;
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 * {@link AmetysObject} for storing tag informations.
035 */
036public class JCRTag extends DefaultTraversableAmetysObject<TagFactory>
037{
038    /** Constants for title metadata. */
039    private static final String __METADATA_TITLE = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":title";
040    /** Constants for description metadata. */
041    private static final String __METADATA_DESC = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":description";
042    
043    /**
044     * Creates a {@link CMSTag}.
045     * @param node the node backing this {@link AmetysObject}.
046     * @param parentPath the parent path in the Ametys hierarchy.
047     * @param factory the {@link TagFactory} which creates the AmetysObject.
048     */
049    public JCRTag (Node node, String parentPath, TagFactory factory)
050    {
051        super(node, parentPath, factory);
052    }
053    
054    /**
055     * Retrieves the title.
056     * @return the title.
057     * @throws AmetysRepositoryException if an error occurs.
058     */
059    public String getTitle() throws AmetysRepositoryException
060    {
061        try
062        {
063            return getNode().getProperty(__METADATA_TITLE).getString();
064        }
065        catch (PathNotFoundException e)
066        {
067            return null;
068        }
069        catch (RepositoryException e)
070        {
071            throw new AmetysRepositoryException("Unable to get title property", e);
072        }
073    }
074    
075    /**
076     * Set the title.
077     * @param title the title.
078     * @throws AmetysRepositoryException if an error occurs.
079     */
080    public void setTitle(String title) throws AmetysRepositoryException
081    {
082        try
083        {
084            getNode().setProperty(__METADATA_TITLE, title);
085        }
086        catch (RepositoryException e)
087        {
088            throw new AmetysRepositoryException("Unable to set title property", e);
089        }
090    }
091    
092    /**
093     * Retrieves the description.
094     * @return the description.
095     * @throws AmetysRepositoryException if an error occurs.
096     */
097    public String getDescription() throws AmetysRepositoryException
098    {
099        try
100        {
101            return getNode().getProperty(__METADATA_DESC).getString();
102        }
103        catch (PathNotFoundException e)
104        {
105            return null;
106        }
107        catch (RepositoryException e)
108        {
109            throw new AmetysRepositoryException("Unable to get description property", e);
110        }
111    }
112    
113    /**
114     * Set the description.
115     * @param description the description.
116     * @throws AmetysRepositoryException if an error occurs.
117     */
118    public void setDescription(String description) throws AmetysRepositoryException
119    {
120        try
121        {
122            getNode().setProperty(__METADATA_DESC, description);
123        }
124        catch (RepositoryException e)
125        {
126            throw new AmetysRepositoryException("Unable to set description property", e);
127        }
128    }
129    
130    /**
131     * Get the json tag
132     * @return the tag to json
133     */
134    public Map<String, Object> toJSON()
135    {
136        Map<String, Object> infos = new HashMap<>();
137
138        infos.put("id", getId());
139        infos.put("name", getName());
140        infos.put("title", getTitle());
141        infos.put("description", getDescription());
142        
143        infos.put("iconSmall", "/plugins/cms/resources/img/tag/tag_16.png");
144        infos.put("iconMedium", "/plugins/cms/resources/img/tag/tag_32.png");
145        infos.put("iconLarge", "/plugins/cms/resources/img/tag/tag_50.png");
146        
147        if (!getChildren().iterator().hasNext())
148        {
149            List<Map<String, Object>> qNodes = new ArrayList<>();
150            infos.put("children", qNodes);
151        }
152
153        return infos;
154    }
155}