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