Interface ModelService

All Known Implementing Classes:
AbstractModelService, DefaultModelService, MockModelService

public interface ModelService
Central point of model management. Manages the creation, loading and writing of models.
Since:
4.0
Spring Bean ID:
modelService
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    attach(Object model)
    Attaches a model instance to the current model context.
    void
    Clears any local transactions settings set by enableTransactions() or disableTransactions() methods which influences on save operations.
    <T> T
    clone(Object original, Class<T> targetType)
    Creates a deep copy of the given model except that the result object type may be changed here.
    <T> T
    clone(Object original, Class<T> targetType, ModelCloningContext ctx)
    Creates a deep copy of the given model except that the result object type may be changed here.
    <T> T
    clone(T original)
    Creates a deep copy of the given model.
    <T> T
    clone(T original, ModelCloningContext ctx)
    Creates a deep copy of the given model.
    <T> T
    create(Class modelClass)
    Creates a new empty model instance of those class which is registered for given type model.
    <T> T
    create(String typeCode)
    Creates a new empty model instance of those class which is registered for given type code.
    void
    detach(PK sourcePK)
    Removes a model instance from the current model context.
    void
    detach(Object model)
    Removes a model instance from the current model context.
    void
    Removes all model instances attached to the current model context.
    void
    Disables transactions for save operation.
    void
    Enables transactions for save operation.
    <T> T
    get(PK sourcePK)
    Returns a model instance for the source object the given pk represents.
    <T> T
    get(Object source)
    Returns a model instance for the given source object.
    <T> T
    get(Object source, String conversionType)
    Special loading method, which allows to specify the type for looking up the converter to be used.
    <T extends Collection>
    T
    getAll(Collection<? extends 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 Collection>
    T
    getAll(Collection<? extends Object> sources, T result, String conversionType)
    Convenience method to load all objects in a source collection and adding the loaded objects to a result collection.
    <T extends Collection>
    T
    getAllSources(Collection<? extends Object> models, T result)
    Convenience method to load all the sources of a given collection of models
    <T> T
    getAttributeValue(Object model, String attributeQualifier)
    Generic attribute value getter to read attribute data from arbitrary model instances.
    <T> T
    getAttributeValue(Object model, String attributeQualifier, Locale locale)
    Generic attribute value getter to read attribute data for given Locale from arbitrary model instances.
    <T> Map<Locale,T>
    getAttributeValues(Object model, String attributeQualifier, Locale... locales)
    Generic attribute value getter to read attribute data for given list of Locales from arbitrary model instances.
    <T> T
    getByExample(T example)
    Deprecated, for removal: This API element is subject to removal in a future version.
    getModelType(Class modelClass)
    Returns the type mapped to a model class.
    Returns the actual type of a given model instance.
    If the given class is a model class the mapped jalo class will be returned.
    <T> T
    Returns underlying source for given model.
    <T> T
    Lock jalo item and wrap it into model.
    void
    Initializes newly created model instance.
    boolean
    Tells if the model is attached to the context
    boolean
    Tells if the model has been modified.
    boolean
    isNew(Object model)
    Tells if the model has been created and has not been saved yet.
    boolean
    Tells if the model has been removed.
    boolean
     
    boolean
    Checks whether the root cause of the given exception is the database rejecting a new item due to some unique key constraint violation.
    boolean
    Checks if the given model represents the current persistent state of its source.
    void
    lock(PK itemPK)
    Lock item with given PK in for changes.
    void
    lock(Object source)
    Lock jalo item for changes.
    void
    refresh(Object model)
    Requests the persistence layer to fetch a new, updated version of the passed model.
    void
    remove(PK pk)
    Tries to remove the source belonging to the specified PK.
    void
    remove(Object model)
    Tries to remove the source (and all part-of elements) of the given model.
    void
    removeAll(Object... models)
    Tries to remove the source (and all part-of elements) of the given collection of models.
    void
    removeAll(Collection<? extends Object> models)
    Tries to remove the source (and all part-of elements) of the given collection of models.
    void
    save(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.
    void
    Saves all modified and new model instances which are attached to the current request context.
    void
    saveAll(Object... models)
    Saves several model model instances at once.
    void
    saveAll(Collection<? extends Object> models)
    Saves several model model instances at once.
    void
    setAttributeValue(Object model, String attributeQualifier, Object value)
    Generic attribute value setter to modify attribute data on arbitrary model instances.
    <T> void
    setAttributeValue(Object model, String attributeQualifier, Map<Locale,T> value)
    Generic attribute value setter to modify attribute data on arbitrary model instances for a given Locale.
    <T> T
    toModelLayer(Object persistentValue)
    Converts arbitrary values into a representation to be used inside the model layer.
    <T> T
    Converts arbitrary model layer values into a form which may be used in persistence layer.
  • Method Details

    • attach

      void attach(Object model)
      Attaches 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
      Throws:
      IllegalStateException - if the current context already holds a model for the same source element
    • detach

      void detach(Object model)
      Removes a model instance from the current model context. Afterward it will no longer be saved automatically via 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

      void detach(PK sourcePK)
      Removes a model instance from the current model context. Afterward it will no longer be saved automatically via 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
    • detachAll

      void detachAll()
      Removes all model instances attached to the current model context. Afterward they will no longer be saved automatically via saveAll(). Also note that loading models for their sources again results in new instances being attached to the context!
    • clone

      <T> T clone(T original)
      Creates 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

      <T> T clone(Object original, Class<T> targetType)
      Creates 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

      <T> T clone(T original, ModelCloningContext ctx)
      Creates 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. See ModelCloningContext for details.
    • clone

      <T> T clone(Object original, Class<T> targetType, ModelCloningContext ctx)
      Creates 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. See ModelCloningContext for details.
    • create

      <T> T create(Class modelClass)
      Creates a new empty model instance of those class which is registered for given type model.
      Parameters:
      modelClass - the model class to create a new instance of
      Returns:
      new empty model
    • create

      <T> T create(String typeCode)
      Creates 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
    • get

      <T> T get(Object source)
      Returns 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

      <T> T get(Object source, String conversionType)
      Special loading method, which allows to specify the type for looking up the converter to be used. Normally there is no need to prefer this to get(Object) - use with care!
      Parameters:
      source - the persistent object to convert
      conversionType - the type to use when looking up the converter
    • get

      <T> T get(PK sourcePK)
      Returns 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
    • getAll

      <T extends Collection> T getAll(Collection<? extends Object> sources, T result)
      Convenience method to load all objects in a source collection and adding the loaded objects to a result collection.
      Parameters:
      sources - the objects for which to retrieve model instances
      result - the collection to add the model instances to
      Returns:
      the result collects
      See Also:
    • getAll

      <T extends Collection> T getAll(Collection<? extends Object> sources, T result, String conversionType)
      Convenience 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!
      Parameters:
      sources - the objects for which to retrieve model instances
      result - the collection to add the model instances to
      conversionType - the type to be used for looking up the model converter
      Returns:
      the result collects
      See Also:
    • refresh

      void refresh(Object model)
      Requests the persistence layer to fetch a new, updated version of the passed model.
      Parameters:
      model - model to be updated
    • save

      void save(Object model) throws ModelSavingException
      Saves 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
      Throws:
      ModelSavingException - if the save operation failed
      See Also:
    • saveAll

      void saveAll(Collection<? extends Object> models) throws ModelSavingException
      Saves 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:
    • saveAll

      void saveAll(Object... models) throws ModelSavingException
      Saves 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:
    • saveAll

      void saveAll() throws ModelSavingException
      Saves 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 attach(Object)
      Throws:
      ModelSavingException
    • isUniqueConstraintErrorAsRootCause

      boolean isUniqueConstraintErrorAsRootCause(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.
    • remove

      void remove(Object model) throws ModelRemovalException
      Tries to remove the source (and all part-of elements) of the given model.
      Parameters:
      model - the model for which to remove the source
      Throws:
      ModelRemovalException - if the remove action failed
    • removeAll

      void removeAll(Collection<? extends Object> models) throws ModelRemovalException
      Tries 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

      void removeAll(Object... models) throws ModelRemovalException
      Tries 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
    • remove

      void remove(PK pk) throws ModelRemovalException
      Tries 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
    • getSource

      <T> T getSource(Object model)
      Returns 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
    • getAllSources

      <T extends Collection> T getAllSources(Collection<? extends Object> models, T result)
      Convenience method to load all the sources of a given collection of models
      Parameters:
      models - the objects for which to retrieve model instances
      result - the collection to add the model instances to
      Returns:
      the result collects
      Throws:
      ModelValidationException - in case of a validation error
    • getModelType

      String getModelType(Class modelClass)
      Returns the type mapped to a model class.
      Parameters:
      modelClass - the model class
    • getModelTypeClass

      Class getModelTypeClass(Class modelClass)
      If 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
    • getModelType

      String getModelType(Object model)
      Returns the actual type of a given model instance.
      Parameters:
      model - the model instance
    • toModelLayer

      <T> T toModelLayer(Object persistentValue)
      Converts arbitrary values into a representation to be used inside the model layer. Generally this converts all object with existing model converter into their model representation. Collections and maps are copied on demand. Any other objects stay unchanged.
      Parameters:
      persistentValue - the value loaded from persistence layer.
    • toPersistenceLayer

      <T> T toPersistenceLayer(Object modelValue)
      Converts arbitrary model layer values into a form which may be used in persistence layer. Generally this converts all models back into their source. Collections and maps are copied on demand. Any other objects stay unchanged.
      Parameters:
      modelValue - the value to be translated back into persistence layer form
      Throws:
      IllegalStateException - if models are found which do not have a persistence representation (yet) - be sure to save all models before converting them!
    • initDefaults

      void initDefaults(Object model) throws ModelInitializationException
      Initializes newly created model instance.
              ModelService s = ...
              // create plain instance - no defaults whatsoever
              ProductModel myModel = new ProductModel();
              // now load defaults
              s.initDefaults( myModel );
       
      Parameters:
      model -
      Throws:
      ModelInitializationException
    • getAttributeValue

      <T> T getAttributeValue(Object model, String attributeQualifier)
      Generic 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 instance
      attributeQualifier - the attribute qualifier
    • getAttributeValue

      <T> T getAttributeValue(Object model, String attributeQualifier, Locale locale)
      Generic attribute value getter to read attribute data for given Locale from arbitrary model instances.
      Type Parameters:
      T -
      Parameters:
      model - the model instance
      attributeQualifier - the attribute qualifier for which value must be returned
      locale - the locale for which value must be returned
      Throws:
      IllegalArgumentException - when value cannot be find for given locale
      NullPointerException - when model, attributeQualifier or locale is null
    • getAttributeValues

      <T> Map<Locale,T> getAttributeValues(Object model, String attributeQualifier, Locale... locales)
      Generic 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.
      Type Parameters:
      T -
      Parameters:
      model - the model instance
      attributeQualifier - the attribute qualifier for which value must be returned
      locales - Locales for which value must be returned (at least one locale is required)
      Returns:
      immutable map of values mapped to the corresponding Locale
      Throws:
      NullPointerException - when model, attributeQualifier or locale is null
    • setAttributeValue

      void setAttributeValue(Object model, String attributeQualifier, Object value)
      Generic attribute value setter to modify attribute data on arbitrary model instances.
      Parameters:
      model - the model
      attributeQualifier - the attribute qualifier
      value - the attribute value
      Throws:
      NullPointerException - when model or attributeQualifier is null
    • setAttributeValue

      <T> void setAttributeValue(Object model, String attributeQualifier, Map<Locale,T> value)
      Generic attribute value setter to modify attribute data on arbitrary model instances for a given Locale.
      Parameters:
      model - the model instance
      attributeQualifier - the attribute qualifier for which value must be returned
      value - map of values mapped to the corresponding Locale
    • isUpToDate

      boolean isUpToDate(Object model)
      Checks 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
    • isModified

      boolean isModified(Object model)
      Tells 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

      boolean isNew(Object model)
      Tells if the model has been created and has not been saved yet.
      Parameters:
      model - the model to check
    • isRemoved

      boolean isRemoved(Object model)
      Tells if the model has been removed.
      Parameters:
      model - the model to check
    • isAttached

      boolean isAttached(Object model)
      Tells if the model is attached to the context
      Parameters:
      model - the model to check
    • isSourceAttached

      boolean isSourceAttached(Object source)
    • enableTransactions

      void enableTransactions()

      Enables 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 using 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

      void disableTransactions()

      Disables 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 using 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

      void clearTransactionsSettings()
      Clears any local transactions settings set by enableTransactions() or disableTransactions() methods which influences on save operations. After executing this method global setting for transactions will be respected.
    • getByExample

      @Deprecated(since="6.1.0", forRemoval=true) <T> T getByExample(T example)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Search 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.
      Throws:
      ModelNotFoundException - if nothing was found
      AmbiguousIdentifierException - if by the given searchparams to many models where found
    • lock

      void lock(PK itemPK)
      Lock item with given PK in for changes.

      Calls to lock can only be made between calls to Transaction.begin() and Transaction.commit() or Transaction.rollback(). All locks that are acquired during a transaction are automatically released on calls to Transaction.commit() or Transaction.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

      void lock(Object source)
      Lock jalo item for changes. Source must not be null and should be type of Item.

      Calls to lock can only be made between calls to Transaction.begin() and Transaction.commit() or Transaction.rollback(). All locks that are acquired during a transaction are automatically released on calls to Transaction.commit() or Transaction.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

      <T> T getWithLock(Object source)
      Lock jalo item and wrap it into model. Source must not be null and should be type of Item.

      Calls to lock can only be made between calls to Transaction.begin() and Transaction.commit() or Transaction.rollback(). All locks that are acquired during a transaction are automatically released on calls to Transaction.commit() or Transaction.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.