Class MockModelService
- java.lang.Object
-
- de.hybris.platform.servicelayer.internal.service.AbstractService
-
- de.hybris.platform.servicelayer.internal.model.impl.AbstractModelService
-
- de.hybris.platform.servicelayer.model.MockModelService
-
- All Implemented Interfaces:
ModelService,java.io.Serializable,org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanNameAware,org.springframework.beans.factory.InitializingBean
public class MockModelService extends AbstractModelService
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class de.hybris.platform.servicelayer.internal.service.AbstractService
AbstractService.SerializableDTO
-
-
Field Summary
-
Fields inherited from class de.hybris.platform.servicelayer.internal.service.AbstractService
tenant
-
-
Constructor Summary
Constructors Constructor Description MockModelService()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidattach(java.lang.Object model)Attaches a model instance to the current model context.voidclearTransactionsSettings()Clears any local transactions settings set byModelService.enableTransactions()orModelService.disableTransactions()methods which influences onsaveoperations.<T> Tclone(java.lang.Object original, java.lang.Class<T> targetType)Creates a deep copy of the given model except that the result object type may be changed here.<T> Tclone(java.lang.Object original, java.lang.Class<T> targetType, ModelCloningContext ctx)Creates a deep copy of the given model except that the result object type may be changed here.<T> Tclone(T original)Creates a deep copy of the given model.<T> Tclone(T original, ModelCloningContext ctx)Creates a deep copy of the given model.<T> Tcreate(java.lang.Class typeAsModel)Creates a new empty model instance of those class which is registered for given type model.<T> Tcreate(java.lang.String typeCode)Creates a new empty model instance of those class which is registered for given type code.voiddetach(PK sourcePK)Removes a model instance from the current model context.voiddetach(java.lang.Object model)Removes a model instance from the current model context.voiddetachAll()Removes all model instances attached to the current model context.voiddisableTransactions()Disables transactions for save operation.voidenableTransactions()Enables transactions for save operation.<T> Tget(PK sourcePK)Returns a model instance for the source object the given pk represents.<T> Tget(java.lang.Object source)Returns a model instance for the given source object.<T> Tget(java.lang.Object source, java.lang.String conversionType)Special loading method, which allows to specify the type for looking up the converter to be used.<T extends java.util.Collection>
TgetAll(java.util.Collection<? extends java.lang.Object> sources, T result)Convenience method to load all objects in a source collection and adding the loaded objects to a result collection.<T extends java.util.Collection>
TgetAll(java.util.Collection<? extends java.lang.Object> sources, T result, java.lang.String conversionType)Convenience method to load all objects in a source collection and adding the loaded objects to a result collection.<T extends java.util.Collection>
TgetAllSources(java.util.Collection<? extends java.lang.Object> models, T result)Convenience method to load all the sources of a given collection of models<T> TgetAttributeValue(java.lang.Object model, java.lang.String attributeQualifier)Generic attribute value getter to read attribute data from arbitrary model instances.<T> TgetAttributeValue(java.lang.Object model, java.lang.String attributeQualifier, java.util.Locale locale)Generic attribute value getter to read attribute data for given Locale from arbitrary model instances.<T> java.util.Map<java.util.Locale,T>getAttributeValues(java.lang.Object model, java.lang.String attributeQualifier, java.util.Locale... locales)Generic attribute value getter to read attribute data for given list of Locales from arbitrary model instances.<T> TgetByExample(T example)Search for an existing object (in the database) by a given (not saved) example.protected java.lang.ObjectgetModelForPersistentValue(java.lang.Object persistentValue)java.lang.StringgetModelType(java.lang.Class modelClass)Returns the type mapped to a model class.java.lang.StringgetModelType(java.lang.Object model)Returns the actual type of a given model instance.java.lang.ClassgetModelTypeClass(java.lang.Class modelClass)If the given class is a model class the mapped jalo class will be returned.protected java.lang.ObjectgetPersistentValueForModel(java.lang.Object model)<T> TgetSource(java.lang.Object model)Returns underlying source for given model.<T> TgetWithLock(java.lang.Object source)Lock jalo item and wrap it into model.voidinitDefaults(java.lang.Object model)Initializes newly created model instance.booleanisAttached(java.lang.Object model)Tells if the model is attached to the contextbooleanisModified(java.lang.Object model)Tells if the model has been modified.booleanisNew(java.lang.Object model)Tells if the model has been created and has not been saved yet.booleanisRemoved(java.lang.Object model)Tells if the model has been removed.booleanisSourceAttached(java.lang.Object source)booleanisUniqueConstraintErrorAsRootCause(java.lang.Exception e)Checks whether the root cause of the given exception is the database rejecting a new item due to some unique key constraint violation.booleanisUpToDate(java.lang.Object model)Checks if the given model represents the current persistent state of its source.voidlock(PK itemPK)Lock item with given PK in for changes.voidlock(java.lang.Object source)Lock jalo item for changes.voidrefresh(java.lang.Object model)Requests the persistence layer to fetch a new, updated version of the passed model.voidremove(PK pk)Tries to remove the source belonging to the specified PK.voidremove(java.lang.Object model)Tries to remove the source (and all part-of elements) of the given model.voidremoveAll(java.lang.Object... models)Tries to remove the source (and all part-of elements) of the given collection of models.voidremoveAll(java.util.Collection<? extends java.lang.Object> models)Tries to remove the source (and all part-of elements) of the given collection of models.voidsave(java.lang.Object model)Saves all attribute values from the model to the wrapped source object or creates a new one if no source is mapped yet.voidsaveAll()Saves all modified and new model instances which are attached to the current request context.voidsaveAll(java.util.Collection<? extends java.lang.Object> models)Saves several model model instances at once.voidsetAttributeValue(java.lang.Object model, java.lang.String attributeQualifier, java.lang.Object value)Generic attribute value setter to modify attribute data on arbitrary model instances.<T> voidsetAttributeValue(java.lang.Object model, java.lang.String attributeQualifier, java.util.Map<java.util.Locale,T> values)Generic attribute value setter to modify attribute data on arbitrary model instances for a given Locale.-
Methods inherited from class de.hybris.platform.servicelayer.internal.model.impl.AbstractModelService
saveAll, toModelLayer, toPersistenceLayer
-
Methods inherited from class de.hybris.platform.servicelayer.internal.service.AbstractService
afterPropertiesSet, getCurrentTenant, setBeanName, setCurrentTenant, writeReplace
-
-
-
-
Method Detail
-
refresh
public void refresh(java.lang.Object model)
Description copied from interface:ModelServiceRequests the persistence layer to fetch a new, updated version of the passed model.- Parameters:
model- model to be updated
-
remove
public void remove(java.lang.Object model)
Description copied from interface:ModelServiceTries to remove the source (and all part-of elements) of the given model.- Parameters:
model- the model for which to remove the source
-
remove
public void remove(PK pk) throws ModelRemovalException
Description copied from interface:ModelServiceTries to remove the source belonging to the specified PK.- Parameters:
pk- the primary key of the source to remove- Throws:
ModelRemovalException- if the remove action failed
-
isUpToDate
public boolean isUpToDate(java.lang.Object model)
Description copied from interface:ModelServiceChecks if the given model represents the current persistent state of its source. It is considered to be up to date if- the model is not modified
- the model is not new
- a source exists for this model
- the persistence version of the source item is the same as stored inside the model
- Parameters:
model- the model to check
-
save
public void save(java.lang.Object model)
Description copied from interface:ModelServiceSaves all attribute values from the model to the wrapped source object or creates a new one if no source is mapped yet.- Parameters:
model- the model to save- See Also:
ModelService.saveAll(Collection)
-
saveAll
public void saveAll(java.util.Collection<? extends java.lang.Object> models) throws ModelSavingExceptionDescription copied from interface:ModelServiceSaves several model model instances at once. If at least one instance causes a validation error no modifications are written.- Parameters:
models- the model instances to save- Throws:
ModelSavingException- See Also:
ModelService.save(Object)
-
isUniqueConstraintErrorAsRootCause
public boolean isUniqueConstraintErrorAsRootCause(java.lang.Exception e)
Description copied from interface:ModelServiceChecks whether the root cause of the given exception is the database rejecting a new item due to some unique key constraint violation.
-
attach
public void attach(java.lang.Object model)
Description copied from interface:ModelServiceAttaches a model instance to the current model context. This is necessary for all models being created manually using their constructors. This context keeps track of all models being loaded, created or modified. There is a single context per thread/request. Loading models for the same source element multiple times will return a single model instance inside one context (thread/request).- Parameters:
model- the model instance to attach
-
detach
public void detach(java.lang.Object model)
Description copied from interface:ModelServiceRemoves a model instance from the current model context. Afterward it will no longer be saved automatically viaModelService.saveAll(). Please note that loading a model for its source again results in a new instance being attached to the context! This context keeps track of all models being loaded, created or modified. There is a single context per thread/request. Loading models for the same source element multiple times will return a single model instance inside one context (thread/request).- Parameters:
model- the model instance to detach
-
detach
public void detach(PK sourcePK)
Description copied from interface:ModelServiceRemoves a model instance from the current model context. Afterward it will no longer be saved automatically viaModelService.saveAll(). Please note that loading a model for its source again results in a new instance being attached to the context! This context keeps track of all models being loaded, created or modified. There is a single context per thread/request. Loading models for the same source element multiple times will return a single model instance inside one context (thread/request).- Parameters:
sourcePK- the source primary key belonging to the model to be detached
-
saveAll
public void saveAll() throws ModelSavingExceptionDescription copied from interface:ModelServiceSaves all modified and new model instances which are attached to the current request context. A model becomes attached if- it has been loaded
- it has been created
- it has been attached explicitly via
ModelService.attach(Object)
- Throws:
ModelSavingException
-
clone
public <T> T clone(T original)
Description copied from interface:ModelServiceCreates a deep copy of the given model. The resulting object is not persisted yet in order to allow modifications like unique key adjustments etc.
-
clone
public <T> T clone(java.lang.Object original, java.lang.Class<T> targetType)Description copied from interface:ModelServiceCreates a deep copy of the given model except that the result object type may be changed here. Please note that only attributes that exist in both types are actually copied! The resulting object is not persisted yet in order to allow modifications like unique key adjustments etc.
-
clone
public <T> T clone(T original, ModelCloningContext ctx)Description copied from interface:ModelServiceCreates a deep copy of the given model. The resulting object is not persisted yet in order to allow modifications like unique key adjustments etc. The optional context allows to adjust the cloning process in some aspects. SeeModelCloningContextfor details.
-
clone
public <T> T clone(java.lang.Object original, java.lang.Class<T> targetType, ModelCloningContext ctx)Description copied from interface:ModelServiceCreates a deep copy of the given model except that the result object type may be changed here. Please note that only attributes that exist in both types are actually copied! The resulting object is not persisted yet in order to allow modifications like unique key adjustments etc. The optional context allows to adjust the cloning process in some aspects. SeeModelCloningContextfor details.
-
create
public <T> T create(java.lang.Class typeAsModel)
Description copied from interface:ModelServiceCreates a new empty model instance of those class which is registered for given type model.- Parameters:
typeAsModel- the model class to create a new instance of- Returns:
- new empty model
-
create
public <T> T create(java.lang.String typeCode)
Description copied from interface:ModelServiceCreates a new empty model instance of those class which is registered for given type code.- Parameters:
typeCode- type code whose registered model implementation will be used for instantiation- Returns:
- new empty model
-
getModelType
public java.lang.String getModelType(java.lang.Class modelClass)
Description copied from interface:ModelServiceReturns the type mapped to a model class.- Parameters:
modelClass- the model class
-
getSource
public <T> T getSource(java.lang.Object model)
Description copied from interface:ModelServiceReturns underlying source for given model.- Parameters:
model- the model for which to return the source- Returns:
- the underlying source of the given model, or null if the source doesn't (yet) exist
-
get
public <T> T get(java.lang.Object source)
Description copied from interface:ModelServiceReturns a model instance for the given source object. It is tried to find a target class using the type of the source object.- Parameters:
source- the object for which to retrieve a model instance- Returns:
- a model for the given source object
-
get
public <T> T get(java.lang.Object source, java.lang.String conversionType)Description copied from interface:ModelServiceSpecial loading method, which allows to specify the type for looking up the converter to be used. Normally there is no need to prefer this toModelService.get(Object)- use with care!- Parameters:
source- the persistent object to convertconversionType- the type to use when looking up the converter
-
get
public <T> T get(PK sourcePK)
Description copied from interface:ModelServiceReturns a model instance for the source object the given pk represents. It is tried to find a target class using the type of the source object resolved from given PK.- Parameters:
sourcePK- the object pk for which to retrieve a model instance- Returns:
- a model for the given source object pk
-
initDefaults
public void initDefaults(java.lang.Object model)
Description copied from interface:ModelServiceInitializes newly created model instance.ModelService s = ... // create plain instance - no defaults whatsoever ProductModel myModel = new ProductModel(); // now load defaults s.initDefaults( myModel );
-
getModelType
public java.lang.String getModelType(java.lang.Object model)
Description copied from interface:ModelServiceReturns the actual type of a given model instance.- Parameters:
model- the model instance
-
getAttributeValue
public <T> T getAttributeValue(java.lang.Object model, java.lang.String attributeQualifier)Description copied from interface:ModelServiceGeneric attribute value getter to read attribute data from arbitrary model instances. If attribute is localized value for current Locale is returned.- Parameters:
model- the model instanceattributeQualifier- the attribute qualifier
-
getAttributeValue
public <T> T getAttributeValue(java.lang.Object model, java.lang.String attributeQualifier, java.util.Locale locale)Description copied from interface:ModelServiceGeneric attribute value getter to read attribute data for given Locale from arbitrary model instances.- Parameters:
model- the model instanceattributeQualifier- the attribute qualifier for which value must be returnedlocale- the locale for which value must be returned
-
getAttributeValues
public <T> java.util.Map<java.util.Locale,T> getAttributeValues(java.lang.Object model, java.lang.String attributeQualifier, java.util.Locale... locales)Description copied from interface:ModelServiceGeneric attribute value getter to read attribute data for given list of Locales from arbitrary model instances. This method returns immutable map of values mapped to the corresponding locale. If none of the values for a given locale can be found, an empty map will be returned.- Parameters:
model- the model instanceattributeQualifier- the attribute qualifier for which value must be returnedlocales- Locales for which value must be returned (at least one locale is required)- Returns:
- immutable map of values mapped to the corresponding Locale
-
setAttributeValue
public void setAttributeValue(java.lang.Object model, java.lang.String attributeQualifier, java.lang.Object value)Description copied from interface:ModelServiceGeneric attribute value setter to modify attribute data on arbitrary model instances.- Parameters:
model- the modelattributeQualifier- the attribute qualifiervalue- the attribute value
-
setAttributeValue
public <T> void setAttributeValue(java.lang.Object model, java.lang.String attributeQualifier, java.util.Map<java.util.Locale,T> values)Description copied from interface:ModelServiceGeneric attribute value setter to modify attribute data on arbitrary model instances for a given Locale.- Parameters:
model- the model instanceattributeQualifier- the attribute qualifier for which value must be returnedvalues- map of values mapped to the corresponding Locale
-
detachAll
public void detachAll()
Description copied from interface:ModelServiceRemoves all model instances attached to the current model context. Afterward they will no longer be saved automatically viaModelService.saveAll(). Also note that loading models for their sources again results in new instances being attached to the context!
-
getModelForPersistentValue
protected java.lang.Object getModelForPersistentValue(java.lang.Object persistentValue)
- Specified by:
getModelForPersistentValuein classAbstractModelService
-
getPersistentValueForModel
protected java.lang.Object getPersistentValueForModel(java.lang.Object model)
- Specified by:
getPersistentValueForModelin classAbstractModelService
-
isModified
public boolean isModified(java.lang.Object model)
Description copied from interface:ModelServiceTells if the model has been modified. This applies to models which have been create or modified after loading.- Parameters:
model- the model to check
-
isNew
public boolean isNew(java.lang.Object model)
Description copied from interface:ModelServiceTells if the model has been created and has not been saved yet.- Parameters:
model- the model to check
-
isRemoved
public boolean isRemoved(java.lang.Object model)
Description copied from interface:ModelServiceTells if the model has been removed.- Parameters:
model- the model to check
-
getByExample
public <T> T getByExample(T example)
Description copied from interface:ModelServiceSearch for an existing object (in the database) by a given (not saved) example. Every modified attribute in this example is used as search parameter.- Type Parameters:
T- the type of the example and the return type- Parameters:
example- the example- Returns:
- the result if this was found by the example.
-
getModelTypeClass
public java.lang.Class getModelTypeClass(java.lang.Class modelClass)
Description copied from interface:ModelServiceIf the given class is a model class the mapped jalo class will be returned. Otherwise the same class is returned.- Parameters:
modelClass- a model class- Returns:
- the mapped jalo class
-
getAll
public <T extends java.util.Collection> T getAll(java.util.Collection<? extends java.lang.Object> sources, T result)Description copied from interface:ModelServiceConvenience method to load all objects in a source collection and adding the loaded objects to a result collection.- Specified by:
getAllin interfaceModelService- Overrides:
getAllin classAbstractModelService- Parameters:
sources- the objects for which to retrieve model instancesresult- the collection to add the model instances to- Returns:
- the result collects
- See Also:
ModelService.get(Object)
-
getAll
public <T extends java.util.Collection> T getAll(java.util.Collection<? extends java.lang.Object> sources, T result, java.lang.String conversionType)Description copied from interface:ModelServiceConvenience method to load all objects in a source collection and adding the loaded objects to a result collection. This method allows to specify the type to lookup the converter!- Specified by:
getAllin interfaceModelService- Overrides:
getAllin classAbstractModelService- Parameters:
sources- the objects for which to retrieve model instancesresult- the collection to add the model instances toconversionType- the type to be used for looking up the model converter- Returns:
- the result collects
- See Also:
ModelService.get(Object)
-
getAllSources
public <T extends java.util.Collection> T getAllSources(java.util.Collection<? extends java.lang.Object> models, T result)Description copied from interface:ModelServiceConvenience method to load all the sources of a given collection of models- Specified by:
getAllSourcesin interfaceModelService- Overrides:
getAllSourcesin classAbstractModelService- Parameters:
models- the objects for which to retrieve model instancesresult- the collection to add the model instances to- Returns:
- the result collects
-
removeAll
public void removeAll(java.util.Collection<? extends java.lang.Object> models) throws ModelRemovalExceptionDescription copied from interface:ModelServiceTries to remove the source (and all part-of elements) of the given collection of models.- Parameters:
models- the models for which to remove the source- Throws:
ModelRemovalException- if the remove action failed
-
removeAll
public void removeAll(java.lang.Object... models) throws ModelRemovalExceptionDescription copied from interface:ModelServiceTries to remove the source (and all part-of elements) of the given collection of models.- Parameters:
models- the models for which to remove the source- Throws:
ModelRemovalException- if the remove action failed
-
enableTransactions
public void enableTransactions()
Description copied from interface:ModelServiceEnables transactions for save operation. This setting overrides the global setting and has higher priority. Enabling transactions is done via session (session key: "enableTransactionalSaves") so to make it temporary please use
SessionService.executeInLocalView(de.hybris.platform.servicelayer.session.SessionExecutionBody). If you are not planning use local session, please remove completely session key "enableTransactionalSaves" from the session to back to the global setting usingModelService.clearTransactionsSettings()method.Global setting is controlled by property: model.service.transactional.saves
Example usage:
sessionService.executeInLocalView(new SessionExecutionBody() { @Override public Object execute() { modelService.enableTransactions(); modelService.save(someModel); return someModel; } });
-
disableTransactions
public void disableTransactions()
Description copied from interface:ModelServiceDisables transactions for save operation. This setting overrides the global setting and has higher priority. Disabling transactions is done via session (session key: "enableTransactionalSaves") so to make it temporary please use
SessionService.executeInLocalView(de.hybris.platform.servicelayer.session.SessionExecutionBody). If you are not planning use local session, please remove completely session key "enableTransactionalSaves" from the session to back to the global setting usingModelService.clearTransactionsSettings()method.Global setting is controlled by property: model.service.transactional.saves
Example usage:
sessionService.executeInLocalView(new SessionExecutionBody() { @Override public Object execute() { modelService.disableTransactions(); modelService.save(someModel); return someModel; } });
-
clearTransactionsSettings
public void clearTransactionsSettings()
Description copied from interface:ModelServiceClears any local transactions settings set byModelService.enableTransactions()orModelService.disableTransactions()methods which influences onsaveoperations. After executing this method global setting for transactions will be respected.
-
lock
public void lock(PK itemPK)
Description copied from interface:ModelServiceLock item with given PK in for changes.Calls to
lockcan only be made between calls toTransaction.begin()andTransaction.commit()orTransaction.rollback(). All locks that are acquired during a transaction are automatically released on calls toTransaction.commit()orTransaction.rollback(). If a lock is held by another transaction, this method will block until the lock can be acquired.- Parameters:
itemPK- pk of the item to lock.
-
lock
public void lock(java.lang.Object source)
Description copied from interface:ModelServiceLock jalo item for changes. Source must not be null and should be type ofItem.Calls to
lockcan only be made between calls toTransaction.begin()andTransaction.commit()orTransaction.rollback(). All locks that are acquired during a transaction are automatically released on calls toTransaction.commit()orTransaction.rollback(). If a lock is held by another transaction, this method will block until the lock can be acquired.- Parameters:
source- item to lock.
-
getWithLock
public <T> T getWithLock(java.lang.Object source)
Description copied from interface:ModelServiceLock jalo item and wrap it into model. Source must not be null and should be type ofItem.Calls to
lockcan only be made between calls toTransaction.begin()andTransaction.commit()orTransaction.rollback(). All locks that are acquired during a transaction are automatically released on calls toTransaction.commit()orTransaction.rollback(). If a lock is held by another transaction, this method will block until the lock can be acquired.- Type Parameters:
T- Type of returned model.- Parameters:
source- item to lock.- Returns:
- locked model for given source.
-
isAttached
public boolean isAttached(java.lang.Object model)
Description copied from interface:ModelServiceTells if the model is attached to the context- Parameters:
model- the model to check
-
isSourceAttached
public boolean isSourceAttached(java.lang.Object source)
-
-