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.plugins.repository.metadata.jcr;
017
018import javax.jcr.Node;
019import javax.jcr.RepositoryException;
020
021import org.apache.commons.lang3.ArrayUtils;
022
023import org.ametys.plugins.repository.AmetysObjectResolver;
024import org.ametys.plugins.repository.AmetysRepositoryException;
025import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata;
026import org.ametys.plugins.repository.metadata.ModifiableFolder;
027import org.ametys.plugins.repository.metadata.ModifiableRichText;
028import org.ametys.plugins.repository.metadata.RichText;
029import org.ametys.plugins.repository.metadata.UnknownMetadataException;
030
031/**
032 * JCR implementation of a {@link RichText}, based on the ametys:richText nodetype.<br>
033 * @deprecated Use org.ametys.cms.data.RichText instead
034 */
035@Deprecated
036public class JCRRichText extends JCRResource implements ModifiableRichText
037{
038    
039    /** The annotations node name. */
040    private static final String _ANNOTATIONS_NODE_NAME = "annotations";
041    
042    private Node _rootDataNode;
043    private AmetysObjectResolver _resolver;
044    private boolean _lockAlreadyChecked;
045    
046    /**
047     * Constructor
048     * @param node the JCR Node backing this {@link JCRRichText}
049     * @param resolver The resolver, used to resolve object collections.
050     * @throws AmetysRepositoryException if an error occurs
051     */
052    public JCRRichText(Node node, AmetysObjectResolver resolver) throws AmetysRepositoryException
053    {
054        super(node);
055        
056        _resolver = resolver;
057        
058        try
059        {
060            _rootDataNode = node.getNode("data");
061        }
062        catch (RepositoryException e)
063        {
064            throw new AmetysRepositoryException(e);
065        }
066    }
067    
068    public ModifiableFolder getAdditionalDataFolder()
069    {
070        return new JCRFolder(_rootDataNode);
071    }
072    
073    @Override
074    public String[] getAnnotationNames() throws AmetysRepositoryException
075    {
076        try
077        {
078            ModifiableCompositeMetadata annotations = getAnnotationsNode(false);
079            
080            return annotations.getMetadataNames();
081        }
082        catch (UnknownMetadataException e)
083        {
084            // Ignore, just return an empty array.
085        }
086        
087        return new String[0];
088    }
089    
090    @Override
091    public String[] getAnnotationValues(String name) throws AmetysRepositoryException
092    {
093        try
094        {
095            ModifiableCompositeMetadata annotations = getAnnotationsNode(false);
096            if (annotations.hasMetadata(name))
097            {
098                return annotations.getStringArray(name);
099            }
100        }
101        catch (UnknownMetadataException e)
102        {
103            // Ignore, just return an empty array.
104        }
105        
106        return new String[0];
107    }
108    
109    @Override
110    public void addAnnotation(String name, String value) throws AmetysRepositoryException
111    {
112        String[] currentValues = getAnnotationValues(name);
113        getAnnotationsNode(true).setMetadata(name, ArrayUtils.add(currentValues, value));
114    }
115    
116    @Override
117    public void addAnnotation(String name, String[] values) throws AmetysRepositoryException
118    {
119        String[] currentValues = getAnnotationValues(name);
120        getAnnotationsNode(true).setMetadata(name, ArrayUtils.addAll(currentValues, values));
121    }
122    
123    @Override
124    public void removeAnnotations() throws AmetysRepositoryException
125    {
126        try
127        {
128            _checkLock();
129            if (getNode().hasNode(_ANNOTATIONS_NODE_NAME))
130            {
131                getNode().getNode(_ANNOTATIONS_NODE_NAME).remove(); 
132            }
133        }
134        catch (RepositoryException e)
135        {
136            throw new AmetysRepositoryException(e);
137        }
138    }
139    
140    @Override
141    public void removeAnnotation(String name) throws AmetysRepositoryException
142    {
143        ModifiableCompositeMetadata annotations = getAnnotationsNode(true);
144        if (annotations.hasMetadata(name))
145        {
146            annotations.removeMetadata(name);
147        }
148    }
149    
150    /**
151     * Get the annotations container node.
152     * @param createNew <code>true</code> to create the node if it doesn't exist, <code>false</code> otherwise.
153     * @return The annotation container node.
154     * @throws UnknownMetadataException If the node doesn't exist and createNew is false.
155     * @throws AmetysRepositoryException If a repository error occurs.
156     */
157    protected ModifiableCompositeMetadata getAnnotationsNode(boolean createNew) throws UnknownMetadataException, AmetysRepositoryException
158    {
159        try
160        {
161            if (getNode().hasNode(_ANNOTATIONS_NODE_NAME))
162            {
163                Node node = getNode().getNode(_ANNOTATIONS_NODE_NAME);
164                return new JCRCompositeMetadata(node, _resolver);
165            }
166            else if (createNew)
167            {
168                _checkLock();
169                Node node = getNode().addNode(_ANNOTATIONS_NODE_NAME, "ametys:compositeMetadata");
170                return new JCRCompositeMetadata(node, _resolver);
171            }
172            else
173            {
174                throw new UnknownMetadataException("No annotation found");
175            }
176        }
177        catch (RepositoryException e)
178        {
179            throw new AmetysRepositoryException(e);
180        }
181    }
182}