Class ConverterService


  • @Service
    public class ConverterService
    extends Object
    Converts domain objects from the DSpace service layer to rest objects, and from rest objects to resource objects, applying Projections where applicable.
    Author:
    Luca Giamminonni (luca.giamminonni at 4science dot it)
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Projection getProjection​(String projectionName)
      Gets the projection with the given name, or the default (no-op) projection if null is given.
      <T extends HALResource>
      T
      toResource​(RestModel restObject)
      Converts the given rest object to a HALResource object.
      <T extends HALResource>
      T
      toResource​(RestModel restObject, org.springframework.hateoas.Link... oldLinks)
      Converts the given rest object to a HALResource object.
      <M,​R>
      R
      toRest​(M modelObject, Projection projection)
      Converts the given model object to a rest object, using the appropriate DSpaceConverter and the given projection.
      <M,​R>
      org.springframework.data.domain.Page<R>
      toRestPage​(List<M> modelObjects, org.springframework.data.domain.Pageable pageable, long total, Projection projection)
      Converts a list of ModelObjects to a page of Rest Objects using the given Projection This method differences in the sense that we define a total here instead of the size of the list because this method will be called if the list is limited through a DB call already and thus we need to give the total amount of records in the DB; not the size of the given list
      <M,​R>
      org.springframework.data.domain.Page<R>
      toRestPage​(List<M> modelObjects, org.springframework.data.domain.Pageable pageable, Projection projection)
      Converts a list of model objects to a page of rest objects using the given Projection.
    • Constructor Detail

      • ConverterService

        public ConverterService()
    • Method Detail

      • toRest

        public <M,​R> R toRest​(M modelObject,
                                    Projection projection)
        Converts the given model object to a rest object, using the appropriate DSpaceConverter and the given projection.

        The projection's Projection.transformModel(Object) method will be automatically applied before conversion. If the rest object is a RestModel, the projection's Projection.transformRest(RestModel) method will be automatically called after conversion.

        Type Parameters:
        M - the type of model object. A converter Component must exist that takes this as input.
        R - the inferred return type.
        Parameters:
        modelObject - the model object, which may be a JPA entity any other class for which a converter exists.
        projection - the projection to use.
        Returns:
        the converted object. If it's a RestAddressableModel, its RestAddressableModel.getProjection() will be set to the given projection.
        Throws:
        IllegalArgumentException - if there is no compatible converter.
        ClassCastException - if the converter's return type is not compatible with the inferred return type.
      • toRestPage

        public <M,​R> org.springframework.data.domain.Page<R> toRestPage​(List<M> modelObjects,
                                                                              org.springframework.data.domain.Pageable pageable,
                                                                              Projection projection)
        Converts a list of model objects to a page of rest objects using the given Projection.
        Type Parameters:
        M - the model object class.
        R - the rest object class.
        Parameters:
        modelObjects - the list of model objects.
        pageable - the pageable.
        projection - the projection to use.
        Returns:
        the page.
        Throws:
        IllegalArgumentException - if there is no compatible converter.
        ClassCastException - if the converter's return type is not compatible with the inferred return type.
      • toRestPage

        public <M,​R> org.springframework.data.domain.Page<R> toRestPage​(List<M> modelObjects,
                                                                              org.springframework.data.domain.Pageable pageable,
                                                                              long total,
                                                                              Projection projection)
        Converts a list of ModelObjects to a page of Rest Objects using the given Projection This method differences in the sense that we define a total here instead of the size of the list because this method will be called if the list is limited through a DB call already and thus we need to give the total amount of records in the DB; not the size of the given list
        Type Parameters:
        M - the model object class.
        R - the rest object class.
        Parameters:
        modelObjects - the list of model objects.
        pageable - the pageable.
        total - The total amount of objects
        projection - the projection to use.
        Returns:
        the page.
      • toResource

        public <T extends HALResource> T toResource​(RestModel restObject,
                                                    org.springframework.hateoas.Link... oldLinks)
        Converts the given rest object to a HALResource object.

        If the rest object is a RestAddressableModel, the projection returned by RestAddressableModel.getProjection() will be used to determine which optional embeds and links will be added, and Projection.transformResource(HALResource) will be automatically called before returning the final, fully converted resource.

        In all cases, the HalLinkService will be used immediately after the resource is constructed, to ensure all HalLinkFactorys have had a chance to add links as needed.

        Type Parameters:
        T - the return type, a subclass of HALResource.
        Parameters:
        restObject - the input rest object.
        oldLinks - The old links fo the Resource Object
        Returns:
        the fully converted resource, with all automatic links and embeds applied.
      • getProjection

        public Projection getProjection​(@Nullable
                                        String projectionName)
        Gets the projection with the given name, or the default (no-op) projection if null is given.
        Parameters:
        projectionName - the projection name, or null.
        Returns:
        the projection with the given name, or DefaultProjection if null is given.
        Throws:
        IllegalArgumentException - if a name is provided and such a projection cannot be found.