1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.documentlibrary.service.impl;
16  
17  import com.liferay.documentlibrary.DuplicateFileException;
18  import com.liferay.documentlibrary.NoSuchDirectoryException;
19  import com.liferay.portal.NoSuchLayoutException;
20  import com.liferay.portal.PortalException;
21  import com.liferay.portal.SystemException;
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.search.Hits;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.LocaleUtil;
27  import com.liferay.portal.kernel.util.PropsKeys;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.model.LayoutConstants;
31  import com.liferay.portal.model.ResourceConstants;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.service.ServiceContext;
34  import com.liferay.portal.util.PortletKeys;
35  import com.liferay.portal.util.PropsUtil;
36  import com.liferay.portal.util.PropsValues;
37  import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
38  import com.liferay.portlet.documentlibrary.FolderNameException;
39  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
40  import com.liferay.portlet.documentlibrary.model.DLFolder;
41  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
42  import com.liferay.portlet.documentlibrary.service.base.DLFolderLocalServiceBaseImpl;
43  import com.liferay.portlet.tags.util.TagsUtil;
44  
45  import java.util.ArrayList;
46  import java.util.Date;
47  import java.util.List;
48  
49  /**
50   * <a href="DLFolderLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   */
54  public class DLFolderLocalServiceImpl extends DLFolderLocalServiceBaseImpl {
55  
56      public DLFolder addFolder(
57              long userId, long groupId, long parentFolderId, String name,
58              String description, ServiceContext serviceContext)
59          throws PortalException, SystemException {
60  
61          return addFolder(
62              null, userId, groupId, parentFolderId, name, description,
63              serviceContext);
64      }
65  
66      public DLFolder addFolder(
67              String uuid, long userId, long groupId, long parentFolderId,
68              String name, String description, ServiceContext serviceContext)
69          throws PortalException, SystemException {
70  
71          // Folder
72  
73          User user = userPersistence.findByPrimaryKey(userId);
74          parentFolderId = getParentFolderId(groupId, parentFolderId);
75          Date now = new Date();
76  
77          validate(groupId, parentFolderId, name);
78  
79          long folderId = counterLocalService.increment();
80  
81          DLFolder folder = dlFolderPersistence.create(folderId);
82  
83          folder.setUuid(uuid);
84          folder.setGroupId(groupId);
85          folder.setCompanyId(user.getCompanyId());
86          folder.setUserId(user.getUserId());
87          folder.setCreateDate(serviceContext.getCreateDate(now));
88          folder.setModifiedDate(serviceContext.getModifiedDate(now));
89          folder.setParentFolderId(parentFolderId);
90          folder.setName(name);
91          folder.setDescription(description);
92          folder.setExpandoBridgeAttributes(serviceContext);
93  
94          dlFolderPersistence.update(folder, false);
95  
96          // Resources
97  
98          if (serviceContext.getAddCommunityPermissions() ||
99              serviceContext.getAddGuestPermissions()) {
100 
101             addFolderResources(
102                 folder, serviceContext.getAddCommunityPermissions(),
103                 serviceContext.getAddGuestPermissions());
104         }
105         else {
106             addFolderResources(
107                 folder, serviceContext.getCommunityPermissions(),
108                 serviceContext.getGuestPermissions());
109         }
110 
111         // Layout
112 
113         if (PropsValues.DL_LAYOUTS_SYNC_ENABLED &&
114             (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
115 
116             String[] pathArray = folder.getPathArray();
117 
118             String layoutsSyncPrivateFolder = GetterUtil.getString(
119                 PropsUtil.get(PropsKeys.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
120             String layoutsSyncPublicFolder = GetterUtil.getString(
121                 PropsUtil.get(PropsKeys.DL_LAYOUTS_SYNC_PUBLIC_FOLDER));
122 
123             if (pathArray[0].equals(layoutsSyncPrivateFolder) ||
124                 pathArray[0].equals(layoutsSyncPublicFolder)) {
125 
126                 boolean privateLayout = true;
127 
128                 if (pathArray[0].equals(layoutsSyncPublicFolder)) {
129                     privateLayout = false;
130                 }
131 
132                 long parentLayoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
133                 String title = StringPool.BLANK;
134                 String layoutDescription = StringPool.BLANK;
135                 String type = LayoutConstants.TYPE_PORTLET;
136                 boolean hidden = false;
137                 String friendlyURL = StringPool.BLANK;
138 
139                 Layout dlFolderLayout = null;
140 
141                 try {
142                     dlFolderLayout = layoutLocalService.getDLFolderLayout(
143                         folder.getParentFolderId());
144 
145                     parentLayoutId = dlFolderLayout.getLayoutId();
146                 }
147                 catch (NoSuchLayoutException nsle) {
148                 }
149 
150                 layoutLocalService.addLayout(
151                     userId, groupId, privateLayout, parentLayoutId, name, title,
152                     layoutDescription, type, hidden, friendlyURL,
153                     folder.getFolderId());
154             }
155         }
156 
157         // Parent folder
158 
159         if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
160             DLFolder parentFolder = dlFolderPersistence.findByPrimaryKey(
161                 parentFolderId);
162 
163             parentFolder.setLastPostDate(now);
164 
165             dlFolderPersistence.update(parentFolder, false);
166         }
167 
168         return folder;
169     }
170 
171     public void addFolderResources(
172             long folderId, boolean addCommunityPermissions,
173             boolean addGuestPermissions)
174         throws PortalException, SystemException {
175 
176         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
177 
178         addFolderResources(
179             folder, addCommunityPermissions, addGuestPermissions);
180     }
181 
182     public void addFolderResources(
183             DLFolder folder, boolean addCommunityPermissions,
184             boolean addGuestPermissions)
185         throws PortalException, SystemException {
186 
187         resourceLocalService.addResources(
188             folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
189             DLFolder.class.getName(), folder.getFolderId(), false,
190             addCommunityPermissions, addGuestPermissions);
191     }
192 
193     public void addFolderResources(
194             long folderId, String[] communityPermissions,
195             String[] guestPermissions)
196         throws PortalException, SystemException {
197 
198         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
199 
200         addFolderResources(folder, communityPermissions, guestPermissions);
201     }
202 
203     public void addFolderResources(
204             DLFolder folder, String[] communityPermissions,
205             String[] guestPermissions)
206         throws PortalException, SystemException {
207 
208         resourceLocalService.addModelResources(
209             folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
210             DLFolder.class.getName(), folder.getFolderId(),
211             communityPermissions, guestPermissions);
212     }
213 
214     public void deleteFolder(long folderId)
215         throws PortalException, SystemException {
216 
217         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
218 
219         deleteFolder(folder);
220     }
221 
222     public void deleteFolder(DLFolder folder)
223         throws PortalException, SystemException {
224 
225         // Folders
226 
227         List<DLFolder> folders = dlFolderPersistence.findByG_P(
228             folder.getGroupId(), folder.getFolderId());
229 
230         for (DLFolder curFolder : folders) {
231             deleteFolder(curFolder);
232         }
233 
234         // Folder
235 
236         dlFolderPersistence.remove(folder);
237 
238         // Resources
239 
240         resourceLocalService.deleteResource(
241             folder.getCompanyId(), DLFolder.class.getName(),
242             ResourceConstants.SCOPE_INDIVIDUAL, folder.getFolderId());
243 
244         // WebDAVProps
245 
246         webDAVPropsLocalService.deleteWebDAVProps(
247             DLFolder.class.getName(), folder.getPrimaryKey());
248 
249         // File entries
250 
251         dlFileEntryLocalService.deleteFileEntries(folder.getFolderId());
252 
253         // Expando
254 
255         expandoValueLocalService.deleteValues(
256             DLFolder.class.getName(), folder.getFolderId());
257 
258         // Directory
259 
260         try {
261             dlService.deleteDirectory(
262                 folder.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
263                 folder.getFolderId(), StringPool.BLANK);
264         }
265         catch (NoSuchDirectoryException nsde) {
266             if (_log.isDebugEnabled()) {
267                 _log.debug(nsde.getMessage());
268             }
269         }
270     }
271 
272     public void deleteFolders(long groupId)
273         throws PortalException, SystemException {
274 
275         List<DLFolder> folders = dlFolderPersistence.findByG_P(
276             groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
277 
278         for (DLFolder folder : folders) {
279             deleteFolder(folder);
280         }
281     }
282 
283     public List<Object> getFileEntriesAndFileShortcuts(
284             long folderId, int start, int end)
285         throws SystemException {
286 
287         List<Long> folderIds = new ArrayList<Long>();
288 
289         folderIds.add(folderId);
290 
291         return dlFolderFinder.findFE_FS_ByFolderIds(folderIds, start, end);
292     }
293 
294     public List<Object> getFileEntriesAndFileShortcuts(
295             List<Long> folderIds, int start, int end)
296         throws SystemException {
297 
298         return dlFolderFinder.findFE_FS_ByFolderIds(folderIds, start, end);
299     }
300 
301     public int getFileEntriesAndFileShortcutsCount(long folderId)
302         throws SystemException {
303 
304         List<Long> folderIds = new ArrayList<Long>();
305 
306         folderIds.add(folderId);
307 
308         return dlFolderFinder.countFE_FS_ByFolderIds(folderIds);
309     }
310 
311     public int getFileEntriesAndFileShortcutsCount(List<Long> folderIds)
312         throws SystemException {
313 
314         return dlFolderFinder.countFE_FS_ByFolderIds(folderIds);
315     }
316 
317     public DLFolder getFolder(long folderId)
318         throws PortalException, SystemException {
319 
320         return dlFolderPersistence.findByPrimaryKey(folderId);
321     }
322 
323     public DLFolder getFolder(long groupId, long parentFolderId, String name)
324         throws PortalException, SystemException {
325 
326         return dlFolderPersistence.findByG_P_N(groupId, parentFolderId, name);
327     }
328 
329     public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
330             long folderId, int start, int end)
331         throws SystemException {
332 
333         List<Long> folderIds = new ArrayList<Long>();
334 
335         folderIds.add(folderId);
336 
337         return dlFolderFinder.findF_FE_FS_ByFolderIds(folderIds, start, end);
338     }
339 
340     public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
341             List<Long> folderIds, int start, int end)
342         throws SystemException {
343 
344         return dlFolderFinder.findF_FE_FS_ByFolderIds(folderIds, start, end);
345     }
346 
347     public int getFoldersAndFileEntriesAndFileShortcutsCount(long folderId)
348         throws SystemException {
349 
350         List<Long> folderIds = new ArrayList<Long>();
351 
352         folderIds.add(folderId);
353 
354         return dlFolderFinder.countF_FE_FS_ByFolderIds(folderIds);
355     }
356 
357     public int getFoldersAndFileEntriesAndFileShortcutsCount(
358             List<Long> folderIds)
359         throws SystemException {
360 
361         return dlFolderFinder.countF_FE_FS_ByFolderIds(folderIds);
362     }
363 
364     public List<DLFolder> getFolders(long companyId) throws SystemException {
365         return dlFolderPersistence.findByCompanyId(companyId);
366     }
367 
368     public List<DLFolder> getFolders(long groupId, long parentFolderId)
369         throws SystemException {
370 
371         return dlFolderPersistence.findByG_P(groupId, parentFolderId);
372     }
373 
374     public List<DLFolder> getFolders(
375             long groupId, long parentFolderId, int start, int end)
376         throws SystemException {
377 
378         return dlFolderPersistence.findByG_P(
379             groupId, parentFolderId, start, end);
380     }
381 
382     public int getFoldersCount(long groupId, long parentFolderId)
383         throws SystemException {
384 
385         return dlFolderPersistence.countByG_P(groupId, parentFolderId);
386     }
387 
388     public void getSubfolderIds(
389             List<Long> folderIds, long groupId, long folderId)
390         throws SystemException {
391 
392         List<DLFolder> folders = dlFolderPersistence.findByG_P(
393             groupId, folderId);
394 
395         for (DLFolder folder : folders) {
396             folderIds.add(folder.getFolderId());
397 
398             getSubfolderIds(
399                 folderIds, folder.getGroupId(), folder.getFolderId());
400         }
401     }
402 
403     public void reIndex(String[] ids) throws SystemException {
404         long companyId = GetterUtil.getLong(ids[0]);
405 
406         try {
407             List<DLFolder> folders = getFolders(companyId);
408 
409             for (DLFolder folder : folders) {
410                 String portletId = PortletKeys.DOCUMENT_LIBRARY;
411                 long groupId = folder.getGroupId();
412                 long folderId = folder.getFolderId();
413 
414                 String[] newIds = {
415                     String.valueOf(companyId), portletId,
416                     String.valueOf(groupId), String.valueOf(folderId)
417                 };
418 
419                 dlService.reIndex(newIds);
420             }
421         }
422         catch (SystemException se) {
423             throw se;
424         }
425         catch (Exception e) {
426             throw new SystemException(e);
427         }
428     }
429 
430     public Hits search(
431             long companyId, long groupId, long userId, long[] folderIds,
432             String keywords, int start, int end)
433         throws SystemException {
434 
435         return dlLocalService.search(
436             companyId, PortletKeys.DOCUMENT_LIBRARY, groupId, userId, folderIds,
437             keywords, start, end);
438     }
439 
440     public DLFolder updateFolder(
441             long folderId, long parentFolderId, String name,
442             String description, ServiceContext serviceContext)
443         throws PortalException, SystemException {
444 
445         // Folder
446 
447         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
448 
449         parentFolderId = getParentFolderId(folder, parentFolderId);
450 
451         validate(
452             folder.getFolderId(), folder.getGroupId(), parentFolderId, name);
453 
454         folder.setModifiedDate(serviceContext.getModifiedDate(new Date()));
455         folder.setParentFolderId(parentFolderId);
456         folder.setName(name);
457         folder.setDescription(description);
458         folder.setExpandoBridgeAttributes(serviceContext);
459 
460         dlFolderPersistence.update(folder, false);
461 
462         // Layout
463 
464         if (PropsValues.DL_LAYOUTS_SYNC_ENABLED) {
465             String privateFolder = GetterUtil.getString(PropsUtil.get(
466                 PropsKeys.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
467 
468             boolean privateLayout = false;
469 
470             String[] path = folder.getPathArray();
471 
472             if (path[0].equals(privateFolder)) {
473                 privateLayout = true;
474             }
475 
476             Layout layout = layoutLocalService.getDLFolderLayout(
477                 folder.getFolderId());
478 
479             layout.setName(folder.getName());
480 
481             layoutLocalService.updateName(
482                 folder.getGroupId(), privateLayout, layout.getLayoutId(),
483                 folder.getName(),
484                 LocaleUtil.toLanguageId(LocaleUtil.getDefault()));
485         }
486 
487         return folder;
488     }
489 
490     protected long getParentFolderId(long groupId, long parentFolderId)
491         throws SystemException {
492 
493         if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
494             DLFolder parentFolder = dlFolderPersistence.fetchByPrimaryKey(
495                 parentFolderId);
496 
497             if ((parentFolder == null) ||
498                 (groupId != parentFolder.getGroupId())) {
499 
500                 parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
501             }
502         }
503 
504         return parentFolderId;
505     }
506 
507     protected long getParentFolderId(DLFolder folder, long parentFolderId)
508         throws SystemException {
509 
510         if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
511             return parentFolderId;
512         }
513 
514         if (folder.getFolderId() == parentFolderId) {
515             return folder.getParentFolderId();
516         }
517         else {
518             DLFolder parentFolder = dlFolderPersistence.fetchByPrimaryKey(
519                 parentFolderId);
520 
521             if ((parentFolder == null) ||
522                 (folder.getGroupId() != parentFolder.getGroupId())) {
523 
524                 return folder.getParentFolderId();
525             }
526 
527             List<Long> subfolderIds = new ArrayList<Long>();
528 
529             getSubfolderIds(
530                 subfolderIds, folder.getGroupId(), folder.getFolderId());
531 
532             if (subfolderIds.contains(parentFolderId)) {
533                 return folder.getParentFolderId();
534             }
535 
536             return parentFolderId;
537         }
538     }
539 
540     protected void validate(long groupId, long parentFolderId, String name)
541         throws PortalException, SystemException {
542 
543         long folderId = 0;
544 
545         validate(folderId, groupId, parentFolderId, name);
546     }
547 
548     protected void validate(
549             long folderId, long groupId, long parentFolderId, String name)
550         throws PortalException, SystemException {
551 
552         if (!TagsUtil.isValidWord(name)) {
553             throw new FolderNameException();
554         }
555 
556         try {
557             dlFileEntryLocalService.getFileEntryByTitle(parentFolderId, name);
558 
559             throw new DuplicateFileException();
560         }
561         catch (NoSuchFileEntryException nsfee) {
562         }
563 
564         DLFolder folder = dlFolderPersistence.fetchByG_P_N(
565             groupId, parentFolderId, name);
566 
567         if ((folder != null) && (folder.getFolderId() != folderId)) {
568             throw new DuplicateFolderNameException();
569         }
570     }
571 
572     private static Log _log = LogFactoryUtil.getLog(
573         DLFolderLocalServiceImpl.class);
574 
575 }