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