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