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