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