001
014
015 package com.liferay.portlet.documentlibrary.service.impl;
016
017 import com.liferay.documentlibrary.DuplicateFileException;
018 import com.liferay.documentlibrary.NoSuchDirectoryException;
019 import com.liferay.portal.NoSuchLayoutException;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.exception.SystemException;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.LocaleUtil;
026 import com.liferay.portal.kernel.util.PropsKeys;
027 import com.liferay.portal.kernel.util.StringPool;
028 import com.liferay.portal.model.Layout;
029 import com.liferay.portal.model.LayoutConstants;
030 import com.liferay.portal.model.ResourceConstants;
031 import com.liferay.portal.model.User;
032 import com.liferay.portal.service.ServiceContext;
033 import com.liferay.portal.util.PortletKeys;
034 import com.liferay.portal.util.PropsUtil;
035 import com.liferay.portal.util.PropsValues;
036 import com.liferay.portlet.asset.util.AssetUtil;
037 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
038 import com.liferay.portlet.documentlibrary.FolderNameException;
039 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
040 import com.liferay.portlet.documentlibrary.model.DLFolder;
041 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
042 import com.liferay.portlet.documentlibrary.service.base.DLFolderLocalServiceBaseImpl;
043
044 import java.util.ArrayList;
045 import java.util.Date;
046 import java.util.List;
047
048
051 public class DLFolderLocalServiceImpl extends DLFolderLocalServiceBaseImpl {
052
053 public DLFolder addFolder(
054 long userId, long groupId, long parentFolderId, String name,
055 String description, ServiceContext serviceContext)
056 throws PortalException, SystemException {
057
058
059
060 User user = userPersistence.findByPrimaryKey(userId);
061 parentFolderId = getParentFolderId(groupId, parentFolderId);
062 Date now = new Date();
063
064 validate(groupId, parentFolderId, name);
065
066 long folderId = counterLocalService.increment();
067
068 DLFolder folder = dlFolderPersistence.create(folderId);
069
070 folder.setUuid(serviceContext.getUuid());
071 folder.setGroupId(groupId);
072 folder.setCompanyId(user.getCompanyId());
073 folder.setUserId(user.getUserId());
074 folder.setCreateDate(serviceContext.getCreateDate(now));
075 folder.setModifiedDate(serviceContext.getModifiedDate(now));
076 folder.setParentFolderId(parentFolderId);
077 folder.setName(name);
078 folder.setDescription(description);
079 folder.setExpandoBridgeAttributes(serviceContext);
080
081 dlFolderPersistence.update(folder, false);
082
083
084
085 if (serviceContext.getAddCommunityPermissions() ||
086 serviceContext.getAddGuestPermissions()) {
087
088 addFolderResources(
089 folder, serviceContext.getAddCommunityPermissions(),
090 serviceContext.getAddGuestPermissions());
091 }
092 else {
093 addFolderResources(
094 folder, serviceContext.getCommunityPermissions(),
095 serviceContext.getGuestPermissions());
096 }
097
098
099
100 if (PropsValues.DL_LAYOUTS_SYNC_ENABLED &&
101 (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
102
103 String[] pathArray = folder.getPathArray();
104
105 String layoutsSyncPrivateFolder = GetterUtil.getString(
106 PropsUtil.get(PropsKeys.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
107 String layoutsSyncPublicFolder = GetterUtil.getString(
108 PropsUtil.get(PropsKeys.DL_LAYOUTS_SYNC_PUBLIC_FOLDER));
109
110 if (pathArray[0].equals(layoutsSyncPrivateFolder) ||
111 pathArray[0].equals(layoutsSyncPublicFolder)) {
112
113 boolean privateLayout = true;
114
115 if (pathArray[0].equals(layoutsSyncPublicFolder)) {
116 privateLayout = false;
117 }
118
119 long parentLayoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
120 String title = StringPool.BLANK;
121 String layoutDescription = StringPool.BLANK;
122 String type = LayoutConstants.TYPE_PORTLET;
123 boolean hidden = false;
124 String friendlyURL = StringPool.BLANK;
125
126 Layout dlFolderLayout = null;
127
128 try {
129 dlFolderLayout = layoutLocalService.getDLFolderLayout(
130 folder.getParentFolderId());
131
132 parentLayoutId = dlFolderLayout.getLayoutId();
133 }
134 catch (NoSuchLayoutException nsle) {
135 }
136
137 layoutLocalService.addLayout(
138 userId, groupId, privateLayout, parentLayoutId, name, title,
139 layoutDescription, type, hidden, friendlyURL,
140 folder.getFolderId(), new ServiceContext());
141 }
142 }
143
144
145
146 if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
147 DLFolder parentFolder = dlFolderPersistence.findByPrimaryKey(
148 parentFolderId);
149
150 parentFolder.setLastPostDate(now);
151
152 dlFolderPersistence.update(parentFolder, false);
153 }
154
155 return folder;
156 }
157
158 public void addFolderResources(
159 DLFolder folder, boolean addCommunityPermissions,
160 boolean addGuestPermissions)
161 throws PortalException, SystemException {
162
163 resourceLocalService.addResources(
164 folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
165 DLFolder.class.getName(), folder.getFolderId(), false,
166 addCommunityPermissions, addGuestPermissions);
167 }
168
169 public void addFolderResources(
170 DLFolder folder, String[] communityPermissions,
171 String[] guestPermissions)
172 throws PortalException, SystemException {
173
174 resourceLocalService.addModelResources(
175 folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
176 DLFolder.class.getName(), folder.getFolderId(),
177 communityPermissions, guestPermissions);
178 }
179
180 public void addFolderResources(
181 long folderId, boolean addCommunityPermissions,
182 boolean addGuestPermissions)
183 throws PortalException, SystemException {
184
185 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
186
187 addFolderResources(
188 folder, addCommunityPermissions, addGuestPermissions);
189 }
190
191 public void addFolderResources(
192 long folderId, String[] communityPermissions,
193 String[] guestPermissions)
194 throws PortalException, SystemException {
195
196 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
197
198 addFolderResources(folder, communityPermissions, guestPermissions);
199 }
200
201 public void deleteFolder(DLFolder folder)
202 throws PortalException, SystemException {
203
204
205
206 List<DLFolder> folders = dlFolderPersistence.findByG_P(
207 folder.getGroupId(), folder.getFolderId());
208
209 for (DLFolder curFolder : folders) {
210 deleteFolder(curFolder);
211 }
212
213
214
215 dlFolderPersistence.remove(folder);
216
217
218
219 resourceLocalService.deleteResource(
220 folder.getCompanyId(), DLFolder.class.getName(),
221 ResourceConstants.SCOPE_INDIVIDUAL, folder.getFolderId());
222
223
224
225 webDAVPropsLocalService.deleteWebDAVProps(
226 DLFolder.class.getName(), folder.getFolderId());
227
228
229
230 dlFileEntryLocalService.deleteFileEntries(
231 folder.getGroupId(), folder.getFolderId());
232
233
234
235 expandoValueLocalService.deleteValues(
236 DLFolder.class.getName(), folder.getFolderId());
237
238
239
240 try {
241 dlService.deleteDirectory(
242 folder.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
243 folder.getFolderId(), StringPool.BLANK);
244 }
245 catch (NoSuchDirectoryException nsde) {
246 if (_log.isDebugEnabled()) {
247 _log.debug(nsde.getMessage());
248 }
249 }
250 }
251
252 public void deleteFolder(long folderId)
253 throws PortalException, SystemException {
254
255 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
256
257 deleteFolder(folder);
258 }
259
260 public void deleteFolders(long groupId)
261 throws PortalException, SystemException {
262
263 List<DLFolder> folders = dlFolderPersistence.findByG_P(
264 groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
265
266 for (DLFolder folder : folders) {
267 deleteFolder(folder);
268 }
269 }
270
271 public List<DLFolder> getCompanyFolders(long companyId, int start, int end)
272 throws SystemException {
273
274 return dlFolderPersistence.findByCompanyId(companyId, start, end);
275 }
276
277 public int getCompanyFoldersCount(long companyId) throws SystemException {
278 return dlFolderPersistence.countByCompanyId(companyId);
279 }
280
281 public List<Object> getFileEntriesAndFileShortcuts(
282 long groupId, List<Long> folderIds, int status, int start, int end)
283 throws SystemException {
284
285 return dlFolderFinder.findFE_FS_ByG_F_S(
286 groupId, folderIds, status, start, end);
287 }
288
289 public List<Object> getFileEntriesAndFileShortcuts(
290 long groupId, long folderId, int status, int start, int end)
291 throws SystemException {
292
293 List<Long> folderIds = new ArrayList<Long>();
294
295 folderIds.add(folderId);
296
297 return dlFolderFinder.findFE_FS_ByG_F_S(
298 groupId, folderIds, status, start, end);
299 }
300
301 public int getFileEntriesAndFileShortcutsCount(
302 long groupId, List<Long> folderIds, int status)
303 throws SystemException {
304
305 return dlFolderFinder.countFE_FS_ByG_F_S(groupId, folderIds, status);
306 }
307
308 public int getFileEntriesAndFileShortcutsCount(
309 long groupId, long folderId, int status)
310 throws SystemException {
311
312 List<Long> folderIds = new ArrayList<Long>();
313
314 folderIds.add(folderId);
315
316 return dlFolderFinder.countFE_FS_ByG_F_S(groupId, folderIds, status);
317 }
318
319 public DLFolder getFolder(long folderId)
320 throws PortalException, SystemException {
321
322 return dlFolderPersistence.findByPrimaryKey(folderId);
323 }
324
325 public DLFolder getFolder(long groupId, long parentFolderId, String name)
326 throws PortalException, SystemException {
327
328 return dlFolderPersistence.findByG_P_N(groupId, parentFolderId, name);
329 }
330
331 public List<DLFolder> getFolders(long companyId) throws SystemException {
332 return dlFolderPersistence.findByCompanyId(companyId);
333 }
334
335 public List<DLFolder> getFolders(long groupId, long parentFolderId)
336 throws SystemException {
337
338 return dlFolderPersistence.findByG_P(groupId, parentFolderId);
339 }
340
341 public List<DLFolder> getFolders(
342 long groupId, long parentFolderId, int start, int end)
343 throws SystemException {
344
345 return dlFolderPersistence.findByG_P(
346 groupId, parentFolderId, start, end);
347 }
348
349 public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
350 long groupId, List<Long> folderIds, int status, int start, int end)
351 throws SystemException {
352
353 return dlFolderFinder.findF_FE_FS_ByG_F_S(
354 groupId, folderIds, status, start, end);
355 }
356
357 public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
358 long groupId, long folderId, int status, int start, int end)
359 throws SystemException {
360
361 List<Long> folderIds = new ArrayList<Long>();
362
363 folderIds.add(folderId);
364
365 return getFoldersAndFileEntriesAndFileShortcuts(
366 groupId, folderIds, status, start, end);
367 }
368
369 public int getFoldersAndFileEntriesAndFileShortcutsCount(
370 long groupId, List<Long> folderIds, int status)
371 throws SystemException {
372
373 return dlFolderFinder.countF_FE_FS_ByG_F_S(groupId, folderIds, status);
374 }
375
376 public int getFoldersAndFileEntriesAndFileShortcutsCount(
377 long groupId, long folderId, int status)
378 throws SystemException {
379
380 List<Long> folderIds = new ArrayList<Long>();
381
382 folderIds.add(folderId);
383
384 return getFoldersAndFileEntriesAndFileShortcutsCount(
385 groupId, folderIds, status);
386 }
387
388 public int getFoldersCount(long groupId, long parentFolderId)
389 throws SystemException {
390
391 return dlFolderPersistence.countByG_P(groupId, parentFolderId);
392 }
393
394 public void getSubfolderIds(
395 List<Long> folderIds, long groupId, long folderId)
396 throws SystemException {
397
398 List<DLFolder> folders = dlFolderPersistence.findByG_P(
399 groupId, folderId);
400
401 for (DLFolder folder : folders) {
402 folderIds.add(folder.getFolderId());
403
404 getSubfolderIds(
405 folderIds, folder.getGroupId(), folder.getFolderId());
406 }
407 }
408
409 public DLFolder updateFolder(
410 long folderId, long parentFolderId, String name,
411 String description, ServiceContext serviceContext)
412 throws PortalException, SystemException {
413
414
415
416 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
417
418 parentFolderId = getParentFolderId(folder, parentFolderId);
419
420 validate(
421 folder.getFolderId(), folder.getGroupId(), parentFolderId, name);
422
423 folder.setModifiedDate(serviceContext.getModifiedDate(null));
424 folder.setParentFolderId(parentFolderId);
425 folder.setName(name);
426 folder.setDescription(description);
427 folder.setExpandoBridgeAttributes(serviceContext);
428
429 dlFolderPersistence.update(folder, false);
430
431
432
433 if (PropsValues.DL_LAYOUTS_SYNC_ENABLED) {
434 String privateFolder = GetterUtil.getString(PropsUtil.get(
435 PropsKeys.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
436
437 boolean privateLayout = false;
438
439 String[] path = folder.getPathArray();
440
441 if (path[0].equals(privateFolder)) {
442 privateLayout = true;
443 }
444
445 Layout layout = layoutLocalService.getDLFolderLayout(
446 folder.getFolderId());
447
448 layout.setName(folder.getName());
449
450 layoutLocalService.updateName(
451 folder.getGroupId(), privateLayout, layout.getLayoutId(),
452 folder.getName(),
453 LocaleUtil.toLanguageId(LocaleUtil.getDefault()));
454 }
455
456 return folder;
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 long getParentFolderId(long groupId, long parentFolderId)
493 throws SystemException {
494
495 if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
496 DLFolder parentFolder = dlFolderPersistence.fetchByPrimaryKey(
497 parentFolderId);
498
499 if ((parentFolder == null) ||
500 (groupId != parentFolder.getGroupId())) {
501
502 parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
503 }
504 }
505
506 return parentFolderId;
507 }
508
509 protected void validate(
510 long folderId, long groupId, long parentFolderId, String name)
511 throws PortalException, SystemException {
512
513 if (!AssetUtil.isValidWord(name)) {
514 throw new FolderNameException();
515 }
516
517 try {
518 dlFileEntryLocalService.getFileEntryByTitle(
519 groupId, parentFolderId, name);
520
521 throw new DuplicateFileException();
522 }
523 catch (NoSuchFileEntryException nsfee) {
524 }
525
526 DLFolder folder = dlFolderPersistence.fetchByG_P_N(
527 groupId, parentFolderId, name);
528
529 if ((folder != null) && (folder.getFolderId() != folderId)) {
530 throw new DuplicateFolderNameException();
531 }
532 }
533
534 protected void validate(long groupId, long parentFolderId, String name)
535 throws PortalException, SystemException {
536
537 long folderId = 0;
538
539 validate(folderId, groupId, parentFolderId, name);
540 }
541
542 private static Log _log = LogFactoryUtil.getLog(
543 DLFolderLocalServiceImpl.class);
544
545 }