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
123 try {
124 DLAppHelperThreadLocal.setEnabled(false);
125
126 return DLAppLocalServiceUtil.addFileEntry(
127 userId, repository.getRepositoryId(), folderId, fileName,
128 mimeType, fileName, StringPool.BLANK, StringPool.BLANK, file,
129 serviceContext);
130 }
131 finally {
132 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
133 }
134 }
135
136 @Override
137 public FileEntry addPortletFileEntry(
138 long groupId, long userId, String className, long classPK,
139 String portletId, long folderId, InputStream inputStream,
140 String fileName, String mimeType, boolean indexingEnabled)
141 throws PortalException, SystemException {
142
143 if (inputStream == null) {
144 return null;
145 }
146
147 File file = null;
148
149 try {
150 file = FileUtil.createTempFile(inputStream);
151
152 return addPortletFileEntry(
153 groupId, userId, className, classPK, portletId, folderId, file,
154 fileName, mimeType, indexingEnabled);
155 }
156 catch (IOException ioe) {
157 throw new SystemException("Unable to write temporary file", ioe);
158 }
159 finally {
160 FileUtil.delete(file);
161 }
162 }
163
164 @Override
165 public Folder addPortletFolder(
166 long userId, long repositoryId, long parentFolderId,
167 String folderName, ServiceContext serviceContext)
168 throws PortalException, SystemException {
169
170 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
171
172 try {
173 DLAppHelperThreadLocal.setEnabled(false);
174
175 return DLAppLocalServiceUtil.getFolder(
176 repositoryId, parentFolderId, folderName);
177 }
178 catch (NoSuchFolderException nsfe) {
179 return DLAppLocalServiceUtil.addFolder(
180 userId, repositoryId, parentFolderId, folderName,
181 StringPool.BLANK, serviceContext);
182 }
183 finally {
184 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
185 }
186 }
187
188 @Override
189 public Repository addPortletRepository(
190 long groupId, String portletId, ServiceContext serviceContext)
191 throws PortalException, SystemException {
192
193 Repository repository = RepositoryLocalServiceUtil.fetchRepository(
194 groupId, portletId);
195
196 if (repository != null) {
197 return repository;
198 }
199
200 Group group = GroupLocalServiceUtil.getGroup(groupId);
201
202 User user = UserLocalServiceUtil.getDefaultUser(group.getCompanyId());
203
204 long classNameId = PortalUtil.getClassNameId(
205 LiferayRepository.class.getName());
206
207 UnicodeProperties typeSettingsProperties = new UnicodeProperties();
208
209 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
210
211 try {
212 DLAppHelperThreadLocal.setEnabled(false);
213
214 return RepositoryLocalServiceUtil.addRepository(
215 user.getUserId(), groupId, classNameId,
216 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, portletId,
217 StringPool.BLANK, portletId, typeSettingsProperties, true,
218 serviceContext);
219 }
220 finally {
221 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
222 }
223 }
224
225 @Override
226 public void deleteFolder(long folderId)
227 throws PortalException, SystemException {
228
229 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
230
231 try {
232 DLAppHelperThreadLocal.setEnabled(false);
233
234 DLAppLocalServiceUtil.deleteFolder(folderId);
235 }
236 finally {
237 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
238 }
239 }
240
241 @Override
242 public void deletePortletFileEntries(long groupId, long folderId)
243 throws PortalException, SystemException {
244
245 List<DLFileEntry> dlFileEntries =
246 DLFileEntryLocalServiceUtil.getFileEntries(groupId, folderId);
247
248 for (DLFileEntry dlFileEntry : dlFileEntries) {
249 deletePortletFileEntry(dlFileEntry.getFileEntryId());
250 }
251 }
252
253 @Override
254 public void deletePortletFileEntries(
255 long groupId, long folderId, int status)
256 throws PortalException, SystemException {
257
258 List<DLFileEntry> dlFileEntries =
259 DLFileEntryLocalServiceUtil.getFileEntries(
260 groupId, folderId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
261 null);
262
263 for (DLFileEntry dlFileEntry : dlFileEntries) {
264 deletePortletFileEntry(dlFileEntry.getFileEntryId());
265 }
266 }
267
268 @Override
269 public void deletePortletFileEntry(long fileEntryId)
270 throws PortalException, SystemException {
271
272 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
273
274 try {
275 DLAppHelperThreadLocal.setEnabled(false);
276
277 DLAppLocalServiceUtil.deleteFileEntry(fileEntryId);
278 }
279 finally {
280 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
281 }
282 }
283
284 @Override
285 public void deletePortletFileEntry(
286 long groupId, long folderId, String fileName)
287 throws PortalException, SystemException {
288
289 FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
290 groupId, folderId, fileName);
291
292 deletePortletFileEntry(fileEntry.getFileEntryId());
293 }
294
295 @Override
296 public void deletePortletRepository(long groupId, String portletId)
297 throws PortalException, SystemException {
298
299 Repository repository = RepositoryLocalServiceUtil.fetchRepository(
300 groupId, portletId);
301
302 if (repository != null) {
303 RepositoryLocalServiceUtil.deleteRepository(
304 repository.getRepositoryId());
305 }
306 }
307
308 @Override
309 public Repository fetchPortletRepository(long groupId, String portletId)
310 throws SystemException {
311
312 return RepositoryLocalServiceUtil.fetchRepository(groupId, portletId);
313 }
314
315 @Override
316 public List<FileEntry> getPortletFileEntries(long groupId, long folderId)
317 throws SystemException {
318
319 return toFileEntries(
320 DLFileEntryLocalServiceUtil.getFileEntries(groupId, folderId));
321 }
322
323 @Override
324 public List<FileEntry> getPortletFileEntries(
325 long groupId, long folderId, int status)
326 throws SystemException {
327
328 return getPortletFileEntries(
329 groupId, folderId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
330 null);
331 }
332
333 @Override
334 public List<FileEntry> getPortletFileEntries(
335 long groupId, long folderId, int status, int start, int end,
336 OrderByComparator obc)
337 throws SystemException {
338
339 return toFileEntries(
340 DLFileEntryLocalServiceUtil.getFileEntries(
341 groupId, folderId, status, start, end, obc));
342 }
343
344 @Override
345 public int getPortletFileEntriesCount(long groupId, long folderId)
346 throws SystemException {
347
348 return DLFileEntryLocalServiceUtil.getFileEntriesCount(
349 groupId, folderId);
350 }
351
352 @Override
353 public int getPortletFileEntriesCount(
354 long groupId, long folderId, int status)
355 throws SystemException {
356
357 return DLFileEntryLocalServiceUtil.getFileEntriesCount(
358 groupId, folderId, status);
359 }
360
361 @Override
362 public FileEntry getPortletFileEntry(long fileEntryId)
363 throws PortalException, SystemException {
364
365 return DLAppLocalServiceUtil.getFileEntry(fileEntryId);
366 }
367
368 @Override
369 public FileEntry getPortletFileEntry(
370 long groupId, long folderId, String fileName)
371 throws PortalException, SystemException {
372
373 return DLAppLocalServiceUtil.getFileEntry(groupId, folderId, fileName);
374 }
375
376 @Override
377 public FileEntry getPortletFileEntry(String uuid, long groupId)
378 throws PortalException, SystemException {
379
380 return DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(
381 uuid, groupId);
382 }
383
384 @Override
385 public String getPortletFileEntryURL(
386 ThemeDisplay themeDisplay, FileEntry fileEntry, String queryString) {
387
388 return getPortletFileEntryURL(
389 themeDisplay, fileEntry, queryString, true);
390 }
391
392 @Override
393 public String getPortletFileEntryURL(
394 ThemeDisplay themeDisplay, FileEntry fileEntry, String queryString,
395 boolean absoluteURL) {
396
397 StringBundler sb = new StringBundler(10);
398
399 if (themeDisplay != null) {
400 if (absoluteURL) {
401 sb.append(themeDisplay.getPortalURL());
402 }
403 }
404
405 sb.append(PortalUtil.getPathContext());
406 sb.append("/documents/");
407 sb.append(WebServerServlet.PATH_PORTLET_FILE_ENTRY);
408 sb.append(StringPool.SLASH);
409 sb.append(fileEntry.getGroupId());
410 sb.append(StringPool.SLASH);
411
412 String title = fileEntry.getTitle();
413
414 if (fileEntry.isInTrash()) {
415 title = TrashUtil.getOriginalTitle(fileEntry.getTitle());
416 }
417
418 sb.append(HttpUtil.encodeURL(HtmlUtil.unescape(title)));
419
420 sb.append(StringPool.SLASH);
421 sb.append(fileEntry.getUuid());
422
423 if (themeDisplay != null) {
424 PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
425
426 if (portletDisplay != null) {
427 String portletId = portletDisplay.getId();
428
429 if (portletId.equals(PortletKeys.TRASH) &&
430 !queryString.contains("status=")) {
431
432 if (Validator.isNotNull(queryString)) {
433 queryString += StringPool.AMPERSAND;
434 }
435
436 queryString +=
437 "status=" + WorkflowConstants.STATUS_IN_TRASH;
438 }
439 }
440 }
441
442 if (Validator.isNotNull(queryString)) {
443 sb.append(StringPool.QUESTION);
444 sb.append(queryString);
445 }
446
447 String portletFileEntryURL = sb.toString();
448
449 if ((themeDisplay != null) && themeDisplay.isAddSessionIdToURL()) {
450 return PortalUtil.getURLWithSessionId(
451 portletFileEntryURL, themeDisplay.getSessionId());
452 }
453
454 return portletFileEntryURL;
455 }
456
457 @Override
458 public Folder getPortletFolder(long folderId)
459 throws PortalException, SystemException {
460
461 return DLAppLocalServiceUtil.getFolder(folderId);
462 }
463
464 @Override
465 public Folder getPortletFolder(
466 long repositoryId, long parentFolderId, String folderName)
467 throws PortalException, SystemException {
468
469 return DLAppLocalServiceUtil.getFolder(
470 repositoryId, parentFolderId, folderName);
471 }
472
473 @Override
474 public Repository getPortletRepository(long groupId, String portletId)
475 throws PortalException, SystemException {
476
477 return RepositoryLocalServiceUtil.getRepository(groupId, portletId);
478 }
479
480 @Override
481 public FileEntry movePortletFileEntryToTrash(long userId, long fileEntryId)
482 throws PortalException, SystemException {
483
484 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
485
486 try {
487 DLAppHelperThreadLocal.setEnabled(false);
488
489 return DLAppLocalServiceUtil.moveFileEntryToTrash(
490 userId, fileEntryId);
491 }
492 finally {
493 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
494 }
495 }
496
497 @Override
498 public FileEntry movePortletFileEntryToTrash(
499 long groupId, long userId, long folderId, String fileName)
500 throws PortalException, SystemException {
501
502 FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
503 groupId, folderId, fileName);
504
505 return movePortletFileEntryToTrash(userId, fileEntry.getFileEntryId());
506 }
507
508 @Override
509 public void restorePortletFileEntryFromTrash(long userId, long fileEntryId)
510 throws PortalException, SystemException {
511
512 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
513
514 try {
515 DLAppHelperThreadLocal.setEnabled(false);
516
517 DLAppLocalServiceUtil.restoreFileEntryFromTrash(
518 userId, fileEntryId);
519 }
520 finally {
521 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
522 }
523 }
524
525 @Override
526 public void restorePortletFileEntryFromTrash(
527 long groupId, long userId, long folderId, String fileName)
528 throws PortalException, SystemException {
529
530 FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
531 groupId, folderId, fileName);
532
533 restorePortletFileEntryFromTrash(userId, fileEntry.getFileEntryId());
534 }
535
536
539 protected List<FileEntry> toFileEntries(List<DLFileEntry> dlFileEntries) {
540 List<FileEntry> fileEntries = new ArrayList<FileEntry>(
541 dlFileEntries.size());
542
543 for (DLFileEntry dlFileEntry : dlFileEntries) {
544 FileEntry fileEntry = new LiferayFileEntry(dlFileEntry);
545
546 fileEntries.add(fileEntry);
547 }
548
549 if (dlFileEntries instanceof UnmodifiableList) {
550 return new UnmodifiableList<FileEntry>(fileEntries);
551 }
552 else {
553 return fileEntries;
554 }
555 }
556
557 }