001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.repository.cmis.model;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.repository.model.FileVersion;
023    import com.liferay.portal.kernel.util.FileUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.MimeTypesUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.repository.cmis.CMISRepository;
030    import com.liferay.portal.security.auth.PrincipalThreadLocal;
031    import com.liferay.portal.service.CMISRepositoryLocalServiceUtil;
032    import com.liferay.portal.service.UserLocalServiceUtil;
033    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
034    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
035    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalServiceUtil;
036    import com.liferay.portlet.documentlibrary.util.DLUtil;
037    import com.liferay.portlet.expando.model.ExpandoBridge;
038    
039    import java.io.InputStream;
040    import java.io.Serializable;
041    
042    import java.util.Calendar;
043    import java.util.Date;
044    import java.util.HashMap;
045    import java.util.Iterator;
046    import java.util.List;
047    import java.util.Map;
048    import java.util.Set;
049    
050    import org.apache.chemistry.opencmis.client.api.Document;
051    import org.apache.chemistry.opencmis.commons.data.ContentStream;
052    import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
053    
054    /**
055     * @author Alexander Chow
056     */
057    public class CMISFileVersion extends CMISModel implements FileVersion {
058    
059            public CMISFileVersion(
060                    CMISRepository cmisRepository, long fileVersionId, Document document) {
061    
062                    _cmisRepository = cmisRepository;
063                    _fileVersionId = fileVersionId;
064                    _document = document;
065            }
066    
067            public Map<String, Serializable> getAttributes() {
068                    return new HashMap<String, Serializable>();
069            }
070    
071            public String getChangeLog() {
072                    return _document.getCheckinComment();
073            }
074    
075            @Override
076            public long getCompanyId() {
077                    return _cmisRepository.getCompanyId();
078            }
079    
080            public InputStream getContentStream(boolean incrementCounter) {
081                    ContentStream contentStream = _document.getContentStream();
082    
083                    try {
084                            DLAppHelperLocalServiceUtil.getFileAsStream(
085                                    PrincipalThreadLocal.getUserId(), getFileEntry(),
086                                    incrementCounter);
087                    }
088                    catch (Exception e) {
089                            _log.error(e);
090                    }
091    
092                    return contentStream.getStream();
093            }
094    
095            public Date getCreateDate() {
096                    Calendar creationDate = _document.getCreationDate();
097    
098                    return creationDate.getTime();
099            }
100    
101            @Override
102            public ExpandoBridge getExpandoBridge() {
103                    return null;
104            }
105    
106            public String getExtension() {
107                    String extension = FileUtil.getExtension(getTitle());
108    
109                    if (Validator.isNotNull(extension)) {
110                            return extension;
111                    }
112    
113                    Set<String> extensions = MimeTypesUtil.getExtensions(getMimeType());
114    
115                    if (extensions.isEmpty()) {
116                            return extension;
117                    }
118    
119                    Iterator<String> iterator = extensions.iterator();
120    
121                    return iterator.next();
122            }
123    
124            public String getExtraSettings() {
125                    return null;
126            }
127    
128            public FileEntry getFileEntry() throws PortalException, SystemException {
129                    Document document = null;
130    
131                    try {
132                            List<Document> allVersions = _document.getAllVersions();
133    
134                            if (allVersions.isEmpty()) {
135                                    document = _document;
136                            }
137                            else {
138                                    document = allVersions.get(0);
139                            }
140                    }
141                    catch (CmisObjectNotFoundException confe) {
142                            throw new NoSuchFileEntryException(confe);
143                    }
144    
145                    return CMISRepositoryLocalServiceUtil.toFileEntry(
146                            getRepositoryId(), document);
147            }
148    
149            public long getFileEntryId() {
150                    try {
151                            return getFileEntry().getFileEntryId();
152                    }
153                    catch (NoSuchFileEntryException nsfee) {
154                    }
155                    catch (Exception e) {
156                            _log.error(e, e);
157                    }
158    
159                    return 0;
160            }
161    
162            public long getFileVersionId() {
163                    return _fileVersionId;
164            }
165    
166            public long getGroupId() {
167                    return _cmisRepository.getGroupId();
168            }
169    
170            public String getIcon() {
171                    return DLUtil.getFileIcon(getExtension());
172            }
173    
174            public String getMimeType() {
175                    String mimeType = _document.getContentStreamMimeType();
176    
177                    if (Validator.isNotNull(mimeType)) {
178                            return mimeType;
179                    }
180    
181                    return MimeTypesUtil.getContentType(getTitle());
182            }
183    
184            public Object getModel() {
185                    return _document;
186            }
187    
188            public Class<?> getModelClass() {
189                    return DLFileVersion.class;
190            }
191    
192            @Override
193            public String getModelClassName() {
194                    return DLFileVersion.class.getName();
195            }
196    
197            public Date getModifiedDate() {
198                    Calendar modificationDate = _document.getLastModificationDate();
199    
200                    return modificationDate.getTime();
201            }
202    
203            @Override
204            public long getPrimaryKey() {
205                    return _fileVersionId;
206            }
207    
208            public Serializable getPrimaryKeyObj() {
209                    return getPrimaryKey();
210            }
211    
212            public long getRepositoryId() {
213                    return _cmisRepository.getRepositoryId();
214            }
215    
216            public long getSize() {
217                    return _document.getContentStreamLength();
218            }
219    
220            public int getStatus() {
221                    return 0;
222            }
223    
224            public long getStatusByUserId() {
225                    return 0;
226            }
227    
228            public String getStatusByUserName() {
229                    return null;
230            }
231    
232            public String getStatusByUserUuid() {
233                    return null;
234            }
235    
236            public Date getStatusDate() {
237                    return getModifiedDate();
238            }
239    
240            public String getTitle() {
241                    return _document.getName();
242            }
243    
244            public long getUserId() {
245                    try {
246                            return UserLocalServiceUtil.getDefaultUserId(getCompanyId());
247                    }
248                    catch (Exception e) {
249                            return 0;
250                    }
251            }
252    
253            public String getUserName() {
254                    return _document.getCreatedBy();
255            }
256    
257            public String getUserUuid() {
258                    try {
259                            User user = UserLocalServiceUtil.getDefaultUser(getCompanyId());
260    
261                            return user.getUserUuid();
262                    }
263                    catch (Exception e) {
264                            return StringPool.BLANK;
265                    }
266            }
267    
268            public String getVersion() {
269                    return GetterUtil.getString(_document.getVersionLabel());
270            }
271    
272            public boolean isApproved() {
273                    return false;
274            }
275    
276            public boolean isDefaultRepository() {
277                    return false;
278            }
279    
280            public boolean isDraft() {
281                    return false;
282            }
283    
284            public boolean isEscapedModel() {
285                    return false;
286            }
287    
288            public boolean isExpired() {
289                    return false;
290            }
291    
292            public boolean isPending() {
293                    return false;
294            }
295    
296            public void setCompanyId(long companyId) {
297                    _cmisRepository.setCompanyId(companyId);
298            }
299    
300            public void setCreateDate(Date date) {
301            }
302    
303            public void setFileVersionId(long fileVersionId) {
304                    _fileVersionId = fileVersionId;
305            }
306    
307            public void setGroupId(long groupId) {
308                    _cmisRepository.setGroupId(groupId);
309            }
310    
311            public void setModifiedDate(Date date) {
312            }
313    
314            public void setPrimaryKey(long primaryKey) {
315                    setFileVersionId(primaryKey);
316            }
317    
318            public void setPrimaryKeyObj(Serializable primaryKeyObj) {
319                    setPrimaryKey(((Long)primaryKeyObj).longValue());
320            }
321    
322            public void setUserId(long userId) {
323            }
324    
325            public void setUserName(String userName) {
326            }
327    
328            public void setUserUuid(String userUuid) {
329            }
330    
331            public FileVersion toEscapedModel() {
332                    return this;
333            }
334    
335            @Override
336            protected CMISRepository getCmisRepository() {
337                    return _cmisRepository;
338            }
339    
340            private static Log _log = LogFactoryUtil.getLog(CMISFileVersion.class);
341    
342            private CMISRepository _cmisRepository;
343            private Document _document;
344            private long _fileVersionId;
345    
346    }