1
22
23 package com.liferay.portlet.documentlibrary.service.impl;
24
25 import com.liferay.documentlibrary.DuplicateFileException;
26 import com.liferay.portal.NoSuchLayoutException;
27 import com.liferay.portal.PortalException;
28 import com.liferay.portal.SystemException;
29 import com.liferay.portal.kernel.search.Hits;
30 import com.liferay.portal.kernel.util.GetterUtil;
31 import com.liferay.portal.kernel.util.LocaleUtil;
32 import com.liferay.portal.kernel.util.StringPool;
33 import com.liferay.portal.model.Layout;
34 import com.liferay.portal.model.LayoutConstants;
35 import com.liferay.portal.model.ResourceConstants;
36 import com.liferay.portal.model.User;
37 import com.liferay.portal.util.PortletKeys;
38 import com.liferay.portal.util.PropsKeys;
39 import com.liferay.portal.util.PropsUtil;
40 import com.liferay.portal.util.PropsValues;
41 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
42 import com.liferay.portlet.documentlibrary.FolderNameException;
43 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
44 import com.liferay.portlet.documentlibrary.model.DLFolder;
45 import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
46 import com.liferay.portlet.documentlibrary.service.base.DLFolderLocalServiceBaseImpl;
47 import com.liferay.portlet.tags.util.TagsUtil;
48
49 import java.util.ArrayList;
50 import java.util.Date;
51 import java.util.List;
52
53
59 public class DLFolderLocalServiceImpl extends DLFolderLocalServiceBaseImpl {
60
61 public DLFolder addFolder(
62 long userId, long groupId, long parentFolderId, String name,
63 String description, boolean addCommunityPermissions,
64 boolean addGuestPermissions)
65 throws PortalException, SystemException {
66
67 return addFolder(
68 null, userId, groupId, parentFolderId, name, description,
69 Boolean.valueOf(addCommunityPermissions),
70 Boolean.valueOf(addGuestPermissions), null, null);
71 }
72
73 public DLFolder addFolder(
74 String uuid, long userId, long groupId, long parentFolderId,
75 String name, String description, boolean addCommunityPermissions,
76 boolean addGuestPermissions)
77 throws PortalException, SystemException {
78
79 return addFolder(
80 uuid, userId, groupId, parentFolderId, name, description,
81 Boolean.valueOf(addCommunityPermissions),
82 Boolean.valueOf(addGuestPermissions), null, null);
83 }
84
85 public DLFolder addFolder(
86 long userId, long groupId, long parentFolderId, String name,
87 String description, String[] communityPermissions,
88 String[] guestPermissions)
89 throws PortalException, SystemException {
90
91 return addFolder(
92 null, userId, groupId, parentFolderId, name, description, null,
93 null, communityPermissions, guestPermissions);
94 }
95
96 public DLFolder addFolder(
97 String uuid, long userId, long groupId, long parentFolderId,
98 String name, String description, Boolean addCommunityPermissions,
99 Boolean addGuestPermissions, String[] communityPermissions,
100 String[] guestPermissions)
101 throws PortalException, SystemException {
102
103
105 User user = userPersistence.findByPrimaryKey(userId);
106 parentFolderId = getParentFolderId(groupId, parentFolderId);
107 Date now = new Date();
108
109 validate(groupId, parentFolderId, name);
110
111 long folderId = counterLocalService.increment();
112
113 DLFolder folder = dlFolderPersistence.create(folderId);
114
115 folder.setUuid(uuid);
116 folder.setGroupId(groupId);
117 folder.setCompanyId(user.getCompanyId());
118 folder.setUserId(user.getUserId());
119 folder.setCreateDate(now);
120 folder.setModifiedDate(now);
121 folder.setParentFolderId(parentFolderId);
122 folder.setName(name);
123 folder.setDescription(description);
124
125 dlFolderPersistence.update(folder, false);
126
127
129 if ((addCommunityPermissions != null) &&
130 (addGuestPermissions != null)) {
131
132 addFolderResources(
133 folder, addCommunityPermissions.booleanValue(),
134 addGuestPermissions.booleanValue());
135 }
136 else {
137 addFolderResources(folder, communityPermissions, guestPermissions);
138 }
139
140
142 if (parentFolderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
143 DLFolder parentFolder = dlFolderPersistence.findByPrimaryKey(
144 parentFolderId);
145
146 parentFolder.setLastPostDate(now);
147
148 dlFolderPersistence.update(parentFolder, false);
149 }
150
151
153 String[] pathArray = folder.getPathArray();
154
155 if (PropsValues.DL_LAYOUTS_SYNC_ENABLED &&
156 (parentFolderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID)) {
157
158 String layoutsSyncPrivateFolder = GetterUtil.getString(
159 PropsUtil.get(PropsKeys.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
160 String layoutsSyncPublicFolder = GetterUtil.getString(
161 PropsUtil.get(PropsKeys.DL_LAYOUTS_SYNC_PUBLIC_FOLDER));
162
163 if (pathArray[0].equals(layoutsSyncPrivateFolder) ||
164 pathArray[0].equals(layoutsSyncPublicFolder)) {
165
166 boolean privateLayout = true;
167
168 if (pathArray[0].equals(layoutsSyncPublicFolder)) {
169 privateLayout = false;
170 }
171
172 long parentLayoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
173 String title = StringPool.BLANK;
174 String layoutDescription = StringPool.BLANK;
175 String type = LayoutConstants.TYPE_PORTLET;
176 boolean hidden = false;
177 String friendlyURL = StringPool.BLANK;
178
179 Layout dlFolderLayout = null;
180
181 try {
182 dlFolderLayout = layoutLocalService.getDLFolderLayout(
183 folder.getParentFolderId());
184
185 parentLayoutId = dlFolderLayout.getLayoutId();
186 }
187 catch (NoSuchLayoutException nsle) {
188 }
189
190 layoutLocalService.addLayout(
191 userId, groupId, privateLayout, parentLayoutId, name, title,
192 layoutDescription, type, hidden, friendlyURL,
193 folder.getFolderId());
194 }
195 }
196
197 return folder;
198 }
199
200 public void addFolderResources(
201 long folderId, boolean addCommunityPermissions,
202 boolean addGuestPermissions)
203 throws PortalException, SystemException {
204
205 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
206
207 addFolderResources(
208 folder, addCommunityPermissions, addGuestPermissions);
209 }
210
211 public void addFolderResources(
212 DLFolder folder, boolean addCommunityPermissions,
213 boolean addGuestPermissions)
214 throws PortalException, SystemException {
215
216 resourceLocalService.addResources(
217 folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
218 DLFolder.class.getName(), folder.getFolderId(), false,
219 addCommunityPermissions, addGuestPermissions);
220 }
221
222 public void addFolderResources(
223 long folderId, String[] communityPermissions,
224 String[] guestPermissions)
225 throws PortalException, SystemException {
226
227 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
228
229 addFolderResources(folder, communityPermissions, guestPermissions);
230 }
231
232 public void addFolderResources(
233 DLFolder folder, String[] communityPermissions,
234 String[] guestPermissions)
235 throws PortalException, SystemException {
236
237 resourceLocalService.addModelResources(
238 folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
239 DLFolder.class.getName(), folder.getFolderId(),
240 communityPermissions, guestPermissions);
241 }
242
243 public void deleteFolder(long folderId)
244 throws PortalException, SystemException {
245
246 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
247
248 deleteFolder(folder);
249 }
250
251 public void deleteFolder(DLFolder folder)
252 throws PortalException, SystemException {
253
254
256 List<DLFolder> folders = dlFolderPersistence.findByG_P(
257 folder.getGroupId(), folder.getFolderId());
258
259 for (DLFolder curFolder : folders) {
260 deleteFolder(curFolder);
261 }
262
263
265 dlFileEntryLocalService.deleteFileEntries(folder.getFolderId());
266
267
269 webDAVPropsLocalService.deleteWebDAVProps(
270 DLFolder.class.getName(), folder.getPrimaryKey());
271
272
274 resourceLocalService.deleteResource(
275 folder.getCompanyId(), DLFolder.class.getName(),
276 ResourceConstants.SCOPE_INDIVIDUAL, folder.getFolderId());
277
278
280 dlFolderPersistence.remove(folder);
281 }
282
283 public void deleteFolders(long groupId)
284 throws PortalException, SystemException {
285
286 List<DLFolder> folders = dlFolderPersistence.findByG_P(
287 groupId, DLFolderImpl.DEFAULT_PARENT_FOLDER_ID);
288
289 for (DLFolder folder : folders) {
290 deleteFolder(folder);
291 }
292 }
293
294 public List<Object> getFileEntriesAndFileShortcuts(
295 long folderId, int start, int end)
296 throws SystemException {
297
298 List<Long> folderIds = new ArrayList<Long>();
299
300 folderIds.add(folderId);
301
302 return dlFolderFinder.findFE_FS_ByFolderIds(folderIds, start, end);
303 }
304
305 public List<Object> getFileEntriesAndFileShortcuts(
306 List<Long> folderIds, int start, int end)
307 throws SystemException {
308
309 return dlFolderFinder.findFE_FS_ByFolderIds(folderIds, start, end);
310 }
311
312 public int getFileEntriesAndFileShortcutsCount(long folderId)
313 throws SystemException {
314
315 List<Long> folderIds = new ArrayList<Long>();
316
317 folderIds.add(folderId);
318
319 return dlFolderFinder.countFE_FS_ByFolderIds(folderIds);
320 }
321
322 public int getFileEntriesAndFileShortcutsCount(List<Long> folderIds)
323 throws SystemException {
324
325 return dlFolderFinder.countFE_FS_ByFolderIds(folderIds);
326 }
327
328 public DLFolder getFolder(long folderId)
329 throws PortalException, SystemException {
330
331 return dlFolderPersistence.findByPrimaryKey(folderId);
332 }
333
334 public DLFolder getFolder(long groupId, long parentFolderId, String name)
335 throws PortalException, SystemException {
336
337 return dlFolderPersistence.findByG_P_N(groupId, parentFolderId, name);
338 }
339
340 public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
341 long folderId, int start, int end)
342 throws SystemException {
343
344 List<Long> folderIds = new ArrayList<Long>();
345
346 folderIds.add(folderId);
347
348 return dlFolderFinder.findF_FE_FS_ByFolderIds(folderIds, start, end);
349 }
350
351 public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
352 List<Long> folderIds, int start, int end)
353 throws SystemException {
354
355 return dlFolderFinder.findF_FE_FS_ByFolderIds(folderIds, start, end);
356 }
357
358 public int getFoldersAndFileEntriesAndFileShortcutsCount(long folderId)
359 throws SystemException {
360
361 List<Long> folderIds = new ArrayList<Long>();
362
363 folderIds.add(folderId);
364
365 return dlFolderFinder.countF_FE_FS_ByFolderIds(folderIds);
366 }
367
368 public int getFoldersAndFileEntriesAndFileShortcutsCount(
369 List<Long> folderIds)
370 throws SystemException {
371
372 return dlFolderFinder.countF_FE_FS_ByFolderIds(folderIds);
373 }
374
375 public List<DLFolder> getFolders(long companyId) throws SystemException {
376 return dlFolderPersistence.findByCompanyId(companyId);
377 }
378
379 public List<DLFolder> getFolders(long groupId, long parentFolderId)
380 throws SystemException {
381
382 return dlFolderPersistence.findByG_P(groupId, parentFolderId);
383 }
384
385 public List<DLFolder> getFolders(
386 long groupId, long parentFolderId, int start, int end)
387 throws SystemException {
388
389 return dlFolderPersistence.findByG_P(
390 groupId, parentFolderId, start, end);
391 }
392
393 public int getFoldersCount(long groupId, long parentFolderId)
394 throws SystemException {
395
396 return dlFolderPersistence.countByG_P(groupId, parentFolderId);
397 }
398
399 public void getSubfolderIds(
400 List<Long> folderIds, long groupId, long folderId)
401 throws SystemException {
402
403 List<DLFolder> folders = dlFolderPersistence.findByG_P(
404 groupId, folderId);
405
406 for (DLFolder folder : folders) {
407 folderIds.add(folder.getFolderId());
408
409 getSubfolderIds(
410 folderIds, folder.getGroupId(), folder.getFolderId());
411 }
412 }
413
414 public void reIndex(String[] ids) throws SystemException {
415 long companyId = GetterUtil.getLong(ids[0]);
416
417 try {
418 List<DLFolder> folders = getFolders(companyId);
419
420 for (DLFolder folder : folders) {
421 String portletId = PortletKeys.DOCUMENT_LIBRARY;
422 long groupId = folder.getGroupId();
423 long folderId = folder.getFolderId();
424
425 String[] newIds = {
426 String.valueOf(companyId), portletId,
427 String.valueOf(groupId), String.valueOf(folderId)
428 };
429
430 dlService.reIndex(newIds);
431 }
432 }
433 catch (SystemException se) {
434 throw se;
435 }
436 catch (Exception e) {
437 throw new SystemException(e);
438 }
439 }
440
441 public Hits search(
442 long companyId, long groupId, long userId, long[] folderIds,
443 String keywords, int start, int end)
444 throws SystemException {
445
446 return dlLocalService.search(
447 companyId, PortletKeys.DOCUMENT_LIBRARY, groupId, userId, folderIds,
448 keywords, start, end);
449 }
450
451 public DLFolder updateFolder(
452 long folderId, long parentFolderId, String name,
453 String description)
454 throws PortalException, SystemException {
455
456 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
457
458 parentFolderId = getParentFolderId(folder, parentFolderId);
459
460 validate(
461 folder.getFolderId(), folder.getGroupId(), parentFolderId, name);
462
463 folder.setModifiedDate(new Date());
464 folder.setParentFolderId(parentFolderId);
465 folder.setName(name);
466 folder.setDescription(description);
467
468 dlFolderPersistence.update(folder, false);
469
470 if (PropsValues.DL_LAYOUTS_SYNC_ENABLED) {
471 String privateFolder = GetterUtil.getString(PropsUtil.get(
472 PropsKeys.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
473
474 boolean privateLayout = false;
475
476 String[] path = folder.getPathArray();
477
478 if (path[0].equals(privateFolder)) {
479 privateLayout = true;
480 }
481
482 Layout layout = layoutLocalService.getDLFolderLayout(
483 folder.getFolderId());
484
485 layout.setName(folder.getName());
486
487 layoutLocalService.updateName(
488 folder.getGroupId(), privateLayout, layout.getLayoutId(),
489 folder.getName(),
490 LocaleUtil.toLanguageId(LocaleUtil.getDefault()));
491 }
492
493 return folder;
494 }
495
496 protected long getParentFolderId(long groupId, long parentFolderId)
497 throws SystemException {
498
499 if (parentFolderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
500 DLFolder parentFolder = dlFolderPersistence.fetchByPrimaryKey(
501 parentFolderId);
502
503 if ((parentFolder == null) ||
504 (groupId != parentFolder.getGroupId())) {
505
506 parentFolderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
507 }
508 }
509
510 return parentFolderId;
511 }
512
513 protected long getParentFolderId(DLFolder folder, long parentFolderId)
514 throws SystemException {
515
516 if (parentFolderId == DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
517 return parentFolderId;
518 }
519
520 if (folder.getFolderId() == parentFolderId) {
521 return folder.getParentFolderId();
522 }
523 else {
524 DLFolder parentFolder = dlFolderPersistence.fetchByPrimaryKey(
525 parentFolderId);
526
527 if ((parentFolder == null) ||
528 (folder.getGroupId() != parentFolder.getGroupId())) {
529
530 return folder.getParentFolderId();
531 }
532
533 List<Long> subfolderIds = new ArrayList<Long>();
534
535 getSubfolderIds(
536 subfolderIds, folder.getGroupId(), folder.getFolderId());
537
538 if (subfolderIds.contains(parentFolderId)) {
539 return folder.getParentFolderId();
540 }
541
542 return parentFolderId;
543 }
544 }
545
546 protected void validate(long groupId, long parentFolderId, String name)
547 throws PortalException, SystemException {
548
549 long folderId = 0;
550
551 validate(folderId, groupId, parentFolderId, name);
552 }
553
554 protected void validate(
555 long folderId, long groupId, long parentFolderId, String name)
556 throws PortalException, SystemException {
557
558 if (!TagsUtil.isValidWord(name)) {
559 throw new FolderNameException();
560 }
561
562 try {
563 dlFileEntryLocalService.getFileEntryByTitle(parentFolderId, name);
564
565 throw new DuplicateFileException();
566 }
567 catch (NoSuchFileEntryException nsfee) {
568 }
569
570 DLFolder folder = dlFolderPersistence.fetchByG_P_N(
571 groupId, parentFolderId, name);
572
573 if ((folder != null) && (folder.getFolderId() != folderId)) {
574 throw new DuplicateFolderNameException();
575 }
576 }
577
578 }