MotechDataService

public interface MotechDataService<T>

This is a basic service interface with CRUD operations. Mainly it is used as super interface to create service interface related with the given entity schema in org.motechproject.mds.builder.EntityInfrastructureBuilder but it can be also used by other service interfaces inside this package.

Parameters:
  • <T> – the type of entity schema.

Methods

count

long count()

Gets the total number of instances.

Returns:number of instances

countForFilters

long countForFilters(Filters filters)

Gets a total number of instances, after being filtered by the given filter.

Parameters:
  • filters – filters to use
Returns:

number of filtered instances

create

T create(T object)

Creates the given instance in MDS.

Parameters:
  • object – instance to create
Returns:

created instance

createOrUpdate

T createOrUpdate(T object)

Updates the given instance in MDS if it exists (checks the presence of the instances id to verify that) or creates a new one if it doesn’t.

Parameters:
  • object – instance to update or create
Returns:

updated or created instance

delete

void delete(T object)

Deletes given instance from MDS.

Parameters:
  • object – instance to delete

delete

void delete(String primaryKeyName, Object value)

Deletes instance from MDS, by its primary key value.

Parameters:
  • primaryKeyName – datastore primary key name
  • value – value of the primary key

deleteAll

void deleteAll()

Removes all instances of type {@value T} from MDS.

deleteById

void deleteById(long id)

Deletes instance from MDS, by its id.

Parameters:
  • id – id of the instance to delete.

doInTransaction

<R> R doInTransaction(TransactionCallback<R> transactionCallback)

Allows to wrap several instructions into a single transaction. Developers should implement the TransactionCallback interface and override the TransactionCallback.doInTransaction(org.springframework.transaction.TransactionStatus) method with whatever should be done in the transaction.

Parameters:
  • transactionCallback – implementation of the TransactionCallback
  • <R> – type that should be returned from the transaction
Returns:

anything of type {@value R}. Left to the developer, implementing the transaction

evictAllCache

void evictAllCache()

Evicts all cached entities. This affects all entities.

evictCacheForInstance

void evictCacheForInstance(T instance)

Evicts cache for a single entity instance.

Parameters:
  • instance – the instance to clear the cache for

evictEntityCache

void evictEntityCache(boolean withSubclasses)

Evicts cache for the entity class of this data service.

Parameters:
  • withSubclasses – if true, the cache for subclasses of the entity will be also cleared

executeQuery

<R> R executeQuery(QueryExecution<R> queryExecution)

Allows to execute custom query in MDS. Users are supposed to implement the QueryExecution interface and override its QueryExecution.execute(javax.jdo.Query,org.motechproject.mds.util.InstanceSecurityRestriction) method with their custom behaviour.

Parameters:
  • queryExecution – implementation of the QueryExecution, with custom behaviour
  • <R> – type that should be returned from the custom query
Returns:

anything of type {@value R}. Left to the developer, implementing the custom query.

executeSQLQuery

<R> R executeSQLQuery(SqlQueryExecution<R> queryExecution)

Allows to execute custom SQL query in MDS. Users should implement the SqlQueryExecution interface and override its methods, defining their custom query.

Parameters:
  • queryExecution – implementation of the SqlQueryExecution
  • <R> – type that should be returned by the custom sql query
Returns:

anything of type {@value R}, left to the developer, implementing the custom sql query.

filter

List<T> filter(Filters filters, QueryParams queryParams)

Retrieves all instances of type {@value T} from MDS, filtered using specified filters and query params.

Parameters:
  • filters – filters to use filtering instances
  • queryParams – query parameters to use filtering instances
Returns:

a list of instances, filtered using specified parameters

findById

T findById(Long id)

Retrieves instance of type {@value T} and given id from MDS.

Parameters:
  • id – id of the instance
Returns:

instance with the given id

findTrashInstanceById

T findTrashInstanceById(Object instanceId, Object entityId)

Retrieves an instance, that has been moved to trash, by its id. These instances are not retrieved with other retrieve methods.

Parameters:
  • instanceId – id of the instance, that has been moved to trash
  • entityId – id of the entity
Returns:

instance of the given id, from trash

getClassType

Class<T> getClassType()

Returns class type assigned to this service.

Returns:class type

getDetachedField

Object getDetachedField(T instance, String fieldName)

Makes instance persistent and retrieves field values from that persisted instance.

Parameters:
  • instance – instance to retrieve field value from
  • fieldName – name of the field to retrieve
Returns:

value from the field

getVersionFieldName

String getVersionFieldName()

Returns the name of the version field. The version field is not required so this method can return null value.

Returns:the name of the version field

retrieve

T retrieve(String primaryKeyName, Object value)

Retrieves instance from MDS based on the value of the given primary key.

Parameters:
  • primaryKeyName – datastore primary key name
  • value – value of the primary key
Returns:

instance with the given value for the specified primary key

retrieveAll

List<T> retrieveAll()

Retrieves all instances of the {@value T} type.

Returns:all instances

retrieveAll

List<T> retrieveAll(QueryParams queryParams)

Retrieves all instances of the {@value T} type, that match the provided parameters.

Parameters:
  • queryParams – query parameters to be used retrieving instances
Returns:

all isntances matching query parameters

revertFromTrash

void revertFromTrash(Object newInstance, Object trash)

Brings back instance from trash. This will be in fact a new instance, that has got exactly the same values as previous instance, except of its id.

Parameters:
  • newInstance – new instance representation
  • trash – instance from the trash

update

T update(T object)

Updates the given instance in MDS.

Parameters:
  • object – instance to update
Returns:

updated instance

updateFromTransient

T updateFromTransient(T transientObject)

Returns the persistent instance, updated with the values from the transient instance. If there’s no instance of the id from the transient instance, it will create one.

Parameters:
  • transientObject – transient object, from which an update will take place
Returns:

persistent instance, updated with the values from the transient instance

updateFromTransient

T updateFromTransient(T transientObject, Set<String> fieldsToUpdate)

Returns the persistent instance, updated with the values from the transient instance. If there’s no instance of the id from the transient instance, it will create one. Only fields with the names passed to the method will be updated.

Parameters:
  • transientObject – transient object, from which an update will take place
  • fieldsToUpdate – set of field names that should be updated
Returns:

persistent instance, updated with the values from the transient instance