001    /**
002     * Copyright (c) 2000-present 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.util.test;
016    
017    import com.liferay.portal.kernel.search.QueryConfig;
018    import com.liferay.portal.kernel.search.SearchContext;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.util.test.RandomTestUtil;
024    import com.liferay.portal.util.test.ServiceContextTestUtil;
025    import com.liferay.portal.util.test.TestPropsValues;
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.BookmarksEntryLocalServiceUtil;
030    import com.liferay.portlet.bookmarks.service.BookmarksEntryServiceUtil;
031    import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Manuel de la Pe??a
036     */
037    public class BookmarksTestUtil {
038    
039            public static BookmarksEntry addEntry(boolean approved) throws Exception {
040                    return addEntry(TestPropsValues.getGroupId(), approved);
041            }
042    
043            public static BookmarksEntry addEntry(long groupId, boolean approved)
044                    throws Exception {
045    
046                    ServiceContext serviceContext =
047                            ServiceContextTestUtil.getServiceContext(groupId);
048    
049                    return addEntry(
050                            BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID, approved,
051                            serviceContext);
052            }
053    
054            public static BookmarksEntry addEntry(
055                            long folderId, boolean approved, ServiceContext serviceContext)
056                    throws Exception {
057    
058                    boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
059    
060                    try {
061                            WorkflowThreadLocal.setEnabled(true);
062    
063                            String name = "Test Entry";
064                            String url = "http://www.liferay.com";
065                            String description = "This is a test entry.";
066    
067                            serviceContext = (ServiceContext)serviceContext.clone();
068    
069                            serviceContext.setAddGroupPermissions(true);
070                            serviceContext.setAddGuestPermissions(true);
071    
072                            serviceContext.setWorkflowAction(
073                                    WorkflowConstants.ACTION_SAVE_DRAFT);
074    
075                            BookmarksEntry entry = BookmarksEntryServiceUtil.addEntry(
076                                    serviceContext.getScopeGroupId(), folderId, name, url,
077                                    description, serviceContext);
078    
079                            serviceContext.setCommand(Constants.ADD);
080                            serviceContext.setLayoutFullURL("http://localhost");
081    
082                            if (approved) {
083                                    entry.setStatus(WorkflowConstants.STATUS_APPROVED);
084    
085                                    entry = BookmarksEntryServiceUtil.updateEntry(
086                                            entry.getEntryId(), serviceContext.getScopeGroupId(),
087                                            entry.getFolderId(), entry.getName(), entry.getUrl(),
088                                            entry.getUrl(), serviceContext);
089                            }
090    
091                            return entry;
092                    }
093                    finally {
094                            WorkflowThreadLocal.setEnabled(workflowEnabled);
095                    }
096            }
097    
098            public static BookmarksFolder addFolder(
099                            long groupId, long parentFolderId, String name)
100                    throws Exception {
101    
102                    ServiceContext serviceContext =
103                            ServiceContextTestUtil.getServiceContext(groupId);
104    
105                    return addFolder(parentFolderId, name, serviceContext);
106            }
107    
108            public static BookmarksFolder addFolder(long groupId, String name)
109                    throws Exception {
110    
111                    return addFolder(
112                            groupId, BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID, name);
113            }
114    
115            public static BookmarksFolder addFolder(
116                            long parentFolderId, String name, ServiceContext serviceContext)
117                    throws Exception {
118    
119                    String description = "This is a test folder.";
120    
121                    return BookmarksFolderServiceUtil.addFolder(
122                            parentFolderId, name, description, serviceContext);
123            }
124    
125            public static BookmarksFolder addFolder(String name) throws Exception {
126                    return addFolder(TestPropsValues.getGroupId(), name);
127            }
128    
129            public static SearchContext getSearchContext(
130                    long companyId, long groupId, long folderId, String keywords) {
131    
132                    return getSearchContext(
133                            companyId, groupId, folderId, keywords, false, false);
134            }
135    
136            public static SearchContext getSearchContext(
137                    long companyId, long groupId, long folderId, String keywords,
138                    boolean highlight, boolean score) {
139    
140                    SearchContext searchContext = new SearchContext();
141    
142                    searchContext.setCompanyId(companyId);
143                    searchContext.setFolderIds(new long[] {folderId});
144                    searchContext.setGroupIds(new long[] {groupId});
145                    searchContext.setKeywords(keywords);
146    
147                    QueryConfig queryConfig = new QueryConfig();
148    
149                    queryConfig.setHighlightEnabled(highlight);
150                    queryConfig.setScoreEnabled(score);
151    
152                    searchContext.setQueryConfig(queryConfig);
153    
154                    return searchContext;
155            }
156    
157            public static BookmarksEntry updateEntry(BookmarksEntry entry)
158                    throws Exception {
159    
160                    return updateEntry(entry, RandomTestUtil.randomString());
161            }
162    
163            public static BookmarksEntry updateEntry(BookmarksEntry entry, String name)
164                    throws Exception {
165    
166                    ServiceContext serviceContext =
167                            ServiceContextTestUtil.getServiceContext(entry.getGroupId());
168    
169                    serviceContext.setCommand(Constants.UPDATE);
170                    serviceContext.setLayoutFullURL("http://localhost");
171    
172                    return BookmarksEntryLocalServiceUtil.updateEntry(
173                            TestPropsValues.getUserId(), entry.getEntryId(), entry.getGroupId(),
174                            entry.getFolderId(), name, entry.getUrl(), entry.getDescription(),
175                            serviceContext);
176            }
177    
178    }