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