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