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