001    /**
002     * Copyright (c) 2000-2012 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.List;
046    import java.util.Map;
047    
048    import org.apache.chemistry.opencmis.client.api.Document;
049    import org.apache.chemistry.opencmis.commons.data.ContentStream;
050    import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
051    
052    /**
053     * @author Alexander Chow
054     */
055    public class CMISFileVersion extends CMISModel implements FileVersion {
056    
057            public CMISFileVersion(
058                    CMISRepository cmisRepository, String uuid, long fileVersionId,
059                    Document document) {
060    
061                    _cmisRepository = cmisRepository;
062                    _uuid = uuid;
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                    return FileUtil.getExtension(getTitle());
108            }
109    
110            public String getExtraSettings() {
111                    return null;
112            }
113    
114            public FileEntry getFileEntry() throws PortalException, SystemException {
115                    Document document = null;
116    
117                    try {
118                            List<Document> allVersions = _document.getAllVersions();
119    
120                            if (allVersions.isEmpty()) {
121                                    document = _document;
122                            }
123                            else {
124                                    document = allVersions.get(0);
125                            }
126                    }
127                    catch (CmisObjectNotFoundException confe) {
128                            throw new NoSuchFileEntryException(confe);
129                    }
130    
131                    return CMISRepositoryLocalServiceUtil.toFileEntry(
132                            getRepositoryId(), document);
133            }
134    
135            public long getFileEntryId() {
136                    try {
137                            return getFileEntry().getFileEntryId();
138                    }
139                    catch (NoSuchFileEntryException nsfee) {
140                    }
141                    catch (Exception e) {
142                            _log.error(e, e);
143                    }
144    
145                    return 0;
146            }
147    
148            public long getFileVersionId() {
149                    return _fileVersionId;
150            }
151    
152            public long getGroupId() {
153                    return _cmisRepository.getGroupId();
154            }
155    
156            public String getIcon() {
157                    return DLUtil.getFileIcon(getExtension());
158            }
159    
160            public String getMimeType() {
161                    String mimeType = _document.getContentStreamMimeType();
162    
163                    if (Validator.isNotNull(mimeType)) {
164                            return mimeType;
165                    }
166    
167                    return MimeTypesUtil.getContentType(getTitle());
168            }
169    
170            public Object getModel() {
171                    return _document;
172            }
173    
174            public Class<?> getModelClass() {
175                    return DLFileVersion.class;
176            }
177    
178            @Override
179            public String getModelClassName() {
180                    return DLFileVersion.class.getName();
181            }
182    
183            public Date getModifiedDate() {
184                    Calendar modificationDate = _document.getLastModificationDate();
185    
186                    return modificationDate.getTime();
187            }
188    
189            @Override
190            public long getPrimaryKey() {
191                    return _fileVersionId;
192            }
193    
194            public Serializable getPrimaryKeyObj() {
195                    return getPrimaryKey();
196            }
197    
198            public long getRepositoryId() {
199                    return _cmisRepository.getRepositoryId();
200            }
201    
202            public long getSize() {
203                    return _document.getContentStreamLength();
204            }
205    
206            public int getStatus() {
207                    return 0;
208            }
209    
210            public long getStatusByUserId() {
211                    return 0;
212            }
213    
214            public String getStatusByUserName() {
215                    return null;
216            }
217    
218            public String getStatusByUserUuid() {
219                    return null;
220            }
221    
222            public Date getStatusDate() {
223                    return getModifiedDate();
224            }
225    
226            public String getTitle() {
227                    return _document.getName();
228            }
229    
230            public long getUserId() {
231                    try {
232                            return UserLocalServiceUtil.getDefaultUserId(getCompanyId());
233                    }
234                    catch (Exception e) {
235                            return 0;
236                    }
237            }
238    
239            public String getUserName() {
240                    return _document.getCreatedBy();
241            }
242    
243            public String getUserUuid() {
244                    try {
245                            User user = UserLocalServiceUtil.getDefaultUser(getCompanyId());
246    
247                            return user.getUserUuid();
248                    }
249                    catch (Exception e) {
250                            return StringPool.BLANK;
251                    }
252            }
253    
254            public String getUuid() {
255                    return _uuid;
256            }
257    
258            public String getVersion() {
259                    return GetterUtil.getString(_document.getVersionLabel());
260            }
261    
262            public boolean isApproved() {
263                    return false;
264            }
265    
266            public boolean isDefaultRepository() {
267                    return false;
268            }
269    
270            public boolean isDraft() {
271                    return false;
272            }
273    
274            public boolean isEscapedModel() {
275                    return false;
276            }
277    
278            public boolean isExpired() {
279                    return false;
280            }
281    
282            public boolean isPending() {
283                    return false;
284            }
285    
286            public void setCompanyId(long companyId) {
287                    _cmisRepository.setCompanyId(companyId);
288            }
289    
290            public void setCreateDate(Date date) {
291            }
292    
293            public void setFileVersionId(long fileVersionId) {
294                    _fileVersionId = fileVersionId;
295            }
296    
297            public void setGroupId(long groupId) {
298                    _cmisRepository.setGroupId(groupId);
299            }
300    
301            public void setModifiedDate(Date date) {
302            }
303    
304            public void setPrimaryKey(long primaryKey) {
305                    setFileVersionId(primaryKey);
306            }
307    
308            public void setPrimaryKeyObj(Serializable primaryKeyObj) {
309                    setPrimaryKey(((Long)primaryKeyObj).longValue());
310            }
311    
312            public void setUserId(long userId) {
313            }
314    
315            public void setUserName(String userName) {
316            }
317    
318            public void setUserUuid(String userUuid) {
319            }
320    
321            public FileVersion toEscapedModel() {
322                    return this;
323            }
324    
325            @Override
326            protected CMISRepository getCmisRepository() {
327                    return _cmisRepository;
328            }
329    
330            private static Log _log = LogFactoryUtil.getLog(CMISFileVersion.class);
331    
332            private CMISRepository _cmisRepository;
333            private Document _document;
334            private long _fileVersionId;
335            private String _uuid;
336    
337    }