001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.bookmarks.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.search.Indexer;
020    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.ResourceConstants;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portlet.bookmarks.FolderNameException;
026    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
027    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
028    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
029    import com.liferay.portlet.bookmarks.service.base.BookmarksFolderLocalServiceBaseImpl;
030    
031    import java.util.ArrayList;
032    import java.util.Date;
033    import java.util.List;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Wesley Gong
038     */
039    public class BookmarksFolderLocalServiceImpl
040            extends BookmarksFolderLocalServiceBaseImpl {
041    
042            public BookmarksFolder addFolder(
043                            long userId, long parentFolderId, String name, String description,
044                            ServiceContext serviceContext)
045                    throws PortalException, SystemException {
046    
047                    // Folder
048    
049                    User user = userPersistence.findByPrimaryKey(userId);
050                    long groupId = serviceContext.getScopeGroupId();
051                    parentFolderId = getParentFolderId(groupId, parentFolderId);
052                    Date now = new Date();
053    
054                    validate(name);
055    
056                    long folderId = counterLocalService.increment();
057    
058                    BookmarksFolder folder = bookmarksFolderPersistence.create(folderId);
059    
060                    folder.setUuid(serviceContext.getUuid());
061                    folder.setGroupId(groupId);
062                    folder.setCompanyId(user.getCompanyId());
063                    folder.setUserId(user.getUserId());
064                    folder.setCreateDate(serviceContext.getCreateDate(now));
065                    folder.setModifiedDate(serviceContext.getModifiedDate(now));
066                    folder.setParentFolderId(parentFolderId);
067                    folder.setName(name);
068                    folder.setDescription(description);
069                    folder.setExpandoBridgeAttributes(serviceContext);
070    
071                    bookmarksFolderPersistence.update(folder, false);
072    
073                    // Resources
074    
075                    if (serviceContext.getAddCommunityPermissions() ||
076                            serviceContext.getAddGuestPermissions()) {
077    
078                            addFolderResources(
079                                    folder, serviceContext.getAddCommunityPermissions(),
080                                    serviceContext.getAddGuestPermissions());
081                    }
082                    else {
083                            addFolderResources(
084                                    folder, serviceContext.getCommunityPermissions(),
085                                    serviceContext.getGuestPermissions());
086                    }
087    
088                    return folder;
089            }
090    
091            public void addFolderResources(
092                            BookmarksFolder folder, boolean addCommunityPermissions,
093                            boolean addGuestPermissions)
094                    throws PortalException, SystemException {
095    
096                    resourceLocalService.addResources(
097                            folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
098                            BookmarksFolder.class.getName(), folder.getFolderId(), false,
099                            addCommunityPermissions, addGuestPermissions);
100            }
101    
102            public void addFolderResources(
103                            BookmarksFolder folder, String[] communityPermissions,
104                            String[] guestPermissions)
105                    throws PortalException, SystemException {
106    
107                    resourceLocalService.addModelResources(
108                            folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
109                            BookmarksFolder.class.getName(), folder.getFolderId(),
110                            communityPermissions, guestPermissions);
111            }
112    
113            public void addFolderResources(
114                            long folderId, boolean addCommunityPermissions,
115                            boolean addGuestPermissions)
116                    throws PortalException, SystemException {
117    
118                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
119                            folderId);
120    
121                    addFolderResources(
122                            folder, addCommunityPermissions, addGuestPermissions);
123            }
124    
125            public void addFolderResources(
126                            long folderId, String[] communityPermissions,
127                            String[] guestPermissions)
128                    throws PortalException, SystemException {
129    
130                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
131                            folderId);
132    
133                    addFolderResources(folder, communityPermissions, guestPermissions);
134            }
135    
136            public void deleteFolder(BookmarksFolder folder)
137                    throws PortalException, SystemException {
138    
139                    // Folders
140    
141                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
142                            folder.getGroupId(), folder.getFolderId());
143    
144                    for (BookmarksFolder curFolder : folders) {
145                            deleteFolder(curFolder);
146                    }
147    
148                    // Folder
149    
150                    bookmarksFolderPersistence.remove(folder);
151    
152                    // Resources
153    
154                    resourceLocalService.deleteResource(
155                            folder.getCompanyId(), BookmarksFolder.class.getName(),
156                            ResourceConstants.SCOPE_INDIVIDUAL, folder.getFolderId());
157    
158                    // Entries
159    
160                    bookmarksEntryLocalService.deleteEntries(
161                            folder.getGroupId(), folder.getFolderId());
162    
163                    // Expando
164    
165                    expandoValueLocalService.deleteValues(
166                            BookmarksFolder.class.getName(), folder.getFolderId());
167            }
168    
169            public void deleteFolder(long folderId)
170                    throws PortalException, SystemException {
171    
172                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
173                            folderId);
174    
175                    deleteFolder(folder);
176            }
177    
178            public void deleteFolders(long groupId)
179                    throws PortalException, SystemException {
180    
181                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
182                            groupId, BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
183    
184                    for (BookmarksFolder folder : folders) {
185                            deleteFolder(folder);
186                    }
187            }
188    
189            public List<BookmarksFolder> getCompanyFolders(
190                            long companyId, int start, int end)
191                    throws SystemException {
192    
193                    return bookmarksFolderPersistence.findByCompanyId(
194                            companyId, start, end);
195            }
196    
197            public int getCompanyFoldersCount(long companyId) throws SystemException {
198                    return bookmarksFolderPersistence.countByCompanyId(companyId);
199            }
200    
201            public BookmarksFolder getFolder(long folderId)
202                    throws PortalException, SystemException {
203    
204                    return bookmarksFolderPersistence.findByPrimaryKey(folderId);
205            }
206    
207            public List<BookmarksFolder> getFolders(long groupId)
208                    throws SystemException {
209    
210                    return bookmarksFolderPersistence.findByGroupId(groupId);
211            }
212    
213            public List<BookmarksFolder> getFolders(long groupId, long parentFolderId)
214                    throws SystemException {
215    
216                    return bookmarksFolderPersistence.findByG_P(groupId, parentFolderId);
217            }
218    
219            public List<BookmarksFolder> getFolders(
220                            long groupId, long parentFolderId, int start, int end)
221                    throws SystemException {
222    
223                    return bookmarksFolderPersistence.findByG_P(
224                            groupId, parentFolderId, start, end);
225            }
226    
227            public int getFoldersCount(long groupId, long parentFolderId)
228                    throws SystemException {
229    
230                    return bookmarksFolderPersistence.countByG_P(groupId, parentFolderId);
231            }
232    
233            public void getSubfolderIds(
234                            List<Long> folderIds, long groupId, long folderId)
235                    throws SystemException {
236    
237                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
238                            groupId, folderId);
239    
240                    for (BookmarksFolder folder : folders) {
241                            folderIds.add(folder.getFolderId());
242    
243                            getSubfolderIds(
244                                    folderIds, folder.getGroupId(), folder.getFolderId());
245                    }
246            }
247    
248            public BookmarksFolder updateFolder(
249                            long folderId, long parentFolderId, String name,
250                            String description, boolean mergeWithParentFolder,
251                            ServiceContext serviceContext)
252                    throws PortalException, SystemException {
253    
254                    // Merge folders
255    
256                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
257                            folderId);
258    
259                    parentFolderId = getParentFolderId(folder, parentFolderId);
260    
261                    if (mergeWithParentFolder && (folderId != parentFolderId)) {
262                            mergeFolders(folder, parentFolderId);
263    
264                            return folder;
265                    }
266    
267                    // Folder
268    
269                    validate(name);
270    
271                    folder.setModifiedDate(serviceContext.getModifiedDate(null));
272                    folder.setParentFolderId(parentFolderId);
273                    folder.setName(name);
274                    folder.setDescription(description);
275                    folder.setExpandoBridgeAttributes(serviceContext);
276    
277                    bookmarksFolderPersistence.update(folder, false);
278    
279                    return folder;
280            }
281    
282            protected long getParentFolderId(
283                            BookmarksFolder folder, long parentFolderId)
284                    throws SystemException {
285    
286                    if (parentFolderId ==
287                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
288    
289                            return parentFolderId;
290                    }
291    
292                    if (folder.getFolderId() == parentFolderId) {
293                            return folder.getParentFolderId();
294                    }
295                    else {
296                            BookmarksFolder parentFolder =
297                                    bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
298    
299                            if ((parentFolder == null) ||
300                                    (folder.getGroupId() != parentFolder.getGroupId())) {
301    
302                                    return folder.getParentFolderId();
303                            }
304    
305                            List<Long> subfolderIds = new ArrayList<Long>();
306    
307                            getSubfolderIds(
308                                    subfolderIds, folder.getGroupId(), folder.getFolderId());
309    
310                            if (subfolderIds.contains(parentFolderId)) {
311                                    return folder.getParentFolderId();
312                            }
313    
314                            return parentFolderId;
315                    }
316            }
317    
318            protected long getParentFolderId(long groupId, long parentFolderId)
319                    throws SystemException {
320    
321                    if (parentFolderId !=
322                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
323    
324                            BookmarksFolder parentFolder =
325                                    bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
326    
327                            if ((parentFolder == null) ||
328                                    (groupId != parentFolder.getGroupId())) {
329    
330                                    parentFolderId =
331                                            BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
332                            }
333                    }
334    
335                    return parentFolderId;
336            }
337    
338            protected void mergeFolders(BookmarksFolder fromFolder, long toFolderId)
339                    throws PortalException, SystemException {
340    
341                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
342                                    fromFolder.getGroupId(), fromFolder.getFolderId());
343    
344                    for (BookmarksFolder folder : folders) {
345                            mergeFolders(folder, toFolderId);
346                    }
347    
348                    List<BookmarksEntry> entries = bookmarksEntryPersistence.findByG_F(
349                            fromFolder.getGroupId(), fromFolder.getFolderId());
350    
351                    for (BookmarksEntry entry : entries) {
352                            entry.setFolderId(toFolderId);
353    
354                            bookmarksEntryPersistence.update(entry, false);
355    
356                            Indexer indexer = IndexerRegistryUtil.getIndexer(
357                                    BookmarksEntry.class);
358    
359                            indexer.reindex(entry);
360                    }
361    
362                    deleteFolder(fromFolder);
363            }
364    
365            protected void validate(String name) throws PortalException {
366                    if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1) ||
367                            (name.indexOf("//") != -1)) {
368    
369                            throw new FolderNameException();
370                    }
371            }
372    
373    }