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