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.kernel.util.Validator;
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.util.PortalUtil;
39 import com.liferay.portal.util.PortletKeys;
40 import com.liferay.portal.util.PropsKeys;
41 import com.liferay.portal.util.PropsUtil;
42 import com.liferay.portal.util.PropsValues;
43 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
44 import com.liferay.portlet.documentlibrary.FolderNameException;
45 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
46 import com.liferay.portlet.documentlibrary.model.DLFolder;
47 import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
48 import com.liferay.portlet.documentlibrary.service.base.DLFolderLocalServiceBaseImpl;
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.getPortletGroupId(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 String[] pathArray = folder.getPathArray();
159
160 if (PropsValues.DL_LAYOUTS_SYNC_ENABLED &&
161 (parentFolderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID)) {
162
163 String layoutsSyncPrivateFolder = GetterUtil.getString(
164 PropsUtil.get(PropsKeys.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
165 String layoutsSyncPublicFolder = GetterUtil.getString(
166 PropsUtil.get(PropsKeys.DL_LAYOUTS_SYNC_PUBLIC_FOLDER));
167
168 if (pathArray[0].equals(layoutsSyncPrivateFolder) ||
169 pathArray[0].equals(layoutsSyncPublicFolder)) {
170
171 boolean privateLayout = true;
172
173 if (pathArray[0].equals(layoutsSyncPublicFolder)) {
174 privateLayout = false;
175 }
176
177 long parentLayoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
178 String title = StringPool.BLANK;
179 String layoutDescription = StringPool.BLANK;
180 String type = LayoutConstants.TYPE_PORTLET;
181 boolean hidden = false;
182 String friendlyURL = StringPool.BLANK;
183
184 Layout dlFolderLayout = null;
185
186 try {
187 dlFolderLayout = layoutLocalService.getDLFolderLayout(
188 folder.getParentFolderId());
189
190 parentLayoutId = dlFolderLayout.getLayoutId();
191 }
192 catch (NoSuchLayoutException nsle) {
193 }
194
195 layoutLocalService.addLayout(
196 userId, groupId, privateLayout, parentLayoutId, name, title,
197 layoutDescription, type, hidden, friendlyURL,
198 folder.getFolderId());
199 }
200 }
201
202 return folder;
203 }
204
205 public void addFolderResources(
206 long folderId, boolean addCommunityPermissions,
207 boolean addGuestPermissions)
208 throws PortalException, SystemException {
209
210 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
211
212 addFolderResources(
213 folder, addCommunityPermissions, addGuestPermissions);
214 }
215
216 public void addFolderResources(
217 DLFolder folder, boolean addCommunityPermissions,
218 boolean addGuestPermissions)
219 throws PortalException, SystemException {
220
221 resourceLocalService.addResources(
222 folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
223 DLFolder.class.getName(), folder.getFolderId(), false,
224 addCommunityPermissions, addGuestPermissions);
225 }
226
227 public void addFolderResources(
228 long folderId, String[] communityPermissions,
229 String[] guestPermissions)
230 throws PortalException, SystemException {
231
232 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
233
234 addFolderResources(folder, communityPermissions, guestPermissions);
235 }
236
237 public void addFolderResources(
238 DLFolder folder, String[] communityPermissions,
239 String[] guestPermissions)
240 throws PortalException, SystemException {
241
242 resourceLocalService.addModelResources(
243 folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
244 DLFolder.class.getName(), folder.getFolderId(),
245 communityPermissions, guestPermissions);
246 }
247
248 public void deleteFolder(long folderId)
249 throws PortalException, SystemException {
250
251 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
252
253 deleteFolder(folder);
254 }
255
256 public void deleteFolder(DLFolder folder)
257 throws PortalException, SystemException {
258
259
261 List<DLFolder> folders = dlFolderPersistence.findByG_P(
262 folder.getGroupId(), folder.getFolderId());
263
264 for (DLFolder curFolder : folders) {
265 deleteFolder(curFolder);
266 }
267
268
270 dlFileEntryLocalService.deleteFileEntries(folder.getFolderId());
271
272
274 webDAVPropsLocalService.deleteWebDAVProps(
275 DLFolder.class.getName(), folder.getPrimaryKey());
276
277
279 resourceLocalService.deleteResource(
280 folder.getCompanyId(), DLFolder.class.getName(),
281 ResourceConstants.SCOPE_INDIVIDUAL, folder.getFolderId());
282
283
285 dlFolderPersistence.remove(folder.getFolderId());
286 }
287
288 public void deleteFolders(long groupId)
289 throws PortalException, SystemException {
290
291 List<DLFolder> folders = dlFolderPersistence.findByG_P(
292 groupId, DLFolderImpl.DEFAULT_PARENT_FOLDER_ID);
293
294 for (DLFolder folder : folders) {
295 deleteFolder(folder);
296 }
297 }
298
299 public DLFolder getFolder(long folderId)
300 throws PortalException, SystemException {
301
302 return dlFolderPersistence.findByPrimaryKey(folderId);
303 }
304
305 public DLFolder getFolder(long groupId, long parentFolderId, String name)
306 throws PortalException, SystemException {
307
308 return dlFolderPersistence.findByG_P_N(groupId, parentFolderId, name);
309 }
310
311 public List<DLFolder> getFolders(long companyId) throws SystemException {
312 return dlFolderPersistence.findByCompanyId(companyId);
313 }
314
315 public List<DLFolder> getFolders(long groupId, long parentFolderId)
316 throws SystemException {
317
318 return dlFolderPersistence.findByG_P(groupId, parentFolderId);
319 }
320
321 public List<DLFolder> getFolders(
322 long groupId, long parentFolderId, int start, int end)
323 throws SystemException {
324
325 return dlFolderPersistence.findByG_P(
326 groupId, parentFolderId, start, end);
327 }
328
329 public int getFoldersCount(long groupId, long parentFolderId)
330 throws SystemException {
331
332 return dlFolderPersistence.countByG_P(groupId, parentFolderId);
333 }
334
335 public void getSubfolderIds(
336 List<Long> folderIds, long groupId, long folderId)
337 throws SystemException {
338
339 List<DLFolder> folders = dlFolderPersistence.findByG_P(
340 groupId, folderId);
341
342 for (DLFolder folder : folders) {
343 folderIds.add(folder.getFolderId());
344
345 getSubfolderIds(
346 folderIds, folder.getGroupId(), folder.getFolderId());
347 }
348 }
349
350 public void reIndex(String[] ids) throws SystemException {
351 long companyId = GetterUtil.getLong(ids[0]);
352
353 try {
354 List<DLFolder> folders = getFolders(companyId);
355
356 for (DLFolder folder : folders) {
357 String portletId = PortletKeys.DOCUMENT_LIBRARY;
358 long groupId = folder.getGroupId();
359 long folderId = folder.getFolderId();
360
361 String[] newIds = {
362 String.valueOf(companyId), portletId,
363 String.valueOf(groupId), String.valueOf(folderId)
364 };
365
366 dlService.reIndex(newIds);
367 }
368 }
369 catch (SystemException se) {
370 throw se;
371 }
372 catch (Exception e) {
373 throw new SystemException(e);
374 }
375 }
376
377 public Hits search(
378 long companyId, long groupId, long[] folderIds, String keywords,
379 int start, int end)
380 throws SystemException {
381
382 return dlLocalService.search(
383 companyId, PortletKeys.DOCUMENT_LIBRARY, groupId, folderIds,
384 keywords, start, end);
385 }
386
387 public DLFolder updateFolder(
388 long folderId, long parentFolderId, String name,
389 String description)
390 throws PortalException, SystemException {
391
392 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
393
394 parentFolderId = getParentFolderId(folder, parentFolderId);
395
396 validate(
397 folder.getFolderId(), folder.getGroupId(), parentFolderId, name);
398
399 folder.setModifiedDate(new Date());
400 folder.setParentFolderId(parentFolderId);
401 folder.setName(name);
402 folder.setDescription(description);
403
404 dlFolderPersistence.update(folder, false);
405
406 if (PropsValues.DL_LAYOUTS_SYNC_ENABLED) {
407 String privateFolder = GetterUtil.getString(PropsUtil.get(
408 PropsKeys.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
409
410 boolean privateLayout = false;
411
412 String[] path = folder.getPathArray();
413
414 if (path[0].equals(privateFolder)) {
415 privateLayout = true;
416 }
417
418 Layout layout = layoutLocalService.getDLFolderLayout(
419 folder.getFolderId());
420
421 layout.setName(folder.getName());
422
423 layoutLocalService.updateName(
424 folder.getGroupId(), privateLayout, layout.getLayoutId(),
425 folder.getName(),
426 LocaleUtil.toLanguageId(LocaleUtil.getDefault()));
427 }
428
429 return folder;
430 }
431
432 protected long getParentFolderId(long groupId, long parentFolderId)
433 throws SystemException {
434
435 if (parentFolderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
436 DLFolder parentFolder = dlFolderPersistence.fetchByPrimaryKey(
437 parentFolderId);
438
439 if ((parentFolder == null) ||
440 (groupId != parentFolder.getGroupId())) {
441
442 parentFolderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
443 }
444 }
445
446 return parentFolderId;
447 }
448
449 protected long getParentFolderId(DLFolder folder, long parentFolderId)
450 throws SystemException {
451
452 if (parentFolderId == DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
453 return parentFolderId;
454 }
455
456 if (folder.getFolderId() == parentFolderId) {
457 return folder.getParentFolderId();
458 }
459 else {
460 DLFolder parentFolder = dlFolderPersistence.fetchByPrimaryKey(
461 parentFolderId);
462
463 if ((parentFolder == null) ||
464 (folder.getGroupId() != parentFolder.getGroupId())) {
465
466 return folder.getParentFolderId();
467 }
468
469 List<Long> subfolderIds = new ArrayList<Long>();
470
471 getSubfolderIds(
472 subfolderIds, folder.getGroupId(), folder.getFolderId());
473
474 if (subfolderIds.contains(parentFolderId)) {
475 return folder.getParentFolderId();
476 }
477
478 return parentFolderId;
479 }
480 }
481
482 protected void validate(long groupId, long parentFolderId, String name)
483 throws PortalException, SystemException {
484
485 long folderId = 0;
486
487 validate(folderId, groupId, parentFolderId, name);
488 }
489
490 protected void validate(
491 long folderId, long groupId, long parentFolderId, String name)
492 throws PortalException, SystemException {
493
494 if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1) ||
495 (name.indexOf("//") != -1)) {
496
497 throw new FolderNameException();
498 }
499
500 try {
501 dlFileEntryLocalService.getFileEntryByTitle(parentFolderId, name);
502
503 throw new DuplicateFileException();
504 }
505 catch (NoSuchFileEntryException nsfee) {
506 }
507
508 DLFolder folder = dlFolderPersistence.fetchByG_P_N(
509 groupId, parentFolderId, name);
510
511 if ((folder != null) && (folder.getFolderId() != folderId)) {
512 throw new DuplicateFolderNameException();
513 }
514 }
515
516 }