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