org.odata4j.producer.inmemory
Class InMemoryProducer

java.lang.Object
  extended by org.odata4j.producer.inmemory.InMemoryProducer
All Implemented Interfaces:
OExtensible<ODataProducer>, ODataProducer

public class InMemoryProducer
extends Object
implements ODataProducer

An in-memory implementation of an ODATA Producer. Uses the standard Java bean and property model to access information within entities.


Nested Class Summary
static class InMemoryProducer.RequestContext
           
 
Field Summary
static String ID_PROPNAME
           
 
Constructor Summary
InMemoryProducer(String namespace)
          Creates a new instance of an in-memory POJO producer.
InMemoryProducer(String namespace, int maxResults)
          Creates a new instance of an in-memory POJO producer.
InMemoryProducer(String namespace, String containerName, int maxResults, EdmDecorator decorator, InMemoryTypeMapping typeMapping)
          Creates a new instance of an in-memory POJO producer.
InMemoryProducer(String namespace, String containerName, int maxResults, EdmDecorator decorator, InMemoryTypeMapping typeMapping, boolean flattenEdm)
           
 
Method Summary
protected  void addPropertiesFromObject(Object obj, PropertyModel propertyModel, EdmStructuralType structuralType, List<OProperty<?>> properties, PropertyPathHelper pathHelper)
          Transforms a POJO into a list of OProperties based on a given EdmStructuralType.
 BaseResponse callFunction(EdmFunctionImport name, Map<String,OFunctionParameter> params, QueryInfo queryInfo)
          Calls a function (aka Service Operation).
 void close()
          Releases any resources managed by this producer.
 EntityResponse createEntity(String entitySetName, OEntity entity)
          Creates a new OData entity.
 EntityResponse createEntity(String entitySetName, OEntityKey entityKey, String navProp, OEntity entity)
          Creates a new OData entity as a reference of an existing entity, implicitly linked to the existing entity by a navigation property.
 void createLink(OEntityId sourceEntity, String targetNavProp, OEntityId targetEntity)
          Creates a link between two entities.
 void deleteEntity(String entitySetName, OEntityKey entityKey)
          Deletes an existing entity.
 void deleteLink(OEntityId sourceEntity, String targetNavProp, OEntityKey targetEntityKey)
          Deletes an existing link between two entities.
protected
<T> T
fillInPojo(OStructuralObject sobj, EdmStructuralType stype, PropertyModel propertyModel, Class<T> pojoClass)
          Populates a new POJO instance of type pojoClass using data from the given structural object.
protected  InMemoryComplexTypeInfo<?> findComplexTypeInfoForClass(Class<?> clazz)
           
protected  InMemoryEntityInfo<?> findEntityInfoForClass(Class<?> clazz)
           
protected  InMemoryEntityInfo<?> findEntityInfoForEntitySet(String entitySetName)
           
protected  EdmEntitySet findEntitySetForNavProperty(EdmNavigationProperty navProp)
           
<TExtension extends OExtension<ODataProducer>>
TExtension
findExtension(Class<TExtension> clazz)
          Finds an extension instance given an interface, if one exists.
protected  void fireUnmarshalEvent(Object pojo, OStructuralObject sobj, org.odata4j.producer.inmemory.InMemoryProducer.TriggerType ttype)
           
 String getContainerName()
           
 EntitiesResponse getEntities(String entitySetName, QueryInfo queryInfo)
          Gets all the entities for a given set matching the query information.
 CountResponse getEntitiesCount(String entitySetName, QueryInfo queryInfo)
          Gets the count of all the entities for a given set matching the query information.
protected  EntitiesResponse getEntitiesResponse(InMemoryProducer.RequestContext rc, EdmEntitySet targetEntitySet, Enumerable<Object> objects, PropertyModel propertyModel)
           
 EntityResponse getEntity(String entitySetName, OEntityKey entityKey, EntityQueryInfo queryInfo)
          Obtains a single entity based on its type and key.
