mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-07 22:15:45 +01:00
start implementing paging and server side sorting for users, groups and repositories
This commit is contained in:
@@ -81,4 +81,20 @@ public interface Manager<T extends ModelObject, E extends Exception>
|
||||
* @return
|
||||
*/
|
||||
public Collection<T> getAll();
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param sortby
|
||||
* @param desc
|
||||
* @param start
|
||||
* @param limit
|
||||
*
|
||||
* @return
|
||||
* @since 1.4
|
||||
*/
|
||||
public Collection<T> getAll(String sortby, boolean desc, int start,
|
||||
int limit);
|
||||
}
|
||||
|
||||
@@ -119,4 +119,19 @@ public class AssertUtil
|
||||
throw new IllegalStateException("object is not valid");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* throws an IllegalArgumentException if the value is smaller then 0
|
||||
*
|
||||
*
|
||||
* @param value
|
||||
* @since 1.4
|
||||
*/
|
||||
public static void assertPositive(int value)
|
||||
{
|
||||
if (value < 0)
|
||||
{
|
||||
throw new IllegalArgumentException("value is smaller then 0");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* 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.util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*
|
||||
* @param <T>
|
||||
* @since 1.4
|
||||
*/
|
||||
public interface CollectionAppender<T>
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param collection
|
||||
* @param item
|
||||
*/
|
||||
public void append(Collection<T> collection, T item);
|
||||
}
|
||||
@@ -38,9 +38,13 @@ package sonia.scm.util;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
@@ -177,6 +181,117 @@ public class Util
|
||||
return time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param values
|
||||
* @param comparator
|
||||
* @param start
|
||||
* @param limit
|
||||
* @param <T>
|
||||
*
|
||||
* @return
|
||||
* @since 1.4
|
||||
*/
|
||||
public static <T> Collection<T> createSubCollection(Collection<T> values,
|
||||
Comparator<T> comparator, int start, int limit)
|
||||
{
|
||||
return createSubCollection(values, comparator, null, start, limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param values
|
||||
* @param start
|
||||
* @param limit
|
||||
* @param <T>
|
||||
*
|
||||
* @return
|
||||
* @since 1.4
|
||||
*/
|
||||
public static <T> Collection<T> createSubCollection(Collection<T> values,
|
||||
int start, int limit)
|
||||
{
|
||||
return createSubCollection(values, null, null, start, limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param values
|
||||
* @param appender
|
||||
* @param start
|
||||
* @param limit
|
||||
* @param <T>
|
||||
*
|
||||
* @return
|
||||
* @since 1.4
|
||||
*/
|
||||
public static <T> Collection<T> createSubCollection(Collection<T> values,
|
||||
CollectionAppender<T> appender, int start, int limit)
|
||||
{
|
||||
return createSubCollection(values, null, appender, start, limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param values
|
||||
* @param comparator
|
||||
* @param appender
|
||||
* @param start
|
||||
* @param limit
|
||||
* @param <T>
|
||||
*
|
||||
* @return
|
||||
* @since 1.4
|
||||
*/
|
||||
public static <T> Collection<T> createSubCollection(Collection<T> values,
|
||||
Comparator<T> comparator, CollectionAppender<T> appender, int start,
|
||||
int limit)
|
||||
{
|
||||
List<T> result = new ArrayList<T>();
|
||||
List<T> valueList = new ArrayList(values);
|
||||
|
||||
if (comparator != null)
|
||||
{
|
||||
Collections.sort(valueList, comparator);
|
||||
}
|
||||
|
||||
int length = valueList.size();
|
||||
int end = start + limit;
|
||||
|
||||
if (length > start)
|
||||
{
|
||||
if (length < end)
|
||||
{
|
||||
end = length;
|
||||
}
|
||||
|
||||
valueList = valueList.subList(start, end);
|
||||
|
||||
if (appender == null)
|
||||
{
|
||||
result = valueList;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (T v : valueList)
|
||||
{
|
||||
appender.append(result, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -42,6 +42,7 @@ import sonia.scm.LastModifiedAware;
|
||||
import sonia.scm.Manager;
|
||||
import sonia.scm.ModelObject;
|
||||
import sonia.scm.security.ScmSecurityException;
|
||||
import sonia.scm.util.AssertUtil;
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
@@ -51,12 +52,14 @@ import java.util.Date;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.DefaultValue;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.CacheControl;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.EntityTag;
|
||||
@@ -296,13 +299,21 @@ public abstract class AbstractManagerResource<T extends ModelObject,
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param start
|
||||
* @param limit
|
||||
* @param sortby
|
||||
* @param desc
|
||||
* @return
|
||||
*/
|
||||
@GET
|
||||
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||
public Response getAll(@Context Request request)
|
||||
public Response getAll(@Context Request request, @DefaultValue("0")
|
||||
@QueryParam("start") int start, @DefaultValue("-1")
|
||||
@QueryParam("limit") int limit, @QueryParam("sortby") String sortby,
|
||||
@DefaultValue("false")
|
||||
@QueryParam("desc") boolean desc)
|
||||
{
|
||||
Collection<T> items = manager.getAll();
|
||||
Collection<T> items = fetchItems(sortby, desc, start, limit);
|
||||
|
||||
if (Util.isNotEmpty(items))
|
||||
{
|
||||
@@ -473,6 +484,37 @@ public abstract class AbstractManagerResource<T extends ModelObject,
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param sortby
|
||||
* @param desc
|
||||
* @param start
|
||||
* @param limit
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private Collection<T> fetchItems(String sortby, boolean desc, int start,
|
||||
int limit)
|
||||
{
|
||||
AssertUtil.assertPositive(start);
|
||||
|
||||
Collection<T> items = null;
|
||||
|
||||
if (limit > 0)
|
||||
{
|
||||
items = manager.getAll(sortby, desc, start, limit);
|
||||
}
|
||||
else
|
||||
{
|
||||
items = manager.getAll();
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
||||
@@ -54,6 +54,7 @@ import sonia.scm.search.SearchUtil;
|
||||
import sonia.scm.security.SecurityContext;
|
||||
import sonia.scm.store.Store;
|
||||
import sonia.scm.store.StoreFactory;
|
||||
import sonia.scm.util.CollectionAppender;
|
||||
import sonia.scm.util.SecurityUtil;
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
@@ -362,6 +363,34 @@ public class XmlGroupManager extends AbstractGroupManager
|
||||
return groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param sortby
|
||||
* @param desc
|
||||
* @param start
|
||||
* @param limit
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Collection<Group> getAll(String sortby, boolean desc, int start,
|
||||
int limit)
|
||||
{
|
||||
|
||||
// TODO sort
|
||||
return Util.createSubCollection(groupDB.values(),
|
||||
new CollectionAppender<Group>()
|
||||
{
|
||||
@Override
|
||||
public void append(Collection<Group> collection, Group item)
|
||||
{
|
||||
collection.add(item.clone());
|
||||
}
|
||||
}, start, limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -59,8 +59,10 @@ import sonia.scm.security.ScmSecurityException;
|
||||
import sonia.scm.store.Store;
|
||||
import sonia.scm.store.StoreFactory;
|
||||
import sonia.scm.util.AssertUtil;
|
||||
import sonia.scm.util.CollectionAppender;
|
||||
import sonia.scm.util.IOUtil;
|
||||
import sonia.scm.util.SecurityUtil;
|
||||
import sonia.scm.util.Util;
|
||||
import sonia.scm.web.security.WebSecurityContext;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
@@ -394,6 +396,34 @@ public class XmlRepositoryManager extends AbstractRepositoryManager
|
||||
return repositories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param sortby
|
||||
* @param desc
|
||||
* @param start
|
||||
* @param limit
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Collection<Repository> getAll(String sortby, boolean desc, int start,
|
||||
int limit)
|
||||
{
|
||||
|
||||
// TODO sort
|
||||
return Util.createSubCollection(repositoryDB.values(),
|
||||
new CollectionAppender<Repository>()
|
||||
{
|
||||
@Override
|
||||
public void append(Collection<Repository> collection, Repository item)
|
||||
{
|
||||
collection.add(item.clone());
|
||||
}
|
||||
}, start, limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -35,6 +35,7 @@ package sonia.scm.user.xml;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.collect.Collections2;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
@@ -55,6 +56,7 @@ import sonia.scm.user.User;
|
||||
import sonia.scm.user.UserAllreadyExistException;
|
||||
import sonia.scm.user.UserException;
|
||||
import sonia.scm.util.AssertUtil;
|
||||
import sonia.scm.util.CollectionAppender;
|
||||
import sonia.scm.util.IOUtil;
|
||||
import sonia.scm.util.SecurityUtil;
|
||||
import sonia.scm.util.Util;
|
||||
@@ -65,8 +67,11 @@ import sonia.scm.web.security.WebSecurityContext;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
@@ -402,6 +407,34 @@ public class XmlUserManager extends AbstractUserManager
|
||||
return users;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param sortby
|
||||
* @param desc
|
||||
* @param start
|
||||
* @param limit
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Collection<User> getAll(String sortby, boolean desc, int start,
|
||||
int limit)
|
||||
{
|
||||
|
||||
// TODO sort
|
||||
return Util.createSubCollection(userDB.values(),
|
||||
new CollectionAppender<User>()
|
||||
{
|
||||
@Override
|
||||
public void append(Collection<User> collection, User item)
|
||||
{
|
||||
collection.add(item.clone());
|
||||
}
|
||||
}, start, limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user