Example usage for org.springframework.data.domain PageImpl PageImpl

List of usage examples for org.springframework.data.domain PageImpl PageImpl

Introduction

In this page you can find the example usage for org.springframework.data.domain PageImpl PageImpl.

Prototype

public PageImpl(List<T> content, Pageable pageable, long total) 

Source Link

Document

Constructor of PageImpl .

Usage

From source file:org.dspace.app.rest.repository.EPersonRestRepository.java

/**
 * Find the epersons matching the query q parameter. The search is delegated to the
 * {@link EPersonService#search(Context, String, int, int)} method
 *
 * @param q//  ww w  .  jav  a 2s. co m
 *            is the *required* query string
 * @param pageable
 *            contains the pagination information
 * @return a Page of EPersonRest instances matching the user query
 */
@SearchRestMethod(name = "byName")
public Page<EPersonRest> findByName(@Parameter(value = "q", required = true) String q, Pageable pageable) {
    List<EPerson> epersons = null;
    int total = 0;
    try {
        Context context = obtainContext();
        epersons = es.search(context, q, pageable.getOffset(), pageable.getOffset() + pageable.getPageSize());
        total = es.searchResultCount(context, q);
    } catch (SQLException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    Page<EPersonRest> page = new PageImpl<EPerson>(epersons, pageable, total).map(converter);
    return page;
}

From source file:org.dspace.app.rest.repository.ItemRestRepository.java

@Override
@PreAuthorize("hasAuthority('ADMIN')")
public Page<ItemRest> findAll(Context context, Pageable pageable) {
    Iterator<Item> it = null;
    List<Item> items = new ArrayList<Item>();
    int total = 0;
    try {/*ww  w.  ja  v a2  s  . com*/
        total = is.countTotal(context);
        it = is.findAll(context, pageable.getPageSize(), pageable.getOffset());
        while (it.hasNext()) {
            Item i = it.next();
            items.add(i);
        }
    } catch (SQLException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    Page<ItemRest> page = new PageImpl<Item>(items, pageable, total).map(dsoConverter);
    return page;
}

From source file:org.dspace.app.rest.repository.SubmissionUploadRestRepository.java

@Override
public Page<SubmissionUploadRest> findAll(Context context, Pageable pageable) {
    List<SubmissionConfig> subConfs = new ArrayList<SubmissionConfig>();
    subConfs = submissionConfigReader.getAllSubmissionConfigs(pageable.getPageSize(), pageable.getOffset());
    List<SubmissionUploadRest> results = new ArrayList<>();
    for (SubmissionConfig config : subConfs) {
        for (int i = 0; i < config.getNumberOfSteps(); i++) {
            SubmissionStepConfig step = config.getStep(i);
            if (SubmissionStepConfig.UPLOAD_STEP_NAME.equals(step.getType())) {
                UploadConfiguration uploadConfig = uploadConfigurationService.getMap().get(step.getId());
                if (uploadConfig != null) {
                    try {
                        results.add(convert(context, uploadConfig));
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                    }/*from  www . j av  a 2  s .co  m*/
                }
            }
        }
    }
    return new PageImpl<SubmissionUploadRest>(results, pageable, results.size());
}

From source file:org.dspace.app.rest.repository.WorkspaceItemRestRepository.java

@Override
public Page<WorkspaceItemRest> findAll(Context context, Pageable pageable) {
    List<WorkspaceItem> witems = null;
    int total = 0;
    try {/*w  w  w.jav a  2  s  .c  om*/
        total = wis.countTotal(context);
        witems = wis.findAll(context, pageable.getPageSize(), pageable.getOffset());
    } catch (SQLException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    Page<WorkspaceItemRest> page = new PageImpl<WorkspaceItem>(witems, pageable, total).map(converter);
    return page;
}

From source file:org.dspace.app.rest.repository.WorkspaceItemRestRepository.java

@SearchRestMethod(name = "findBySubmitter")
public Page<WorkspaceItemRest> findBySubmitter(@Parameter(value = "uuid", required = true) UUID submitterID,
        Pageable pageable) {/*from   w  ww  . j  a v  a2s  .  c o m*/
    List<WorkspaceItem> witems = null;
    int total = 0;
    try {
        Context context = obtainContext();
        EPerson ep = epersonService.find(context, submitterID);
        witems = wis.findByEPerson(context, ep, pageable.getPageSize(), pageable.getOffset());
        total = wis.countByEPerson(context, ep);
    } catch (SQLException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    Page<WorkspaceItemRest> page = new PageImpl<WorkspaceItem>(witems, pageable, total).map(converter);
    return page;
}

From source file:org.dspace.app.rest.RestResourceController.java

private <ID extends Serializable> ResourceSupport findRelEntryInternal(HttpServletRequest request,
        String apiCategory, String model, String id, String rel, String relid, Pageable page,
        PagedResourcesAssembler assembler, String projection) {
    checkModelPluralForm(apiCategory, model);
    DSpaceRestRepository<RestModel, ID> repository = utils.getResourceRepository(apiCategory, model);
    Class<RestModel> domainClass = repository.getDomainClass();

    LinkRest linkRest = utils.getLinkRest(rel, domainClass);
    if (linkRest != null) {
        LinkRestRepository linkRepository = utils.getLinkResourceRepository(apiCategory, model,
                linkRest.name());// ww w  .  j  av  a 2  s .  c om
        Method linkMethod = repositoryUtils.getLinkMethod("getResource", linkRepository);

        try {
            Object object = linkMethod.invoke(linkRepository, request, id, relid, page, projection);
            Link link = linkTo(this.getClass(), apiCategory, English.plural(model)).slash(id).slash(rel)
                    .withSelfRel();
            List result = new ArrayList();
            result.add(object);
            PageImpl<RestModel> pageResult = new PageImpl(result, page, 1);
            return assembler.toResource(pageResult.map(linkRepository::wrapResource), link);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    return null;
}

From source file:org.dspace.app.rest.RestResourceController.java

private <ID extends Serializable> ResourceSupport findRelInternal(HttpServletRequest request,
        String apiCategory, String model, ID uuid, String rel, Pageable page, PagedResourcesAssembler assembler,
        String projection) {//from  ww  w .  j a  v a 2 s .c  om
    checkModelPluralForm(apiCategory, model);
    DSpaceRestRepository<RestModel, ID> repository = utils.getResourceRepository(apiCategory, model);
    Class<RestModel> domainClass = repository.getDomainClass();

    LinkRest linkRest = utils.getLinkRest(rel, domainClass);

    if (linkRest != null) {
        LinkRestRepository linkRepository = utils.getLinkResourceRepository(apiCategory, model,
                linkRest.name());
        Method linkMethod = repositoryUtils.getLinkMethod(linkRest.method(), linkRepository);

        if (linkMethod == null) {
            // TODO custom exception
            throw new RuntimeException(
                    "Method for relation " + rel + " not found: " + linkRest.name() + ":" + linkRest.method());
        } else {
            try {
                Page<? extends Serializable> pageResult = (Page<? extends RestModel>) linkMethod
                        .invoke(linkRepository, request, uuid, page, projection);
                Link link = linkTo(this.getClass(), apiCategory, English.plural(model)).slash(uuid).slash(rel)
                        .withSelfRel();
                PagedResources<? extends ResourceSupport> result = assembler
                        .toResource(pageResult.map(linkRepository::wrapResource), link);
                return result;
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    }
    RestModel modelObject = repository.findOne(uuid);
    DSpaceResource result = repository.wrapResource(modelObject, rel);
    if (result.getLink(rel) == null) {
        // TODO create a custom exception
        throw new ResourceNotFoundException(rel + "undefined for " + model);
    } else if (result.getEmbedded().get(rel) instanceof EmbeddedPage) {
        // this is a very inefficient scenario. We have an embedded list
        // already fully retrieved that we need to limit with pagination
        // parameter. BTW change the default sorting is not implemented at
        // the current stage and could be overcompex to implement
        // if we really want to implement pagination we should implement a
        // link repository so to fall in the previous block code
        EmbeddedPage ep = (EmbeddedPage) result.getEmbedded().get(rel);
        List<? extends RestModel> fullList = ep.getFullList();
        if (fullList == null || fullList.size() == 0)
            return null;
        int start = page.getOffset();
        int end = (start + page.getPageSize()) > fullList.size() ? fullList.size()
                : (start + page.getPageSize());
        DSpaceRestRepository<RestModel, ?> resourceRepository = utils
                .getResourceRepository(fullList.get(0).getCategory(), fullList.get(0).getType());
        PageImpl<RestModel> pageResult = new PageImpl(fullList.subList(start, end), page, fullList.size());
        return assembler.toResource(pageResult.map(resourceRepository::wrapResource));
    } else {
        ResourceSupport resu = (ResourceSupport) result.getEmbedded().get(rel);
        return resu;
    }
}

From source file:org.dspace.app.rest.RestResourceController.java

@RequestMapping(method = RequestMethod.GET)
@SuppressWarnings("unchecked")
public <T extends RestModel> PagedResources<DSpaceResource<T>> findAll(@PathVariable String apiCategory,
        @PathVariable String model, Pageable page, PagedResourcesAssembler assembler,
        @RequestParam(required = false) String projection) {
    DSpaceRestRepository<T, ?> repository = utils.getResourceRepository(apiCategory, model);
    Link link = linkTo(methodOn(this.getClass(), apiCategory, English.plural(model)).findAll(apiCategory, model,
            page, assembler, projection)).withSelfRel();

    Page<DSpaceResource<T>> resources;
    try {//from  ww  w  . jav  a  2  s.  co m
        resources = repository.findAll(page).map(repository::wrapResource);
    } catch (PaginationException pe) {
        resources = new PageImpl<DSpaceResource<T>>(new ArrayList<DSpaceResource<T>>(), page, pe.getTotal());
    }
    PagedResources<DSpaceResource<T>> result = assembler.toResource(resources, link);
    if (repositoryUtils.haveSearchMethods(repository)) {
        result.add(linkTo(this.getClass(), apiCategory, model).slash("search").withRel("search"));
    }
    return result;
}

From source file:org.dspace.app.rest.utils.Utils.java

public <T> Page<T> getPage(List<T> fullContents, Pageable pageable) {
    int total = fullContents.size();
    List<T> pageContent = null;
    if (pageable.getOffset() > total) {
        throw new PaginationException(total);
    } else {//from  w  ww. ja  va 2s  . co m
        if (pageable.getOffset() + pageable.getPageSize() > total) {
            pageContent = fullContents.subList(pageable.getOffset(), total);
        } else {
            pageContent = fullContents.subList(pageable.getOffset(),
                    pageable.getOffset() + pageable.getPageSize());
        }
        return new PageImpl<T>(pageContent, pageable, total);
    }
}

From source file:org.eclipse.hawkbit.repository.jpa.JpaTargetManagement.java

private static Page<Target> convertPage(final Page<JpaTarget> findAll, final Pageable pageable) {
    return new PageImpl<>(Collections.unmodifiableList(findAll.getContent()), pageable,
            findAll.getTotalElements());
}