Class ConverterService
- java.lang.Object
-
- org.dspace.app.rest.converter.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, applyingProjections where applicable.- Author:
- Luca Giamminonni (luca.giamminonni at 4science dot it)
-
-
Constructor Summary
Constructors Constructor Description ConverterService()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ProjectiongetProjection(String projectionName)Gets the projection with the given name, or the default (no-op) projection if null is given.<T extends HALResource>
TtoResource(RestModel restObject)Converts the given rest object to aHALResourceobject.<T extends HALResource>
TtoResource(RestModel restObject, org.springframework.hateoas.Link... oldLinks)Converts the given rest object to aHALResourceobject.<M,R>
RtoRest(M modelObject, Projection projection)Converts the given model object to a rest object, using the appropriateDSpaceConverterand 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 givenProjectionThis 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 givenProjection.
-
-
-
Method Detail
-
toRest
public <M,R> R toRest(M modelObject, Projection projection)Converts the given model object to a rest object, using the appropriateDSpaceConverterand the given projection.The projection's
Projection.transformModel(Object)method will be automatically applied before conversion. If the rest object is aRestModel, the projection'sProjection.transformRest(RestModel)method will be automatically called after conversion.- Type Parameters:
M- the type of model object. A converterComponentmust 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, itsRestAddressableModel.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 givenProjection.- 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 givenProjectionThis 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 objectsprojection- the projection to use.- Returns:
- the page.
-
toResource
public <T extends HALResource> T toResource(RestModel restObject)
Converts the given rest object to aHALResourceobject.If the rest object is a
RestAddressableModel, the projection returned byRestAddressableModel.getProjection()will be used to determine which optional embeds and links will be added, andProjection.transformResource(HALResource)will be automatically called before returning the final, fully converted resource.In all cases, the
HalLinkServicewill be used immediately after the resource is constructed, to ensure allHalLinkFactorys have had a chance to add links as needed.- Type Parameters:
T- the return type, a subclass ofHALResource.- Parameters:
restObject- the input rest object.- Returns:
- the fully converted resource, with all automatic links and embeds applied.
- Throws:
IllegalArgumentException- if there is no compatible resource constructor.ClassCastException- if the resource 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 aHALResourceobject.If the rest object is a
RestAddressableModel, the projection returned byRestAddressableModel.getProjection()will be used to determine which optional embeds and links will be added, andProjection.transformResource(HALResource)will be automatically called before returning the final, fully converted resource.In all cases, the
HalLinkServicewill be used immediately after the resource is constructed, to ensure allHalLinkFactorys have had a chance to add links as needed.- Type Parameters:
T- the return type, a subclass ofHALResource.- 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, ornull.- Returns:
- the projection with the given name, or
DefaultProjectionifnullis given. - Throws:
IllegalArgumentException- if a name is provided and such a projection cannot be found.
-
-