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