001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.StringPool;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.repository.cmis.CMISRepository;
027    import com.liferay.portal.security.auth.PrincipalThreadLocal;
028    import com.liferay.portal.service.CMISRepositoryLocalServiceUtil;
029    import com.liferay.portal.service.UserLocalServiceUtil;
030    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
031    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalServiceUtil;
032    import com.liferay.portlet.documentlibrary.util.DLUtil;
033    import com.liferay.portlet.expando.model.ExpandoBridge;
034    
035    import java.io.InputStream;
036    import java.io.Serializable;
037    
038    import java.util.Calendar;
039    import java.util.Date;
040    import java.util.HashMap;
041    import java.util.List;
042    import java.util.Map;
043    
044    import org.apache.chemistry.opencmis.client.api.Document;
045    import org.apache.chemistry.opencmis.commons.data.ContentStream;
046    
047    /**
048     * @author Alexander Chow
049     */
050    public class CMISFileVersion extends CMISModel implements FileVersion {
051    
052            public CMISFileVersion(
053                    CMISRepository cmisRepository, long fileVersionId, Document document) {
054    
055                    _cmisRepository = cmisRepository;
056                    _fileVersionId = fileVersionId;
057                    _document = document;
058            }
059    
060            public Map<String, Serializable> getAttributes() {
061                    return new HashMap<String, Serializable>();
062            }
063    
064            public String getChangeLog() {
065                    return _document.getCheckinComment();
066            }
067    
068            @Override
069            public long getCompanyId() {
070                    return _cmisRepository.getCompanyId();
071            }
072    
073            public InputStream getContentStream(boolean incrementCounter) {
074                    ContentStream contentStream = _document.getContentStream();
075    
076                    try {
077                            DLAppHelperLocalServiceUtil.getFileAsStream(
078                                    PrincipalThreadLocal.getUserId(), getFileEntry(),
079                                    incrementCounter);
080                    }
081                    catch (Exception e) {
082                            _log.error(e);
083                    }
084    
085                    return contentStream.getStream();
086            }
087    
088            public Date getCreateDate() {
089                    Calendar creationDate = _document.getCreationDate();
090    
091                    return creationDate.getTime();
092            }
093    
094            @Override
095            public ExpandoBridge getExpandoBridge() {
096                    return null;
097            }
098    
099            public String getExtension() {
100                    return FileUtil.getExtension(getTitle());
101            }
102    
103            public String getExtraSettings() {
104                    return null;
105            }
106    
107            public FileEntry getFileEntry() throws PortalException, SystemException {
108                    Document document = null;
109    
110                    List<Document> allVersions = _document.getAllVersions();
111    
112                    if (allVersions.isEmpty()) {
113                            document = _document;
114                    }
115                    else {
116                            document = allVersions.get(0);
117                    }
118    
119                    return CMISRepositoryLocalServiceUtil.toFileEntry(
120                            getRepositoryId(), document);
121            }
122    
123            public long getFileEntryId() {
124                    try {
125                            return getFileEntry().getFileEntryId();
126                    }
127                    catch (Exception e) {
128                            _log.error(e, e);
129                    }
130    
131                    return 0;
132            }
133    
134            public long getFileVersionId() {
135                    return _fileVersionId;
136            }
137    
138            public long getGroupId() {
139                    return _cmisRepository.getGroupId();
140            }
141    
142            public String getIcon() {
143                    return DLUtil.getFileIcon(getExtension());
144            }
145    
146            public String getMimeType() {
147                    return _document.getContentStreamMimeType();
148            }
149    
150            public Object getModel() {
151                    return _document;
152            }
153    
154            public Class<?> getModelClass() {
155                    return DLFileVersion.class;
156            }
157    
158            @Override
159            public String getModelClassName() {
160                    return DLFileVersion.class.getName();
161            }
162    
163            public Date getModifiedDate() {
164                    Calendar modificationDate = _document.getLastModificationDate();
165    
166                    return modificationDate.getTime();
167            }
168    
169            @Override
170            public long getPrimaryKey() {
171                    return _fileVersionId;
172            }
173    
174            public Serializable getPrimaryKeyObj() {
175                    return getPrimaryKey();
176            }
177    
178            public long getRepositoryId() {
179                    return _cmisRepository.getRepositoryId();
180            }
181    
182            public long getSize() {
183                    return _document.getContentStreamLength();
184            }
185    
186            public int getStatus() {
187                    return 0;
188            }
189    
190            public long getStatusByUserId() {
191                    return 0;
192            }
193    
194            public String getStatusByUserName() {
195                    return null;
196            }
197    
198            public String getStatusByUserUuid() {
199                    return null;
200            }
201    
202            public Date getStatusDate() {
203                    return getModifiedDate();
204            }
205    
206            public String getTitle() {
207                    return _document.getName();
208            }
209    
210            public long getUserId() {
211                    try {
212                            return UserLocalServiceUtil.getDefaultUserId(getCompanyId());
213                    }
214                    catch (Exception e) {
215                            return 0;
216                    }
217            }
218    
219            public String getUserName() {
220                    return _document.getCreatedBy();
221            }
222    
223            public String getUserUuid() {
224                    try {
225                            User user = UserLocalServiceUtil.getDefaultUser(
226                                    getCompanyId());
227    
228                            return user.getUserUuid();
229                    }
230                    catch (Exception e) {
231                            return StringPool.BLANK;
232                    }
233            }
234    
235            public String getVersion() {
236                    return _document.getVersionLabel();
237            }
238    
239            public boolean isApproved() {
240                    return false;
241            }
242    
243            public boolean isDefaultRepository() {
244                    return false;
245            }
246    
247            public boolean isDraft() {
248                    return false;
249            }
250    
251            public boolean isEscapedModel() {
252                    return false;
253            }
254    
255            public boolean isExpired() {
256                    return false;
257            }
258    
259            public boolean isPending() {
260                    return false;
261            }
262    
263            public void setCompanyId(long companyId) {
264                    _cmisRepository.setCompanyId(companyId);
265            }
266    
267            public void setCreateDate(Date date) {
268            }
269    
270            public void setFileVersionId(long fileVersionId) {
271                    _fileVersionId = fileVersionId;
272            }
273    
274            public void setGroupId(long groupId) {
275                    _cmisRepository.setGroupId(groupId);
276            }
277    
278            public void setModifiedDate(Date date) {
279            }
280    
281            public void setPrimaryKey(long primaryKey) {
282                    setFileVersionId(primaryKey);
283            }
284    
285            public void setPrimaryKeyObj(Serializable primaryKeyObj) {
286                    setPrimaryKey(((Long)primaryKeyObj).longValue());
287            }
288    
289            public void setUserId(long userId) {
290            }
291    
292            public void setUserName(String userName) {
293            }
294    
295            public void setUserUuid(String userUuid) {
296            }
297    
298            public FileVersion toEscapedModel() {
299                    return this;
300            }
301    
302            @Override
303            protected CMISRepository getCmisRepository() {
304                    return _cmisRepository;
305            }
306    
307            private static Log _log = LogFactoryUtil.getLog(CMISFileVersion.class);
308    
309            private CMISRepository _cmisRepository;
310            private Document _document;
311            private long _fileVersionId;
312    
313    }