001
014
015 package com.liferay.portal.portletfilerepository;
016
017 import com.liferay.portal.kernel.dao.orm.QueryUtil;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.repository.InvalidRepositoryIdException;
023 import com.liferay.portal.kernel.repository.LocalRepository;
024 import com.liferay.portal.kernel.repository.model.FileEntry;
025 import com.liferay.portal.kernel.repository.model.Folder;
026 import com.liferay.portal.kernel.repository.util.RepositoryTrashUtil;
027 import com.liferay.portal.kernel.security.pacl.DoPrivileged;
028 import com.liferay.portal.kernel.systemevent.SystemEventHierarchyEntryThreadLocal;
029 import com.liferay.portal.kernel.util.ContentTypes;
030 import com.liferay.portal.kernel.util.FileUtil;
031 import com.liferay.portal.kernel.util.HtmlUtil;
032 import com.liferay.portal.kernel.util.HttpUtil;
033 import com.liferay.portal.kernel.util.ListUtil;
034 import com.liferay.portal.kernel.util.MimeTypesUtil;
035 import com.liferay.portal.kernel.util.ObjectValuePair;
036 import com.liferay.portal.kernel.util.OrderByComparator;
037 import com.liferay.portal.kernel.util.StringBundler;
038 import com.liferay.portal.kernel.util.StringPool;
039 import com.liferay.portal.kernel.util.UnicodeProperties;
040 import com.liferay.portal.kernel.util.Validator;
041 import com.liferay.portal.kernel.workflow.WorkflowConstants;
042 import com.liferay.portal.model.Group;
043 import com.liferay.portal.model.Repository;
044 import com.liferay.portal.model.User;
045 import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
046 import com.liferay.portal.repository.portletrepository.PortletRepository;
047 import com.liferay.portal.service.GroupLocalServiceUtil;
048 import com.liferay.portal.service.RepositoryLocalServiceUtil;
049 import com.liferay.portal.service.ServiceContext;
050 import com.liferay.portal.service.UserLocalServiceUtil;
051 import com.liferay.portal.theme.PortletDisplay;
052 import com.liferay.portal.theme.ThemeDisplay;
053 import com.liferay.portal.util.PortalUtil;
054 import com.liferay.portal.util.PortletKeys;
055 import com.liferay.portal.webserver.WebServerServlet;
056 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
057 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
058 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
059 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
060 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
061 import com.liferay.portlet.documentlibrary.util.DLAppHelperThreadLocal;
062 import com.liferay.portlet.documentlibrary.util.comparator.DLFileEntryOrderByComparator;
063 import com.liferay.portlet.trash.util.TrashUtil;
064
065 import java.io.File;
066 import java.io.IOException;
067 import java.io.InputStream;
068
069 import java.util.ArrayList;
070 import java.util.Collections;
071 import java.util.List;
072
073
077 @DoPrivileged
078 public class PortletFileRepositoryImpl implements PortletFileRepository {
079
080 @Override
081 public void addPortletFileEntries(
082 long groupId, long userId, String className, long classPK,
083 String portletId, long folderId,
084 List<ObjectValuePair<String, InputStream>> inputStreamOVPs)
085 throws PortalException {
086
087 for (int i = 0; i < inputStreamOVPs.size(); i++) {
088 ObjectValuePair<String, InputStream> inputStreamOVP =
089 inputStreamOVPs.get(i);
090
091 InputStream inputStream = inputStreamOVP.getValue();
092 String fileName = inputStreamOVP.getKey();
093
094 addPortletFileEntry(
095 groupId, userId, className, classPK, portletId, folderId,
096 inputStream, fileName, StringPool.BLANK, true);
097 }
098 }
099
100 @Override
101 public FileEntry addPortletFileEntry(
102 long groupId, long userId, String className, long classPK,
103 String portletId, long folderId, File file, String fileName,
104 String mimeType, boolean indexingEnabled)
105 throws PortalException {
106
107 if (Validator.isNull(fileName)) {
108 return null;
109 }
110
111 ServiceContext serviceContext = new ServiceContext();
112
113 serviceContext.setAddGroupPermissions(true);
114 serviceContext.setAddGuestPermissions(true);
115
116 Repository repository = addPortletRepository(
117 groupId, portletId, serviceContext);
118
119 serviceContext.setAttribute("className", className);
120 serviceContext.setAttribute("classPK", String.valueOf(classPK));
121 serviceContext.setIndexingEnabled(indexingEnabled);
122
123 if (Validator.isNull(mimeType) ||
124 mimeType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
125
126 mimeType = MimeTypesUtil.getContentType(file, fileName);
127 }
128
129 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
130
131 try {
132 DLAppHelperThreadLocal.setEnabled(false);
133
134 LocalRepository localRepository =
135 RepositoryLocalServiceUtil.getLocalRepositoryImpl(
136 repository.getRepositoryId());
137
138 return localRepository.addFileEntry(
139 userId, folderId, fileName, mimeType, fileName,
140 StringPool.BLANK, StringPool.BLANK, file, serviceContext);
141 }
142 finally {
143 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
144 }
145 }
146
147 @Override
148 public FileEntry addPortletFileEntry(
149 long groupId, long userId, String className, long classPK,
150 String portletId, long folderId, InputStream inputStream,
151 String fileName, String mimeType, boolean indexingEnabled)
152 throws PortalException {
153
154 if (inputStream == null) {
155 return null;
156 }
157
158 File file = null;
159
160 try {
161 file = FileUtil.createTempFile(inputStream);
162
163 return addPortletFileEntry(
164 groupId, userId, className, classPK, portletId, folderId, file,
165 fileName, mimeType, indexingEnabled);
166 }
167 catch (IOException ioe) {
168 throw new SystemException("Unable to write temporary file", ioe);
169 }
170 finally {
171 FileUtil.delete(file);
172 }
173 }
174
175 @Override
176 public Folder addPortletFolder(
177 long userId, long repositoryId, long parentFolderId,
178 String folderName, ServiceContext serviceContext)
179 throws PortalException {
180
181 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
182
183 LocalRepository localRepository =
184 RepositoryLocalServiceUtil.getLocalRepositoryImpl(repositoryId);
185
186 try {
187 DLAppHelperThreadLocal.setEnabled(false);
188
189 return localRepository.getFolder(parentFolderId, folderName);
190 }
191 catch (NoSuchFolderException nsfe) {
192 return localRepository.addFolder(
193 userId, parentFolderId, folderName, StringPool.BLANK,
194 serviceContext);
195 }
196 finally {
197 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
198 }
199 }
200
201 @Override
202 public Folder addPortletFolder(
203 long groupId, long userId, String portletId, long parentFolderId,
204 String folderName, ServiceContext serviceContext)
205 throws PortalException {
206
207 Repository repository = addPortletRepository(
208 groupId, portletId, serviceContext);
209
210 return addPortletFolder(
211 userId, repository.getRepositoryId(), parentFolderId, folderName,
212 serviceContext);
213 }
214
215 @Override
216 public Repository addPortletRepository(
217 long groupId, String portletId, ServiceContext serviceContext)
218 throws PortalException {
219
220 Repository repository = RepositoryLocalServiceUtil.fetchRepository(
221 groupId, portletId);
222
223 if (repository != null) {
224 return repository;
225 }
226
227 Group group = GroupLocalServiceUtil.getGroup(groupId);
228
229 User user = UserLocalServiceUtil.getDefaultUser(group.getCompanyId());
230
231 long classNameId = PortalUtil.getClassNameId(
232 PortletRepository.class.getName());
233
234 UnicodeProperties typeSettingsProperties = new UnicodeProperties();
235
236 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
237
238 try {
239 DLAppHelperThreadLocal.setEnabled(false);
240
241 return RepositoryLocalServiceUtil.addRepository(
242 user.getUserId(), groupId, classNameId,
243 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, portletId,
244 StringPool.BLANK, portletId, typeSettingsProperties, true,
245 serviceContext);
246 }
247 finally {
248 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
249 }
250 }
251
252
255 @Deprecated
256 @Override
257 public void deleteFolder(long folderId) throws PortalException {
258 deletePortletFolder(folderId);
259 }
260
261 @Override
262 public void deletePortletFileEntries(long groupId, long folderId)
263 throws PortalException {
264
265 List<DLFileEntry> dlFileEntries =
266 DLFileEntryLocalServiceUtil.getFileEntries(groupId, folderId);
267
268 for (DLFileEntry dlFileEntry : dlFileEntries) {
269 deletePortletFileEntry(dlFileEntry.getFileEntryId());
270 }
271 }
272
273 @Override
274 public void deletePortletFileEntries(
275 long groupId, long folderId, int status)
276 throws PortalException {
277
278 List<DLFileEntry> dlFileEntries =
279 DLFileEntryLocalServiceUtil.getFileEntries(
280 groupId, folderId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
281 null);
282
283 for (DLFileEntry dlFileEntry : dlFileEntries) {
284 deletePortletFileEntry(dlFileEntry.getFileEntryId());
285 }
286 }
287
288 @Override
289 public void deletePortletFileEntry(long fileEntryId)
290 throws PortalException {
291
292 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
293
294 try {
295 DLAppHelperThreadLocal.setEnabled(false);
296
297 SystemEventHierarchyEntryThreadLocal.push(FileEntry.class);
298
299 LocalRepository localRepository =
300 RepositoryLocalServiceUtil.getLocalRepositoryImpl(
301 0, fileEntryId, 0);
302
303 localRepository.deleteFileEntry(fileEntryId);
304 }
305 catch (InvalidRepositoryIdException irie) {
306 if (_log.isWarnEnabled()) {
307 _log.warn(irie, irie);
308 }
309 }
310 finally {
311 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
312
313 SystemEventHierarchyEntryThreadLocal.pop(FileEntry.class);
314 }
315 }
316
317 @Override
318 public void deletePortletFileEntry(
319 long groupId, long folderId, String fileName)
320 throws PortalException {
321
322 LocalRepository localRepository =
323 RepositoryLocalServiceUtil.getLocalRepositoryImpl(groupId);
324
325 FileEntry fileEntry = localRepository.getFileEntry(folderId, fileName);
326
327 deletePortletFileEntry(fileEntry.getFileEntryId());
328 }
329
330 @Override
331 public void deletePortletFolder(long folderId) throws PortalException {
332 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
333
334 try {
335 DLAppHelperThreadLocal.setEnabled(false);
336
337 SystemEventHierarchyEntryThreadLocal.push(Folder.class);
338
339 LocalRepository localRepository =
340 RepositoryLocalServiceUtil.getLocalRepositoryImpl(
341 folderId, 0, 0);
342
343 localRepository.deleteFolder(folderId);
344 }
345 catch (InvalidRepositoryIdException irie) {
346 if (_log.isWarnEnabled()) {
347 _log.warn(irie, irie);
348 }
349 }
350 finally {
351 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
352
353 SystemEventHierarchyEntryThreadLocal.pop(Folder.class);
354 }
355 }
356
357 @Override
358 public void deletePortletRepository(long groupId, String portletId)
359 throws PortalException {
360
361 Repository repository = RepositoryLocalServiceUtil.fetchRepository(
362 groupId, portletId);
363
364 if (repository != null) {
365 RepositoryLocalServiceUtil.deleteRepository(
366 repository.getRepositoryId());
367 }
368 }
369
370 @Override
371 public Repository fetchPortletRepository(long groupId, String portletId) {
372 return RepositoryLocalServiceUtil.fetchRepository(groupId, portletId);
373 }
374
375 @Override
376 public String getDownloadPortletFileEntryURL(
377 ThemeDisplay themeDisplay, FileEntry fileEntry, String queryString) {
378
379 return getDownloadPortletFileEntryURL(
380 themeDisplay, fileEntry, queryString, true);
381 }
382
383 @Override
384 public String getDownloadPortletFileEntryURL(
385 ThemeDisplay themeDisplay, FileEntry fileEntry, String queryString,
386 boolean absoluteURL) {
387
388 String portletFileEntryURL = getPortletFileEntryURL(
389 themeDisplay, fileEntry, queryString, absoluteURL);
390
391 return HttpUtil.addParameter(portletFileEntryURL, "download", true);
392 }
393
394 @Override
395 public List<FileEntry> getPortletFileEntries(long groupId, long folderId) {
396 return toFileEntries(
397 DLFileEntryLocalServiceUtil.getFileEntries(groupId, folderId));
398 }
399
400 @Override
401 public List<FileEntry> getPortletFileEntries(
402 long groupId, long folderId, int status) {
403
404 return getPortletFileEntries(
405 groupId, folderId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
406 null);
407 }
408
409 @Override
410 public List<FileEntry> getPortletFileEntries(
411 long groupId, long folderId, int status, int start, int end,
412 OrderByComparator<FileEntry> obc) {
413
414 return toFileEntries(
415 DLFileEntryLocalServiceUtil.getFileEntries(
416 groupId, folderId, status, start, end,
417 DLFileEntryOrderByComparator.getOrderByComparator(obc)));
418 }
419
420 @Override
421 public List<FileEntry> getPortletFileEntries(
422 long groupId, long folderId, OrderByComparator<FileEntry> obc) {
423
424 return toFileEntries(
425 DLFileEntryLocalServiceUtil.getFileEntries(
426 groupId, folderId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
427 DLFileEntryOrderByComparator.getOrderByComparator(obc)));
428 }
429
430 @Override
431 public int getPortletFileEntriesCount(long groupId, long folderId) {
432 return DLFileEntryLocalServiceUtil.getFileEntriesCount(
433 groupId, folderId);
434 }
435
436 @Override
437 public int getPortletFileEntriesCount(
438 long groupId, long folderId, int status) {
439
440 return DLFileEntryLocalServiceUtil.getFileEntriesCount(
441 groupId, folderId, status);
442 }
443
444 @Override
445 public FileEntry getPortletFileEntry(long fileEntryId)
446 throws PortalException {
447
448 try {
449 LocalRepository localRepository =
450 RepositoryLocalServiceUtil.getLocalRepositoryImpl(
451 0, fileEntryId, 0);
452
453 return localRepository.getFileEntry(fileEntryId);
454 }
455 catch (InvalidRepositoryIdException irid) {
456 throw new NoSuchFileEntryException(irid);
457 }
458 }
459
460 @Override
461 public FileEntry getPortletFileEntry(
462 long groupId, long folderId, String fileName)
463 throws PortalException {
464
465 LocalRepository localRepository =
466 RepositoryLocalServiceUtil.getLocalRepositoryImpl(groupId);
467
468 return localRepository.getFileEntry(folderId, fileName);
469 }
470
471 @Override
472 public FileEntry getPortletFileEntry(String uuid, long groupId)
473 throws PortalException {
474
475 LocalRepository localRepository =
476 RepositoryLocalServiceUtil.getLocalRepositoryImpl(groupId);
477
478 return localRepository.getFileEntryByUuid(uuid);
479 }
480
481 @Override
482 public String getPortletFileEntryURL(
483 ThemeDisplay themeDisplay, FileEntry fileEntry, String queryString) {
484
485 return getPortletFileEntryURL(
486 themeDisplay, fileEntry, queryString, true);
487 }
488
489 @Override
490 public String getPortletFileEntryURL(
491 ThemeDisplay themeDisplay, FileEntry fileEntry, String queryString,
492 boolean absoluteURL) {
493
494 StringBundler sb = new StringBundler(10);
495
496 if (themeDisplay != null) {
497 if (absoluteURL) {
498 sb.append(themeDisplay.getPortalURL());
499 }
500 }
501
502 sb.append(PortalUtil.getPathContext());
503 sb.append("/documents/");
504 sb.append(WebServerServlet.PATH_PORTLET_FILE_ENTRY);
505 sb.append(StringPool.SLASH);
506 sb.append(fileEntry.getGroupId());
507 sb.append(StringPool.SLASH);
508
509 String title = fileEntry.getTitle();
510
511 if (fileEntry.isInTrash()) {
512 title = TrashUtil.getOriginalTitle(fileEntry.getTitle());
513 }
514
515 sb.append(HttpUtil.encodeURL(HtmlUtil.unescape(title)));
516
517 sb.append(StringPool.SLASH);
518 sb.append(HttpUtil.encodeURL(fileEntry.getUuid()));
519
520 if (themeDisplay != null) {
521 PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
522
523 if (portletDisplay != null) {
524 String portletId = portletDisplay.getId();
525
526 if (portletId.equals(PortletKeys.TRASH) &&
527 !queryString.contains("status=")) {
528
529 if (Validator.isNotNull(queryString)) {
530 queryString += StringPool.AMPERSAND;
531 }
532
533 queryString +=
534 "status=" + WorkflowConstants.STATUS_IN_TRASH;
535 }
536 }
537 }
538
539 if (Validator.isNotNull(queryString)) {
540 sb.append(StringPool.QUESTION);
541 sb.append(queryString);
542 }
543
544 String portletFileEntryURL = sb.toString();
545
546 if ((themeDisplay != null) && themeDisplay.isAddSessionIdToURL()) {
547 return PortalUtil.getURLWithSessionId(
548 portletFileEntryURL, themeDisplay.getSessionId());
549 }
550
551 return portletFileEntryURL;
552 }
553
554 @Override
555 public Folder getPortletFolder(long folderId) throws PortalException {
556 try {
557 LocalRepository localRepository =
558 RepositoryLocalServiceUtil.getLocalRepositoryImpl(
559 folderId, 0, 0);
560
561 return localRepository.getFolder(folderId);
562 }
563 catch (InvalidRepositoryIdException irid) {
564 throw new NoSuchFolderException(irid);
565 }
566 }
567
568 @Override
569 public Folder getPortletFolder(
570 long repositoryId, long parentFolderId, String folderName)
571 throws PortalException {
572
573 LocalRepository localRepository =
574 RepositoryLocalServiceUtil.getLocalRepositoryImpl(repositoryId);
575
576 return localRepository.getFolder(parentFolderId, folderName);
577 }
578
579 @Override
580 public Repository getPortletRepository(long groupId, String portletId)
581 throws PortalException {
582
583 return RepositoryLocalServiceUtil.getRepository(groupId, portletId);
584 }
585
586 @Override
587 public FileEntry movePortletFileEntryToTrash(long userId, long fileEntryId)
588 throws PortalException {
589
590 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
591
592 try {
593 DLAppHelperThreadLocal.setEnabled(false);
594
595 LocalRepository localRepository =
596 RepositoryLocalServiceUtil.getLocalRepositoryImpl(
597 0, fileEntryId, 0);
598
599 return RepositoryTrashUtil.moveFileEntryToTrash(
600 userId, localRepository.getRepositoryId(), fileEntryId);
601 }
602 finally {
603 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
604 }
605 }
606
607 @Override
608 public FileEntry movePortletFileEntryToTrash(
609 long groupId, long userId, long folderId, String fileName)
610 throws PortalException {
611
612 LocalRepository localRepository =
613 RepositoryLocalServiceUtil.getLocalRepositoryImpl(groupId);
614
615 FileEntry fileEntry = localRepository.getFileEntry(folderId, fileName);
616
617 return movePortletFileEntryToTrash(userId, fileEntry.getFileEntryId());
618 }
619
620 @Override
621 public void restorePortletFileEntryFromTrash(long userId, long fileEntryId)
622 throws PortalException {
623
624 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
625
626 try {
627 DLAppHelperThreadLocal.setEnabled(false);
628
629 LocalRepository localRepository =
630 RepositoryLocalServiceUtil.getLocalRepositoryImpl(
631 0, fileEntryId, 0);
632
633 RepositoryTrashUtil.restoreFileEntryFromTrash(
634 userId, localRepository.getRepositoryId(), fileEntryId);
635 }
636 finally {
637 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
638 }
639 }
640
641 @Override
642 public void restorePortletFileEntryFromTrash(
643 long groupId, long userId, long folderId, String fileName)
644 throws PortalException {
645
646 LocalRepository localRepository =
647 RepositoryLocalServiceUtil.getLocalRepositoryImpl(groupId);
648
649 FileEntry fileEntry = localRepository.getFileEntry(folderId, fileName);
650
651 restorePortletFileEntryFromTrash(userId, fileEntry.getFileEntryId());
652 }
653
654
657 protected List<FileEntry> toFileEntries(List<DLFileEntry> dlFileEntries) {
658 List<FileEntry> fileEntries = new ArrayList<FileEntry>(
659 dlFileEntries.size());
660
661 for (DLFileEntry dlFileEntry : dlFileEntries) {
662 FileEntry fileEntry = new LiferayFileEntry(dlFileEntry);
663
664 fileEntries.add(fileEntry);
665 }
666
667 if (ListUtil.isUnmodifiableList(dlFileEntries)) {
668 return Collections.unmodifiableList(fileEntries);
669 }
670 else {
671 return fileEntries;
672 }
673 }
674
675 private static Log _log = LogFactoryUtil.getLog(
676 PortletFileRepositoryImpl.class);
677
678 }