001    /**
002     * Copyright (c) 2000-2011 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, false);
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    
113            public void deleteFolder(long folderId)
114                    throws PortalException, SystemException {
115    
116                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
117                            folderId);
118    
119                    deleteFolder(folder);
120            }
121    
122            public void deleteFolders(long groupId)
123                    throws PortalException, SystemException {
124    
125                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
126                            groupId, BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
127    
128                    for (BookmarksFolder folder : folders) {
129                            deleteFolder(folder);
130                    }
131            }
132    
133            public List<BookmarksFolder> getCompanyFolders(
134                            long companyId, int start, int end)
135                    throws SystemException {
136    
137                    return bookmarksFolderPersistence.findByCompanyId(
138                            companyId, start, end);
139            }
140    
141            public int getCompanyFoldersCount(long companyId) throws SystemException {
142                    return bookmarksFolderPersistence.countByCompanyId(companyId);
143            }
144    
145            public BookmarksFolder getFolder(long folderId)
146                    throws PortalException, SystemException {
147    
148                    return bookmarksFolderPersistence.findByPrimaryKey(folderId);
149            }
150    
151            public List<BookmarksFolder> getFolders(long groupId)
152                    throws SystemException {
153    
154                    return bookmarksFolderPersistence.findByGroupId(groupId);
155            }
156    
157            public List<BookmarksFolder> getFolders(long groupId, long parentFolderId)
158                    throws SystemException {
159    
160                    return bookmarksFolderPersistence.findByG_P(groupId, parentFolderId);
161            }
162    
163            public List<BookmarksFolder> getFolders(
164                            long groupId, long parentFolderId, int start, int end)
165                    throws SystemException {
166    
167                    return bookmarksFolderPersistence.findByG_P(
168                            groupId, parentFolderId, start, end);
169            }
170    
171            public int getFoldersCount(long groupId, long parentFolderId)
172                    throws SystemException {
173    
174                    return bookmarksFolderPersistence.countByG_P(groupId, parentFolderId);
175            }
176    
177            public void getSubfolderIds(
178                            List<Long> folderIds, long groupId, long folderId)
179                    throws SystemException {
180    
181                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
182                            groupId, folderId);
183    
184                    for (BookmarksFolder folder : folders) {
185                            folderIds.add(folder.getFolderId());
186    
187                            getSubfolderIds(
188                                    folderIds, folder.getGroupId(), folder.getFolderId());
189                    }
190            }
191    
192            public BookmarksFolder updateFolder(
193                            long folderId, long parentFolderId, String name,
194                            String description, boolean mergeWithParentFolder,
195                            ServiceContext serviceContext)
196                    throws PortalException, SystemException {
197    
198                    // Merge folders
199    
200                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
201                            folderId);
202    
203                    parentFolderId = getParentFolderId(folder, parentFolderId);
204    
205                    if (mergeWithParentFolder && (folderId != parentFolderId)) {
206                            mergeFolders(folder, parentFolderId);
207    
208                            return folder;
209                    }
210    
211                    // Folder
212    
213                    validate(name);
214    
215                    folder.setModifiedDate(serviceContext.getModifiedDate(null));
216                    folder.setParentFolderId(parentFolderId);
217                    folder.setName(name);
218                    folder.setDescription(description);
219                    folder.setExpandoBridgeAttributes(serviceContext);
220    
221                    bookmarksFolderPersistence.update(folder, false);
222    
223                    return folder;
224            }
225    
226            protected long getParentFolderId(
227                            BookmarksFolder folder, long parentFolderId)
228                    throws SystemException {
229    
230                    if (parentFolderId ==
231                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
232    
233                            return parentFolderId;
234                    }
235    
236                    if (folder.getFolderId() == parentFolderId) {
237                            return folder.getParentFolderId();
238                    }
239                    else {
240                            BookmarksFolder parentFolder =
241                                    bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
242    
243                            if ((parentFolder == null) ||
244                                    (folder.getGroupId() != parentFolder.getGroupId())) {
245    
246                                    return folder.getParentFolderId();
247                            }
248    
249                            List<Long> subfolderIds = new ArrayList<Long>();
250    
251                            getSubfolderIds(
252                                    subfolderIds, folder.getGroupId(), folder.getFolderId());
253    
254                            if (subfolderIds.contains(parentFolderId)) {
255                                    return folder.getParentFolderId();
256                            }
257    
258                            return parentFolderId;
259                    }
260            }
261    
262            protected long getParentFolderId(long groupId, long parentFolderId)
263                    throws SystemException {
264    
265                    if (parentFolderId !=
266                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
267    
268                            BookmarksFolder parentFolder =
269                                    bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
270    
271                            if ((parentFolder == null) ||
272                                    (groupId != parentFolder.getGroupId())) {
273    
274                                    parentFolderId =
275                                            BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
276                            }
277                    }
278    
279                    return parentFolderId;
280            }
281    
282            protected void mergeFolders(BookmarksFolder fromFolder, long toFolderId)
283                    throws PortalException, SystemException {
284    
285                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
286                                    fromFolder.getGroupId(), fromFolder.getFolderId());
287    
288                    for (BookmarksFolder folder : folders) {
289                            mergeFolders(folder, toFolderId);
290                    }
291    
292                    List<BookmarksEntry> entries = bookmarksEntryPersistence.findByG_F(
293                            fromFolder.getGroupId(), fromFolder.getFolderId());
294    
295                    for (BookmarksEntry entry : entries) {
296                            entry.setFolderId(toFolderId);
297    
298                            bookmarksEntryPersistence.update(entry, false);
299    
300                            Indexer indexer = IndexerRegistryUtil.getIndexer(
301                                    BookmarksEntry.class);
302    
303                            indexer.reindex(entry);
304                    }
305    
306                    deleteFolder(fromFolder);
307            }
308    
309            protected void validate(String name) throws PortalException {
310                    if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1) ||
311                            (name.indexOf("//") != -1)) {
312    
313                            throw new FolderNameException();
314                    }
315            }
316    
317    }