1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.bookmarks.service.impl;
24  
25  import com.liferay.counter.service.CounterLocalServiceUtil;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.model.impl.ResourceImpl;
31  import com.liferay.portal.service.ResourceLocalServiceUtil;
32  import com.liferay.portal.service.persistence.UserUtil;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portlet.bookmarks.FolderNameException;
35  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
36  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
37  import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
38  import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
39  import com.liferay.portlet.bookmarks.service.base.BookmarksFolderLocalServiceBaseImpl;
40  import com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryUtil;
41  import com.liferay.portlet.bookmarks.service.persistence.BookmarksFolderUtil;
42  
43  import java.util.ArrayList;
44  import java.util.Date;
45  import java.util.Iterator;
46  import java.util.List;
47  
48  /**
49   * <a href="BookmarksFolderLocalServiceImpl.java.html"><b><i>View Source</i></b>
50   * </a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class BookmarksFolderLocalServiceImpl
56      extends BookmarksFolderLocalServiceBaseImpl {
57  
58      public BookmarksFolder addFolder(
59              long userId, long plid, long parentFolderId, String name,
60              String description, boolean addCommunityPermissions,
61              boolean addGuestPermissions)
62          throws PortalException, SystemException {
63  
64          return addFolder(
65              userId, plid, parentFolderId, name, description,
66              Boolean.valueOf(addCommunityPermissions),
67              Boolean.valueOf(addGuestPermissions), null, null);
68      }
69  
70      public BookmarksFolder addFolder(
71              long userId, long plid, long parentFolderId, String name,
72              String description, String[] communityPermissions,
73              String[] guestPermissions)
74          throws PortalException, SystemException {
75  
76          return addFolder(
77              userId, plid, parentFolderId, name, description, null, null,
78              communityPermissions, guestPermissions);
79      }
80  
81      public BookmarksFolder addFolder(
82              long userId, long plid, long parentFolderId, String name,
83              String description, Boolean addCommunityPermissions,
84              Boolean addGuestPermissions, String[] communityPermissions,
85              String[] guestPermissions)
86          throws PortalException, SystemException {
87  
88          // Folder
89  
90          User user = UserUtil.findByPrimaryKey(userId);
91          long groupId = PortalUtil.getPortletGroupId(plid);
92          parentFolderId = getParentFolderId(groupId, parentFolderId);
93          Date now = new Date();
94  
95          validate(name);
96  
97          long folderId = CounterLocalServiceUtil.increment();
98  
99          BookmarksFolder folder = BookmarksFolderUtil.create(folderId);
100 
101         folder.setGroupId(groupId);
102         folder.setCompanyId(user.getCompanyId());
103         folder.setUserId(user.getUserId());
104         folder.setCreateDate(now);
105         folder.setModifiedDate(now);
106         folder.setParentFolderId(parentFolderId);
107         folder.setName(name);
108         folder.setDescription(description);
109 
110         BookmarksFolderUtil.update(folder);
111 
112         // Resources
113 
114         if ((addCommunityPermissions != null) &&
115             (addGuestPermissions != null)) {
116 
117             addFolderResources(
118                 folder, addCommunityPermissions.booleanValue(),
119                 addGuestPermissions.booleanValue());
120         }
121         else {
122             addFolderResources(folder, communityPermissions, guestPermissions);
123         }
124 
125         return folder;
126     }
127 
128     public void addFolderResources(
129             long folderId, boolean addCommunityPermissions,
130             boolean addGuestPermissions)
131         throws PortalException, SystemException {
132 
133         BookmarksFolder folder = BookmarksFolderUtil.findByPrimaryKey(folderId);
134 
135         addFolderResources(
136             folder, addCommunityPermissions, addGuestPermissions);
137     }
138 
139     public void addFolderResources(
140             BookmarksFolder folder, boolean addCommunityPermissions,
141             boolean addGuestPermissions)
142         throws PortalException, SystemException {
143 
144         ResourceLocalServiceUtil.addResources(
145             folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
146             BookmarksFolder.class.getName(), folder.getFolderId(), false,
147             addCommunityPermissions, addGuestPermissions);
148     }
149 
150     public void addFolderResources(
151             long folderId, String[] communityPermissions,
152             String[] guestPermissions)
153         throws PortalException, SystemException {
154 
155         BookmarksFolder folder = BookmarksFolderUtil.findByPrimaryKey(folderId);
156 
157         addFolderResources(folder, communityPermissions, guestPermissions);
158     }
159 
160     public void addFolderResources(
161             BookmarksFolder folder, String[] communityPermissions,
162             String[] guestPermissions)
163         throws PortalException, SystemException {
164 
165         ResourceLocalServiceUtil.addModelResources(
166             folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
167             BookmarksFolder.class.getName(), folder.getFolderId(),
168             communityPermissions, guestPermissions);
169     }
170 
171     public void deleteFolder(long folderId)
172         throws PortalException, SystemException {
173 
174         BookmarksFolder folder = BookmarksFolderUtil.findByPrimaryKey(folderId);
175 
176         deleteFolder(folder);
177     }
178 
179     public void deleteFolder(BookmarksFolder folder)
180         throws PortalException, SystemException {
181 
182         // Folders
183 
184         Iterator itr = BookmarksFolderUtil.findByG_P(
185             folder.getGroupId(), folder.getFolderId()).iterator();
186 
187         while (itr.hasNext()) {
188             BookmarksFolder curFolder = (BookmarksFolder)itr.next();
189 
190             deleteFolder(curFolder);
191         }
192 
193         // Entries
194 
195         BookmarksEntryLocalServiceUtil.deleteEntries(folder.getFolderId());
196 
197         // Resources
198 
199         ResourceLocalServiceUtil.deleteResource(
200             folder.getCompanyId(), BookmarksFolder.class.getName(),
201             ResourceImpl.SCOPE_INDIVIDUAL, folder.getFolderId());
202 
203         // Folder
204 
205         BookmarksFolderUtil.remove(folder.getFolderId());
206     }
207 
208     public void deleteFolders(long groupId)
209         throws PortalException, SystemException {
210 
211         Iterator itr = BookmarksFolderUtil.findByG_P(
212             groupId, BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID).iterator();
213 
214         while (itr.hasNext()) {
215             BookmarksFolder folder = (BookmarksFolder)itr.next();
216 
217             deleteFolder(folder);
218         }
219     }
220 
221     public BookmarksFolder getFolder(long folderId)
222         throws PortalException, SystemException {
223 
224         return BookmarksFolderUtil.findByPrimaryKey(folderId);
225     }
226 
227     public List getFolders(
228             long groupId, long parentFolderId, int begin, int end)
229         throws SystemException {
230 
231         return BookmarksFolderUtil.findByG_P(
232             groupId, parentFolderId, begin, end);
233     }
234 
235     public int getFoldersCount(long groupId, long parentFolderId)
236         throws SystemException {
237 
238         return BookmarksFolderUtil.countByG_P(groupId, parentFolderId);
239     }
240 
241     public void getSubfolderIds(
242             List folderIds, long groupId, long folderId)
243         throws SystemException {
244 
245         Iterator itr = BookmarksFolderUtil.findByG_P(
246             groupId, folderId).iterator();
247 
248         while (itr.hasNext()) {
249             BookmarksFolder folder = (BookmarksFolder)itr.next();
250 
251             folderIds.add(new Long(folder.getFolderId()));
252 
253             getSubfolderIds(
254                 folderIds, folder.getGroupId(), folder.getFolderId());
255         }
256     }
257 
258     public BookmarksFolder updateFolder(
259             long folderId, long parentFolderId, String name,
260             String description, boolean mergeWithParentFolder)
261         throws PortalException, SystemException {
262 
263         // Folder
264 
265         BookmarksFolder folder = BookmarksFolderUtil.findByPrimaryKey(folderId);
266 
267         parentFolderId = getParentFolderId(folder, parentFolderId);
268 
269         validate(name);
270 
271         folder.setModifiedDate(new Date());
272         folder.setParentFolderId(parentFolderId);
273         folder.setName(name);
274         folder.setDescription(description);
275 
276         BookmarksFolderUtil.update(folder);
277 
278         // Merge folders
279 
280         if (mergeWithParentFolder && (folderId != parentFolderId) &&
281             (parentFolderId != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID)) {
282 
283             mergeFolders(folder, parentFolderId);
284         }
285 
286         return folder;
287     }
288 
289     protected long getParentFolderId(long groupId, long parentFolderId)
290         throws SystemException {
291 
292         if (parentFolderId != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
293             BookmarksFolder parentFolder =
294                 BookmarksFolderUtil.fetchByPrimaryKey(parentFolderId);
295 
296             if ((parentFolder == null) ||
297                 (groupId != parentFolder.getGroupId())) {
298 
299                 parentFolderId = BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID;
300             }
301         }
302 
303         return parentFolderId;
304     }
305 
306     protected long getParentFolderId(
307             BookmarksFolder folder, long parentFolderId)
308         throws SystemException {
309 
310         if (parentFolderId == BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
311             return parentFolderId;
312         }
313 
314         if (folder.getFolderId() == parentFolderId) {
315             return folder.getParentFolderId();
316         }
317         else {
318             BookmarksFolder parentFolder =
319                 BookmarksFolderUtil.fetchByPrimaryKey(parentFolderId);
320 
321             if ((parentFolder == null) ||
322                 (folder.getGroupId() != parentFolder.getGroupId())) {
323 
324                 return folder.getParentFolderId();
325             }
326 
327             List subfolderIds = new ArrayList();
328 
329             getSubfolderIds(
330                 subfolderIds, folder.getGroupId(), folder.getFolderId());
331 
332             if (subfolderIds.contains(new Long(parentFolderId))) {
333                 return folder.getParentFolderId();
334             }
335 
336             return parentFolderId;
337         }
338     }
339 
340     protected void mergeFolders(BookmarksFolder fromFolder, long toFolderId)
341         throws PortalException, SystemException {
342 
343         Iterator itr = BookmarksFolderUtil.findByG_P(
344             fromFolder.getGroupId(), fromFolder.getFolderId()).iterator();
345 
346         while (itr.hasNext()) {
347             BookmarksFolder folder = (BookmarksFolder)itr.next();
348 
349             mergeFolders(folder, toFolderId);
350         }
351 
352         itr = BookmarksEntryUtil.findByFolderId(
353             fromFolder.getFolderId()).iterator();
354 
355         while (itr.hasNext()) {
356 
357             // Entry
358 
359             BookmarksEntry entry = (BookmarksEntry)itr.next();
360 
361             entry.setFolderId(toFolderId);
362 
363             BookmarksEntryUtil.update(entry);
364         }
365 
366         BookmarksFolderUtil.remove(fromFolder.getFolderId());
367     }
368 
369     protected void validate(String name) throws PortalException {
370         if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1) ||
371             (name.indexOf("//") != -1)) {
372 
373             throw new FolderNameException();
374         }
375     }
376 
377 }