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.io.InputStream;
019import java.io.OutputStream;
020import java.util.Date;
021import java.util.GregorianCalendar;
022
023import javax.jcr.Binary;
024import javax.jcr.Node;
025import javax.jcr.PathNotFoundException;
026import javax.jcr.RepositoryException;
027import javax.jcr.lock.Lock;
028
029import org.ametys.plugins.repository.AmetysRepositoryException;
030import org.ametys.plugins.repository.RepositoryConstants;
031import org.ametys.plugins.repository.metadata.ModifiableResource;
032
033/**
034 * Java binding of a nt:resource JCR Node
035 * @deprecated Use org.ametys.cms.data.Resource instead
036 */
037@Deprecated
038public class JCRResource implements ModifiableResource
039{
040    private Node _node;
041    private boolean _lockAlreadyChecked;
042    
043    /**
044     * Constructor
045     * @param node the nt:resource Node
046     */
047    public JCRResource(Node node)
048    {
049        _node = node;
050    }
051    
052    /**
053     * Retrieves the underlying node.
054     * @return the underlying node.
055     */
056    public Node getNode()
057    {
058        return _node;
059    }
060    
061    @Override
062    public void rename(String newName) throws AmetysRepositoryException
063    {
064        try
065        {
066            _checkLock();
067            getNode().getSession().move(getNode().getPath(), getNode().getParent().getPath() + "/" + newName);
068        }
069        catch (RepositoryException e)
070        {
071            throw new AmetysRepositoryException(e);
072        }        
073    }
074
075    public String getEncoding() throws AmetysRepositoryException
076    {
077        try
078        {
079            return _node.getProperty("jcr:encoding").getString();
080        }
081        catch (PathNotFoundException e)
082        {
083            // Not found
084            return null;
085        }
086        catch (RepositoryException e)
087        {
088            throw new AmetysRepositoryException(e);
089        }
090    }
091
092    public InputStream getInputStream() throws AmetysRepositoryException
093    {
094        try
095        {
096            return _node.getProperty("jcr:data").getBinary().getStream();
097        }
098        catch (RepositoryException e)
099        {
100            throw new AmetysRepositoryException(e);
101        }
102    }
103
104    public Date getLastModified() throws AmetysRepositoryException
105    {
106        try
107        {
108            return _node.getProperty("jcr:lastModified").getDate().getTime();
109        }
110        catch (RepositoryException e)
111        {
112            throw new AmetysRepositoryException(e);
113        }
114    }
115
116    public long getLength() throws AmetysRepositoryException
117    {
118        try
119        {
120            return _node.getProperty("jcr:data").getLength();
121        }
122        catch (RepositoryException e)
123        {
124            throw new AmetysRepositoryException(e);
125        }
126    }
127
128    public String getMimeType() throws AmetysRepositoryException
129    {
130        try
131        {
132            return _node.getProperty("jcr:mimeType").getString();
133        }
134
135        catch (RepositoryException e)
136        {
137            throw new AmetysRepositoryException(e);
138        }
139    }
140
141    public void setEncoding(String encoding) throws AmetysRepositoryException
142    {
143        try
144        {
145            _checkLock();
146            _node.setProperty("jcr:encoding", encoding);
147        }
148        catch (RepositoryException e)
149        {
150            throw new AmetysRepositoryException(e);
151        }
152    }
153
154    public void setInputStream(InputStream stream) throws AmetysRepositoryException
155    {
156        try
157        {
158            _checkLock();
159            Binary binary = _node.getSession().getValueFactory().createBinary(stream);
160            _node.setProperty("jcr:data", binary);
161        }
162        catch (RepositoryException e)
163        {
164            throw new AmetysRepositoryException(e);
165        }
166    }
167
168    public void setLastModified(Date lastModifiedDate) throws AmetysRepositoryException
169    {
170        GregorianCalendar gc = new GregorianCalendar();
171        gc.setTime(lastModifiedDate);
172        
173        try
174        {
175            _checkLock();
176            _node.setProperty("jcr:lastModified", gc);
177        }
178        catch (RepositoryException e)
179        {
180            throw new AmetysRepositoryException(e);
181        }
182    }
183
184    public void setMimeType(String mimeType) throws AmetysRepositoryException
185    {
186        try
187        {
188            _checkLock();
189            _node.setProperty("jcr:mimeType", mimeType);
190        }
191        catch (RepositoryException e)
192        {
193            throw new AmetysRepositoryException(e);
194        }
195    }
196    
197    public OutputStream getOutputStream() throws AmetysRepositoryException
198    {
199        return new JCROutputStream(_node, false);
200    }
201    
202    /**
203     * Check if the node is locked
204     * @throws RepositoryException if an error occurred
205     */
206    protected void _checkLock() throws RepositoryException
207    {
208        if (!_lockAlreadyChecked && _node.isLocked())
209        {
210            Lock lock = _node.getSession().getWorkspace().getLockManager().getLock(_node.getPath());
211            Node lockHolder = lock.getNode();
212            
213            lockHolder.getSession().getWorkspace().getLockManager().addLockToken(lockHolder.getProperty(RepositoryConstants.METADATA_LOCKTOKEN).getString());
214            _lockAlreadyChecked = true;
215        }
216    }
217}