protected  Object getEntityPojo(InMemoryProducer.RequestContext rc)
          Given an entity set and an entity key, returns the pojo that is that entity instance.
 EntityIdResponse getLinks(OEntityId sourceEntity, String targetNavProp)
          Returns the value of an entity's navigation property as a collection of entity links (or a single link if the association cardinality is 1).
 EdmDataServices getMetadata()
          Obtains the service metadata for this producer.
 MetadataProducer getMetadataProducer()
          Obtains the ODataProducer implementation that serves the metadata as OData EDM constructs.
protected  BaseResponse getNavProperty(EdmNavigationProperty navProp, InMemoryProducer.RequestContext rc)
          Gets the entity(s) on the target end of a NavigationProperty.
 BaseResponse getNavProperty(String entitySetName, OEntityKey entityKey, String navProp, QueryInfo queryInfo)
          Given a specific entity, follow one of its navigation properties, applying constraints as appropriate.
 CountResponse getNavPropertyCount(String entitySetName, OEntityKey entityKey, String navProp, QueryInfo queryInfo)
          Given a specific entity, follow one of its navigation properties, applying constraints as appropriate.
protected  Iterable<?> getRelatedPojos(EdmNavigationProperty navProp, Object srcObject, InMemoryEntityInfo<?> srcInfo)
           
 void mergeEntity(String entitySetName, OEntity entity)
          Modifies an existing entity using merge semantics.
protected  InMemoryEdmGenerator newEdmGenerator(String namespace, InMemoryTypeMapping typeMapping, String idPropName, Map<String,InMemoryEntityInfo<?>> eis, Map<String,InMemoryComplexTypeInfo<?>> complexTypesInfo)
           
<TEntity,TKey>
void
register(Class<TEntity> entityClass, Class<TKey> keyClass, String entitySetName, Func<Iterable<TEntity>> get, Func1<TEntity,TKey> id)
          Registers a new entity set based on a POJO type using the default property model.
<TEntity,TKey>
void
register(Class<TEntity> entityClass, PropertyModel propertyModel, String entitySetName, Func<Iterable<TEntity>> get, String... keys)
          Registers a new entity set based on a POJO type and a property model.
<TEntity> void
register(Class<TEntity> entityClass, PropertyModel propertyModel, String entitySetName, String entityTypeName, Func<Iterable<TEntity>> get, Func1<InMemoryProducer.RequestContext,Iterable<TEntity>> getWithContext, String... keys)
           
<TEntity> void
register(Class<TEntity> entityClass, PropertyModel propertyModel, String entitySetName, String entityTypeName, Func<Iterable<TEntity>> get, String... keys)
           
<TEntity> void
register(Class<TEntity> entityClass, String entitySetName, Func<Iterable<TEntity>> get, String... keys)
          Registers a new entity based on a POJO, with support for composite keys.
<TEntity> void
register(Class<TEntity> entityClass, String entitySetName, String entityTypeName, Func<Iterable<TEntity>> get, String... keys)
          Registers a new entity based on a POJO, with support for composite keys.
<TEntity> void
registerComplexType(Class<TEntity> complexTypeClass, String typeName)
          Registers a POJO class as an EdmComplexType.
<TEntity> void
registerComplexType(Class<TEntity> complexTypeClass, String typeName, PropertyModel propertyModel)
           
 void setIncludeNullPropertyValues(boolean value)
           
protected  OEntity toOEntity(EdmEntitySet ees, Object obj, PropertyPathHelper pathHelper)
           
<T> T
toPojo(OComplexObject entity, Class<T> pojoClass)
          Transforms an OComplexObject into a POJO of the given class
<T> T
toPojo(OEntity entity, Class<T> pojoClass)
          Transforms the given entity into a POJO of type pojoClass.
 void updateEntity(String entitySetName, OEntity entity)
          Modifies an existing entity using update semantics.
 void updateLink(OEntityId sourceEntity, String targetNavProp, OEntityKey oldTargetEntityKey, OEntityId newTargetEntity)
          Replaces an existing link between two entities.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ID_PROPNAME

public static final String ID_PROPNAME
See Also:
Constant Field Values
Constructor Detail

InMemoryProducer

public InMemoryProducer(String namespace)
Creates a new instance of an in-memory POJO producer.

Parameters:
namespace - the namespace of the schema registrations

InMemoryProducer

