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