001    /**
002     * Copyright (c) 2000-2012 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.setUserName(user.getFullName());
065                    folder.setCreateDate(serviceContext.getCreateDate(now));
066                    folder.setModifiedDate(serviceContext.getModifiedDate(now));
067                    folder.setParentFolderId(parentFolderId);
068                    folder.setName(name);
069                    folder.setDescription(description);
070                    folder.setExpandoBridgeAttributes(serviceContext);
071    
072                    bookmarksFolderPersistence.update(folder);
073    
074                    // Resources
075    
076                    resourceLocalService.addModelResources(folder, serviceContext);
077    
078                    return folder;
079            }
080    
081            public void deleteFolder(BookmarksFolder folder)
082                    throws PortalException, SystemException {
083    
084                    // Folders
085    
086                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
087                            folder.getGroupId(), folder.getFolderId());
088    
089                    for (BookmarksFolder curFolder : folders) {
090                            deleteFolder(curFolder);
091                    }
092    
093                    // Folder
094    
095                    bookmarksFolderPersistence.remove(folder);
096    
097                    // Resources
098    
099                    resourceLocalService.deleteResource(
100                            folder, ResourceConstants.SCOPE_INDIVIDUAL);
101    
102                    // Entries
103    
104                    bookmarksEntryLocalService.deleteEntries(
105                            folder.getGroupId(), folder.getFolderId());
106    
107                    // Expando
108    
109                    expandoValueLocalService.deleteValues(
110                            BookmarksFolder.class.getName(), folder.getFolderId());
111    
112                    // Subscriptions
113    
114                    subscriptionLocalService.deleteSubscriptions(
115                            folder.getCompanyId(), BookmarksFolder.class.getName(),
116                            folder.getFolderId());
117            }
118    
119            public void deleteFolder(long folderId)
120                    throws PortalException, SystemException {
121    
122                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
123                            folderId);
124    
125                    deleteFolder(folder);
126            }
127    
128            public void deleteFolders(long groupId)
129                    throws PortalException, SystemException {
130    
131                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
132                            groupId, BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
133    
134                    for (BookmarksFolder folder : folders) {
135                            deleteFolder(folder);
136                    }
137            }
138    
139            public List<BookmarksFolder> getCompanyFolders(
140                            long companyId, int start, int end)
141                    throws SystemException {
142    
143                    return bookmarksFolderPersistence.findByCompanyId(
144                            companyId, start, end);
145            }
146    
147            public int getCompanyFoldersCount(long companyId) throws SystemException {
148                    return bookmarksFolderPersistence.countByCompanyId(companyId);
149            }
150    
151            public BookmarksFolder getFolder(long folderId)
152                    throws PortalException, SystemException {
153    
154                    return bookmarksFolderPersistence.findByPrimaryKey(folderId);
155            }
156    
157            public List<BookmarksFolder> getFolders(long groupId)
158                    throws SystemException {
159    
160                    return bookmarksFolderPersistence.findByGroupId(groupId);
161            }
162    
163            public List<BookmarksFolder> getFolders(long groupId, long parentFolderId)
164                    throws SystemException {
165    
166                    return bookmarksFolderPersistence.findByG_P(groupId, parentFolderId);
167            }
168    
169            public List<BookmarksFolder> getFolders(
170                            long groupId, long parentFolderId, int start, int end)
171                    throws SystemException {
172    
173                    return bookmarksFolderPersistence.findByG_P(
174                            groupId, parentFolderId, start, end);
175            }
176    
177            public int getFoldersCount(long groupId, long parentFolderId)
178                    throws SystemException {
179    
180                    return bookmarksFolderPersistence.countByG_P(groupId, parentFolderId);
181            }
182    
183            public void getSubfolderIds(
184                            List<Long> folderIds, long groupId, long folderId)
185                    throws SystemException {
186    
187                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
188                            groupId, folderId);
189    
190                    for (BookmarksFolder folder : folders) {
191                            folderIds.add(folder.getFolderId());
192    
193                            getSubfolderIds(
194                                    folderIds, folder.getGroupId(), folder.getFolderId());
195                    }
196            }
197    
198            public void subscribeFolder(long userId, long groupId, long folderId)
199                    throws PortalException, SystemException {
200    
201                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
202                            folderId = groupId;
203                    }
204    
205                    subscriptionLocalService.addSubscription(
206                            userId, groupId, BookmarksFolder.class.getName(), folderId);
207            }
208    
209            public void unsubscribeFolder(long userId, long groupId, long folderId)
210                    throws PortalException, SystemException {
211    
212                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
213                            folderId = groupId;
214                    }
215    
216                    subscriptionLocalService.deleteSubscription(
217                            userId, BookmarksFolder.class.getName(), folderId);
218            }
219    
220            public BookmarksFolder updateFolder(
221                            long folderId, long parentFolderId, String name, String description,
222                            boolean mergeWithParentFolder, ServiceContext serviceContext)
223                    throws PortalException, SystemException {
224    
225                    // Merge folders
226    
227                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
228                            folderId);
229    
230                    parentFolderId = getParentFolderId(folder, parentFolderId);
231    
232                    if (mergeWithParentFolder && (folderId != parentFolderId)) {
233                            mergeFolders(folder, parentFolderId);
234    
235                            return folder;
236                    }
237    
238                    // Folder
239    
240                    validate(name);
241    
242                    folder.setModifiedDate(serviceContext.getModifiedDate(null));
243                    folder.setParentFolderId(parentFolderId);
244                    folder.setName(name);
245                    folder.setDescription(description);
246                    folder.setExpandoBridgeAttributes(serviceContext);
247    
248                    bookmarksFolderPersistence.update(folder);
249    
250                    return folder;
251            }
252    
253            protected long getParentFolderId(
254                            BookmarksFolder folder, long parentFolderId)
255                    throws SystemException {
256    
257                    if (parentFolderId ==
258                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
259    
260                            return parentFolderId;
261                    }
262    
263                    if (folder.getFolderId() == parentFolderId) {
264                            return folder.getParentFolderId();
265                    }
266                    else {
267                            BookmarksFolder parentFolder =
268                                    bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
269    
270                            if ((parentFolder == null) ||
271                                    (folder.getGroupId() != parentFolder.getGroupId())) {
272    
273                                    return folder.getParentFolderId();
274                            }
275    
276                            List<Long> subfolderIds = new ArrayList<Long>();
277    
278                            getSubfolderIds(
279                                    subfolderIds, folder.getGroupId(), folder.getFolderId());
280    
281                            if (subfolderIds.contains(parentFolderId)) {
282                                    return folder.getParentFolderId();
283                            }
284    
285                            return parentFolderId;
286                    }
287            }
288    
289            protected long getParentFolderId(long groupId, long parentFolderId)
290                    throws SystemException {
291    
292                    if (parentFolderId !=
293                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
294    
295                            BookmarksFolder parentFolder =
296                                    bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
297    
298                            if ((parentFolder == null) ||
299                                    (groupId != parentFolder.getGroupId())) {
300    
301                                    parentFolderId =
302                                            BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
303                            }
304                    }
305    
306                    return parentFolderId;
307            }
308    
309            protected void mergeFolders(BookmarksFolder fromFolder, long toFolderId)
310                    throws PortalException, SystemException {
311    
312                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
313                                    fromFolder.getGroupId(), fromFolder.getFolderId());
314    
315                    for (BookmarksFolder folder : folders) {
316                            mergeFolders(folder, toFolderId);
317                    }
318    
319                    List<BookmarksEntry> entries = bookmarksEntryPersistence.findByG_F(
320                            fromFolder.getGroupId(), fromFolder.getFolderId());
321    
322                    for (BookmarksEntry entry : entries) {
323                            entry.setFolderId(toFolderId);
324    
325                            bookmarksEntryPersistence.update(entry);
326    
327                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
328                                    BookmarksEntry.class);
329    
330                            indexer.reindex(entry);
331                    }
332    
333                    deleteFolder(fromFolder);
334            }
335    
336            protected void validate(String name) throws PortalException {
337                    if (Validator.isNull(name) || name.contains("\\\\") ||
338                            name.contains("//")) {
339    
340                            throw new FolderNameException();
341                    }
342            }
343    
344    }