Search in sources :

Example 6 with Direction

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);
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) Direction(org.springframework.data.domain.Sort.Direction) Test(org.junit.Test)

Example 7 with Direction

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);
}
Also used : Order(org.springframework.data.domain.Sort.Order) Direction(org.springframework.data.domain.Sort.Direction) Nullable(org.springframework.lang.Nullable)

Example 8 with Direction

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);
}
Also used : Order(org.springframework.data.domain.Sort.Order) ArrayList(java.util.ArrayList) Direction(org.springframework.data.domain.Sort.Direction)

Example 9 with Direction

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));
    }
}
Also used : Order(org.springframework.data.domain.Sort.Order) MongoException(com.mongodb.MongoException) Part(org.springframework.data.repository.query.parser.Part) Sort(org.springframework.data.domain.Sort) Index(org.springframework.data.mongodb.core.index.Index) PartTree(org.springframework.data.repository.query.parser.PartTree) Direction(org.springframework.data.domain.Sort.Direction) UncategorizedMongoDbException(org.springframework.data.mongodb.UncategorizedMongoDbException)

Example 10 with Direction

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);
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) Direction(org.springframework.data.domain.Sort.Direction) Test(org.junit.Test)

Aggregations

Direction (org.springframework.data.domain.Sort.Direction)13 Test (org.junit.Test)8 PageImpl (org.springframework.data.domain.PageImpl)8 Order (org.springframework.data.domain.Sort.Order)8 Specification (org.springframework.data.jpa.domain.Specification)6 IdentifiableTestEntity (ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity)4 IdentifiableTestEntitySpecification (ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntitySpecification)4 Pageable (org.springframework.data.domain.Pageable)4 User (ca.corefacility.bioinformatics.irida.model.user.User)3 Project (ca.corefacility.bioinformatics.irida.model.project.Project)2 ArrayList (java.util.ArrayList)2 RemoteAPI (ca.corefacility.bioinformatics.irida.model.RemoteAPI)1 UserSpecification (ca.corefacility.bioinformatics.irida.repositories.specification.UserSpecification)1 MongoException (com.mongodb.MongoException)1 ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)1 DistributionWithAuditInfo (com.synopsys.integration.alert.common.rest.model.DistributionWithAuditInfo)1 Principal (java.security.Principal)1 List (java.util.List)1 PageRequest (org.springframework.data.domain.PageRequest)1 Sort (org.springframework.data.domain.Sort)1