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