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.explorer.cmis;
017
018import java.io.InputStream;
019import java.util.Calendar;
020import java.util.Date;
021
022import org.apache.chemistry.opencmis.client.api.Document;
023import org.apache.chemistry.opencmis.client.api.Folder;
024import org.apache.chemistry.opencmis.commons.data.ContentStream;
025
026import org.ametys.core.user.UserIdentity;
027import org.ametys.core.user.population.UserPopulationDAO;
028import org.ametys.plugins.explorer.ExplorerNode;
029import org.ametys.plugins.explorer.resources.Resource;
030import org.ametys.plugins.repository.AmetysObject;
031import org.ametys.plugins.repository.AmetysRepositoryException;
032import org.ametys.plugins.repository.metadata.UnknownMetadataException;
033
034/**
035 * Implementation of an {@link Resource}, backed by a server CMIS.<br>
036 */
037public class CMISResource implements Resource 
038{
039    private final Document _cmisDocument;
040    private final CMISRootResourcesCollection _root;
041    private AmetysObject _parent;
042    
043    /** 
044     * Creates a {@link CMISResource} 
045     * @param document The CMIS document
046     * @param root The root of the virtual tree
047     * @param parent the parent {@link AmetysObject} if known
048     */
049    public CMISResource(Document document, CMISRootResourcesCollection root, AmetysObject parent) 
050    {
051        _cmisDocument = document;
052        _root = root;
053        _parent = parent;
054    }
055
056    /**
057     * Retrieves the {@link CMISRootResourcesCollection}
058     * @return the {@link CMISRootResourcesCollection}
059     */
060    public CMISRootResourcesCollection getCmisRoot ()
061    {
062        return _root;
063    }
064    
065    /**
066     * Retrieves the {@link Folder}
067     * @return the {@link Folder}
068     */
069    public Document getCmisDocument ()
070    {
071        return _cmisDocument;
072    }
073    
074    @Override
075    public String getId() throws AmetysRepositoryException 
076    {
077        return getCmisRoot().getId() + "/" + getCmisDocument().getId();
078    }
079
080    @Override
081    public String getName()
082    {
083        String fileName = getCmisDocument().getName();
084
085        if (fileName == null)
086        {
087            fileName = getCmisDocument().getContentStreamFileName();
088        }
089        if (fileName == null)
090        {
091            ContentStream cs = getCmisDocument().getContentStream();
092            if (cs != null)
093            {
094                fileName = cs.getFileName();
095            }
096            else
097            {
098                throw new AmetysRepositoryException("contentStream is empty on CMIS Document '" + getCmisDocument().getId() + "'");
099            }
100
101            if (fileName == null)
102            {
103                throw new AmetysRepositoryException("fileName is null on CMIS Document '" + getCmisDocument().getId() + "'");
104            }
105        }
106        return fileName;
107    }
108
109    @Override
110    @SuppressWarnings("unchecked")
111    public AmetysObject getParent() throws AmetysRepositoryException 
112    {
113        if (_parent != null)
114        {
115            return _parent;
116        }
117        
118        Folder parent = getCmisDocument().getParents().iterator().next();
119        
120        if (parent.getId().equals(_root.getRootFolder().getId()))
121        {
122            _parent = getCmisRoot();
123            return _parent;
124        }
125        else
126        {
127            _parent = new CMISResourcesCollection(parent, _root, null);
128            return _parent;
129        }
130    }
131
132    @Override
133    public String getParentPath() throws AmetysRepositoryException 
134    {
135        return getParent().getPath();
136    }
137
138    @Override
139    public String getPath() throws AmetysRepositoryException
140    {
141        return getParentPath() + "/" + getName();
142    }
143
144    @Override
145    public UserIdentity getCreator() 
146    {
147        // FIXME We cannot convert the CMIS remote user to an Ametys user. We need to know the population. If not found, return the system user.
148        // getCmisDocument().getCreatedBy();
149        return UserPopulationDAO.SYSTEM_USER_IDENTITY;
150    }
151
152    @Override
153    public InputStream getInputStream() throws AmetysRepositoryException 
154    {
155        ContentStream contentStream = getCmisDocument().getContentStream();
156        if (contentStream != null)
157        {
158            return contentStream.getStream();
159        }
160        return null;
161    }
162
163    @Override
164    public Date getLastModified() 
165    {
166        Calendar calendar = getCmisDocument().getLastModificationDate();
167        return calendar.getTime();
168    }
169
170    @Override
171    public long getLength()
172    {
173        return getCmisDocument().getContentStreamLength();
174    }
175
176   
177    @Override
178    public String getMimeType() throws AmetysRepositoryException
179    {
180        String mimeType = getCmisDocument().getContentStreamMimeType();
181        
182        return mimeType != null ? mimeType : "application/unknown"; 
183    }
184
185    @Override
186    public String[] getKeywords() throws AmetysRepositoryException
187    {
188        return new String[0];
189    }
190
191    @Override
192    public String getKeywordsAsString() throws AmetysRepositoryException
193    {
194        return "";
195    }
196    
197    @Override
198    public String getResourcePath() throws AmetysRepositoryException
199    {
200        return ((ExplorerNode) getParent()).getExplorerPath() + "/" + getName();
201    }
202    
203    // Dublin Core metadata. //
204    
205    @Override
206    public String getDCTitle() throws AmetysRepositoryException
207    {
208        return null;
209    }
210    
211    @Override
212    public String getDCCreator() throws AmetysRepositoryException
213    {
214        return null;
215    }
216    
217    @Override
218    public String[] getDCSubject() throws AmetysRepositoryException
219    {
220        return null;
221    }
222    
223    @Override
224    public String getDCDescription() throws AmetysRepositoryException
225    {
226        return null;
227    }
228    
229    @Override
230    public String getDCPublisher() throws AmetysRepositoryException
231    {
232        return null;
233    }
234    
235    @Override
236    public String getDCContributor() throws AmetysRepositoryException
237    {
238        return null;
239    }
240    
241    @Override
242    public Date getDCDate() throws AmetysRepositoryException
243    {
244        return null;
245    }
246    
247    @Override
248    public String getDCType() throws AmetysRepositoryException
249    {
250        return null;
251    }
252    
253    @Override
254    public String getDCFormat() throws AmetysRepositoryException
255    {
256        return null;
257    }
258    
259    @Override
260    public String getDCIdentifier() throws AmetysRepositoryException
261    {
262        return null;
263    }
264    
265    @Override
266    public String getDCSource() throws AmetysRepositoryException
267    {
268        return null;
269    }
270    
271    @Override
272    public String getDCLanguage() throws AmetysRepositoryException
273    {
274        return null;
275    }
276    
277    @Override
278    public String getDCRelation() throws AmetysRepositoryException
279    {
280        return null;
281    }
282    
283    @Override
284    public String getDCCoverage() throws AmetysRepositoryException
285    {
286        return null;
287    }
288    
289    @Override
290    public String getDCRights() throws AmetysRepositoryException
291    {
292        return null;
293    }
294
295    public Date getCreationDate() throws UnknownMetadataException, AmetysRepositoryException
296    {
297        Calendar calendar = getCmisDocument().getCreationDate();
298        return calendar.getTime();
299    }
300
301    public UserIdentity getLastContributor() throws UnknownMetadataException, AmetysRepositoryException
302    {
303        return UserIdentity.stringToUserIdentity(getCmisDocument().getLastModifiedBy());
304    }
305}