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