added group search

This commit is contained in:
Sebastian Sdorra
2011-02-12 16:11:35 +01:00
parent cb56de5670
commit e557fd1c72
7 changed files with 352 additions and 74 deletions

View File

@@ -1,117 +0,0 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.api.rest;
/**
*
* @author Sebastian Sdorra
*/
public class SearchResult
{
/**
* Constructs ...
*
*/
public SearchResult() {}
/**
* Constructs ...
*
*
* @param value
* @param label
*/
public SearchResult(String value, String label)
{
this.value = value;
this.label = label;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public String getLabel()
{
return label;
}
/**
* Method description
*
*
* @return
*/
public String getValue()
{
return value;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param label
*/
public void setLabel(String label)
{
this.label = label;
}
/**
* Method description
*
*
* @param value
*/
public void setValue(String value)
{
this.value = value;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private String label;
/** Field description */
private String value;
}

View File

@@ -1,109 +0,0 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.api.rest;
//~--- JDK imports ------------------------------------------------------------
import java.util.Collection;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Sebastian Sdorra
*/
@XmlRootElement(name = "search-results")
public class SearchResults extends RestActionResult
{
/**
* Constructs ...
*
*/
public SearchResults() {}
/**
* Constructs ...
*
*
* @param success
*/
public SearchResults(boolean success)
{
super(success);
}
/**
* Constructs ...
*
*
* @param results
*/
public SearchResults(Collection<SearchResult> results)
{
super(true);
this.results = results;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public Collection<SearchResult> getResults()
{
return results;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param results
*/
public void setResults(Collection<SearchResult> results)
{
this.results = results;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Collection<SearchResult> results;
}

View File

@@ -36,36 +36,29 @@ package sonia.scm.api.rest.resources;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.HandlerEvent;
import sonia.scm.api.rest.SearchResult;
import sonia.scm.api.rest.SearchResults;
import sonia.scm.cache.Cache;
import sonia.scm.cache.CacheManager;
import sonia.scm.search.SearchRequest;
import sonia.scm.group.Group;
import sonia.scm.group.GroupListener;
import sonia.scm.group.GroupManager;
import sonia.scm.search.SearchHandler;
import sonia.scm.search.SearchResult;
import sonia.scm.search.SearchResults;
import sonia.scm.user.User;
import sonia.scm.user.UserListener;
import sonia.scm.user.UserManager;
import sonia.scm.util.SecurityUtil;
import sonia.scm.util.Util;
import sonia.scm.web.security.WebSecurityContext;
//~--- JDK imports ------------------------------------------------------------
import java.util.Collection;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response.Status;
/**
*
@@ -73,15 +66,14 @@ import javax.ws.rs.core.Response.Status;
*/
@Singleton
@Path("search")
public class SearchResource implements UserListener
public class SearchResource implements UserListener, GroupListener
{
/** Field description */
public static final String CACHE_USER = "sonia.cache.search.users";
public static final String CACHE_GROUP = "sonia.cache.search.groups";
/** the logger for SearchResource */
private static final Logger logger =
LoggerFactory.getLogger(SearchResource.class);
/** Field description */
public static final String CACHE_USER = "sonia.cache.search.users";
//~--- constructors ---------------------------------------------------------
@@ -91,17 +83,32 @@ public class SearchResource implements UserListener
*
* @param securityContextProvider
* @param userManager
* @param groupManager
* @param cacheManager
*/
@Inject
public SearchResource(Provider<WebSecurityContext> securityContextProvider,
UserManager userManager, CacheManager cacheManager)
UserManager userManager, GroupManager groupManager,
CacheManager cacheManager)
{
this.securityContextProvider = securityContextProvider;
this.userManager = userManager;
this.userManager.addListener(this);
this.userSearchCache = cacheManager.getCache(String.class,
SearchResults.class, CACHE_USER);
// create user searchhandler
userManager.addListener(this);
Cache<String, SearchResults> userCache =
cacheManager.getCache(String.class, SearchResults.class, CACHE_USER);
this.userSearchHandler = new SearchHandler<User>(securityContextProvider,
userCache, userManager);
// create group searchhandler
groupManager.addListener(this);
Cache<String, SearchResults> groupCache =
cacheManager.getCache(String.class, SearchResults.class, CACHE_GROUP);
this.groupSearchHandler = new SearchHandler<Group>(securityContextProvider,
groupCache, groupManager);
}
//~--- methods --------------------------------------------------------------
@@ -116,7 +123,51 @@ public class SearchResource implements UserListener
@Override
public void onEvent(User user, HandlerEvent event)
{
userSearchCache.clear();
userSearchHandler.clearCache();
}
/**
* Method description
*
*
* @param group
* @param event
*/
@Override
public void onEvent(Group group, HandlerEvent event)
{
throw new UnsupportedOperationException("Not supported yet.");
}
/**
* Method description
*
*
* @param queryString
*
* @return
*/
@GET
@Path("groups")
public SearchResults searchGroups(@QueryParam("query") String queryString)
{
return groupSearchHandler.search(queryString,
new Function<Group, SearchResult>()
{
@Override
public SearchResult apply(Group group)
{
String label = group.getName();
String description = group.getDescription();
if (description != null)
{
label = label.concat(" (").concat(description).concat(")");
}
return new SearchResult(group.getName(), label);
}
});
}
/**
@@ -131,62 +182,26 @@ public class SearchResource implements UserListener
@Path("users")
public SearchResults searchUsers(@QueryParam("query") String queryString)
{
SecurityUtil.assertIsNotAnonymous(securityContextProvider);
if (Util.isEmpty(queryString))
return userSearchHandler.search(queryString,
new Function<User, SearchResult>()
{
throw new WebApplicationException(Status.BAD_REQUEST);
}
SearchResults result = userSearchCache.get(queryString);
if (result == null)
{
SearchRequest request = new SearchRequest(queryString, true);
request.setMaxResults(5);
Collection<User> users = userManager.search(request);
result = new SearchResults();
if (Util.isNotEmpty(users))
@Override
public SearchResult apply(User user)
{
Collection<SearchResult> resultCollection =
Collections2.transform(users, new Function<User, SearchResult>()
{
@Override
public SearchResult apply(User user)
{
StringBuilder label = new StringBuilder(user.getName());
StringBuilder label = new StringBuilder(user.getName());
label.append(" (").append(user.getDisplayName()).append(")");
label.append(" (").append(user.getDisplayName()).append(")");
return new SearchResult(user.getName(), label.toString());
}
});
result.setSuccess(true);
result.setResults(resultCollection);
userSearchCache.put(queryString, result);
return new SearchResult(user.getName(), label.toString());
}
}
else if (logger.isDebugEnabled())
{
logger.debug("return searchresults for {} from cache", queryString);
}
return result;
});
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Provider<WebSecurityContext> securityContextProvider;
private SearchHandler<Group> groupSearchHandler;
/** Field description */
private UserManager userManager;
/** Field description */
private Cache<String, SearchResults> userSearchCache;
private SearchHandler<User> userSearchHandler;
}