use of org.springframework.data.domain.Sort.Direction in project irida by phac-nml.
the class ProjectSamplesControllerTest method testGetProjectsAvailableToCopySamplesAsUser.
@Test
public void testGetProjectsAvailableToCopySamplesAsUser() {
String term = "";
int page = 0;
int pagesize = 10;
Direction order = Direction.ASC;
final Project p = new Project();
User puser = new User(USER_NAME, null, null, null, null, null);
puser.setSystemRole(Role.ROLE_USER);
Page<Project> projects = new PageImpl<>(Lists.newArrayList(TestDataFactory.constructProject(), TestDataFactory.constructProject()));
when(projectService.read(1L)).thenReturn(p);
when(projectService.getUnassociatedProjects(p, term, page, pagesize, order, ProjectSamplesController.PROJECT_NAME_PROPERTY)).thenReturn(projects);
Map<String, Object> projectsAvailableToCopySamples = controller.getProjectsAvailableToCopySamples(1L, term, pagesize, page);
assertTrue(projectsAvailableToCopySamples.containsKey("total"));
assertEquals(2L, projectsAvailableToCopySamples.get("total"));
assertTrue(projectsAvailableToCopySamples.containsKey("projects"));
verify(projectService).getUnassociatedProjects(p, term, page, pagesize, order, ProjectSamplesController.PROJECT_NAME_PROPERTY);
}
use of org.springframework.data.domain.Sort.Direction in project spring-data-commons by spring-projects.
the class OrderAdapter method unmarshal.
/*
* (non-Javadoc)
* @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
*/
@Nullable
@Override
public Order unmarshal(@Nullable OrderDto source) {
if (source == null) {
return null;
}
Direction direction = source.direction;
String property = source.property;
if (direction == null || property == null) {
return null;
}
return new Order(direction, property);
}
use of org.springframework.data.domain.Sort.Direction in project spring-data-commons by spring-projects.
the class SortHandlerMethodArgumentResolver method foldIntoExpressions.
/**
* Folds the given {@link Sort} instance into a {@link List} of sort expressions, accumulating {@link Order} instances
* of the same direction into a single expression if they are in order.
*
* @param sort must not be {@literal null}.
* @return
*/
protected List<String> foldIntoExpressions(Sort sort) {
List<String> expressions = new ArrayList<>();
ExpressionBuilder builder = null;
for (Order order : sort) {
Direction direction = order.getDirection();
if (builder == null) {
builder = new ExpressionBuilder(direction);
} else if (!builder.hasSameDirectionAs(order)) {
builder.dumpExpressionIfPresentInto(expressions);
builder = new ExpressionBuilder(direction);
}
builder.add(order.getProperty());
}
return builder == null ? Collections.emptyList() : builder.dumpExpressionIfPresentInto(expressions);
}
use of org.springframework.data.domain.Sort.Direction in project spring-data-mongodb by spring-projects.
the class IndexEnsuringQueryCreationListener method onCreation.
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.QueryCreationListener#onCreation(org.springframework.data.repository.query.RepositoryQuery)
*/
public void onCreation(PartTreeMongoQuery query) {
PartTree tree = query.getTree();
if (!tree.hasPredicate()) {
return;
}
Index index = new Index();
index.named(query.getQueryMethod().getName());
Sort sort = tree.getSort();
for (Part part : tree.getParts()) {
if (GEOSPATIAL_TYPES.contains(part.getType())) {
return;
}
if (isIndexOnUnwrappedType(part)) {
return;
}
String property = part.getProperty().toDotPath();
Direction order = toDirection(sort, property);
index.on(property, order);
}
// Add fixed sorting criteria to index
if (sort.isSorted()) {
for (Order order : sort) {
index.on(order.getProperty(), order.getDirection());
}
}
if (query.getQueryMethod().hasAnnotatedCollation()) {
String collation = query.getQueryMethod().getAnnotatedCollation();
if (!collation.contains("?")) {
index = index.collation(Collation.parse(collation));
}
}
MongoEntityMetadata<?> metadata = query.getQueryMethod().getEntityInformation();
try {
indexOperationsProvider.indexOps(metadata.getCollectionName(), metadata.getJavaType()).ensureIndex(index);
} catch (UncategorizedMongoDbException e) {
if (e.getCause() instanceof MongoException) {
/*
* As of MongoDB 4.2 index creation raises an error when creating an index for the very same keys with
* different name, whereas previous versions silently ignored this.
* Because an index is by default named after the repository finder method it is not uncommon that an index
* for the very same property combination might already exist with a different name.
* So you see, that's why we need to ignore the error here.
*
* For details please see: https://docs.mongodb.com/master/release-notes/4.2-compatibility/#indexes
*/
if (((MongoException) e.getCause()).getCode() != 85) {
throw e;
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Created %s!", index));
}
}
use of org.springframework.data.domain.Sort.Direction in project irida by phac-nml.
the class ProjectSamplesControllerTest method testGetProjectsAvailableToCopySamplesAsAdmin.
@Test
public void testGetProjectsAvailableToCopySamplesAsAdmin() {
String term = "";
int page = 0;
int pagesize = 10;
Direction order = Direction.ASC;
String property = "name";
final Project p = new Project();
User puser = new User(USER_NAME, null, null, null, null, null);
puser.setSystemRole(Role.ROLE_ADMIN);
Page<Project> projects = new PageImpl<>(Lists.newArrayList(TestDataFactory.constructProject(), TestDataFactory.constructProject()));
when(projectService.read(1L)).thenReturn(p);
when(projectService.getUnassociatedProjects(p, term, page, pagesize, order, property)).thenReturn(projects);
Map<String, Object> projectsAvailableToCopySamples = controller.getProjectsAvailableToCopySamples(1L, term, pagesize, page);
assertTrue(projectsAvailableToCopySamples.containsKey("total"));
assertEquals(2L, projectsAvailableToCopySamples.get("total"));
assertTrue(projectsAvailableToCopySamples.containsKey("projects"));
verify(projectService).getUnassociatedProjects(p, term, page, pagesize, order, property);
}
Aggregations