1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.bookmarks.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.search.Indexer;
20  import com.liferay.portal.kernel.search.IndexerRegistryUtil;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.model.ResourceConstants;
23  import com.liferay.portal.model.User;
24  import com.liferay.portal.service.ServiceContext;
25  import com.liferay.portlet.bookmarks.FolderNameException;
26  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
27  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
28  import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
29  import com.liferay.portlet.bookmarks.service.base.BookmarksFolderLocalServiceBaseImpl;
30  
31  import java.util.ArrayList;
32  import java.util.Date;
33  import java.util.List;
34  
35  /**
36   * <a href="BookmarksFolderLocalServiceImpl.java.html"><b><i>View Source</i></b>
37   * </a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Wesley Gong
41   */
42  public class BookmarksFolderLocalServiceImpl
43      extends BookmarksFolderLocalServiceBaseImpl {
44  
45      public BookmarksFolder addFolder(
46              String uuid, long userId, long parentFolderId, String name,
47              String description, ServiceContext serviceContext)
48          throws PortalException, SystemException {
49  
50          // Folder
51  
52          User user = userPersistence.findByPrimaryKey(userId);
53          long groupId = serviceContext.getScopeGroupId();
54          parentFolderId = getParentFolderId(groupId, parentFolderId);
55          Date now = new Date();
56  
57          validate(name);
58  
59          long folderId = counterLocalService.increment();
60  
61          BookmarksFolder folder = bookmarksFolderPersistence.create(folderId);
62  
63          folder.setUuid(uuid);
64          folder.setGroupId(groupId);
65          folder.setCompanyId(user.getCompanyId());
66          folder.setUserId(user.getUserId());
67          folder.setCreateDate(now);
68          folder.setModifiedDate(now);
69          folder.setParentFolderId(parentFolderId);
70          folder.setName(name);
71          folder.setDescription(description);
72          folder.setExpandoBridgeAttributes(serviceContext);
73  
74          bookmarksFolderPersistence.update(folder, false);
75  
76          // Resources
77  
78          if (serviceContext.getAddCommunityPermissions() ||
79              serviceContext.getAddGuestPermissions()) {
80  
81              addFolderResources(
82                  folder, serviceContext.getAddCommunityPermissions(),
83                  serviceContext.getAddGuestPermissions());
84          }
85          else {
86              addFolderResources(
87                  folder, serviceContext.getCommunityPermissions(),
88                  serviceContext.getGuestPermissions());
89          }
90  
91          return folder;
92      }
93  
94      public void addFolderResources(
95              BookmarksFolder folder, boolean addCommunityPermissions,
96              boolean addGuestPermissions)
97          throws PortalException, SystemException {
98  
99          resourceLocalService.addResources(
100             folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
101             BookmarksFolder.class.getName(), folder.getFolderId(), false,
102             addCommunityPermissions, addGuestPermissions);
103     }
104 
105     public void addFolderResources(
106             BookmarksFolder folder, String[] communityPermissions,
107             String[] guestPermissions)
108         throws PortalException, SystemException {
109 
110         resourceLocalService.addModelResources(
111             folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
112             BookmarksFolder.class.getName(), folder.getFolderId(),
113             communityPermissions, guestPermissions);
114     }
115 
116     public void addFolderResources(
117             long folderId, boolean addCommunityPermissions,
118             boolean addGuestPermissions)
119         throws PortalException, SystemException {
120 
121         BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
122             folderId);
123 
124         addFolderResources(
125             folder, addCommunityPermissions, addGuestPermissions);
126     }
127 
128     public void addFolderResources(
129             long folderId, String[] communityPermissions,
130             String[] guestPermissions)
131         throws PortalException, SystemException {
132 
133         BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
134             folderId);
135 
136         addFolderResources(folder, communityPermissions, guestPermissions);
137     }
138 
139     public void deleteFolder(BookmarksFolder folder)
140         throws PortalException, SystemException {
141 
142         // Folder
143 
144         bookmarksFolderPersistence.remove(folder);
145 
146         // Resources
147 
148         resourceLocalService.deleteResource(
149             folder.getCompanyId(), BookmarksFolder.class.getName(),
150             ResourceConstants.SCOPE_INDIVIDUAL, folder.getFolderId());
151 
152         // Entries
153 
154         bookmarksEntryLocalService.deleteEntries(
155             folder.getGroupId(), folder.getFolderId());
156 
157         // Folders
158 
159         List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
160             folder.getGroupId(), folder.getFolderId());
161 
162         for (BookmarksFolder curFolder : folders) {
163             deleteFolder(curFolder);
164         }
165 
166         // Expando
167 
168         expandoValueLocalService.deleteValues(
169             BookmarksFolder.class.getName(), folder.getFolderId());
170     }
171 
172     public void deleteFolder(long folderId)
173         throws PortalException, SystemException {
174 
175         BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
176             folderId);
177 
178         deleteFolder(folder);
179     }
180 
181     public void deleteFolders(long groupId)
182         throws PortalException, SystemException {
183 
184         List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
185             groupId, BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
186 
187         for (BookmarksFolder folder : folders) {
188             deleteFolder(folder);
189         }
190     }
191 
192     public List<BookmarksFolder> getCompanyFolders(
193             long companyId, int start, int end)
194         throws SystemException {
195 
196         return bookmarksFolderPersistence.findByCompanyId(
197             companyId, start, end);
198     }
199 
200     public int getCompanyFoldersCount(long companyId) throws SystemException {
201         return bookmarksFolderPersistence.countByCompanyId(companyId);
202     }
203 
204     public BookmarksFolder getFolder(long folderId)
205         throws PortalException, SystemException {
206 
207         return bookmarksFolderPersistence.findByPrimaryKey(folderId);
208     }
209 
210     public List<BookmarksFolder> getFolders(long groupId)
211         throws SystemException {
212 
213         return bookmarksFolderPersistence.findByGroupId(groupId);
214     }
215 
216     public List<BookmarksFolder> getFolders(long groupId, long parentFolderId)
217         throws SystemException {
218 
219         return bookmarksFolderPersistence.findByG_P(groupId, parentFolderId);
220     }
221 
222     public List<BookmarksFolder> getFolders(
223             long groupId, long parentFolderId, int start, int end)
224         throws SystemException {
225 
226         return bookmarksFolderPersistence.findByG_P(
227             groupId, parentFolderId, start, end);
228     }
229 
230     public int getFoldersCount(long groupId, long parentFolderId)
231         throws SystemException {
232 
233         return bookmarksFolderPersistence.countByG_P(groupId, parentFolderId);
234     }
235 
236     public void getSubfolderIds(
237             List<Long> folderIds, long groupId, long folderId)
238         throws SystemException {
239 
240         List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
241             groupId, folderId);
242 
243         for (BookmarksFolder folder : folders) {
244             folderIds.add(folder.getFolderId());
245 
246             getSubfolderIds(
247                 folderIds, folder.getGroupId(), folder.getFolderId());
248         }
249     }
250 
251     public BookmarksFolder updateFolder(
252             long folderId, long parentFolderId, String name,
253             String description, boolean mergeWithParentFolder,
254             ServiceContext serviceContext)
255         throws PortalException, SystemException {
256 
257         // Merge folders
258 
259         BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
260             folderId);
261 
262         parentFolderId = getParentFolderId(folder, parentFolderId);
263 
264         if (mergeWithParentFolder && (folderId != parentFolderId)) {
265             mergeFolders(folder, parentFolderId);
266 
267             return folder;
268         }
269 
270         // Folder
271 
272         validate(name);
273 
274         folder.setModifiedDate(new Date());
275         folder.setParentFolderId(parentFolderId);
276         folder.setName(name);
277         folder.setDescription(description);
278         folder.setExpandoBridgeAttributes(serviceContext);
279 
280         bookmarksFolderPersistence.update(folder, false);
281 
282         return folder;
283     }
284 
285     protected long getParentFolderId(
286             BookmarksFolder folder, long parentFolderId)
287         throws SystemException {
288 
289         if (parentFolderId ==
290                 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
291 
292             return parentFolderId;
293         }
294 
295         if (folder.getFolderId() == parentFolderId) {
296             return folder.getParentFolderId();
297         }
298         else {
299             BookmarksFolder parentFolder =
300                 bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
301 
302             if ((parentFolder == null) ||
303                 (folder.getGroupId() != parentFolder.getGroupId())) {
304 
305                 return folder.getParentFolderId();
306             }
307 
308             List<Long> subfolderIds = new ArrayList<Long>();
309 
310             getSubfolderIds(
311                 subfolderIds, folder.getGroupId(), folder.getFolderId());
312 
313             if (subfolderIds.contains(parentFolderId)) {
314                 return folder.getParentFolderId();
315             }
316 
317             return parentFolderId;
318         }
319     }
320 
321     protected long getParentFolderId(long groupId, long parentFolderId)
322         throws SystemException {
323 
324         if (parentFolderId !=
325                 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
326 
327             BookmarksFolder parentFolder =
328                 bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
329 
330             if ((parentFolder == null) ||
331                 (groupId != parentFolder.getGroupId())) {
332 
333                 parentFolderId =
334                     BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
335             }
336         }
337 
338         return parentFolderId;
339     }
340 
341     protected void mergeFolders(BookmarksFolder fromFolder, long toFolderId)
342         throws PortalException, SystemException {
343 
344         List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
345                 fromFolder.getGroupId(), fromFolder.getFolderId());
346 
347         for (BookmarksFolder folder : folders) {
348             mergeFolders(folder, toFolderId);
349         }
350 
351         List<BookmarksEntry> entries = bookmarksEntryPersistence.findByG_F(
352             fromFolder.getGroupId(), fromFolder.getFolderId());
353 
354         for (BookmarksEntry entry : entries) {
355             entry.setFolderId(toFolderId);
356 
357             bookmarksEntryPersistence.update(entry, false);
358 
359             Indexer indexer = IndexerRegistryUtil.getIndexer(
360                 BookmarksEntry.class);
361 
362             indexer.reindex(entry);
363         }
364 
365         deleteFolder(fromFolder);
366     }
367 
368     protected void validate(String name) throws PortalException {
369         if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1) ||
370             (name.indexOf("//") != -1)) {
371 
372             throw new FolderNameException();
373         }
374     }
375 
376 }