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.
    • 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,
                                                                              long total,
                                                                              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.
        total - the total number of items.
        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​(org.springframework.data.domain.Page<M> modelObjects,
                                                                              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 page of model objects.
        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.
      • 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.