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