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

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

Introduction

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

Prototype

@Override
    public <U> Page<U> map(Function<? super T, ? extends U> converter) 

Source Link

Usage

From source file:org.dspace.app.rest.model.hateoas.DSpaceResource.java

public DSpaceResource(T data, Utils utils, String... rels) {
    this.data = data;

    if (data != null) {
        try {//ww  w .  jav  a  2 s  . c  o  m
            LinksRest links = data.getClass().getDeclaredAnnotation(LinksRest.class);
            if (links != null && rels != null) {
                List<String> relsList = Arrays.asList(rels);
                for (LinkRest linkAnnotation : links.links()) {
                    if (!relsList.contains(linkAnnotation.name())) {
                        continue;
                    }
                    String name = linkAnnotation.name();
                    Link linkToSubResource = utils.linkToSubResource(data, name);
                    String apiCategory = data.getCategory();
                    String model = data.getType();
                    LinkRestRepository linkRepository = utils.getLinkResourceRepository(apiCategory, model,
                            linkAnnotation.name());

                    if (!linkRepository.isEmbeddableRelation(data, linkAnnotation.name())) {
                        continue;
                    }
                    try {
                        //RestModel linkClass = linkAnnotation.linkClass().newInstance();
                        Method[] methods = linkRepository.getClass().getMethods();
                        boolean found = false;
                        for (Method m : methods) {
                            if (StringUtils.equals(m.getName(), linkAnnotation.method())) {
                                // TODO add support for single linked object other than for collections
                                Page<? extends Serializable> pageResult = (Page<? extends RestModel>) m.invoke(
                                        linkRepository, null, ((BaseObjectRest) data).getId(), null, null);
                                EmbeddedPage ep = new EmbeddedPage(linkToSubResource.getHref(), pageResult,
                                        null);
                                embedded.put(name, ep);
                                found = true;
                            }
                        }
                        // TODO custom exception
                        if (!found) {
                            throw new RuntimeException("Method for relation " + linkAnnotation.name()
                                    + " not found: " + linkAnnotation.method());
                        }
                    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    }
                }
            }

            for (PropertyDescriptor pd : Introspector.getBeanInfo(data.getClass()).getPropertyDescriptors()) {
                Method readMethod = pd.getReadMethod();
                String name = pd.getName();
                if (readMethod != null && !"class".equals(name)) {
                    LinkRest linkAnnotation = readMethod.getAnnotation(LinkRest.class);

                    if (linkAnnotation != null) {
                        if (StringUtils.isNotBlank(linkAnnotation.name())) {
                            name = linkAnnotation.name();
                        }
                        Link linkToSubResource = utils.linkToSubResource(data, name);
                        // no method is specified to retrieve the linked object(s) so check if it is already here
                        if (StringUtils.isBlank(linkAnnotation.method())) {
                            this.add(linkToSubResource);
                            Object linkedObject = readMethod.invoke(data);
                            Object wrapObject = linkedObject;
                            if (linkedObject instanceof RestModel) {
                                RestModel linkedRM = (RestModel) linkedObject;
                                wrapObject = utils
                                        .getResourceRepository(linkedRM.getCategory(), linkedRM.getType())
                                        .wrapResource(linkedRM);
                            } else {
                                if (linkedObject instanceof List) {
                                    List<RestModel> linkedRMList = (List<RestModel>) linkedObject;
                                    if (linkedRMList.size() > 0) {

                                        DSpaceRestRepository<RestModel, ?> resourceRepository = utils
                                                .getResourceRepository(linkedRMList.get(0).getCategory(),
                                                        linkedRMList.get(0).getType());
                                        // TODO should we force pagination also of embedded resource? 
                                        // This will force a pagination with size 10 for embedded collections as well
                                        //                                 int pageSize = 1;
                                        //                                 PageImpl<RestModel> page = new PageImpl(
                                        //                                       linkedRMList.subList(0,
                                        //                                             linkedRMList.size() > pageSize ? pageSize : linkedRMList.size()), new PageRequest(0, pageSize), linkedRMList.size()); 
                                        PageImpl<RestModel> page = new PageImpl(linkedRMList);
                                        wrapObject = new EmbeddedPage(linkToSubResource.getHref(),
                                                page.map(resourceRepository::wrapResource), linkedRMList);
                                    } else {
                                        wrapObject = null;
                                    }
                                }
                            }
                            if (linkedObject != null) {
                                embedded.put(name, wrapObject);
                            } else {
                                embedded.put(name, null);
                            }
                            Method writeMethod = pd.getWriteMethod();
                            writeMethod.invoke(data, new Object[] { null });
                        } else {
                            // call the link repository
                            try {
                                //RestModel linkClass = linkAnnotation.linkClass().newInstance();
                                String apiCategory = data.getCategory();
                                String model = data.getType();
                                LinkRestRepository linkRepository = utils.getLinkResourceRepository(apiCategory,
                                        model, linkAnnotation.name());
                                Method[] methods = linkRepository.getClass().getMethods();
                                boolean found = false;
                                for (Method m : methods) {
                                    if (StringUtils.equals(m.getName(), linkAnnotation.method())) {
                                        // TODO add support for single linked object other than for collections
                                        Page<? extends Serializable> pageResult = (Page<? extends RestModel>) m
                                                .invoke(linkRepository, null, ((BaseObjectRest) data).getId(),
                                                        null, null);
                                        EmbeddedPage ep = new EmbeddedPage(linkToSubResource.getHref(),
                                                pageResult, null);
                                        embedded.put(name, ep);
                                        found = true;
                                    }
                                }
                                // TODO custom exception
                                if (!found) {
                                    throw new RuntimeException("Method for relation " + linkAnnotation.name()
                                            + " not found: " + linkAnnotation.method());
                                }
                            } catch (IllegalAccessException | IllegalArgumentException
                                    | InvocationTargetException e) {
                                throw new RuntimeException(e.getMessage(), e);
                            }
                        }
                    } else if (RestModel.class.isAssignableFrom(readMethod.getReturnType())) {
                        Link linkToSubResource = utils.linkToSubResource(data, name);
                        this.add(linkToSubResource);
                        RestModel linkedObject = (RestModel) readMethod.invoke(data);
                        if (linkedObject != null) {
                            embedded.put(name, utils
                                    .getResourceRepository(linkedObject.getCategory(), linkedObject.getType())
                                    .wrapResource(linkedObject));
                        } else {
                            embedded.put(name, null);
                        }

                        Method writeMethod = pd.getWriteMethod();
                        writeMethod.invoke(data, new Object[] { null });
                    }
                }
            }
        } catch (IntrospectionException | IllegalArgumentException | IllegalAccessException
                | InvocationTargetException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        this.add(utils.linkToSingleResource(data, Link.REL_SELF));
    }
}

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());//w  w w  . j  a v a 2  s .  c  o  m
        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;
}