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