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