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