public InMemoryProducer(String namespace,
                        int maxResults)
Creates a new instance of an in-memory POJO producer.

Parameters:
namespace - the namespace of the schema registrations
maxResults - the maximum number of entities to return in a single call

InMemoryProducer

public InMemoryProducer(String namespace,
                        String containerName,
                        int maxResults,
                        EdmDecorator decorator,
                        InMemoryTypeMapping typeMapping)
Creates a new instance of an in-memory POJO producer.

Parameters:
namespace - the namespace of the schema registrations
containerName - the container name for generated metadata
maxResults - the maximum number of entities to return in a single call
decorator - a decorator to use for edm customizations
typeMapping - optional mapping between java types and edm types, null for default

InMemoryProducer

public InMemoryProducer(String namespace,
                        String containerName,
                        int maxResults,
                        EdmDecorator decorator,
                        InMemoryTypeMapping typeMapping,
                        boolean flattenEdm)
Method Detail

addPropertiesFromObject

protected void addPropertiesFromObject(Object obj,
                                       PropertyModel propertyModel,
                                       EdmStructuralType structuralType,
                                       List<OProperty<?>> properties,
                                       PropertyPathHelper pathHelper)
Transforms a POJO into a list of OProperties based on a given EdmStructuralType.

Parameters:
obj - the POJO to transform
propertyModel - the PropertyModel to use to access POJO class structure and values.
structuralType - the EdmStructuralType
properties - put properties into this list.

callFunction

public BaseResponse callFunction(EdmFunctionImport name,
                                 Map<String,OFunctionParameter> params,
                                 QueryInfo queryInfo)
Description copied from interface: ODataProducer
Calls a function (aka Service Operation).

Specified by:
callFunction in interface ODataProducer
Parameters:
name - the name of the function
params - the parameters to the function
queryInfo - additional query parameters to apply to collection-valued results
Returns:
a BaseResponse appropriately typed to hold the function results From the spec:
    The return type of <Function> MUST be one of the following:
        An EDMSimpleType or collection of EDMSimpleTypes.
        An entity type or collection of entity types.
        A complex type or collection of complex types.
        A row type or collection of row types.
        <ReturnType> can contain a maximum of one <CollectionType> element.
        <ReturnType> can contain a maximum of one <ReferenceType> element.
        <ReturnType> can contain a maximum of one <RowType> element.
        A ref type or collection of ref types.

close

public void close()
Description copied from interface: ODataProducer
Releases any resources managed by this producer.

Specified by:
close in interface ODataProducer

createEntity

public EntityResponse createEntity(String entitySetName,
                                   OEntity entity)
Description copied from interface: ODataProducer
Creates a new OData entity.

Specified by:
createEntity in interface ODataProducer
Parameters:
entitySetName - the entity-set name
entity - the request entity sent from the client
Returns:
the newly-created entity, fully populated with the key and default properties
See Also:
[odata.org] Creating new Entries

createEntity

public EntityResponse createEntity(String entitySetName,
                                   OEntityKey entityKey,
                                   String navProp,
                                   OEntity entity)
Description copied from interface: ODataProducer
Creates a new OData entity as a reference of an existing entity, implicitly linked to the existing entity by a navigation property.

Specified by:
createEntity in interface ODataProducer
Parameters:
entitySetName - the entity-set name of the existing entity
entityKey - the entity-key of the existing entity
navProp - the navigation property off of the existing entity
entity - the request entity sent from the client
Returns:
the newly-created entity, fully populated with the key and default properties, and linked to the existing entity
See Also:
[odata.org] Creating new Entries

createLink

public void createLink(OEntityId sourceEntity,
                       String targetNavProp,
                       OEntityId targetEntity)
Description copied from interface: ODataProducer
Creates a link between two entities.

Specified by:
createLink in interface ODataProducer
Parameters:
sourceEntity - an entity with at least one navigation property
targetNavProp - the navigation property
targetEntity - the link target entity
See Also:
[odata.org] Creating Links between Entries

deleteEntity

public void deleteEntity(String entitySetName,
                         OEntityKey entityKey)
Description copied from interface: ODataProducer
Deletes an existing entity.

