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.MimeTypesUtil;
026 import com.liferay.portal.kernel.util.ObjectValuePair;
027 import com.liferay.portal.kernel.util.OrderByComparator;
028 import com.liferay.portal.kernel.util.StringPool;
029 import com.liferay.portal.kernel.util.UnicodeProperties;
030 import com.liferay.portal.kernel.util.UnmodifiableList;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.model.Group;
033 import com.liferay.portal.model.Repository;
034 import com.liferay.portal.model.User;
035 import com.liferay.portal.repository.liferayrepository.LiferayRepository;
036 import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
037 import com.liferay.portal.service.GroupLocalServiceUtil;
038 import com.liferay.portal.service.RepositoryLocalServiceUtil;
039 import com.liferay.portal.service.ServiceContext;
040 import com.liferay.portal.service.UserLocalServiceUtil;
041 import com.liferay.portal.util.PortalUtil;
042 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
043 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
044 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
045 import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
046 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
047 import com.liferay.portlet.documentlibrary.util.DLAppHelperThreadLocal;
048
049 import java.io.File;
050 import java.io.IOException;
051 import java.io.InputStream;
052
053 import java.util.ArrayList;
054 import java.util.List;
055
056
060 @DoPrivileged
061 public class PortletFileRepositoryImpl implements PortletFileRepository {
062
063 @Override
064 public void addPortletFileEntries(
065 long groupId, long userId, String className, long classPK,
066 String portletId, long folderId,
067 List<ObjectValuePair<String, InputStream>> inputStreamOVPs)
068 throws PortalException, SystemException {
069
070 for (int i = 0; i < inputStreamOVPs.size(); i++) {
071 ObjectValuePair<String, InputStream> inputStreamOVP =
072 inputStreamOVPs.get(i);
073
074 InputStream inputStream = inputStreamOVP.getValue();
075 String fileName = inputStreamOVP.getKey();
076
077 addPortletFileEntry(
078 groupId, userId, className, classPK, portletId, folderId,
079 inputStream, fileName, StringPool.BLANK, true);
080 }
081 }
082
083 @Override
084 public FileEntry addPortletFileEntry(
085 long groupId, long userId, String className, long classPK,
086 String portletId, long folderId, File file, String fileName,
087 String mimeType, boolean indexingEnabled)
088 throws PortalException, SystemException {
089
090 if (Validator.isNull(fileName)) {
091 return null;
092 }
093
094 ServiceContext serviceContext = new ServiceContext();
095
096 serviceContext.setAddGroupPermissions(true);
097 serviceContext.setAddGuestPermissions(true);
098
099 Repository repository = addPortletRepository(
100 groupId, portletId, serviceContext);
101
102 serviceContext.setAttribute("className", className);
103 serviceContext.setAttribute("classPK", String.valueOf(classPK));
104 serviceContext.setIndexingEnabled(indexingEnabled);
105
106 if (Validator.isNull(mimeType) ||
107 mimeType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
108
109 mimeType = MimeTypesUtil.getContentType(file, fileName);
110 }
111
112 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
113
114 try {
115 DLAppHelperThreadLocal.setEnabled(false);
116
117 return DLAppLocalServiceUtil.addFileEntry(
118 userId, repository.getRepositoryId(), folderId, fileName,
119 mimeType, fileName, StringPool.BLANK, StringPool.BLANK, file,
120 serviceContext);
121 }
122 finally {
123 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
124 }
125 }
126
127 @Override
128 public FileEntry addPortletFileEntry(
129 long groupId, long userId, String className, long classPK,
130 String portletId, long folderId, InputStream inputStream,
131 String fileName, String mimeType, boolean indexingEnabled)
132 throws PortalException, SystemException {
133
134 if (inputStream == null) {
135 return null;
136 }
137
138 File file = null;
139
140 try {
141 file = FileUtil.createTempFile(inputStream);
142
143 return addPortletFileEntry(
144 groupId, userId, className, classPK, portletId, folderId, file,
145 fileName, mimeType, indexingEnabled);
146 }
147 catch (IOException ioe) {
148 throw new SystemException("Unable to write temporary file", ioe);
149 }
150 finally {
151 FileUtil.delete(file);
152 }
153 }
154
155 @Override
156 public Folder addPortletFolder(
157 long userId, long repositoryId, long parentFolderId,
158 String folderName, ServiceContext serviceContext)
159 throws PortalException, SystemException {
160
161 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
162
163 try {
164 DLAppHelperThreadLocal.setEnabled(false);
165
166 return DLAppLocalServiceUtil.getFolder(
167 repositoryId, parentFolderId, folderName);
168 }
169 catch (NoSuchFolderException nsfe) {
170 return DLAppLocalServiceUtil.addFolder(
171 userId, repositoryId, parentFolderId, folderName,
172 StringPool.BLANK, serviceContext);
173 }
174 finally {
175 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
176 }
177 }
178
179 @Override
180 public Repository addPortletRepository(
181 long groupId, String portletId, ServiceContext serviceContext)
182 throws PortalException, SystemException {
183
184 Repository repository = RepositoryLocalServiceUtil.fetchRepository(
185 groupId, portletId);
186
187 if (repository != null) {
188 return repository;
189 }
190
191 Group group = GroupLocalServiceUtil.getGroup(groupId);
192
193 User user = UserLocalServiceUtil.getDefaultUser(group.getCompanyId());
194
195 long classNameId = PortalUtil.getClassNameId(
196 LiferayRepository.class.getName());
197
198 UnicodeProperties typeSettingsProperties = new UnicodeProperties();
199
200 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
201
202 try {
203 DLAppHelperThreadLocal.setEnabled(false);
204
205 return RepositoryLocalServiceUtil.addRepository(
206 user.getUserId(), groupId, classNameId,
207 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, portletId,
208 StringPool.BLANK, portletId, typeSettingsProperties, true,
209 serviceContext);
210 }
211 finally {
212 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
213 }
214 }
215
216 @Override
217 public void deleteFolder(long folderId)
218 throws PortalException, SystemException {
219
220 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
221
222 try {
223 DLAppHelperThreadLocal.setEnabled(false);
224
225 DLAppLocalServiceUtil.deleteFolder(folderId);
226 }
227 finally {
228 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
229 }
230 }
231
232 @Override
233 public void deletePortletFileEntries(long groupId, long folderId)
234 throws PortalException, SystemException {
235
236 List<DLFileEntry> dlFileEntries =
237 DLFileEntryLocalServiceUtil.getFileEntries(groupId, folderId);
238
239 for (DLFileEntry dlFileEntry : dlFileEntries) {
240 deletePortletFileEntry(dlFileEntry.getFileEntryId());
241 }
242 }
243
244 @Override
245 public void deletePortletFileEntries(
246 long groupId, long folderId, int status)
247 throws PortalException, SystemException {
248
249 List<DLFileEntry> dlFileEntries =
250 DLFileEntryLocalServiceUtil.getFileEntries(
251 groupId, folderId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
252 null);
253
254 for (DLFileEntry dlFileEntry : dlFileEntries) {
255 deletePortletFileEntry(dlFileEntry.getFileEntryId());
256 }
257 }
258
259 @Override
260 public void deletePortletFileEntry(long fileEntryId)
261 throws PortalException, SystemException {
262
263 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
264
265 try {
266 DLAppHelperThreadLocal.setEnabled(false);
267
268 DLAppLocalServiceUtil.deleteFileEntry(fileEntryId);
269 }
270 finally {
271 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
272 }
273 }
274
275 @Override
276 public void deletePortletFileEntry(
277 long groupId, long folderId, String fileName)
278 throws PortalException, SystemException {
279
280 FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
281 groupId, folderId, fileName);
282
283 deletePortletFileEntry(fileEntry.getFileEntryId());
284 }
285
286 @Override
287 public void deletePortletRepository(long groupId, String portletId)
288 throws PortalException, SystemException {
289
290 Repository repository = RepositoryLocalServiceUtil.fetchRepository(
291 groupId, portletId);
292
293 if (repository != null) {
294 RepositoryLocalServiceUtil.deleteRepository(
295 repository.getRepositoryId());
296 }
297 }
298
299 @Override
300 public Repository fetchPortletRepository(long groupId, String portletId)
301 throws SystemException {
302
303 return RepositoryLocalServiceUtil.fetchRepository(groupId, portletId);
304 }
305
306 @Override
307 public List<FileEntry> getPortletFileEntries(long groupId, long folderId)
308 throws SystemException {
309
310 return toFileEntries(
311 DLFileEntryLocalServiceUtil.getFileEntries(groupId, folderId));
312 }
313
314 @Override
315 public List<FileEntry> getPortletFileEntries(
316 long groupId, long folderId, int status)
317 throws SystemException {
318
319 return getPortletFileEntries(
320 groupId, folderId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
321 null);
322 }
323
324 @Override
325 public List<FileEntry> getPortletFileEntries(
326 long groupId, long folderId, int status, int start, int end,
327 OrderByComparator obc)
328 throws SystemException {
329
330 return toFileEntries(
331 DLFileEntryLocalServiceUtil.getFileEntries(
332 groupId, folderId, status, start, end, obc));
333 }
334
335 @Override
336 public int getPortletFileEntriesCount(long groupId, long folderId)
337 throws SystemException {
338
339 return DLFileEntryLocalServiceUtil.getFileEntriesCount(
340 groupId, folderId);
341 }
342
343 @Override
344 public int getPortletFileEntriesCount(
345 long groupId, long folderId, int status)
346 throws SystemException {
347
348 return DLFileEntryLocalServiceUtil.getFileEntriesCount(
349 groupId, folderId, status);
350 }
351
352 @Override
353 public FileEntry getPortletFileEntry(long fileEntryId)
354 throws PortalException, SystemException {
355
356 return DLAppLocalServiceUtil.getFileEntry(fileEntryId);
357 }
358
359 @Override
360 public FileEntry getPortletFileEntry(
361 long groupId, long folderId, String fileName)
362 throws PortalException, SystemException {
363
364 return DLAppLocalServiceUtil.getFileEntry(groupId, folderId, fileName);
365 }
366
367 @Override
368 public Folder getPortletFolder(long folderId)
369 throws PortalException, SystemException {
370
371 return DLAppLocalServiceUtil.getFolder(folderId);
372 }
373
374 @Override
375 public Folder getPortletFolder(
376 long repositoryId, long parentFolderId, String folderName)
377 throws PortalException, SystemException {
378
379 return DLAppLocalServiceUtil.getFolder(
380 repositoryId, parentFolderId, folderName);
381 }
382
383 @Override
384 public Repository getPortletRepository(long groupId, String portletId)
385 throws PortalException, SystemException {
386
387 return RepositoryLocalServiceUtil.getRepository(groupId, portletId);
388 }
389
390 @Override
391 public FileEntry movePortletFileEntryToTrash(long userId, long fileEntryId)
392 throws PortalException, SystemException {
393
394 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
395
396 try {
397 DLAppHelperThreadLocal.setEnabled(false);
398
399 return DLAppLocalServiceUtil.moveFileEntryToTrash(
400 userId, fileEntryId);
401 }
402 finally {
403 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
404 }
405 }
406
407 @Override
408 public FileEntry movePortletFileEntryToTrash(
409 long groupId, long userId, long folderId, String fileName)
410 throws PortalException, SystemException {
411
412 FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
413 groupId, folderId, fileName);
414
415 return movePortletFileEntryToTrash(userId, fileEntry.getFileEntryId());
416 }
417
418 @Override
419 public void restorePortletFileEntryFromTrash(long userId, long fileEntryId)
420 throws PortalException, SystemException {
421
422 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
423
424 try {
425 DLAppHelperThreadLocal.setEnabled(false);
426
427 DLAppLocalServiceUtil.restoreFileEntryFromTrash(
428 userId, fileEntryId);
429 }
430 finally {
431 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
432 }
433 }
434
435 @Override
436 public void restorePortletFileEntryFromTrash(
437 long groupId, long userId, long folderId, String fileName)
438 throws PortalException, SystemException {
439
440 FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
441 groupId, folderId, fileName);
442
443 restorePortletFileEntryFromTrash(userId, fileEntry.getFileEntryId());
444 }
445
446
449 protected List<FileEntry> toFileEntries(List<DLFileEntry> dlFileEntries) {
450 List<FileEntry> fileEntries = new ArrayList<FileEntry>(
451 dlFileEntries.size());
452
453 for (DLFileEntry dlFileEntry : dlFileEntries) {
454 FileEntry fileEntry = new LiferayFileEntry(dlFileEntry);
455
456 fileEntries.add(fileEntry);
457 }
458
459 if (dlFileEntries instanceof UnmodifiableList) {
460 return new UnmodifiableList<FileEntry>(fileEntries);
461 }
462 else {
463 return fileEntries;
464 }
465 }
466
467 }