Example usage for org.springframework.data.rest.webmvc RootResourceInformation getPersistentEntity

List of usage examples for org.springframework.data.rest.webmvc RootResourceInformation getPersistentEntity

Introduction

In this page you can find the example usage for org.springframework.data.rest.webmvc RootResourceInformation getPersistentEntity.

Prototype

public PersistentEntity<?, ?> getPersistentEntity() 

Source Link

Usage

From source file:org.lightadmin.core.web.RepositoryMetadataController.java

@RequestMapping(value = BASE_MAPPING + "/metadata", method = GET)
public HttpEntity<JsonConfigurationMetadata> schema(RootResourceInformation resourceInformation) {
    JsonConfigurationMetadata jsonConfigurationMetadata = domainTypeToJsonMetadataConverter
            .convert(resourceInformation.getPersistentEntity());

    return new ResponseEntity<JsonConfigurationMetadata>(jsonConfigurationMetadata, OK);
}

From source file:org.lightadmin.core.web.RepositoryFilePropertyController.java

@RequestMapping(value = BASE_MAPPING + "/binary", method = GET)
public ResponseEntity<?> filePropertyValueOfEntity(RootResourceInformation repoRequest,
        @BackendId Serializable id, @PathVariable String property) throws Exception {
    PersistentEntity<?, ?> persistentEntity = repoRequest.getPersistentEntity();
    RepositoryInvoker invoker = repoRequest.getInvoker();

    Object domainObj = invoker.invokeFindOne(id);

    if (null == domainObj) {
        throw new ResourceNotFoundException();
    }/*from  w  w w  . j  av a2 s .  c  o  m*/

    PersistentProperty<?> prop = persistentEntity.getPersistentProperty(property);
    if (null == prop) {
        throw new ResourceNotFoundException();
    }

    if (isOfFileType(prop)) {
        return toResponseEntity(OK, new HttpHeaders(),
                new Resource<FilePropertyValue>(evaluateFilePropertyValue(domainObj, prop)));
    }

    return toEmptyResponse(METHOD_NOT_ALLOWED);
}

From source file:org.lightadmin.core.web.RepositoryFilePropertyController.java

@RequestMapping(value = BASE_MAPPING + "/file", method = DELETE)
public ResponseEntity<?> deleteFileOfPropertyOfEntity(RootResourceInformation repoRequest,
        @BackendId Serializable id, @PathVariable String property) throws Exception {
    PersistentEntity<?, ?> persistentEntity = repoRequest.getPersistentEntity();
    RepositoryInvoker invoker = repoRequest.getInvoker();

    Object domainObj = invoker.invokeFindOne(id);

    if (null == domainObj) {
        throw new ResourceNotFoundException();
    }/*from   www .j  a v  a 2s  .c  o m*/

    PersistentProperty<?> prop = persistentEntity.getPersistentProperty(property);
    if (null == prop) {
        throw new ResourceNotFoundException();
    }

    if (!isOfFileType(prop)) {
        return toEmptyResponse(METHOD_NOT_ALLOWED);
    }

    fileResourceStorage().delete(domainObj, prop);

    invoker.invokeSave(domainObj);

    return toEmptyResponse(OK);
}

From source file:org.lightadmin.core.web.RepositoryFilePropertyController.java

@RequestMapping(value = BASE_MAPPING + "/file", method = GET)
public void filePropertyOfEntity(RootResourceInformation repoRequest, ServletResponse response,
        @BackendId Serializable id, @PathVariable String property) throws Exception {
    PersistentEntity<?, ?> persistentEntity = repoRequest.getPersistentEntity();
    RepositoryInvoker invoker = repoRequest.getInvoker();

    Object domainObj = invoker.invokeFindOne(id);

    if (null == domainObj) {
        throw new ResourceNotFoundException();
    }//ww  w  . j a v a2  s  .  com

    PersistentProperty<?> prop = persistentEntity.getPersistentProperty(property);
    if (null == prop) {
        throw new ResourceNotFoundException();
    }

    if (isOfFileType(prop)) {
        fileResourceLoader().downloadFile(domainObj, prop, (HttpServletResponse) response);
    }
}

