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 java.util.ArrayList;
019import java.util.Collection;
020
021import javax.jcr.ItemExistsException;
022import javax.jcr.Node;
023import javax.jcr.NodeIterator;
024import javax.jcr.PathNotFoundException;
025import javax.jcr.RepositoryException;
026import javax.jcr.lock.Lock;
027
028import org.apache.jackrabbit.JcrConstants;
029import org.apache.jackrabbit.util.Text;
030
031import org.ametys.plugins.repository.AmetysRepositoryException;
032import org.ametys.plugins.repository.RepositoryConstants;
033import org.ametys.plugins.repository.RepositoryIntegrityViolationException;
034import org.ametys.plugins.repository.jcr.NodeTypeHelper;
035import org.ametys.plugins.repository.metadata.ModifiableFile;
036import org.ametys.plugins.repository.metadata.ModifiableFolder;
037import org.ametys.plugins.repository.metadata.UnknownMetadataException;
038
039/**
040 * Java binding of a nt:folder JCR Node.<p>
041 * {@link Text#escapeIllegalJcrChars(String)} is used for
042 * escaping invalid JCR characters or character sequences.
043 * @deprecated org.ametys.cms.data.RichText new class doesn't use folder anymore
044 */
045@Deprecated
046public class JCRFolder implements ModifiableFolder
047{
048    private Node _node;
049    private boolean _lockAlreadyChecked;
050    
051    /**
052     * Constructor
053     * @param node the nt:folder Node
054     */
055    public JCRFolder(Node node)
056    {
057        _node = node;
058    }
059    
060    /**
061     * Retrieves the underlying node.
062     * @return the underlying node.
063     */
064    public Node getNode()
065    {
066        return _node;
067    }
068    
069    @Override
070    public String getName() throws AmetysRepositoryException
071    {
072        try
073        {
074            return Text.unescapeIllegalJcrChars(_node.getName());
075        }
076        catch (RepositoryException e)
077        {
078            throw new AmetysRepositoryException(e);
079        }
080    }
081
082    public Collection<ModifiableFolder> getFolders()
083    {
084        ArrayList<ModifiableFolder> folders = new ArrayList<>();
085        try
086        {
087            NodeIterator it = _node.getNodes();
088            while (it.hasNext())
089            {
090                Node node = it.nextNode();
091                
092                if (NodeTypeHelper.isNodeType(node, JcrConstants.NT_FOLDER))
093                {
094                    folders.add(new JCRFolder(node));
095                }
096            }
097            
098            return folders;
099        }
100        catch (RepositoryException e)
101        {
102            throw new AmetysRepositoryException(e);
103        }
104    }
105
106    public ModifiableFolder getFolder(String folderName)
107    {
108        try
109        {
110            Node node = _node.getNode(Text.escapeIllegalJcrChars(folderName));
111            return new JCRFolder(node);
112        }
113        catch (PathNotFoundException e)
114        {
115            throw new UnknownMetadataException(e);
116        }
117        catch (RepositoryException e)
118        {
119            throw new AmetysRepositoryException(e);
120        }
121    }
122    
123    public ModifiableFolder addFolder(String folderName)
124    {
125        try
126        {
127            _checkLock();
128            Node node = _node.addNode(Text.escapeIllegalJcrChars(folderName), JcrConstants.NT_FOLDER);
129            return new JCRFolder(node);
130        }
131        catch (ItemExistsException e)
132        {
133            throw new RepositoryIntegrityViolationException(e);
134        }
135        catch (RepositoryException e)
136        {
137            throw new AmetysRepositoryException(e);
138        }
139    }
140    
141    public Collection<ModifiableFile> getFiles()
142    {
143        ArrayList<ModifiableFile> files = new ArrayList<>();
144        try
145        {
146            NodeIterator it = _node.getNodes();
147            while (it.hasNext())
148            {
149                Node node = it.nextNode();
150                
151                if (NodeTypeHelper.isNodeType(node, JcrConstants.NT_FILE))
152                {
153                    files.add(new JCRFile(node));
154                }
155            }
156            
157            return files;
158        }
159        catch (RepositoryException e)
160        {
161            throw new AmetysRepositoryException(e);
162        }
163    }
164    
165    @Override
166    public boolean hasFile(String fileName) throws AmetysRepositoryException
167    {
168        try
169        {
170            return _node.hasNode(Text.escapeIllegalJcrChars(fileName));
171        }
172        catch (RepositoryException e)
173        {
174            throw new AmetysRepositoryException(e);
175        }
176    }
177
178    public ModifiableFile getFile(String fileName)
179    {
180        try
181        {
182            Node node = _node.getNode(Text.escapeIllegalJcrChars(fileName));
183            return new JCRFile(node);
184        }
185        catch (PathNotFoundException e)
186        {
187            throw new UnknownMetadataException(e);
188        }
189        catch (RepositoryException e)
190        {
191            throw new AmetysRepositoryException(e);
192        }
193    }
194    
195    public ModifiableFile addFile(String fileName)
196    {
197        try
198        {
199            _checkLock();
200            Node node = _node.addNode(Text.escapeIllegalJcrChars(fileName), JcrConstants.NT_FILE);
201            return new JCRFile(node);
202        }
203        catch (ItemExistsException e)
204        {
205            throw new RepositoryIntegrityViolationException(e);
206        }
207        catch (RepositoryException e)
208        {
209            throw new AmetysRepositoryException(e);
210        }
211    }
212
213    @Override
214    public void remove(String name) throws AmetysRepositoryException
215    {
216        try
217        {
218            _checkLock();
219            Node node = _node.getNode(Text.escapeIllegalJcrChars(name));
220            node.remove();
221        }
222        catch (PathNotFoundException e)
223        {
224            throw new UnknownMetadataException(e);
225        }
226        catch (RepositoryException e)
227        {
228            throw new AmetysRepositoryException(e);
229        }
230    }
231
232    @Override
233    public void removeAll() throws AmetysRepositoryException
234    {
235        try
236        {
237            _checkLock();
238            NodeIterator it = _node.getNodes();
239            while (it.hasNext())
240            {
241                Node node = it.nextNode();
242                node.remove();
243            }
244        }
245        catch (RepositoryException e)
246        {
247            throw new AmetysRepositoryException(e);
248        }
249    }
250    
251    private void _checkLock() throws RepositoryException
252    {
253        if (!_lockAlreadyChecked && _node.isLocked())
254        {
255            Lock lock = _node.getSession().getWorkspace().getLockManager().getLock(_node.getPath());
256            Node lockHolder = lock.getNode();
257            
258            lockHolder.getSession().getWorkspace().getLockManager().addLockToken(lockHolder.getProperty(RepositoryConstants.METADATA_LOCKTOKEN).getString());
259            _lockAlreadyChecked = true;
260        }
261    }
262}