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