001
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
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
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
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
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 isInTrash() {
574 return false;
575 }
576
577 @Override
578 public boolean isInTrashContainer() {
579 return false;
580 }
581
582 @Override
583 public boolean isManualCheckInRequired() {
584 try {
585 RepositoryEntry repositoryEntry =
586 RepositoryEntryLocalServiceUtil.getRepositoryEntry(
587 _fileEntryId);
588
589 return repositoryEntry.isManualCheckInRequired();
590 }
591 catch (Exception e) {
592 if (_log.isInfoEnabled()) {
593 _log.info("Unable to retrieve repository entry", e);
594 }
595
596 return false;
597 }
598 }
599
600 @Override
601 public boolean isSupportsLocking() {
602 return true;
603 }
604
605 @Override
606 public boolean isSupportsMetadata() {
607 return false;
608 }
609
610 @Override
611 public boolean isSupportsSocial() {
612 return false;
613 }
614
615 @Override
616 public void setCompanyId(long companyId) {
617 _cmisRepository.setCompanyId(companyId);
618 }
619
620 @Override
621 public void setCreateDate(Date date) {
622 }
623
624 public void setFileEntryId(long fileEntryId) {
625 _fileEntryId = fileEntryId;
626 }
627
628 @Override
629 public void setGroupId(long groupId) {
630 _cmisRepository.setGroupId(groupId);
631 }
632
633 @Override
634 public void setModifiedDate(Date date) {
635 }
636
637 public void setPrimaryKey(long primaryKey) {
638 setFileEntryId(primaryKey);
639 }
640
641 @Override
642 public void setPrimaryKeyObj(Serializable primaryKeyObj) {
643 setPrimaryKey(((Long)primaryKeyObj).longValue());
644 }
645
646 @Override
647 public void setUserId(long userId) {
648 }
649
650 @Override
651 public void setUserName(String userName) {
652 }
653
654 @Override
655 public void setUserUuid(String userUuid) {
656 }
657
658 @Override
659 public void setUuid(String uuid) {
660 }
661
662 @Override
663 public FileEntry toEscapedModel() {
664 return this;
665 }
666
667 @Override
668 public FileEntry toUnescapedModel() {
669 return this;
670 }
671
672 protected List<Document> getAllVersions() throws PortalException {
673 if (_allVersions == null) {
674 try {
675 _document.refresh();
676
677 _allVersions = _document.getAllVersions();
678 }
679 catch (CmisObjectNotFoundException confe) {
680 throw new NoSuchFileEntryException(confe);
681 }
682 }
683
684 return _allVersions;
685 }
686
687 @Override
688 protected CMISRepository getCmisRepository() {
689 return _cmisRepository;
690 }
691
692 private static Log _log = LogFactoryUtil.getLog(CMISFileEntry.class);
693
694 private List<Document> _allVersions;
695 private CMISRepository _cmisRepository;
696 private Document _document;
697 private long _fileEntryId;
698 private FileVersion _latestFileVersion;
699 private String _uuid;
700
701 }