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