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