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