Class MemoryHelper

java.lang.Object
org.ofbiz.core.entity.MemoryHelper
All Implemented Interfaces:
GenericHelper

public class MemoryHelper extends Object implements GenericHelper
Partial GenericHelper implementation that is entirely memory-based, to be used for simple unit testing (can't do anything beyond searches for primary keys, findByOr and findByAnd).
Author:
Pat Lightbody
  • Constructor Details

    • MemoryHelper

      public MemoryHelper(String helperName)
  • Method Details

    • clearCache

      public static void clearCache()
    • getHelperName

      public String getHelperName()
      Description copied from interface: GenericHelper
      Gets the name of the server configuration that corresponds to this helper
      Specified by:
      getHelperName in interface GenericHelper
      Returns:
      server configuration name
    • create

      public GenericValue create(GenericValue value) throws GenericEntityException
      Description copied from interface: GenericHelper
      Creates a Entity in the form of a GenericValue and write it to the database
      Specified by:
      create in interface GenericHelper
      Returns:
      GenericValue instance containing the new instance
      Throws:
      GenericEntityException
    • create

      public GenericValue create(GenericPK primaryKey) throws GenericEntityException
      Throws:
      GenericEntityException
    • findByPrimaryKey

      public GenericValue findByPrimaryKey(GenericPK primaryKey) throws GenericEntityException
      Description copied from interface: GenericHelper
      Find a Generic Entity by its Primary Key
      Specified by:
      findByPrimaryKey in interface GenericHelper
      Parameters:
      primaryKey - The primary key to find by.
      Returns:
      The GenericValue corresponding to the primaryKey
      Throws:
      GenericEntityException
    • findByPrimaryKeyPartial

      public GenericValue findByPrimaryKeyPartial(GenericPK primaryKey, Set<String> keys) throws GenericEntityException
      Description copied from interface: GenericHelper
      Find a Generic Entity by its Primary Key and only returns the values requested by the passed keys (names)
      Specified by:
      findByPrimaryKeyPartial in interface GenericHelper
      Parameters:
      primaryKey - The primary key to find by.
      keys - The keys, or names, of the values to retrieve; only these values will be retrieved
      Returns:
      The GenericValue corresponding to the primaryKey
      Throws:
      GenericEntityException
    • findAllByPrimaryKeys

      public List<GenericValue> findAllByPrimaryKeys(List<? extends GenericPK> primaryKeys) throws GenericEntityException
      Description copied from interface: GenericHelper
      Find a number of Generic Value objects by their Primary Keys, all at once
      Specified by:
      findAllByPrimaryKeys in interface GenericHelper
      Parameters:
      primaryKeys - A List of primary keys to find by.
      Returns:
      List of GenericValue objects corresponding to the passed primaryKey objects
      Throws:
      GenericEntityException
    • removeByPrimaryKey

      public int removeByPrimaryKey(GenericPK primaryKey) throws GenericEntityException
      Description copied from interface: GenericHelper
      Remove a Generic Entity corresponding to the primaryKey
      Specified by:
      removeByPrimaryKey in interface GenericHelper
      Parameters:
      primaryKey - The primary key of the entity to remove.
      Returns:
      int representing number of rows effected by this operation
      Throws:
      GenericEntityException
    • findByAnd

      public List<GenericValue> findByAnd(ModelEntity modelEntity, Map<String,?> fields, List<String> orderBy) throws GenericEntityException
      Description copied from interface: GenericHelper
      Finds Generic Entity records by all of the specified fields (ie: combined using AND)
      Specified by:
      findByAnd in interface GenericHelper
      Parameters:
      modelEntity - The ModelEntity of the Entity as defined in the entity XML file
      fields - The fields of the named entity to query by with their corresponging values
      orderBy - The fields of the named entity to order the query by; optionally add a " ASC" for ascending or " DESC" for descending
      Returns:
      List of GenericValue instances that match the query
      Throws:
      GenericEntityException
    • findByAnd

      public List<GenericValue> findByAnd(ModelEntity modelEntity, List<? extends EntityCondition> expressions, List<String> orderBy) throws GenericEntityException
      Throws:
      GenericEntityException
    • findByLike

      public List<GenericValue> findByLike(ModelEntity modelEntity, Map<String,?> fields, List<String> orderBy) throws GenericEntityException
      Throws:
      GenericEntityException
    • findByOr

      public List<GenericValue> findByOr(ModelEntity modelEntity, Map<String,?> fields, List<String> orderBy) throws GenericEntityException
      Description copied from interface: GenericHelper
      Finds Generic Entity records by all of the specified fields (ie: combined using OR)
      Specified by:
      findByOr in interface GenericHelper
      Parameters:
      modelEntity - The ModelEntity of the Entity as defined in the entity XML file
      fields - The fields of the named entity to query by with their corresponging values
      orderBy - The fields of the named entity to order the query by; optionally add a " ASC" for ascending or " DESC" for descending
      Returns:
      List of GenericValue instances that match the query
      Throws:
      GenericEntityException
    • findByOr

      public List<GenericValue> findByOr(ModelEntity modelEntity, List<? extends EntityCondition> expressions, List<String> orderBy) throws GenericEntityException
      Throws:
      GenericEntityException
    • findByCondition

      public List<GenericValue> findByCondition(ModelEntity modelEntity, EntityCondition entityCondition, Collection<String> fieldsToSelect, List<String> orderBy) throws GenericEntityException
      Description copied from interface: GenericHelper
      Finds GenericValues by the conditions specified in the EntityCondition object, the the EntityCondition javadoc for more details.
      Specified by:
      findByCondition in interface GenericHelper
      Parameters:
      modelEntity - The ModelEntity of the Entity as defined in the entity XML file
      entityCondition - The EntityCondition object that specifies how to constrain this query
      fieldsToSelect - The fields of the named entity to get from the database; if empty or null all fields will be retreived
      orderBy - The fields of the named entity to order the query by; optionally add a " ASC" for ascending or " DESC" for descending
      Returns:
      List of GenericValue objects representing the result
      Throws:
      GenericEntityException
    • findByMultiRelation

      public List<GenericValue> findByMultiRelation(GenericValue value, ModelRelation modelRelationOne, ModelEntity modelEntityOne, ModelRelation modelRelationTwo, ModelEntity modelEntityTwo, List<String> orderBy) throws GenericEntityException
      Specified by:
      findByMultiRelation in interface GenericHelper
      Throws:
      GenericEntityException
    • findListIteratorByCondition

      public EntityListIterator findListIteratorByCondition(ModelEntity modelEntity, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, Collection<String> fieldsToSelect, List<String> orderBy, EntityFindOptions findOptions) throws GenericEntityException
      The memory implementation does the *minimum* that it can to allow tests to work. In particular it will return *all* values for a particular entity from this method.
      Specified by:
      findListIteratorByCondition in interface GenericHelper
      Parameters:
      modelEntity - The ModelEntity of the Entity as defined in the entity XML file
      whereEntityCondition - The EntityCondition object that specifies how to constrain this query before any groupings are done (if this is a view entity with group-by aliases)
      havingEntityCondition - The EntityCondition object that specifies how to constrain this query after any groupings are done (if this is a view entity with group-by aliases)
      fieldsToSelect - The fields of the named entity to get from the database; if empty or null all fields will be retreived
      orderBy - The fields of the named entity to order the query by; optionally add a " ASC" for ascending or " DESC" for descending
      findOptions - An instance of EntityFindOptions that specifies advanced query options. See the EntityFindOptions JavaDoc for more details.
      Returns:
      EntityListIterator representing the result of the query: NOTE THAT THIS MUST BE CLOSED WHEN YOU ARE DONE WITH IT, AND DON'T LEAVE IT OPEN TOO LONG BECAUSE IT WILL MAINTAIN A DATABASE CONNECTION.
      Throws:
      GenericEntityException
    • removeByAnd

      public int removeByAnd(ModelEntity modelEntity, Map<String,?> fields) throws GenericEntityException
      Description copied from interface: GenericHelper
      Removes/deletes Generic Entity records found by all of the specified fields (ie: combined using AND)
      Specified by:
      removeByAnd in interface GenericHelper
      Parameters:
      modelEntity - The ModelEntity of the Entity as defined in the entity XML file
      fields - The fields of the named entity to query by with their corresponging values
      Returns:
      int representing number of rows effected by this operation
      Throws:
      GenericEntityException
    • removeByCondition

      public int removeByCondition(ModelEntity modelEntity, EntityCondition whereCondition) throws GenericEntityException
      Description copied from interface: GenericHelper
      Removes/deletes Generic Entity records found by all of the specified fields (ie: combined using AND)
      Specified by:
      removeByCondition in interface GenericHelper
      Parameters:
      modelEntity - The ModelEntity of the Entity as defined in the entity XML file
      whereCondition - The EntityCondition object that specifies how to constrain this query
      Returns:
      int representing number of rows effected by this operation
      Throws:
      GenericEntityException
    • store

      public int store(GenericValue value) throws GenericEntityException
      Description copied from interface: GenericHelper
      Store the Entity from the GenericValue to the persistent store
      Specified by:
      store in interface GenericHelper
      Parameters:
      value - GenericValue instance containing the entity
      Returns:
      int representing number of rows effected by this operation
      Throws:
      GenericEntityException
    • storeAll

      public int storeAll(List<? extends GenericValue> values) throws GenericEntityException
      Description copied from interface: GenericHelper
      Store the Entities from the List GenericValue instances to the persistent store. This is different than the normal store method in that the store method only does an update, while the storeAll method checks to see if each entity exists, then either does an insert or an update as appropriate. These updates all happen in one transaction, so they will either all succeed or all fail, if the data source supports transactions. This is just like to othersToStore feature of the GenericEntity on a create or store.
      Specified by:
      storeAll in interface GenericHelper
      Parameters:
      values - List of GenericValue instances containing the entities to store
      Returns:
      int representing number of rows effected by this operation
      Throws:
      GenericEntityException
    • removeAll

      public int removeAll(List<? extends GenericEntity> dummyPKs) throws GenericEntityException
      Description copied from interface: GenericHelper
      Remove the Entities from the List from the persistent store.
      The List contains GenericEntity objects, can be either GenericPK or GenericValue.
      If a certain entity contains a complete primary key, the entity in the datasource corresponding to that primary key will be removed, this is like a removeByPrimary Key.
      On the other hand, if a certain entity is an incomplete or non primary key, if will behave like the removeByAnd method.
      These updates all happen in one transaction, so they will either all succeed or all fail, if the data source supports transactions.
      Specified by:
      removeAll in interface GenericHelper
      Parameters:
      dummyPKs - List of GenericEntity instances containing the entities or by and fields to remove
      Returns:
      int representing number of rows effected by this operation
      Throws:
      GenericEntityException
    • checkDataSource

      public void checkDataSource(Map<String,? extends ModelEntity> modelEntities, Collection<String> messages, boolean addMissing) throws GenericEntityException
      Description copied from interface: GenericHelper
      Check the datasource to make sure the entity definitions are correct, optionally adding missing entities or fields on the server
      Specified by:
      checkDataSource in interface GenericHelper
      Parameters:
      modelEntities - Map of entityName names and ModelEntity values
      messages - Collection to put any result messages in
      addMissing - Flag indicating whether or not to add missing entities and fields on the server
      Throws:
      GenericEntityException
    • count

      public int count(ModelEntity modelEntity, String fieldName, EntityCondition entityCondition, EntityFindOptions findOptions) throws GenericEntityException
      Description copied from interface: GenericHelper
      Returns the count of the results that matches the specified condition
      Specified by:
      count in interface GenericHelper
      Parameters:
      modelEntity - The ModelEntity of the Entity as defined in the entity XML file
      fieldName - The field of the named entity to count, if null this is equivalent to count(*)
      entityCondition - The EntityCondition object that specifies how to constrain this query The expressions to use for the lookup, each consisting of at least a field name, an EntityOperator, and a value to compare to
      findOptions - An instance of EntityFindOptions that specifies advanced query options. The only option that is used is distinct, in which case a select (distinct fieldname) is issued.

      If you issue a distinct without a fieldName it will be ignored as select count (distinct *) makes no sense

      Returns:
      the number of rows that match the query
      Throws:
      GenericEntityException
    • transform

      public List<GenericValue> transform(ModelEntity modelEntity, EntityCondition entityCondition, List<String> orderBy, String lockField, Transformation transformation) throws GenericEntityException
      Description copied from interface: GenericHelper
      Applies the given transformation to any entities matching the given condition.
      Specified by:
      transform in interface GenericHelper
      Parameters:
      modelEntity - the type of entity to transform (required)
      entityCondition - the condition that selects the entities to transform (null means transform all)
      orderBy - the order in which the entities should be selected for updating (null means no ordering)
      lockField - the entity field to use for optimistic locking; the value of this field will be read between the SELECT and the UPDATE to determine whether another process has updated one of the target records in the meantime; if so, the transformation will be reapplied and another UPDATE attempted
      transformation - the transformation to apply (required)
      Returns:
      the transformed entities in the order they were selected (never null)
      Throws:
      GenericEntityException