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