001/*
002 *  Copyright 2019 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.data;
017
018import java.io.InputStream;
019import java.time.ZoneId;
020import java.time.ZonedDateTime;
021
022import javax.jcr.RepositoryException;
023import javax.jcr.Session;
024
025import org.ametys.core.util.DateUtils;
026import org.ametys.plugins.explorer.resources.ModifiableResource;
027import org.ametys.plugins.explorer.resources.Resource;
028import org.ametys.plugins.repository.AmetysObjectResolver;
029import org.ametys.plugins.repository.AmetysRepositoryException;
030import org.ametys.plugins.repository.UnknownAmetysObjectException;
031
032/**
033 * Class representing a file from explorer
034 */
035public class ExplorerFile implements File
036{
037    private AmetysObjectResolver _resolver;
038    private String _resourceId;
039    private Resource _resource;
040    private Session _session;
041    
042    /**
043     * Constructor of the explorer file
044     * @param resolver resolver used to get the resource from its identifier
045     * @param resourceId resource's identifier
046     */
047    public ExplorerFile(AmetysObjectResolver resolver, String resourceId)
048    {
049        this(resolver, resourceId, null);
050    }
051    
052    /**
053     * Constructor of the explorer file
054     * @param resolver resolver used to get the resource from its identifier
055     * @param resourceId resource's identifier
056     * @param session the current session. If <code>null</code>, a new session will be used to retrieve the resource's data
057     */
058    public ExplorerFile(AmetysObjectResolver resolver, String resourceId, Session session)
059    {
060        _resolver = resolver;
061        _resourceId = resourceId;
062        _session = session;
063    }
064    
065    /**
066     * Retrieves the resource's identifier
067     * @return the resource's identifier
068     */
069    public String getResourceId()
070    {
071        return _resourceId;
072    }
073    
074    public InputStream getInputStream()
075    {
076        return getResource().getInputStream();
077    }
078    
079    public ZonedDateTime getLastModificationDate()
080    {
081        return DateUtils.asZonedDateTime(getResource().getLastModified(), ZoneId.systemDefault());
082    }
083
084    public void setLastModificationDate(ZonedDateTime lastModificationDate)
085    {
086        if (getResource() instanceof ModifiableResource resource)
087        {
088            resource.setLastModified(DateUtils.asDate(lastModificationDate));
089        }
090        else
091        {
092            throw new UnsupportedOperationException("Impossible to set the last modification date on resource '" + getResource().getId() + "'. The resource is not modifiable.");
093        }
094    }
095
096    public long getLength()
097    {
098        return getResource().getLength();
099    }
100
101    public String getMimeType()
102    {
103        return getResource().getMimeType();
104    }
105
106    public void setMimeType(String mimeType)
107    {
108        if (getResource() instanceof ModifiableResource resource)
109        {
110            resource.setMimeType(mimeType);
111        }
112        else
113        {
114            throw new UnsupportedOperationException("Impossible to set the MimeType on resource '" + getResource().getId() + "'. The resource is not modifiable.");
115        }
116    }
117
118    public String getName()
119    {
120        return getResource().getName();
121    }
122
123    /**
124     * Retrieves the file resource
125     * @return the file resource
126     * @throws AmetysRepositoryException if an error occurs while resolving the resource by its id 
127     * @throws UnknownAmetysObjectException if the resource does not exist
128     */
129    public Resource getResource() throws AmetysRepositoryException, UnknownAmetysObjectException
130    {
131        if (_resource == null)
132        {
133            if (_session != null)
134            {
135                try
136                {
137                    _resource = _resolver.resolveById(_resourceId, _session);
138                }
139                catch (RepositoryException e)
140                {
141                    throw new AmetysRepositoryException("Unable to retrieve the resource with the id '" + _resourceId + "'.", e);
142                }
143            }
144            else
145            {
146                _resource = _resolver.resolveById(_resourceId);
147            }
148        }
149        
150        return _resource; 
151    }
152}