Specified by:
deleteEntity in interface ODataProducer
Parameters:
entitySetName - the entity-set name of the entity
entityKey - the entity-key of the entity
See Also:
[odata.org] Deleting Entries

deleteLink

public void deleteLink(OEntityId sourceEntity,
                       String targetNavProp,
                       OEntityKey targetEntityKey)
Description copied from interface: ODataProducer
Deletes an existing link between two entities.

Specified by:
deleteLink in interface ODataProducer
Parameters:
sourceEntity - an entity with at least one navigation property
targetNavProp - the navigation property
targetEntityKey - if the navigation property represents a set, the key identifying the target entity within the set, else n/a

fillInPojo

protected <T> T fillInPojo(OStructuralObject sobj,
                           EdmStructuralType stype,
                           PropertyModel propertyModel,
                           Class<T> pojoClass)
                throws InstantiationException,
                       IllegalAccessException,
                       IllegalArgumentException,
                       InvocationTargetException
Populates a new POJO instance of type pojoClass using data from the given structural object.

Throws:
InstantiationException
IllegalAccessException
IllegalArgumentException
InvocationTargetException

findComplexTypeInfoForClass

protected InMemoryComplexTypeInfo<?> findComplexTypeInfoForClass(Class<?> clazz)

findEntityInfoForClass

protected InMemoryEntityInfo<?> findEntityInfoForClass(Class<?> clazz)

findEntityInfoForEntitySet

protected InMemoryEntityInfo<?> findEntityInfoForEntitySet(String entitySetName)

findEntitySetForNavProperty

protected EdmEntitySet findEntitySetForNavProperty(EdmNavigationProperty navProp)

findExtension

public <TExtension extends OExtension<ODataProducer>> TExtension findExtension(Class<TExtension> clazz)
Description copied from interface: OExtensible
Finds an extension instance given an interface, if one exists.

Specified by:
findExtension in interface OExtensible<ODataProducer>
Type Parameters:
TExtension - type of extension
Parameters:
clazz - the extension interface
Returns:
the extension instance, or null if no extension exists for this type

fireUnmarshalEvent

protected void fireUnmarshalEvent(Object pojo,
                                  OStructuralObject sobj,
                                  org.odata4j.producer.inmemory.InMemoryProducer.TriggerType ttype)
                           throws IllegalAccessException,
                                  IllegalArgumentException,
                                  InvocationTargetException
Throws:
IllegalAccessException
IllegalArgumentException
InvocationTargetException

getContainerName

public String getContainerName()

getEntities

public EntitiesResponse getEntities(String entitySetName,
                                    QueryInfo queryInfo)
Description copied from interface: ODataProducer
Gets all the entities for a given set matching the query information.

Specified by:
getEntities in interface ODataProducer
Parameters:
entitySetName - the entity-set name for entities to return
queryInfo - the additional constraints to apply to the entities
Returns:
a packaged collection of entities to pass back to the client

getEntitiesCount

public CountResponse getEntitiesCount(String entitySetName,
                                      QueryInfo queryInfo)
Description copied from interface: ODataProducer
Gets the count of all the entities for a given set matching the query information.

Specified by:
getEntitiesCount in interface ODataProducer
Parameters:
entitySetName - the entity-set name for entities whose count is returned
queryInfo - the additional constraints to apply to the entities
Returns:
count of the entities

getEntitiesResponse

protected EntitiesResponse getEntitiesResponse(InMemoryProducer.RequestContext rc,
                                               EdmEntitySet targetEntitySet,
                                               Enumerable<Object> objects,
                                               PropertyModel propertyModel)

getEntity

public EntityResponse getEntity(String entitySetName,
                                OEntityKey entityKey,
                                EntityQueryInfo queryInfo)
Description copied from interface: ODataProducer
Obtains a single entity based on its type and key.

Specified by:
getEntity in interface ODataProducer
Parameters:
entitySetName - the entity-set name for entities to return
entityKey - the unique entity-key of the entity to start with
queryInfo - the additional constraints applicable to single-entity queries
Returns:
the resulting entity

getEntityPojo