From source file:org.lightadmin.core.web.RepositoryScopedSearchController.java

@RequestMapping(value = BASE_MAPPING + "/search/count", method = GET)
public ResponseEntity<?> countItems(DomainTypeAdministrationConfiguration domainTypeAdministrationConfiguration,
        RootResourceInformation repoRequest, WebRequest request, @PathVariable String scopeName) {
    DynamicRepositoryInvoker repositoryInvoker = (DynamicRepositoryInvoker) repoRequest.getInvoker();

    PersistentEntity<?, ?> persistentEntity = repoRequest.getPersistentEntity();

    final ScopeMetadata scope = domainTypeAdministrationConfiguration.getScopes().getScope(scopeName);

    final Specification filterSpecification = specificationFromRequest(request, persistentEntity);

    if (isPredicateScope(scope)) {
        final PredicateScopeMetadata predicateScope = (PredicateScopeMetadata) scope;

        return new ResponseEntity(countItemsBySpecificationAndPredicate(repositoryInvoker, filterSpecification,
                predicateScope.predicate()), HttpStatus.OK);
    }/*from   ww  w  .  j av a 2 s. c  o m*/

    if (isSpecificationScope(scope)) {
        final Specification scopeSpecification = ((ScopeMetadataUtils.SpecificationScopeMetadata) scope)
                .specification();

        return new ResponseEntity(
                countItemsBySpecification(repositoryInvoker, and(scopeSpecification, filterSpecification)),
                HttpStatus.OK);
    }

    return new ResponseEntity(countItemsBySpecification(repositoryInvoker, filterSpecification), HttpStatus.OK);
}

From source file:org.lightadmin.core.web.RepositoryScopedSearchController.java

@RequestMapping(value = BASE_MAPPING + "/search", method = GET)
public ResponseEntity<?> filterEntities(
        DomainTypeAdministrationConfiguration domainTypeAdministrationConfiguration,
        RootResourceInformation repoRequest, PersistentEntityResourceAssembler assembler, WebRequest request,
        Pageable pageable, @PathVariable String scopeName) throws Exception {
    DynamicRepositoryInvoker repositoryInvoker = (DynamicRepositoryInvoker) repoRequest.getInvoker();
    PersistentEntity<?, ?> persistentEntity = repoRequest.getPersistentEntity();

    final ScopeMetadata scope = domainTypeAdministrationConfiguration.getScopes().getScope(scopeName);

    final Specification filterSpecification = specificationFromRequest(request, persistentEntity);

    if (isPredicateScope(scope)) {
        final PredicateScopeMetadata predicateScope = (PredicateScopeMetadata) scope;

        final Page page = findBySpecificationAndPredicate(repositoryInvoker, filterSpecification,
                predicateScope.predicate(), pageable);

        Object resources = resultToResources(page, assembler);

        return new ResponseEntity(resources, HttpStatus.OK);
    }/*from   w ww .j  a  v  a 2s .  c  om*/

    if (isSpecificationScope(scope)) {
        final Specification scopeSpecification = ((ScopeMetadataUtils.SpecificationScopeMetadata) scope)
                .specification();

        Page page = findItemsBySpecification(repositoryInvoker, and(scopeSpecification, filterSpecification),
                pageable);

        Object resources = resultToResources(page, assembler);

        return new ResponseEntity(resources, HttpStatus.OK);
    }

    Page page = findItemsBySpecification(repositoryInvoker, filterSpecification, pageable);

    Object resources = resultToResources(page, assembler);

    return new ResponseEntity(resources, HttpStatus.OK);
}

