Package org.springframework.data.rest.core.invoke

Examples of org.springframework.data.rest.core.invoke.RepositoryInvoker


    ResourceMetadata resourceMetadata = resourceMetadataResolver.resolveArgument(parameter, mavContainer, webRequest,
        binderFactory);

    Class<?> domainType = resourceMetadata.getDomainType();
    RepositoryInvoker repositoryInvoker = invokerFactory.getInvokerFor(domainType);
    PersistentEntity<?, ?> persistentEntity = repositories.getPersistentEntity(domainType);

    // TODO reject if ResourceMetadata cannot be resolved
    return new RootResourceInformation(resourceMetadata, persistentEntity, repositoryInvoker);
  }
View Full Code Here


  @RequestMapping(value = BASE_MAPPING, method = RequestMethod.DELETE)
  public ResponseEntity<? extends ResourceSupport> deletePropertyReference(final RootResourceInformation repoRequest,
      @BackendId Serializable id, @PathVariable String property) throws Exception {

    final RepositoryInvoker repoMethodInvoker = repoRequest.getInvoker();

    if (!repoMethodInvoker.exposesDelete()) {
      return new ResponseEntity<Resource<?>>(HttpStatus.METHOD_NOT_ALLOWED);
    }

    Function<ReferencedProperty, ResourceSupport> handler = new Function<ReferencedProperty, ResourceSupport>() {

      @Override
      public Resource<?> apply(ReferencedProperty prop) throws HttpRequestMethodNotSupportedException {

        if (null == prop.propertyValue) {
          return null;
        }

        if (prop.property.isCollectionLike()) {
          throw new HttpRequestMethodNotSupportedException("DELETE");
        } else if (prop.property.isMap()) {
          throw new HttpRequestMethodNotSupportedException("DELETE");
        } else {
          prop.wrapper.setProperty(prop.property, null);
        }

        publisher.publishEvent(new BeforeLinkDeleteEvent(prop.wrapper.getBean(), prop.propertyValue));
        Object result = repoMethodInvoker.invokeSave(prop.wrapper.getBean());
        publisher.publishEvent(new AfterLinkDeleteEvent(result, prop.propertyValue));

        return null;
      }
    };
View Full Code Here

  public ResponseEntity<? extends ResourceSupport> createPropertyReference(
      final RootResourceInformation resourceInformation, final HttpMethod requestMethod,
      final @RequestBody Resources<Object> incoming, @BackendId Serializable id, @PathVariable String property)
      throws Exception {

    final RepositoryInvoker invoker = resourceInformation.getInvoker();

    Function<ReferencedProperty, ResourceSupport> handler = new Function<ReferencedProperty, ResourceSupport>() {

      @Override
      public ResourceSupport apply(ReferencedProperty prop) throws HttpRequestMethodNotSupportedException {

        Class<?> propertyType = prop.property.getType();

        if (prop.property.isCollectionLike()) {

          Collection<Object> coll = CollectionFactory.createCollection(propertyType, 0);

          // Either load the exist collection to add to it (PATCH)
          if (HttpMethod.PATCH.equals(requestMethod)) {
            coll = (Collection<Object>) prop.propertyValue;
          }

          // Add to the existing collection
          for (Link l : incoming.getLinks()) {
            Object propVal = loadPropertyValue(prop.propertyType, l);
            coll.add(propVal);
          }

          prop.wrapper.setProperty(prop.property, coll);

        } else if (prop.property.isMap()) {

          Map<String, Object> m = CollectionFactory.createMap(propertyType, 0);

          // Either load the exist collection to add to it (PATCH)
          if (HttpMethod.PATCH.equals(requestMethod)) {
            m = (Map<String, Object>) prop.propertyValue;
          }

          // Add to the existing collection
          for (Link l : incoming.getLinks()) {
            Object propVal = loadPropertyValue(prop.propertyType, l);
            m.put(l.getRel(), propVal);
          }

          prop.wrapper.setProperty(prop.property, m);

        } else {

          if (HttpMethod.PATCH.equals(requestMethod)) {
            throw new HttpRequestMethodNotSupportedException(HttpMethod.PATCH.name(), new String[] { "PATCH" },
                "Cannot PATCH a reference to this singular property since the property type is not a List or a Map.");
          }

          if (incoming.getLinks().size() != 1) {
            throw new IllegalArgumentException(
                "Must send only 1 link to update a property reference that isn't a List or a Map.");
          }

          Object propVal = loadPropertyValue(prop.propertyType, incoming.getLinks().get(0));
          prop.wrapper.setProperty(prop.property, propVal);
        }

        publisher.publishEvent(new BeforeLinkSaveEvent(prop.wrapper.getBean(), prop.propertyValue));
        Object result = invoker.invokeSave(prop.wrapper.getBean());
        publisher.publishEvent(new AfterLinkSaveEvent(result, prop.propertyValue));

        return null;
      }
    };
View Full Code Here

  @ResponseBody
  public ResponseEntity<ResourceSupport> deletePropertyReferenceId(final RootResourceInformation repoRequest,
      @BackendId Serializable id, @PathVariable String property, final @PathVariable String propertyId)
      throws Exception {

    final RepositoryInvoker invoker = repoRequest.getInvoker();

    if (!invoker.exposesSave()) {
      throw new HttpRequestMethodNotSupportedException(HttpMethod.DELETE.name());
    }

    Function<ReferencedProperty, ResourceSupport> handler = new Function<ReferencedProperty, ResourceSupport>() {

      @Override
      public ResourceSupport apply(ReferencedProperty prop) {

        if (null == prop.propertyValue) {
          return null;
        }

        if (prop.property.isCollectionLike()) {
          Collection<Object> coll = (Collection<Object>) prop.propertyValue;
          Iterator<Object> itr = coll.iterator();
          while (itr.hasNext()) {
            Object obj = itr.next();
            BeanWrapper<Object> propValWrapper = BeanWrapper.create(obj, null);
            String s = propValWrapper.getProperty(prop.entity.getIdProperty()).toString();
            if (propertyId.equals(s)) {
              itr.remove();
            }
          }
        } else if (prop.property.isMap()) {
          Map<Object, Object> m = (Map<Object, Object>) prop.propertyValue;
          Iterator<Object> itr = m.keySet().iterator();
          while (itr.hasNext()) {
            Object key = itr.next();
            BeanWrapper<Object> propValWrapper = BeanWrapper.create(m.get(key), null);
            String s = propValWrapper.getProperty(prop.entity.getIdProperty()).toString();
            if (propertyId.equals(s)) {
              itr.remove();
            }
          }
        } else {
          prop.wrapper.setProperty(prop.property, null);
        }

        publisher.publishEvent(new BeforeLinkDeleteEvent(prop.wrapper.getBean(), prop.propertyValue));
        Object result = invoker.invokeSave(prop.wrapper.getBean());
        publisher.publishEvent(new AfterLinkDeleteEvent(result, prop.propertyValue));

        return null;
      }
    };
View Full Code Here

  }

  private ResourceSupport doWithReferencedProperty(RootResourceInformation repoRequest, Serializable id,
      String propertyPath, Function<ReferencedProperty, ResourceSupport> handler, HttpMethod method) throws Exception {

    RepositoryInvoker invoker = repoRequest.getInvoker();

    if (!invoker.exposesFindOne()) {
      throw new HttpRequestMethodNotSupportedException(method.name());
    }

    Object domainObj = invoker.invokeFindOne(id);

    if (null == domainObj) {
      throw new ResourceNotFoundException();
    }
View Full Code Here

  public ResponseEntity<?> headCollectionResource(RootResourceInformation resourceInformation)
      throws HttpRequestMethodNotSupportedException {

    resourceInformation.verifySupportedMethod(HttpMethod.HEAD, ResourceType.COLLECTION);

    RepositoryInvoker invoker = resourceInformation.getInvoker();

    if (null == invoker) {
      throw new ResourceNotFoundException();
    }
View Full Code Here

      DefaultedPageable pageable, Sort sort, PersistentEntityResourceAssembler assembler)
      throws ResourceNotFoundException, HttpRequestMethodNotSupportedException {

    resourceInformation.verifySupportedMethod(HttpMethod.GET, ResourceType.COLLECTION);

    RepositoryInvoker invoker = resourceInformation.getInvoker();

    if (null == invoker) {
      throw new ResourceNotFoundException();
    }

    Iterable<?> results;

    if (pageable.getPageable() != null) {
      results = invoker.invokeFindAll(pageable.getPageable());
    } else {
      results = invoker.invokeFindAll(sort);
    }

    ResourceMetadata metadata = resourceInformation.getResourceMetadata();
    SearchResourceMappings searchMappings = metadata.getSearchResourceMappings();
    List<Link> links = new ArrayList<Link>();
View Full Code Here

    // Force ID on unmarshalled object
    BeanWrapper<Object> incomingWrapper = BeanWrapper.create(payload.getContent(), conversionService);
    incomingWrapper.setProperty(payload.getPersistentEntity().getIdProperty(), id);

    RepositoryInvoker invoker = resourceInformation.getInvoker();
    Object objectToSave = incomingWrapper.getBean();

    return invoker.invokeFindOne(id) == null ? createAndReturn(objectToSave, invoker, assembler) : saveAndReturn(
        objectToSave, invoker, PUT, assembler);
  }
View Full Code Here

  public ResponseEntity<?> deleteItemResource(RootResourceInformation resourceInformation, @BackendId Serializable id)
      throws ResourceNotFoundException, HttpRequestMethodNotSupportedException {

    resourceInformation.verifySupportedMethod(HttpMethod.DELETE, ResourceType.ITEM);

    RepositoryInvoker invoker = resourceInformation.getInvoker();
    Object domainObj = invoker.invokeFindOne(id);

    if (domainObj == null) {
      throw new ResourceNotFoundException();
    }

    publisher.publishEvent(new BeforeDeleteEvent(domainObj));
    invoker.invokeDelete(id);
    publisher.publishEvent(new AfterDeleteEvent(domainObj));

    return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
  }
View Full Code Here

  private Object getItemResource(RootResourceInformation resourceInformation, Serializable id)
      throws HttpRequestMethodNotSupportedException, ResourceNotFoundException {

    resourceInformation.verifySupportedMethod(HttpMethod.GET, ResourceType.ITEM);

    RepositoryInvoker repoMethodInvoker = resourceInformation.getInvoker();

    if (!repoMethodInvoker.exposesFindOne()) {
      throw new ResourceNotFoundException();
    }

    return repoMethodInvoker.invokeFindOne(id);
  }
View Full Code Here

TOP

Related Classes of org.springframework.data.rest.core.invoke.RepositoryInvoker

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.