protected Object getEntityPojo(InMemoryProducer.RequestContext rc)
Given an entity set and an entity key, returns the pojo that is that entity instance. The default implementation iterates over the entire set of pojos to find the desired instance.

Parameters:
rc - the current ReqeustContext, may be valuable to the ei.getWithContext impl
Returns:
the pojo

getLinks

public EntityIdResponse getLinks(OEntityId sourceEntity,
                                 String targetNavProp)
Description copied from interface: ODataProducer
Returns the value of an entity's navigation property as a collection of entity links (or a single link if the association cardinality is 1).

Specified by:
getLinks in interface ODataProducer
Parameters:
sourceEntity - an entity with at least one navigation property
targetNavProp - the navigation property
Returns:
a collection of entity links (or a single link if the association cardinality is 1)

getMetadata

public EdmDataServices getMetadata()
Description copied from interface: ODataProducer
Obtains the service metadata for this producer.

Specified by:
getMetadata in interface ODataProducer
Returns:
a fully-constructed metadata object

getMetadataProducer

public MetadataProducer getMetadataProducer()
Description copied from interface: ODataProducer
Obtains the ODataProducer implementation that serves the metadata as OData EDM constructs.

Specified by:
getMetadataProducer in interface ODataProducer
Returns:
the MetadataProducer object

getNavProperty

protected BaseResponse getNavProperty(EdmNavigationProperty navProp,
                                      InMemoryProducer.RequestContext rc)
Gets the entity(s) on the target end of a NavigationProperty.

Parameters:
navProp - the navigation property
rc - the request context
Returns:
a BaseResponse with either a single Entity (can be null) or a set of entities.

getNavProperty

public BaseResponse getNavProperty(String entitySetName,
                                   OEntityKey entityKey,
                                   String navProp,
                                   QueryInfo queryInfo)
Description copied from interface: ODataProducer
Given a specific entity, follow one of its navigation properties, applying constraints as appropriate. Return the resulting entity, entities, or property value.

Specified by:
getNavProperty in interface ODataProducer
Parameters:
entitySetName - the entity-set of the entity to start with
entityKey - the unique entity-key of the entity to start with
navProp - the navigation property to follow
queryInfo - additional constraints to apply to the result
Returns:
the resulting entity, entities, or property value

getNavPropertyCount

public CountResponse getNavPropertyCount(String entitySetName,
                                         OEntityKey entityKey,
                                         String navProp,
                                         QueryInfo queryInfo)
Description copied from interface: ODataProducer
Given a specific entity, follow one of its navigation properties, applying constraints as appropriate. Return the count of the resulting entities.

Specified by:
getNavPropertyCount in interface ODataProducer
Parameters:
entitySetName - the entity-set of the entity to start with
entityKey - the unique entity-key of the entity to start with
navProp - the navigation property to follow
queryInfo - additional constraints to apply to the result
Returns:
the count of the resulting entities

getRelatedPojos

protected Iterable<?> getRelatedPojos(EdmNavigationProperty navProp,
                                      Object srcObject,
                                      InMemoryEntityInfo<?> srcInfo)

mergeEntity

public void mergeEntity(String entitySetName,
                        OEntity entity)
Description copied from interface: ODataProducer
Modifies an existing entity using merge semantics.

Specified by:
mergeEntity in interface ODataProducer
Parameters:
entitySetName - the entity-set name
entity - the entity modifications sent from the client
See Also:
[odata.org] Updating Entries

newEdmGenerator

protected InMemoryEdmGenerator newEdmGenerator(String namespace,
                                               InMemoryTypeMapping typeMapping,
                                               String idPropName,
                                               Map<String,InMemoryEntityInfo<?>> eis,
                                               Map<String,InMemoryComplexTypeInfo<?>> complexTypesInfo)

register

public <TEntity,TKey> void register(Class<TEntity> entityClass,
                                    Class<TKey> keyClass,
                                    String entitySetName,
                                    Func<Iterable<TEntity>> get,
                                    Func1<TEntity,TKey> id)
Registers a new entity set based on a POJO type using the default property model.


register

public <TEntity,TKey> void register(Class<TEntity> entityClass,
                                    PropertyModel propertyModel,
                                    String entitySetName,
                                    Func<Iterable<TEntity>> get,
                                    String... keys)
