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.jackrabbit.util.Text;
022
023import org.ametys.plugins.repository.AmetysRepositoryException;
024import org.ametys.plugins.repository.metadata.ModifiableFile;
025
026/**
027 * Java binding of a nt:file JCR Node.<p>
028 * {@link Text#escapeIllegalJcrChars(String)} is used for
029 * escaping invalid JCR characters or character sequences.
030 * @deprecated org.ametys.cms.data.RichText new class doesn't use folder anymore
031 */
032@Deprecated
033public class JCRFile implements ModifiableFile
034{
035    private Node _node;
036    
037    /**
038     * Constructor
039     * @param node the nt:file Node
040     */
041    public JCRFile(Node node)
042    {
043        _node = node;
044    }
045    
046    /**
047     * Retrieves the underlying node.
048     * @return the underlying node.
049     */
050    public Node getNode()
051    {
052        return _node;
053    }
054    
055    @Override
056    public String getName() throws AmetysRepositoryException
057    {
058        try
059        {
060            return Text.unescapeIllegalJcrChars(_node.getName());
061        }
062        catch (RepositoryException e)
063        {
064            throw new AmetysRepositoryException(e);
065        }
066    }
067    
068    public JCRResource getResource() throws AmetysRepositoryException
069    {
070        try
071        {
072            Node resourceNode = null;
073            
074            if (_node.hasNode("jcr:content"))
075            {
076                // Already exists
077                resourceNode = _node.getNode("jcr:content");
078            }
079            else
080            {
081                resourceNode = _node.addNode("jcr:content", "nt:resource");
082            }
083            
084            return new JCRResource(resourceNode);
085        }
086        catch (RepositoryException e)
087        {
088            throw new AmetysRepositoryException(e);
089        }
090    }
091}