1
22
23 package com.liferay.portlet.documentlibrary.service.impl;
24
25 import com.liferay.lock.ExpiredLockException;
26 import com.liferay.lock.InvalidLockException;
27 import com.liferay.lock.NoSuchLockException;
28 import com.liferay.lock.model.Lock;
29 import com.liferay.portal.PortalException;
30 import com.liferay.portal.SystemException;
31 import com.liferay.portal.kernel.log.Log;
32 import com.liferay.portal.kernel.log.LogFactoryUtil;
33 import com.liferay.portal.kernel.util.FileUtil;
34 import com.liferay.portal.kernel.util.ListUtil;
35 import com.liferay.portal.kernel.util.Validator;
36 import com.liferay.portal.security.auth.PrincipalException;
37 import com.liferay.portal.security.permission.ActionKeys;
38 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
39 import com.liferay.portlet.documentlibrary.model.DLFolder;
40 import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
41 import com.liferay.portlet.documentlibrary.service.base.DLFolderServiceBaseImpl;
42 import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
43
44 import java.io.File;
45 import java.io.InputStream;
46
47 import java.rmi.RemoteException;
48
49 import java.util.HashSet;
50 import java.util.Iterator;
51 import java.util.List;
52 import java.util.Set;
53
54
60 public class DLFolderServiceImpl extends DLFolderServiceBaseImpl {
61
62 public DLFolder addFolder(
63 long groupId, long parentFolderId, String name, String description,
64 boolean addCommunityPermissions, boolean addGuestPermissions)
65 throws PortalException, SystemException {
66
67 DLFolderPermission.check(
68 getPermissionChecker(), groupId, parentFolderId,
69 ActionKeys.ADD_FOLDER);
70
71 return dlFolderLocalService.addFolder(
72 getUserId(), groupId, parentFolderId, name, description,
73 addCommunityPermissions, addGuestPermissions);
74 }
75
76 public DLFolder addFolder(
77 long groupId, long parentFolderId, String name, String description,
78 String[] communityPermissions, String[] guestPermissions)
79 throws PortalException, SystemException {
80
81 DLFolderPermission.check(
82 getPermissionChecker(), groupId, parentFolderId,
83 ActionKeys.ADD_FOLDER);
84
85 return dlFolderLocalService.addFolder(
86 getUserId(), groupId, parentFolderId, name, description,
87 communityPermissions, guestPermissions);
88 }
89
90 public DLFolder copyFolder(
91 long groupId, long sourceFolderId, long parentFolderId, String name,
92 String description, boolean addCommunityPermissions,
93 boolean addGuestPermissions)
94 throws PortalException, RemoteException, SystemException {
95
96 DLFolder srcFolder = getFolder(sourceFolderId);
97
98 DLFolder destFolder = addFolder(
99 groupId, parentFolderId, name, description, addCommunityPermissions,
100 addGuestPermissions);
101
102 copyFolder(
103 srcFolder, destFolder, addCommunityPermissions,
104 addGuestPermissions);
105
106 return destFolder;
107 }
108
109 public void deleteFolder(long folderId)
110 throws PortalException, RemoteException, SystemException {
111
112 DLFolderPermission.check(
113 getPermissionChecker(), folderId, ActionKeys.DELETE);
114
115 boolean hasLock = lockService.hasLock(
116 DLFolder.class.getName(), folderId, getUserId());
117
118 Lock lock = null;
119
120 if (!hasLock) {
121
122
124 lock = lockFolder(folderId);
125 }
126
127 try {
128 dlFolderLocalService.deleteFolder(folderId);
129 }
130 finally {
131 if (!hasLock) {
132
133
135 unlockFolder(folderId, lock.getUuid());
136 }
137 }
138 }
139
140 public void deleteFolder(long groupId, long parentFolderId, String name)
141 throws PortalException, RemoteException, SystemException {
142
143 long folderId = getFolderId(groupId, parentFolderId, name);
144
145 deleteFolder(folderId);
146 }
147
148 public DLFolder getFolder(long folderId)
149 throws PortalException, SystemException {
150
151 DLFolderPermission.check(
152 getPermissionChecker(), folderId, ActionKeys.VIEW);
153
154 return dlFolderLocalService.getFolder(folderId);
155 }
156
157 public DLFolder getFolder(long groupId, long parentFolderId, String name)
158 throws PortalException, SystemException {
159
160 DLFolder folder = dlFolderLocalService.getFolder(
161 groupId, parentFolderId, name);
162
163 DLFolderPermission.check(
164 getPermissionChecker(), folder, ActionKeys.VIEW);
165
166 return folder;
167 }
168
169 public long getFolderId(long groupId, long parentFolderId, String name)
170 throws PortalException, SystemException {
171
172 DLFolder folder = getFolder(groupId, parentFolderId, name);
173
174 return folder.getFolderId();
175 }
176
177 public List<DLFolder> getFolders(long groupId, long parentFolderId)
178 throws PortalException, SystemException {
179
180 List<DLFolder> folders = dlFolderLocalService.getFolders(
181 groupId, parentFolderId);
182
183 folders = ListUtil.copy(folders);
184
185 Iterator<DLFolder> itr = folders.iterator();
186
187 while (itr.hasNext()) {
188 DLFolder folder = itr.next();
189
190 if (!DLFolderPermission.contains(
191 getPermissionChecker(), folder.getFolderId(),
192 ActionKeys.VIEW)) {
193
194 itr.remove();
195 }
196 }
197
198 return folders;
199 }
200
201 public boolean hasInheritableLock(long folderId) throws PortalException {
202 boolean inheritable = false;
203
204 try {
205 Lock lock = lockService.getLock(DLFolder.class.getName(), folderId);
206
207 inheritable = lock.isInheritable();
208 }
209 catch (ExpiredLockException ele) {
210 }
211 catch (NoSuchLockException nsle) {
212 }
213
214 return inheritable;
215 }
216
217 public Lock lockFolder(long folderId)
218 throws PortalException, RemoteException, SystemException {
219
220 return lockFolder(
221 folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
222 }
223
224 public Lock lockFolder(
225 long folderId, String owner, boolean inheritable,
226 long expirationTime)
227 throws PortalException, RemoteException, SystemException {
228
229 if ((expirationTime <= 0) ||
230 (expirationTime > DLFolderImpl.LOCK_EXPIRATION_TIME)) {
231
232 expirationTime = DLFolderImpl.LOCK_EXPIRATION_TIME;
233 }
234
235 Lock lock = lockService.lock(
236 DLFolder.class.getName(), folderId, getUser().getUserId(), owner,
237 inheritable, expirationTime);
238
239 Set<String> fileNames = new HashSet<String>();
240
241 try {
242 List<DLFileEntry> fileEntries = dlFileEntryService.getFileEntries(
243 folderId);
244
245 for (DLFileEntry fileEntry : fileEntries) {
246 dlFileEntryService.lockFileEntry(
247 folderId, fileEntry.getName(), owner, expirationTime);
248
249 fileNames.add(fileEntry.getName());
250 }
251 }
252 catch (Exception e) {
253 for (String fileName : fileNames) {
254 dlFileEntryService.unlockFileEntry(folderId, fileName);
255 }
256
257 unlockFolder(folderId, lock.getUuid());
258
259 if (e instanceof PortalException) {
260 throw (PortalException)e;
261 }
262 else if (e instanceof RemoteException) {
263 throw (RemoteException)e;
264 }
265 else if (e instanceof SystemException) {
266 throw (SystemException)e;
267 }
268 else {
269 throw new PortalException(e);
270 }
271 }
272
273 return lock;
274 }
275
276 public Lock refreshFolderLock(String lockUuid, long expirationTime)
277 throws PortalException {
278
279 return lockService.refresh(lockUuid, expirationTime);
280 }
281
282 public void reIndexSearch(long companyId)
283 throws PortalException, SystemException {
284
285 if (!getPermissionChecker().isOmniadmin()) {
286 throw new PrincipalException();
287 }
288
289 String[] ids = new String[] {String.valueOf(companyId)};
290
291 dlFolderLocalService.reIndex(ids);
292 }
293
294 public void unlockFolder(long folderId, String lockUuid)
295 throws PortalException {
296
297 if (Validator.isNotNull(lockUuid)) {
298 try {
299 Lock lock = lockService.getLock(
300 DLFolder.class.getName(), folderId);
301
302 if (!lock.getUuid().equals(lockUuid)) {
303 throw new InvalidLockException("UUIDs do not match");
304 }
305 }
306 catch (PortalException pe) {
307 if (pe instanceof ExpiredLockException ||
308 pe instanceof NoSuchLockException) {
309 }
310 else {
311 throw pe;
312 }
313 }
314 }
315
316 lockService.unlock(DLFolder.class.getName(), folderId);
317
318 try {
319 List<DLFileEntry> fileEntries = dlFileEntryService.getFileEntries(
320 folderId);
321
322 for (DLFileEntry fileEntry : fileEntries) {
323 dlFileEntryService.unlockFileEntry(
324 folderId, fileEntry.getName());
325 }
326 }
327 catch (Exception e) {
328 _log.error(e, e);
329 }
330 }
331
332 public void unlockFolder(
333 long groupId, long parentFolderId, String name, String lockUuid)
334 throws PortalException, SystemException {
335
336 long folderId = getFolderId(groupId, parentFolderId, name);
337
338 unlockFolder(folderId, lockUuid);
339 }
340
341 public DLFolder updateFolder(
342 long folderId, long parentFolderId, String name, String description)
343 throws PortalException, RemoteException, SystemException {
344
345 DLFolderPermission.check(
346 getPermissionChecker(), folderId, ActionKeys.UPDATE);
347
348 boolean hasLock = lockService.hasLock(
349 DLFolder.class.getName(), folderId, getUserId());
350
351 Lock lock = null;
352
353 if (!hasLock) {
354
355
357 lock = lockFolder(folderId);
358 }
359
360 try {
361 return dlFolderLocalService.updateFolder(
362 folderId, parentFolderId, name, description);
363 }
364 finally {
365 if (!hasLock) {
366
367
369 unlockFolder(folderId, lock.getUuid());
370 }
371 }
372 }
373
374 public boolean verifyInheritableLock(long folderId, String lockUuid)
375 throws PortalException {
376
377 boolean verified = false;
378
379 try {
380 Lock lock = lockService.getLock(DLFolder.class.getName(), folderId);
381
382 if (!lock.isInheritable()) {
383 throw new NoSuchLockException();
384 }
385
386 if (lock.getUuid().equals(lockUuid)) {
387 verified = true;
388 }
389 }
390 catch (ExpiredLockException ele) {
391 throw new NoSuchLockException(ele);
392 }
393
394 return verified;
395 }
396
397 protected void copyFolder(
398 DLFolder srcFolder, DLFolder destFolder,
399 boolean addCommunityPermissions, boolean addGuestPermissions)
400 throws PortalException, RemoteException, SystemException {
401
402 List<DLFileEntry> srcFileEntries = dlFileEntryService.getFileEntries(
403 srcFolder.getFolderId());
404
405 for (DLFileEntry srcFileEntry : srcFileEntries) {
406 String name = srcFileEntry.getName();
407 String title = srcFileEntry.getTitleWithExtension();
408 String description = srcFileEntry.getDescription();
409 String[] tagsEntries = null;
410 String extraSettings = srcFileEntry.getExtraSettings();
411
412 File file = null;
413
414 try {
415 file = FileUtil.createTempFile(FileUtil.getExtension(name));
416
417 InputStream is = dlLocalService.getFileAsStream(
418 srcFolder.getCompanyId(), srcFolder.getFolderId(), name);
419
420 FileUtil.write(file, is);
421 }
422 catch (Exception e) {
423 _log.error(e, e);
424
425 continue;
426 }
427
428 dlFileEntryService.addFileEntry(
429 destFolder.getFolderId(), name, title, description, tagsEntries,
430 extraSettings, file, addCommunityPermissions,
431 addGuestPermissions);
432
433 file.delete();
434 }
435
436 List<DLFolder> srcSubfolders = getFolders(
437 srcFolder.getGroupId(), srcFolder.getFolderId());
438
439 for (DLFolder srcSubfolder : srcSubfolders) {
440 String name = srcSubfolder.getName();
441 String description = srcSubfolder.getDescription();
442
443 DLFolder destSubfolder = addFolder(
444 destFolder.getGroupId(), destFolder.getFolderId(), name,
445 description, addCommunityPermissions, addGuestPermissions);
446
447 copyFolder(
448 srcSubfolder, destSubfolder, addCommunityPermissions,
449 addGuestPermissions);
450 }
451 }
452
453 private static Log _log = LogFactoryUtil.getLog(DLFolderServiceImpl.class);
454
455 }