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.cms.tag.jcr;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import javax.jcr.Node;
022import javax.jcr.PathNotFoundException;
023import javax.jcr.RepositoryException;
024
025import org.ametys.cms.tag.CMSTag;
026import org.ametys.cms.tag.CMSTag.TagVisibility;
027import org.ametys.cms.tag.TagTargetType;
028import org.ametys.cms.tag.TagTargetTypeExtensionPoint;
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.plugins.repository.AmetysRepositoryException;
031import org.ametys.plugins.repository.RepositoryConstants;
032
033/**
034 * {@link JCRTag} for storing tag informations.
035 */
036public class CMSJCRTag extends JCRTag
037{
038    /** Constants for visibility metadata. */
039    private static final String __METADATA_VISIBILITY = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":visibility";
040    /** Constants for type metadata. */
041    private static final String __METADATA_TARGET = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":target";
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 CMSJCRTag (Node node, String parentPath, CMSTagFactory factory)
050    {
051        super(node, parentPath, factory);
052    }
053    
054    /**
055     * Retrieves the visibility type.
056     * @return the visibility type.
057     * @throws AmetysRepositoryException if an error occurs.
058     */
059    public TagVisibility getVisibility() throws AmetysRepositoryException
060    {
061        try
062        {
063            return TagVisibility.valueOf(getNode().getProperty(__METADATA_VISIBILITY).getString());
064        }
065        catch (PathNotFoundException e)
066        {
067            return TagVisibility.PUBLIC;
068        }
069        catch (RepositoryException e)
070        {
071            throw new AmetysRepositoryException("Unable to get visibility property", e);
072        }
073    }
074    
075    /**
076     * Set the visibility type.
077     * @param visibility the visibility type.
078     * @throws AmetysRepositoryException if an error occurs.
079     */
080    public void setVisibility(TagVisibility visibility) throws AmetysRepositoryException
081    {
082        try
083        {
084            getNode().setProperty(__METADATA_VISIBILITY, visibility.name());
085        }
086        catch (RepositoryException e)
087        {
088            throw new AmetysRepositoryException("Unable to set visibility property", e);
089        }
090    }
091    
092    /**
093     * Retrieves the target type.
094     * @return the target type.
095     * @throws AmetysRepositoryException if an error occurs.
096     */
097    public String getTargetType() throws AmetysRepositoryException
098    {
099        try
100        {
101            return getNode().getProperty(__METADATA_TARGET).getString();
102        }
103        catch (PathNotFoundException e)
104        {
105            return null;
106        }
107        catch (RepositoryException e)
108        {
109            throw new AmetysRepositoryException("Unable to get type property", e);
110        }
111    }
112    
113    /**
114     * Set the target type.
115     * @param target the target type.
116     * @throws AmetysRepositoryException if an error occurs.
117     */
118    public void setTargetType(TagTargetType target) throws AmetysRepositoryException
119    {
120        try
121        {
122            getNode().setProperty(__METADATA_TARGET, target.getName());
123        }
124        catch (RepositoryException e)
125        {
126            throw new AmetysRepositoryException("Unable to set type property", e);
127        }
128    }
129    
130    @Override
131    public Map<String, Object> toJSON()
132    {
133        Map<String, Object> infos = super.toJSON();
134        
135        CMSTagFactory factory = (CMSTagFactory) _getFactory();
136        TagTargetTypeExtensionPoint targetTypeEP = factory.getTagProviderEP();
137        
138        String targetType = getTargetType();
139        if (targetType != null)
140        {
141            Map<String, Object> target2json = new HashMap<>();
142            TagTargetType tagTargetType = targetTypeEP.getTagTargetType(targetType);
143            target2json.put("name", tagTargetType.getName());
144            target2json.put("label", tagTargetType.getLabel());
145            target2json.put("description", tagTargetType.getDescription());
146            
147            infos.put("target-info", target2json);
148            infos.put("target", tagTargetType.getName());
149        }
150        
151        TagVisibility visibility = getVisibility();
152        infos.put("visibility", visibility.toString());
153        if (TagVisibility.PRIVATE.equals(visibility))
154        {
155            infos.put("iconSmall", "/plugins/cms/resources/img/tag/tag_private_16.png");
156            infos.put("iconMedium", "/plugins/cms/resources/img/tag/tag_private_32.png");
157            infos.put("iconLarge", "/plugins/cms/resources/img/tag/tag_private_50.png");
158        }
159        else
160        {
161            infos.put("iconSmall", "/plugins/cms/resources/img/tag/tag_16.png");
162            infos.put("iconMedium", "/plugins/cms/resources/img/tag/tag_32.png");
163            infos.put("iconLarge", "/plugins/cms/resources/img/tag/tag_50.png");
164        }
165        
166        return infos;
167    }
168}