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.RepositoryException;
022    import com.liferay.portal.kernel.repository.model.FileEntry;
023    import com.liferay.portal.kernel.repository.model.FileVersion;
024    import com.liferay.portal.kernel.repository.model.Folder;
025    import com.liferay.portal.kernel.util.ContentTypes;
026    import com.liferay.portal.kernel.util.FileUtil;
027    import com.liferay.portal.kernel.util.GetterUtil;
028    import com.liferay.portal.kernel.util.MimeTypesUtil;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.model.Lock;
032    import com.liferay.portal.model.RepositoryEntry;
033    import com.liferay.portal.model.User;
034    import com.liferay.portal.repository.cmis.CMISRepository;
035    import com.liferay.portal.security.auth.PrincipalThreadLocal;
036    import com.liferay.portal.security.permission.PermissionChecker;
037    import com.liferay.portal.service.CMISRepositoryLocalServiceUtil;
038    import com.liferay.portal.service.RepositoryEntryLocalServiceUtil;
039    import com.liferay.portal.service.persistence.LockUtil;
040    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
041    import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
042    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
043    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalServiceUtil;
044    import com.liferay.portlet.documentlibrary.util.DLUtil;
045    
046    import java.io.InputStream;
047    import java.io.Serializable;
048    
049    import java.util.ArrayList;
050    import java.util.Date;
051    import java.util.HashMap;
052    import java.util.List;
053    import java.util.Map;
054    import java.util.Set;
055    
056    import org.apache.chemistry.opencmis.client.api.Document;
057    import org.apache.chemistry.opencmis.commons.data.AllowableActions;
058    import org.apache.chemistry.opencmis.commons.data.CmisExtensionElement;
059    import org.apache.chemistry.opencmis.commons.data.ContentStream;
060    import org.apache.chemistry.opencmis.commons.enums.Action;
061    import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
062    
063    /**
064     * @author Alexander Chow
065     */
066    public class CMISFileEntry extends CMISModel implements FileEntry {
067    
068            public CMISFileEntry(
069                    CMISRepository cmisRepository, String uuid, long fileEntryId,
070                    Document document) {
071    
072                    _cmisRepository = cmisRepository;
073                    _uuid = uuid;
074                    _fileEntryId = fileEntryId;
075                    _document = document;
076            }
077    
078            public boolean containsPermission(
079                            PermissionChecker permissionChecker, String actionId)
080                    throws SystemException {
081    
082                    return containsPermission(_document, actionId);
083            }
084    
085            public Map<String, Serializable> getAttributes() {
086                    return new HashMap<String, Serializable>();
087            }
088    
089            @Override
090            public long getCompanyId() {
091                    return _cmisRepository.getCompanyId();
092            }
093    
094            public InputStream getContentStream() {
095                    ContentStream contentStream = _document.getContentStream();
096    
097                    try {
098                            DLAppHelperLocalServiceUtil.getFileAsStream(
099                                    PrincipalThreadLocal.getUserId(), this, true);
100                    }
101                    catch (Exception e) {
102                            _log.error(e, e);
103                    }
104    
105                    return contentStream.getStream();
106            }
107    
108            public InputStream getContentStream(String version) throws PortalException {
109                    if (Validator.isNull(version)) {
110                            return getContentStream();
111                    }
112    
113                    for (Document document : getAllVersions()) {
114                            if (version.equals(document.getVersionLabel())) {
115                                    ContentStream contentStream = document.getContentStream();
116    
117                                    try {
118                                            DLAppHelperLocalServiceUtil.getFileAsStream(
119                                                    PrincipalThreadLocal.getUserId(), this, true);
120                                    }
121                                    catch (Exception e) {
122                                            _log.error(e, e);
123                                    }
124    
125                                    return contentStream.getStream();
126                            }
127                    }
128    
129                    throw new NoSuchFileVersionException(
130                            "No CMIS file version with {fileEntryId=" + getFileEntryId() +
131                                    ", version=" + version + "}");
132            }
133    
134            public Date getCreateDate() {
135                    return _document.getCreationDate().getTime();
136            }
137    
138            public String getExtension() {
139                    return FileUtil.getExtension(getTitle());
140            }
141    
142            public long getFileEntryId() {
143                    return _fileEntryId;
144            }
145    
146            public FileVersion getFileVersion()
147                    throws PortalException, SystemException {
148    
149                    return getLatestFileVersion();
150            }
151    
152            public FileVersion getFileVersion(String version)
153                    throws PortalException, SystemException {
154    
155                    if (Validator.isNull(version)) {
156                            return getFileVersion();
157                    }
158    
159                    for (Document document : getAllVersions()) {
160                            if (version.equals(document.getVersionLabel())) {
161                                    return CMISRepositoryLocalServiceUtil.toFileVersion(
162                                            getRepositoryId(), document);
163                            }
164                    }
165    
166                    throw new NoSuchFileVersionException(
167                            "No CMIS file version with {fileEntryId=" + getFileEntryId() +
168                                    ", version=" + version + "}");
169            }
170    
171            public List<FileVersion> getFileVersions(int status)
172                    throws SystemException {
173    
174                    try {
175                            List<Document> documents = getAllVersions();
176    
177                            List<FileVersion> fileVersions = new ArrayList<FileVersion>(
178                                    documents.size());
179    
180                            for (Document document : documents) {
181                                    FileVersion fileVersion =
182                                            CMISRepositoryLocalServiceUtil.toFileVersion(
183                                                    getRepositoryId(), document);
184    
185                                    fileVersions.add(fileVersion);
186                            }
187    
188                            return fileVersions;
189                    }
190                    catch (PortalException pe) {
191                            throw new RepositoryException(pe);
192                    }
193            }
194    
195            public Folder getFolder() {
196                    Folder parentFolder = null;
197    
198                    try {
199                            parentFolder = super.getParentFolder();
200    
201                            if (parentFolder != null) {
202                                    return parentFolder;
203                            }
204                    }
205                    catch (Exception e) {
206                    }
207    
208                    try {
209                            List<org.apache.chemistry.opencmis.client.api.Folder>
210                                    cmisParentFolders = _document.getParents();
211    
212                            if (cmisParentFolders.isEmpty()) {
213                                    _document = _document.getObjectOfLatestVersion(false);
214    
215                                    cmisParentFolders = _document.getParents();
216                            }
217    
218                            parentFolder = CMISRepositoryLocalServiceUtil.toFolder(
219                                    getRepositoryId(), cmisParentFolders.get(0));
220    
221                            setParentFolder(parentFolder);
222                    }
223                    catch (Exception e) {
224                            _log.error(e, e);
225                    }
226    
227                    return parentFolder;
228            }
229    
230            public long getFolderId() {
231                    Folder folder = getFolder();
232    
233                    return folder.getFolderId();
234            }
235    
236            public long getGroupId() {
237                    return _cmisRepository.getGroupId();
238            }
239    
240            public String getIcon() {
241                    return DLUtil.getFileIcon(getExtension());
242            }
243    
244            public FileVersion getLatestFileVersion()
245                    throws PortalException, SystemException {
246    
247                    if (_latestFileVersion != null) {
248                            return _latestFileVersion;
249                    }
250    
251                    List<Document> documents = getAllVersions();
252    
253                    if (!documents.isEmpty()) {
254                            Document latestDocumentVersion = documents.get(0);
255    
256                            _latestFileVersion = CMISRepositoryLocalServiceUtil.toFileVersion(
257                                    getRepositoryId(), latestDocumentVersion);
258                    }
259                    else {
260                            _latestFileVersion = CMISRepositoryLocalServiceUtil.toFileVersion(
261                                    getRepositoryId(), _document);
262                    }
263    
264                    return _latestFileVersion;
265            }
266    
267            public Lock getLock() {
268                    if (!isCheckedOut()) {
269                            return null;
270                    }
271    
272                    String checkedOutBy = _document.getVersionSeriesCheckedOutBy();
273    
274                    User user = getUser(checkedOutBy);
275    
276                    Lock lock = LockUtil.create(0);
277    
278                    lock.setCompanyId(getCompanyId());
279    
280                    if (user != null) {
281                            lock.setUserId(user.getUserId());
282                            lock.setUserName(user.getFullName());
283                    }
284    
285                    lock.setCreateDate(new Date());
286    
287                    return lock;
288            }
289    
290            public String getMimeType() {
291                    String mimeType = _document.getContentStreamMimeType();
292    
293                    if (Validator.isNotNull(mimeType)) {
294                            return mimeType;
295                    }
296    
297                    return MimeTypesUtil.getContentType(getTitle());
298            }
299    
300            public String getMimeType(String version) {
301                    if (Validator.isNull(version)) {
302                            return getMimeType();
303                    }
304    
305                    try {
306                            for (Document document : getAllVersions()) {
307                                    if (!version.equals(document.getVersionLabel())) {
308                                            continue;
309                                    }
310    
311                                    String mimeType = document.getContentStreamMimeType();
312    
313                                    if (Validator.isNotNull(mimeType)) {
314                                            return mimeType;
315                                    }
316    
317                                    return MimeTypesUtil.getContentType(document.getName());
318                            }
319                    }
320                    catch (PortalException pe) {
321                            _log.error(pe, pe);
322                    }
323    
324                    return ContentTypes.APPLICATION_OCTET_STREAM;
325            }
326    
327            public Object getModel() {
328                    return _document;
329            }
330    
331            public Class<?> getModelClass() {
332                    return DLFileEntry.class;
333            }
334    
335            @Override
336            public String getModelClassName() {
337                    return DLFileEntry.class.getName();
338            }
339    
340            public Date getModifiedDate() {
341                    return _document.getLastModificationDate().getTime();
342            }
343    
344            @Override
345            public long getPrimaryKey() {
346                    return _fileEntryId;
347            }
348    
349            public Serializable getPrimaryKeyObj() {
350                    return getPrimaryKey();
351            }
352    
353            public int getReadCount() {
354                    return 0;
355            }
356    
357            public long getRepositoryId() {
358                    return _cmisRepository.getRepositoryId();
359            }
360    
361            public long getSize() {
362                    return _document.getContentStreamLength();
363            }
364    
365            public String getTitle() {
366                    return _document.getName();
367            }
368    
369            public long getUserId() {
370                    User user = getUser(_document.getCreatedBy());
371    
372                    if (user == null) {
373                            return 0;
374                    }
375                    else {
376                            return user.getUserId();
377                    }
378            }
379    
380            public String getUserName() {
381                    User user = getUser(_document.getCreatedBy());
382    
383                    if (user == null) {
384                            return StringPool.BLANK;
385                    }
386                    else {
387                            return user.getFullName();
388                    }
389            }
390    
391            public String getUserUuid() {
392                    User user = getUser(_document.getCreatedBy());
393    
394                    try {
395                            return user.getUserUuid();
396                    }
397                    catch (Exception e) {
398                    }
399    
400                    return StringPool.BLANK;
401            }
402    
403            public String getUuid() {
404                    return _uuid;
405            }
406    
407            public String getVersion() {
408                    return GetterUtil.getString(_document.getVersionLabel(), null);
409            }
410    
411            public long getVersionUserId() {
412                    return 0;
413            }
414    
415            public String getVersionUserName() {
416                    return _document.getLastModifiedBy();
417            }
418    
419            public String getVersionUserUuid() {
420                    return StringPool.BLANK;
421            }
422    
423            public boolean hasLock() {
424                    if (!isCheckedOut()) {
425                            return false;
426                    }
427    
428                    AllowableActions allowableActions = _document.getAllowableActions();
429    
430                    Set<Action> allowableActionsSet =
431                            allowableActions.getAllowableActions();
432    
433                    if (allowableActionsSet.contains(Action.CAN_CHECK_IN)) {
434                            return true;
435                    }
436    
437                    List<CmisExtensionElement> cmisExtensionElements =
438                            allowableActions.getExtensions();
439    
440                    for (CmisExtensionElement cmisExtensionElement :
441                                    cmisExtensionElements) {
442    
443                            String name = cmisExtensionElement.getName();
444    
445                            if (name.equals("canCheckInSpecified")) {
446                                    return GetterUtil.getBoolean(cmisExtensionElement.getValue());
447                            }
448                    }
449    
450                    return false;
451            }
452    
453            public boolean isCheckedOut() {
454                    return _document.isVersionSeriesCheckedOut();
455            }
456    
457            public boolean isDefaultRepository() {
458                    return false;
459            }
460    
461            public boolean isEscapedModel() {
462                    return false;
463            }
464    
465            public boolean isManualCheckInRequired() {
466                    try {
467                            RepositoryEntry repositoryEntry =
468                                    RepositoryEntryLocalServiceUtil.getRepositoryEntry(
469                                            _fileEntryId);
470    
471                            return repositoryEntry.isManualCheckInRequired();
472                    }
473                    catch (Exception e) {
474                            if (_log.isInfoEnabled()) {
475                                    _log.info("Unable to retrieve repository entry", e);
476                            }
477    
478                            return false;
479                    }
480            }
481    
482            public boolean isSupportsLocking() {
483                    return true;
484            }
485    
486            public boolean isSupportsMetadata() {
487                    return false;
488            }
489    
490            public boolean isSupportsSocial() {
491                    return false;
492            }
493    
494            public void setCompanyId(long companyId) {
495                    _cmisRepository.setCompanyId(companyId);
496            }
497    
498            public void setCreateDate(Date date) {
499            }
500    
501            public void setFileEntryId(long fileEntryId) {
502                    _fileEntryId = fileEntryId;
503            }
504    
505            public void setGroupId(long groupId) {
506                    _cmisRepository.setGroupId(groupId);
507            }
508    
509            public void setModifiedDate(Date date) {
510            }
511    
512            public void setPrimaryKey(long primaryKey) {
513                    setFileEntryId(primaryKey);
514            }
515    
516            public void setPrimaryKeyObj(Serializable primaryKeyObj) {
517                    setPrimaryKey(((Long)primaryKeyObj).longValue());
518            }
519    
520            public void setUserId(long userId) {
521            }
522    
523            public void setUserName(String userName) {
524            }
525    
526            public void setUserUuid(String userUuid) {
527            }
528    
529            public FileEntry toEscapedModel() {
530                    return this;
531            }
532    
533            protected List<Document> getAllVersions() throws PortalException {
534                    if (_allVersions == null) {
535                            try {
536                                    _document.refresh();
537    
538                                    _allVersions = _document.getAllVersions();
539                            }
540                            catch (CmisObjectNotFoundException confe) {
541                                    throw new NoSuchFileEntryException(confe);
542                            }
543                    }
544    
545                    return _allVersions;
546            }
547    
548            @Override
549            protected CMISRepository getCmisRepository() {
550                    return _cmisRepository;
551            }
552    
553            private static Log _log = LogFactoryUtil.getLog(CMISFileEntry.class);
554    
555            private List<Document> _allVersions;
556            private CMISRepository _cmisRepository;
557            private Document _document;
558            private long _fileEntryId;
559            private FileVersion _latestFileVersion;
560            private String _uuid;
561    
562    }