Registers a new entity set based on a POJO type and a property model.

Parameters:
entityClass - the class of the entities that are to be stored in the set
propertyModel - a way to get/set properties on the POJO
entitySetName - the alias the set will be known by; this is what is used in the ODATA URL
get - a function to iterate over the elements in the set
keys - one or more keys for the entity

register

public <TEntity> void register(Class<TEntity> entityClass,
                               PropertyModel propertyModel,
                               String entitySetName,
                               String entityTypeName,
                               Func<Iterable<TEntity>> get,
                               Func1<InMemoryProducer.RequestContext,Iterable<TEntity>> getWithContext,
                               String... keys)

register

public <TEntity> void register(Class<TEntity> entityClass,
                               PropertyModel propertyModel,
                               String entitySetName,
                               String entityTypeName,
                               Func<Iterable<TEntity>> get,
                               String... keys)

register

public <TEntity> void register(Class<TEntity> entityClass,
                               String entitySetName,
                               Func<Iterable<TEntity>> get,
                               String... keys)
Registers a new entity based on a POJO, with support for composite keys.

Parameters:
entityClass - the class of the entities that are to be stored in the set
entitySetName - the alias the set will be known by; this is what is used in the OData url
get - a function to iterate over the elements in the set
keys - one or more keys for the entity

register

public <TEntity> void register(Class<TEntity> entityClass,
                               String entitySetName,
                               String entityTypeName,
                               Func<Iterable<TEntity>> get,
                               String... keys)
Registers a new entity based on a POJO, with support for composite keys.

Parameters:
entityClass - the class of the entities that are to be stored in the set
entitySetName - the alias the set will be known by; this is what is used in the OData url
entityTypeName - type name of the entity
get - a function to iterate over the elements in the set
keys - one or more keys for the entity

registerComplexType

public <TEntity> void registerComplexType(Class<TEntity> complexTypeClass,
                                          String typeName)
Registers a POJO class as an EdmComplexType.

Parameters:
complexTypeClass - The POJO Class
typeName - The name of the EdmComplexType

registerComplexType

public <TEntity> void registerComplexType(Class<TEntity> complexTypeClass,
                                          String typeName,
                                          PropertyModel propertyModel)

setIncludeNullPropertyValues

public void setIncludeNullPropertyValues(boolean value)

toOEntity

protected OEntity toOEntity(EdmEntitySet ees,
                            Object obj,
                            PropertyPathHelper pathHelper)

toPojo

public <T> T toPojo(OComplexObject entity,
                    Class<T> pojoClass)
         throws InstantiationException,
                IllegalAccessException,
                IllegalArgumentException,
                InvocationTargetException
Transforms an OComplexObject into a POJO of the given class

Throws:
InstantiationException
IllegalAccessException
IllegalArgumentException
InvocationTargetException

toPojo

public <T> T toPojo(OEntity entity,
                    Class<T> pojoClass)
         throws InstantiationException,
                IllegalAccessException,
                IllegalArgumentException,
                InvocationTargetException
Transforms the given entity into a POJO of type pojoClass.

Throws:
InstantiationException
IllegalAccessException
IllegalArgumentException
InvocationTargetException

updateEntity

public void updateEntity(String entitySetName,
                         OEntity entity)
Description copied from interface: ODataProducer
Modifies an existing entity using update semantics.

Specified by:
updateEntity in interface ODataProducer
Parameters:
entitySetName - the entity-set name
entity - the entity modifications sent from the client
See Also:
[odata.org] Updating Entries

updateLink

public void updateLink(OEntityId sourceEntity,
                       String targetNavProp,
                       OEntityKey oldTargetEntityKey,
                       OEntityId newTargetEntity)
Description copied from interface: ODataProducer
Replaces an existing link between two entities.

Specified by:
updateLink in interface ODataProducer
Parameters:
sourceEntity - an entity with at least one navigation property
targetNavProp - the navigation property
oldTargetEntityKey - if the navigation property represents a set, the key identifying the old target entity within the set, else n/a
newTargetEntity - the new link target entity
See Also:
[odata.org] Replacing Links between Entries


http://odata4j.org