From source file:org.springframework.data.rest.webmvc.alps.RootResourceInformationToAlpsDescriptorConverter.java

private Descriptor buildItemResourceDescriptor(RootResourceInformation resourceInformation,
        Descriptor representationDescriptor, HttpMethod method) {

    PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity();
    ResourceMetadata metadata = associations.getMetadataFor(entity.getType());

    return descriptor().//
            id(prefix(method).concat(metadata.getItemResourceRel())).//
            name(metadata.getItemResourceRel()).//
            type(getType(method)).//
            doc(getDocFor(metadata.getItemResourceDescription())).//
            rt("#".concat(representationDescriptor.getId())). //
            descriptors(getProjectionDescriptor(entity.getType(), method)).//
            build();/*from ww w .  j  ava2 s.co  m*/
}

From source file:org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.java

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

    RootResourceInformation resourceInformation = resourceInformationResolver.resolveArgument(parameter,
            mavContainer, webRequest, binderFactory);

    HttpServletRequest nativeRequest = webRequest.getNativeRequest(HttpServletRequest.class);
    ServletServerHttpRequest request = new ServletServerHttpRequest(nativeRequest);
    IncomingRequest incoming = new IncomingRequest(request);

    Class<?> domainType = resourceInformation.getDomainType();
    MediaType contentType = request.getHeaders().getContentType();

    for (HttpMessageConverter converter : messageConverters) {

        if (!converter.canRead(PersistentEntityResource.class, contentType)) {
            continue;
        }/*from w  ww.  j  av a 2s. c o  m*/

        Serializable id = idResolver.resolveArgument(parameter, mavContainer, webRequest, binderFactory);
        Object objectToUpdate = getObjectToUpdate(id, resourceInformation);

        boolean forUpdate = false;
        Object entityIdentifier = null;
        PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity();

        if (objectToUpdate != null) {
            forUpdate = true;
            entityIdentifier = entity.getIdentifierAccessor(objectToUpdate).getIdentifier();
        }

        Object obj = read(resourceInformation, incoming, converter, objectToUpdate);

        if (obj == null) {
            throw new HttpMessageNotReadableException(String.format(ERROR_MESSAGE, domainType));
        }

        if (entityIdentifier != null) {
            entity.getPropertyAccessor(obj).setProperty(entity.getIdProperty(), entityIdentifier);
        }

        Builder build = PersistentEntityResource.build(obj, entity);
        return forUpdate ? build.build() : build.forCreation();
    }

    throw new HttpMessageNotReadableException(String.format(NO_CONVERTER_FOUND, domainType, contentType));
}

From source file:org.springframework.data.rest.webmvc.alps.RootResourceInformationToAlpsDescriptorConverter.java

public Alps convert(RootResourceInformation resourceInformation) {

    Class<?> type = resourceInformation.getDomainType();
    List<Descriptor> descriptors = new ArrayList<Descriptor>();

    Descriptor representationDescriptor = buildRepresentationDescriptor(type);

    descriptors.add(representationDescriptor);

    SupportedHttpMethods supportedHttpMethods = resourceInformation.getSupportedMethods();

    for (HttpMethod method : supportedHttpMethods.getMethodsFor(ResourceType.COLLECTION)) {

        if (!UNDOCUMENTED_METHODS.contains(method)) {
            descriptors.add(buildCollectionResourceDescriptor(type, resourceInformation,
                    representationDescriptor, method));
        }/*w w w.ja  v  a 2  s.c o m*/
    }

    for (HttpMethod method : supportedHttpMethods.getMethodsFor(ResourceType.ITEM)) {

        if (!UNDOCUMENTED_METHODS.contains(method)) {
            descriptors.add(buildItemResourceDescriptor(resourceInformation, representationDescriptor, method));
        }
    }

    descriptors.addAll(buildSearchResourceDescriptors(resourceInformation.getPersistentEntity()));

    return Alps.alps().descriptors(descriptors).build();
}