001    /**
002     * Copyright (c) 2000-2013 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.service.DLAppHelperLocalServiceUtil;
035    import com.liferay.portlet.documentlibrary.util.DLUtil;
036    import com.liferay.portlet.expando.model.ExpandoBridge;
037    
038    import java.io.InputStream;
039    import java.io.Serializable;
040    
041    import java.util.Calendar;
042    import java.util.Date;
043    import java.util.HashMap;
044    import java.util.List;
045    import java.util.Map;
046    
047    import org.apache.chemistry.opencmis.client.api.Document;
048    import org.apache.chemistry.opencmis.commons.data.ContentStream;
049    import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
050    
051    /**
052     * @author Alexander Chow
053     */
054    public class CMISFileVersion extends CMISModel implements FileVersion {
055    
056            public CMISFileVersion(
057                    CMISRepository cmisRepository, String uuid, long fileVersionId,
058                    Document document) {
059    
060                    _cmisRepository = cmisRepository;
061                    _uuid = uuid;
062                    _fileVersionId = fileVersionId;
063                    _document = document;
064            }
065    
066            @Override
067            public Object clone() {
068                    CMISFileVersion cmisFileVersion = new CMISFileVersion(
069                            _cmisRepository, _uuid, _fileVersionId, _document);
070    
071                    cmisFileVersion.setCompanyId(getCompanyId());
072                    setFileVersionId(getFileVersionId());
073                    cmisFileVersion.setGroupId(getGroupId());
074    
075                    try {
076                            cmisFileVersion.setParentFolder(getParentFolder());
077                    }
078                    catch (Exception e) {
079                    }
080    
081                    cmisFileVersion.setPrimaryKey(getPrimaryKey());
082    
083                    return cmisFileVersion;
084            }
085    
086            public Map<String, Serializable> getAttributes() {
087                    return new HashMap<String, Serializable>();
088            }
089    
090            public String getChangeLog() {
091                    return _document.getCheckinComment();
092            }
093    
094            @Override
095            public long getCompanyId() {
096                    return _cmisRepository.getCompanyId();
097            }
098    
099            public InputStream getContentStream(boolean incrementCounter) {
100                    ContentStream contentStream = _document.getContentStream();
101    
102                    try {
103                            DLAppHelperLocalServiceUtil.getFileAsStream(
104                                    PrincipalThreadLocal.getUserId(), getFileEntry(),
105                                    incrementCounter);
106                    }
107                    catch (Exception e) {
108                            _log.error(e);
109                    }
110    
111                    return contentStream.getStream();
112            }
113    
114            public Date getCreateDate() {
115                    Calendar creationDate = _document.getCreationDate();
116    
117                    return creationDate.getTime();
118            }
119    
120            @Override
121            public ExpandoBridge getExpandoBridge() {
122                    return null;
123            }
124    
125            public String getExtension() {
126                    return FileUtil.getExtension(getTitle());
127            }
128    
129            public String getExtraSettings() {
130                    return null;
131            }
132    
133            public FileEntry getFileEntry() throws PortalException, SystemException {
134                    Document document = null;
135    
136                    try {
137                            List<Document> allVersions = _document.getAllVersions();
138    
139                            if (allVersions.isEmpty()) {
140                                    document = _document;
141                            }
142                            else {
143                                    document = allVersions.get(0);
144                            }
145                    }
146                    catch (CmisObjectNotFoundException confe) {
147                            throw new NoSuchFileEntryException(confe);
148                    }
149    
150                    return CMISRepositoryLocalServiceUtil.toFileEntry(
151                            getRepositoryId(), document);
152            }
153    
154            public long getFileEntryId() {
155                    try {
156                            return getFileEntry().getFileEntryId();
157                    }
158                    catch (NoSuchFileEntryException nsfee) {
159                    }
160                    catch (Exception e) {
161                            _log.error(e, e);
162                    }
163    
164                    return 0;
165            }
166    
167            public long getFileVersionId() {
168                    return _fileVersionId;
169            }
170    
171            public long getGroupId() {
172                    return _cmisRepository.getGroupId();
173            }
174    
175            public String getIcon() {
176                    return DLUtil.getFileIcon(getExtension());
177            }
178    
179            public String getMimeType() {
180                    String mimeType = _document.getContentStreamMimeType();
181    
182                    if (Validator.isNotNull(mimeType)) {
183                            return mimeType;
184                    }
185    
186                    return MimeTypesUtil.getContentType(getTitle());
187            }
188    
189            public Object getModel() {
190                    return _document;
191            }
192    
193            public Class<?> getModelClass() {
194                    return CMISFileVersion.class;
195            }
196    
197            @Override
198            public String getModelClassName() {
199                    return CMISFileVersion.class.getName();
200            }
201    
202            public Date getModifiedDate() {
203                    Calendar modificationDate = _document.getLastModificationDate();
204    
205                    return modificationDate.getTime();
206            }
207    
208            @Override
209            public long getPrimaryKey() {
210                    return _fileVersionId;
211            }
212    
213            public Serializable getPrimaryKeyObj() {
214                    return getPrimaryKey();
215            }
216    
217            public long getRepositoryId() {
218                    return _cmisRepository.getRepositoryId();
219            }
220    
221            public long getSize() {
222                    return _document.getContentStreamLength();
223            }
224    
225            public int getStatus() {
226                    return 0;
227            }
228    
229            public long getStatusByUserId() {
230                    return 0;
231            }
232    
233            public String getStatusByUserName() {
234                    return null;
235            }
236    
237            public String getStatusByUserUuid() {
238                    return null;
239            }
240    
241            public Date getStatusDate() {
242                    return getModifiedDate();
243            }
244    
245            public String getTitle() {
246                    return _document.getName();
247            }
248    
249            public long getUserId() {
250                    try {
251                            return UserLocalServiceUtil.getDefaultUserId(getCompanyId());
252                    }
253                    catch (Exception e) {
254                            return 0;
255                    }
256            }
257    
258            public String getUserName() {
259                    return _document.getCreatedBy();
260            }
261    
262            public String getUserUuid() {
263                    try {
264                            User user = UserLocalServiceUtil.getDefaultUser(getCompanyId());
265    
266                            return user.getUserUuid();
267                    }
268                    catch (Exception e) {
269                            return StringPool.BLANK;
270                    }
271            }
272    
273            public String getUuid() {
274                    return _uuid;
275            }
276    
277            public String getVersion() {
278                    return GetterUtil.getString(_document.getVersionLabel());
279            }
280    
281            public boolean isApproved() {
282                    return false;
283            }
284    
285            public boolean isDefaultRepository() {
286                    return false;
287            }
288    
289            public boolean isDraft() {
290                    return false;
291            }
292    
293            public boolean isEscapedModel() {
294                    return false;
295            }
296    
297            public boolean isExpired() {
298                    return false;
299            }
300    
301            public boolean isInTrash() {
302                    return false;
303            }
304    
305            public boolean isInTrashContainer() {
306                    return false;
307            }
308    
309            public boolean isPending() {
310                    return false;
311            }
312    
313            public void setCompanyId(long companyId) {
314                    _cmisRepository.setCompanyId(companyId);
315            }
316    
317            public void setCreateDate(Date date) {
318            }
319    
320            public void setFileVersionId(long fileVersionId) {
321                    _fileVersionId = fileVersionId;
322            }
323    
324            public void setGroupId(long groupId) {
325                    _cmisRepository.setGroupId(groupId);
326            }
327    
328            public void setModifiedDate(Date date) {
329            }
330    
331            public void setPrimaryKey(long primaryKey) {
332                    setFileVersionId(primaryKey);
333            }
334    
335            public void setPrimaryKeyObj(Serializable primaryKeyObj) {
336                    setPrimaryKey(((Long)primaryKeyObj).longValue());
337            }
338    
339            public void setUserId(long userId) {
340            }
341    
342            public void setUserName(String userName) {
343            }
344    
345            public void setUserUuid(String userUuid) {
346            }
347    
348            public void setUuid(String uuid) {
349            }
350    
351            public FileVersion toEscapedModel() {
352                    return this;
353            }
354    
355            public FileVersion toUnescapedModel() {
356                    return this;
357            }
358    
359            @Override
360            protected CMISRepository getCmisRepository() {
361                    return _cmisRepository;
362            }
363    
364            private static Log _log = LogFactoryUtil.getLog(CMISFileVersion.class);
365    
366            private CMISRepository _cmisRepository;
367            private Document _document;
368            private long _fileVersionId;
369            private String _uuid;
370    
371    }