1
14
15 package com.liferay.portlet.documentlibrary.service.impl;
16
17 import com.liferay.documentlibrary.DuplicateFileException;
18 import com.liferay.documentlibrary.NoSuchDirectoryException;
19 import com.liferay.portal.NoSuchLayoutException;
20 import com.liferay.portal.kernel.exception.PortalException;
21 import com.liferay.portal.kernel.exception.SystemException;
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.util.GetterUtil;
25 import com.liferay.portal.kernel.util.LocaleUtil;
26 import com.liferay.portal.kernel.util.PropsKeys;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.model.Layout;
29 import com.liferay.portal.model.LayoutConstants;
30 import com.liferay.portal.model.ResourceConstants;
31 import com.liferay.portal.model.User;
32 import com.liferay.portal.service.ServiceContext;
33 import com.liferay.portal.util.PortletKeys;
34 import com.liferay.portal.util.PropsUtil;
35 import com.liferay.portal.util.PropsValues;
36 import com.liferay.portlet.asset.util.AssetUtil;
37 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
38 import com.liferay.portlet.documentlibrary.FolderNameException;
39 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
40 import com.liferay.portlet.documentlibrary.model.DLFolder;
41 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
42 import com.liferay.portlet.documentlibrary.service.base.DLFolderLocalServiceBaseImpl;
43
44 import java.util.ArrayList;
45 import java.util.Date;
46 import java.util.List;
47
48
53 public class DLFolderLocalServiceImpl extends DLFolderLocalServiceBaseImpl {
54
55 public DLFolder addFolder(
56 String uuid, long userId, long groupId, long parentFolderId,
57 String name, String description, ServiceContext serviceContext)
58 throws PortalException, SystemException {
59
60
62 User user = userPersistence.findByPrimaryKey(userId);
63 parentFolderId = getParentFolderId(groupId, parentFolderId);
64 Date now = new Date();
65
66 validate(groupId, parentFolderId, name);
67
68 long folderId = counterLocalService.increment();
69
70 DLFolder folder = dlFolderPersistence.create(folderId);
71
72 folder.setUuid(uuid);
73 folder.setGroupId(groupId);
74 folder.setCompanyId(user.getCompanyId());
75 folder.setUserId(user.getUserId());
76 folder.setCreateDate(now);
77 folder.setModifiedDate(now);
78 folder.setParentFolderId(parentFolderId);
79 folder.setName(name);
80 folder.setDescription(description);
81 folder.setExpandoBridgeAttributes(serviceContext);
82
83 dlFolderPersistence.update(folder, false);
84
85
87 if (serviceContext.getAddCommunityPermissions() ||
88 serviceContext.getAddGuestPermissions()) {
89
90 addFolderResources(
91 folder, serviceContext.getAddCommunityPermissions(),
92 serviceContext.getAddGuestPermissions());
93 }
94 else {
95 addFolderResources(
96 folder, serviceContext.getCommunityPermissions(),
97 serviceContext.getGuestPermissions());
98 }
99
100
102 if (PropsValues.DL_LAYOUTS_SYNC_ENABLED &&
103 (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
104
105 String[] pathArray = folder.getPathArray();
106
107 String layoutsSyncPrivateFolder = GetterUtil.getString(
108 PropsUtil.get(PropsKeys.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
109 String layoutsSyncPublicFolder = GetterUtil.getString(
110 PropsUtil.get(PropsKeys.DL_LAYOUTS_SYNC_PUBLIC_FOLDER));
111
112 if (pathArray[0].equals(layoutsSyncPrivateFolder) ||
113 pathArray[0].equals(layoutsSyncPublicFolder)) {
114
115 boolean privateLayout = true;
116
117 if (pathArray[0].equals(layoutsSyncPublicFolder)) {
118 privateLayout = false;
119 }
120
121 long parentLayoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
122 String title = StringPool.BLANK;
123 String layoutDescription = StringPool.BLANK;
124 String type = LayoutConstants.TYPE_PORTLET;
125 boolean hidden = false;
126 String friendlyURL = StringPool.BLANK;
127
128 Layout dlFolderLayout = null;
129
130 try {
131 dlFolderLayout = layoutLocalService.getDLFolderLayout(
132 folder.getParentFolderId());
133
134 parentLayoutId = dlFolderLayout.getLayoutId();
135 }
136 catch (NoSuchLayoutException nsle) {
137 }
138
139 layoutLocalService.addLayout(
140 userId, groupId, privateLayout, parentLayoutId, name, title,
141 layoutDescription, type, hidden, friendlyURL,
142 folder.getFolderId(), new ServiceContext());
143 }
144 }
145
146
148 if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
149 DLFolder parentFolder = dlFolderPersistence.findByPrimaryKey(
150 parentFolderId);
151
152 parentFolder.setLastPostDate(now);
153
154 dlFolderPersistence.update(parentFolder, false);
155 }
156
157 return folder;
158 }
159
160 public void addFolderResources(
161 DLFolder folder, boolean addCommunityPermissions,
162 boolean addGuestPermissions)
163 throws PortalException, SystemException {
164
165 resourceLocalService.addResources(
166 folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
167 DLFolder.class.getName(), folder.getFolderId(), false,
168 addCommunityPermissions, addGuestPermissions);
169 }
170
171 public void addFolderResources(
172 DLFolder folder, String[] communityPermissions,
173 String[] guestPermissions)
174 throws PortalException, SystemException {
175
176 resourceLocalService.addModelResources(
177 folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
178 DLFolder.class.getName(), folder.getFolderId(),
179 communityPermissions, guestPermissions);
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 long folderId, String[] communityPermissions,
195 String[] guestPermissions)
196 throws PortalException, SystemException {
197
198 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
199
200 addFolderResources(folder, communityPermissions, guestPermissions);
201 }
202
203 public void deleteFolder(DLFolder folder)
204 throws PortalException, SystemException {
205
206
208 dlFolderPersistence.remove(folder);
209
210
212 resourceLocalService.deleteResource(
213 folder.getCompanyId(), DLFolder.class.getName(),
214 ResourceConstants.SCOPE_INDIVIDUAL, folder.getFolderId());
215
216
218 webDAVPropsLocalService.deleteWebDAVProps(
219 DLFolder.class.getName(), folder.getFolderId());
220
221
223 dlFileEntryLocalService.deleteFileEntries(
224 folder.getGroupId(), folder.getFolderId());
225
226
228 List<DLFolder> folders = dlFolderPersistence.findByG_P(
229 folder.getGroupId(), folder.getFolderId());
230
231 for (DLFolder curFolder : folders) {
232 deleteFolder(curFolder);
233 }
234
235
237 expandoValueLocalService.deleteValues(
238 DLFolder.class.getName(), folder.getFolderId());
239
240
242 try {
243 dlService.deleteDirectory(
244 folder.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
245 folder.getFolderId(), StringPool.BLANK);
246 }
247 catch (NoSuchDirectoryException nsde) {
248 if (_log.isDebugEnabled()) {
249 _log.debug(nsde.getMessage());
250 }
251 }
252 }
253
254 public void deleteFolder(long folderId)
255 throws PortalException, SystemException {
256
257 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
258
259 deleteFolder(folder);
260 }
261
262 public void deleteFolders(long groupId)
263 throws PortalException, SystemException {
264
265 List<DLFolder> folders = dlFolderPersistence.findByG_P(
266 groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
267
268 for (DLFolder folder : folders) {
269 deleteFolder(folder);
270 }
271 }
272
273 public List<DLFolder> getCompanyFolders(long companyId, int start, int end)
274 throws SystemException {
275
276 return dlFolderPersistence.findByCompanyId(companyId, start, end);
277 }
278
279 public int getCompanyFoldersCount(long companyId) throws SystemException {
280 return dlFolderPersistence.countByCompanyId(companyId);
281 }
282
283 public List<Object> getFileEntriesAndFileShortcuts(
284 long groupId, List<Long> folderIds, int status, int start, int end)
285 throws SystemException {
286
287 return dlFolderFinder.findFE_FS_ByG_F_S(
288 groupId, folderIds, status, start, end);
289 }
290
291 public List<Object> getFileEntriesAndFileShortcuts(
292 long groupId, long folderId, int status, int start, int end)
293 throws SystemException {
294
295 List<Long> folderIds = new ArrayList<Long>();
296
297 folderIds.add(folderId);
298
299 return dlFolderFinder.findFE_FS_ByG_F_S(
300 groupId, folderIds, status, start, end);
301 }
302
303 public int getFileEntriesAndFileShortcutsCount(
304 long groupId, List<Long> folderIds, int status)
305 throws SystemException {
306
307 return dlFolderFinder.countFE_FS_ByG_F_S(groupId, folderIds, status);
308 }
309
310 public int getFileEntriesAndFileShortcutsCount(
311 long groupId, long folderId, int status)
312 throws SystemException {
313
314 List<Long> folderIds = new ArrayList<Long>();
315
316 folderIds.add(folderId);
317
318 return dlFolderFinder.countFE_FS_ByG_F_S(groupId, folderIds, status);
319 }
320
321 public DLFolder getFolder(long folderId)
322 throws PortalException, SystemException {
323
324 return dlFolderPersistence.findByPrimaryKey(folderId);
325 }
326
327 public DLFolder getFolder(long groupId, long parentFolderId, String name)
328 throws PortalException, SystemException {
329
330 return dlFolderPersistence.findByG_P_N(groupId, parentFolderId, name);
331 }
332
333 public List<DLFolder> getFolders(long companyId) throws SystemException {
334 return dlFolderPersistence.findByCompanyId(companyId);
335 }
336
337 public List<DLFolder> getFolders(long groupId, long parentFolderId)
338 throws SystemException {
339
340 return dlFolderPersistence.findByG_P(groupId, parentFolderId);
341 }
342
343 public List<DLFolder> getFolders(
344 long groupId, long parentFolderId, int start, int end)
345 throws SystemException {
346
347 return dlFolderPersistence.findByG_P(
348 groupId, parentFolderId, start, end);
349 }
350
351 public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
352 long groupId, List<Long> folderIds, int status, int start, int end)
353 throws SystemException {
354
355 return dlFolderFinder.findF_FE_FS_ByG_F_S(
356 groupId, folderIds, status, start, end);
357 }
358
359 public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
360 long groupId, long folderId, int status, int start, int end)
361 throws SystemException {
362
363 List<Long> folderIds = new ArrayList<Long>();
364
365 folderIds.add(folderId);
366
367 return getFoldersAndFileEntriesAndFileShortcuts(
368 groupId, folderIds, status, start, end);
369 }
370
371 public int getFoldersAndFileEntriesAndFileShortcutsCount(
372 long groupId, List<Long> folderIds, int status)
373 throws SystemException {
374
375 return dlFolderFinder.countF_FE_FS_ByG_F_S(groupId, folderIds, status);
376 }
377
378 public int getFoldersAndFileEntriesAndFileShortcutsCount(
379 long groupId, long folderId, int status)
380 throws SystemException {
381
382 List<Long> folderIds = new ArrayList<Long>();
383
384 folderIds.add(folderId);
385
386 return getFoldersAndFileEntriesAndFileShortcutsCount(
387 groupId, folderIds, status);
388 }
389
390 public int getFoldersCount(long groupId, long parentFolderId)
391 throws SystemException {
392
393 return dlFolderPersistence.countByG_P(groupId, parentFolderId);
394 }
395
396 public void getSubfolderIds(
397 List<Long> folderIds, long groupId, long folderId)
398 throws SystemException {
399
400 List<DLFolder> folders = dlFolderPersistence.findByG_P(
401 groupId, folderId);
402
403 for (DLFolder folder : folders) {
404 folderIds.add(folder.getFolderId());
405
406 getSubfolderIds(
407 folderIds, folder.getGroupId(), folder.getFolderId());
408 }
409 }
410
411 public DLFolder updateFolder(
412 long folderId, long parentFolderId, String name,
413 String description, ServiceContext serviceContext)
414 throws PortalException, SystemException {
415
416
418 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
419
420 parentFolderId = getParentFolderId(folder, parentFolderId);
421
422 validate(
423 folder.getFolderId(), folder.getGroupId(), parentFolderId, name);
424
425 folder.setModifiedDate(new Date());
426 folder.setParentFolderId(parentFolderId);
427 folder.setName(name);
428 folder.setDescription(description);
429 folder.setExpandoBridgeAttributes(serviceContext);
430
431 dlFolderPersistence.update(folder, false);
432
433
435 if (PropsValues.DL_LAYOUTS_SYNC_ENABLED) {
436 String privateFolder = GetterUtil.getString(PropsUtil.get(
437 PropsKeys.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
438
439 boolean privateLayout = false;
440
441 String[] path = folder.getPathArray();
442
443 if (path[0].equals(privateFolder)) {
444 privateLayout = true;
445 }
446
447 Layout layout = layoutLocalService.getDLFolderLayout(
448 folder.getFolderId());
449
450 layout.setName(folder.getName());
451
452 layoutLocalService.updateName(
453 folder.getGroupId(), privateLayout, layout.getLayoutId(),
454 folder.getName(),
455 LocaleUtil.toLanguageId(LocaleUtil.getDefault()));
456 }
457
458 return folder;
459 }
460
461 protected long getParentFolderId(DLFolder folder, long parentFolderId)
462 throws SystemException {
463
464 if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
465 return parentFolderId;
466 }
467
468 if (folder.getFolderId() == parentFolderId) {
469 return folder.getParentFolderId();
470 }
471 else {
472 DLFolder parentFolder = dlFolderPersistence.fetchByPrimaryKey(
473 parentFolderId);
474
475 if ((parentFolder == null) ||
476 (folder.getGroupId() != parentFolder.getGroupId())) {
477
478 return folder.getParentFolderId();
479 }
480
481 List<Long> subfolderIds = new ArrayList<Long>();
482
483 getSubfolderIds(
484 subfolderIds, folder.getGroupId(), folder.getFolderId());
485
486 if (subfolderIds.contains(parentFolderId)) {
487 return folder.getParentFolderId();
488 }
489
490 return parentFolderId;
491 }
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 void validate(
512 long folderId, long groupId, long parentFolderId, String name)
513 throws PortalException, SystemException {
514
515 if (!AssetUtil.isValidWord(name)) {
516 throw new FolderNameException();
517 }
518
519 try {
520 dlFileEntryLocalService.getFileEntryByTitle(
521 groupId, parentFolderId, name);
522
523 throw new DuplicateFileException();
524 }
525 catch (NoSuchFileEntryException nsfee) {
526 }
527
528 DLFolder folder = dlFolderPersistence.fetchByG_P_N(
529 groupId, parentFolderId, name);
530
531 if ((folder != null) && (folder.getFolderId() != folderId)) {
532 throw new DuplicateFolderNameException();
533 }
534 }
535
536 protected void validate(long groupId, long parentFolderId, String name)
537 throws PortalException, SystemException {
538
539 long folderId = 0;
540
541 validate(folderId, groupId, parentFolderId, name);
542 }
543
544 private static Log _log = LogFactoryUtil.getLog(
545 DLFolderLocalServiceImpl.class);
546
547 }