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