Class ItemModelConverter
java.lang.Object
de.hybris.platform.servicelayer.internal.converter.impl.ItemModelConverter
- All Implemented Interfaces:
TypeSystemAwareModelConverter,UpdateableModelConverter,ModelConverter,Cloneable
- Direct Known Subclasses:
PrefetchAllModelConverter,VariantProductModelConverter
public class ItemModelConverter
extends Object
implements Cloneable, UpdateableModelConverter, TypeSystemAwareModelConverter
This is a general
ModelConverter implementation which should be working for almost every
AbstractItemModel. Using the
ItemModelConverter(ModelService, I18NService, CommonI18NService, String, Class, SerializationStrategy, SourceTransformer, SelfHealingService)
constructor you specify which source Item class you want to use (e.g. Product, this will be used for compatibility
checks in load(Object)) and which ItemModel class will be created if you load such an item using
load(Object) . If you need functionality which is beyond this simple implementation, you will have to
provide your own ItemModel implementation.
This class is an internal API and is subject to change without further notice-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classprotected static classstatic classclassstatic classstatic classprotected static classThis class is an internal API and is subject to change without further notice -
Constructor Summary
ConstructorsConstructorDescriptionItemModelConverter(ModelService modelService, I18NService i18nService, CommonI18NService commonI18NService, String type, Class<? extends AbstractItemModel> modelClass, SerializationStrategy serializationStrategy) For backward compatibilityItemModelConverter(ModelService modelService, I18NService i18nService, CommonI18NService commonI18NService, String type, Class<? extends AbstractItemModel> modelClass, SerializationStrategy serializationStrategy, SourceTransformer sourceTransformer, SelfHealingService selfHealingService) Constructs a newItemModelConverterinstance which handles beans of given class type related to given composed type. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddModelModificationListener(Object model, ModelModificationListener listener) Adds the givenModelModificationListenerto the given model.voidafterDetach(Object model, ModelContext ctx) This method is executed after the givenmodelis attached from the givenModelContextvoidafterModification(AbstractItemModel model, PK pk, long persistenceVersion) protected ItemModelConverter.TypeMetaInfoassembleMetaInfo(ComposedType ct, Collection<ItemModelConverter.TypeAttributeInfo> attributes) InitializesItemModelConverter.TypeMetaInfo._modelAttributesAllandItemModelConverter.TypeMetaInfo._modelAttributesInitialusing attribute meta infos.voidbeforeAttach(Object model, ModelContext ctx) This method is executed before the givenmodelis attached to the givenModelContextprotected Collection<ItemModelConverter.TypeAttributeInfo>Calculates attribute meta info objects for a given composed type.protected voidclearAttribute(AbstractItemModel model, String qualifier) protected voidclearLocAttribute(AbstractItemModel model, String qualifier, Locale loc) protected ObjectconvertModelsToObjects(AbstractItemModel model, String qualifier, Object value) Checks if the given object is a model, a collection of models, or a map with models and tries to convert them to related source object.protected ObjectconvertObjectsToModels(PersistenceObject item, String qualifier, Object value) Tries to convert given object, collection of objects or map of objects to related model instances.Instantiates a new instance of configured model class.protected ItemModelInternalContextcreateContextForExistingModel(String type, PK pk, long persistenceVersion, AttributeProvider attributeProvider) protected ItemModelInternalContextprotected ItemModelConverter.ModelAttributeInfoThis method is an internal API and is subject to change without further noticeprotected AbstractItemModelprotected PersistenceObjectPerforms item creation.booleanbooleanReturnstrueif for the given model a source Item exists and if this item Item#isAlive()protected voidfillModel(AbstractItemModel model, ItemModelConverter.AttributesToLoad attributesToLoad, PersistenceObject item) Loads and fills model instance with loaded attribute values.protected ItemModelConverter.AttributesToLoadgetAttributesToReload(AbstractItemModel model, ItemModelConverter.ModifiedAttributeValues savedValues, boolean forCreation) getAttributeValue(Object model, String attributeQualifier) Generic attribute value getter to read attribute data from arbitrary model instances.protected CommonI18NServiceprotected ComposedTypegetComposedType(AbstractItemModel model) protected ConfigIntfEncapsulates fetching ofConfigIntf.getDirtyAttributes(Object model) Returns a map with all modified attribute qualifiers for the given model.getDirtyAttributeValue(Object model, String attributeQualifier) Internal method forDefaultModelServicewhen inspecting dirty attributes during save!getDirtyLocalizedAttributesWithNonDataLocales(Map<Locale, Set<String>> dirtyLocalizedAttributes, Map<Locale, Set<Locale>> dataLocale2NonDataLocales) getDirtyLocalizedAttributeValue(Object model, String attributeQualifier, Locale locale) Internal method forDefaultModelServicewhen inspecting dirty attributes during save!protected DynamicAttributesProviderprotected Set<ItemModelConverter.ModelAttributeInfo>getExcluded(Collection<String> excluded) protected final ObjectgetFieldValue(Object model, ItemModelConverter.ModelAttributeInfo info) protected MethodgetGetter(Object model, ItemModelConverter.ModelAttributeInfo info) protected I18NServiceprotected LocaleProvidergetLocalizedAttributeValue(Object model, String attributeQualifier, Locale locale) Generic attribute value getter to read attribute data from arbitrary model instances.protected Methodprotected MethodgetLocalizedSetter(Object model, ItemModelConverter.ModelAttributeInfo info, Object value) Class<? extends AbstractItemModel>Gets the class used for instantiation of related models.protected ModelServicegetPartOfAttributes(TypeService typeService) Returns a collection of all partof atrributesgetPersistenceSource(Object model) protected MethodgetSetter(Object model, ItemModelConverter.ModelAttributeInfo info, Object value) Deprecated, for removal: This API element is subject to removal in a future version.protected LocaleReturns the exact type of the given model.protected StringgetTypeFromModelContext(Object model) getWritablePartOfAttributes(TypeService typeService) Returns a collection of writable partof atrributesinthashCode()protected booleanhasNonDataLocales(Map<Locale, Set<Locale>> dataLocale2LocaleMappings) voidinit(ConverterRegistry registry) Called once before using the converter the first time.booleanprotected booleanisEnumValue(Object value) booleanisModified(Object model) Returnstrueif the given model is new or the ModelValueHistory of the model contains changed attributes.booleanisModified(Object model, String attribute) Returnstrueif the given model is new or the ModelValueHistory for the given attribute (qualifier) was changed.booleanisModified(Object model, String attribute, Locale loc) Returnstrueif the given model is new or the ModelValueHistory for the given attribute (qualifier) was changed.booleanReturnstrueif the given model was not persisted yet.protected booleanTells whether or not a attribute should be lazy loaded.protected booleanTells whether an attribute is a reference to an item (including Collection and Maps referencing items).booleanReturnstrueif for the given model no source Item exists or this item is not Item#isAlive()booleanisUpToDate(Object model) The given model is not up-to-date (returnsfalse) when: the model is new (seeModelConverter.isNew(Object)) or the model was removed (seeModelConverter.isRemoved(Object)) or the model was locally modified or the Item in the database is newer as the source item of the modelbooleanjaloOnlyAttribute(String qualifier) final AbstractItemModelInstantiates a new instance of configured model class for the given sourceItemand loads all of its attributes.protected voidmergeOldValuesIntoDataLocales(Map<Locale, Set<Locale>> dataLocale2LocaleMappings, String qualifier, LocMap<Locale, Object> oldLocValues) protected booleanmustConvert(Type type) Tells whether or not values of the given type require conversion before using them as model attribute values.protected voidpreLoadAttribute(AbstractItemModel model, String qualifier, Object value) protected voidpreLoadLocAttribute(Map<String, LocMap<Locale, Object>> localizedAttributesFieldValue, AbstractItemModel model, String qualifier, Locale loc, Object value) protected ItemModelConverter.LoadedAttributeValuesreadAttributes(Locale dataLoc, PersistenceObject item, Set<String> qualifiers) Performs bulk-reading of item attribute values,protected AttributePrefetchModeReads configuration settingServicelayerConstants.PARAM_PREFETCHand determines preFetch mode.protected ObjectreadSingleAttribute(Locale dataLoc, PersistenceObject item, String qualifier) Performs reading for a single item attribute.voidClears all attributes of given bean so that they will be reloaded on next request.voidTries to remove the sourceItemof the givenAbstractItemModel.voidremoveModelModificationListener(Object model, ModelModificationListener listener) Removes the ModelModificationListener from the given model.voidsave(Object model, Collection<String> excludedAttributes) Takes all modified values from the givenAbstractItemModeland writes them down to the source Item (if it exists).protected voidsetAttributeLoadError(AbstractItemModel model, String qualifier, ModelLoadingException error) voidsetAttributeValue(Object model, String attributeQualifier, Object value) Generic attribute value setter to modify attribute data on arbitrary model instances.protected voidsetLocAttributeLoadError(AbstractItemModel model, String qualifier, Locale loc, ModelLoadingException error) protected voidstoreAttributes(Item item, ItemModelConverter.ModifiedAttributeValues values) Performs storing of item attribute values.toString()voidType system was changed and converter will respect this changes.void
-
Constructor Details
-
ItemModelConverter
public ItemModelConverter(ModelService modelService, I18NService i18nService, CommonI18NService commonI18NService, String type, Class<? extends AbstractItemModel> modelClass, SerializationStrategy serializationStrategy) For backward compatibility -
ItemModelConverter
public ItemModelConverter(ModelService modelService, I18NService i18nService, CommonI18NService commonI18NService, String type, Class<? extends AbstractItemModel> modelClass, SerializationStrategy serializationStrategy, SourceTransformer sourceTransformer, SelfHealingService selfHealingService) Constructs a newItemModelConverterinstance which handles beans of given class type related to given composed type.- Parameters:
type- specifies the type this handler can handle, is used insave(Object, Collection)if a new item instance is createdmodelClass- specifies whichAbstractItemModelclass will be used to instantiate a new model viaload(Object)
-
-
Method Details
-
readPrefetchSettings
Reads configuration settingServicelayerConstants.PARAM_PREFETCHand determines preFetch mode. Method is called once initially for eachItemModelConverterinstance.ServicelayerConstants.VALUE_PREFETCH_DEFAULTor no value means
-
getConfig
Encapsulates fetching ofConfigIntf. This allows overriding e.g. for test cases. -
getInitialModelAttributes
-
getAllModelAttributes
-
getInfo
-
create
Instantiates a new instance of configured model class.- Specified by:
createin interfaceModelConverter- Returns:
- new empty model
-
isConfiguredForLegacyPersistence
public boolean isConfiguredForLegacyPersistence() -
createModelInstance
-
createContextForNewModel
-
createContextForExistingModel
protected ItemModelInternalContext createContextForExistingModel(String type, PK pk, long persistenceVersion, AttributeProvider attributeProvider) -
load
Instantiates a new instance of configured model class for the given sourceItemand loads all of its attributes.- Specified by:
loadin interfaceModelConverter- Parameters:
source- object which will be used for filling new model- Returns:
- new model instance with data of given source
- Throws:
IllegalArgumentException- if the given source object is not compatible with the item class provided in this classes constructor.
-
addModelModificationListener
Description copied from interface:UpdateableModelConverterAdds the givenModelModificationListenerto the given model.- Specified by:
addModelModificationListenerin interfaceUpdateableModelConverter
-
removeModelModificationListener
Description copied from interface:UpdateableModelConverterRemoves the ModelModificationListener from the given model.- Specified by:
removeModelModificationListenerin interfaceUpdateableModelConverter
-
beforeAttach
Description copied from interface:ModelConverterThis method is executed before the givenmodelis attached to the givenModelContext- Specified by:
beforeAttachin interfaceModelConverter
-
mergeNonDataLocales
-
mergeOldValuesIntoDataLocales
-
getDirtyLocalizedAttributesWithNonDataLocales
-
hasNonDataLocales
-
afterDetach
Description copied from interface:ModelConverterThis method is executed after the givenmodelis attached from the givenModelContext- Specified by:
afterDetachin interfaceModelConverter
-
getWritablePartOfAttributes
Description copied from interface:ModelConverterReturns a collection of writable partof atrributes- Specified by:
getWritablePartOfAttributesin interfaceModelConverter
-
getPartOfAttributes
Description copied from interface:ModelConverterReturns a collection of all partof atrributes- Specified by:
getPartOfAttributesin interfaceModelConverter
-
reload
Clears all attributes of given bean so that they will be reloaded on next request.- Specified by:
reloadin interfaceModelConverter- Parameters:
modelOriginal- model which will be filled from scratch using mapped source
-
remove
Tries to remove the sourceItemof the givenAbstractItemModel.- Specified by:
removein interfaceModelConverter- Parameters:
model- the model for which to remove the source- Throws:
IllegalArgumentException- if the given model is not of type ItemModel
-
save
Takes all modified values from the givenAbstractItemModeland writes them down to the source Item (if it exists). If the item doesn't exist, an exception is thrown since the Item type itself is abstract.- Specified by:
savein interfaceModelConverter- Parameters:
model- the model to save- Throws:
IllegalArgumentException- if the given model is not an instance of ItemModelIllegalStateException- if the source item of the model doesn't exist any more
-
afterModification
-
unloadAttributes
-
getExcluded
-
getSource
Deprecated, for removal: This API element is subject to removal in a future version.since 5.7.0 - Please usegetPersistenceSource(Object)For the given bean the underlyingItemis returned.- Specified by:
getSourcein interfaceModelConverter- 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
- Throws:
IllegalArgumentException- if the given bean is not of typeAbstractItemModelIllegalStateException- is the model has never been persisted or the underlying item does not exist any more
-
getPersistenceSource
- Specified by:
getPersistenceSourcein interfaceModelConverter
-
isModified
Description copied from interface:ModelConverterReturnstrueif the given model is new or the ModelValueHistory of the model contains changed attributes.- Specified by:
isModifiedin interfaceModelConverter- Parameters:
model- the model- Returns:
falseotherwise
-
isModified
Description copied from interface:ModelConverterReturnstrueif the given model is new or the ModelValueHistory for the given attribute (qualifier) was changed. If the attribute is localized the current session dataLocale(see LocaleProvider#getCurrentDataLocale()) is used.- Specified by:
isModifiedin interfaceModelConverter- Parameters:
model- the modelattribute- the attribute qualifier- Returns:
falseif the attribute was not modified.
-
isModified
Description copied from interface:ModelConverterReturnstrueif the given model is new or the ModelValueHistory for the given attribute (qualifier) was changed. For localized attributes the givenLocaleis used to check for modifications. If the Locale isnullthen all Locales will be checked for modifications. For non-localized attributes the given Locale will be ignored.- Specified by:
isModifiedin interfaceModelConverter- Parameters:
model- the modelattribute- the attribute qualifierloc- the Locale. Can benull.- Returns:
falseotherwise.
-
exists
Description copied from interface:ModelConverterReturnstrueif for the given model a source Item exists and if this item Item#isAlive()- Specified by:
existsin interfaceModelConverter- Parameters:
model- the model- Returns:
falseif the model was not persisted yet (no underlying item) or the item was removed.
-
isRemoved
Description copied from interface:ModelConverterReturnstrueif for the given model no source Item exists or this item is not Item#isAlive()- Specified by:
isRemovedin interfaceModelConverter- Parameters:
model- the model- Returns:
falseif the modelModelConverter.isNew(Object)or the item is alive.
-
isNew
Description copied from interface:ModelConverterReturnstrueif the given model was not persisted yet.- Specified by:
isNewin interfaceModelConverter- Parameters:
model- the model- Returns:
falseif an AttributeProvider for the given model exists.
-
isUpToDate
Description copied from interface:ModelConverterThe given model is not up-to-date (returnsfalse) when:- the model is new (see
ModelConverter.isNew(Object)) or - the model was removed (see
ModelConverter.isRemoved(Object)) or - the model was locally modified or
- the Item in the database is newer as the source item of the model
- Specified by:
isUpToDatein interfaceModelConverter- Parameters:
model- the model- Returns:
trueif the model is up-to-date
- the model is new (see
-
getAttributeValue
Description copied from interface:ModelConverterGeneric attribute value getter to read attribute data from arbitrary model instances.- Specified by:
getAttributeValuein interfaceModelConverter- Parameters:
model- the model instanceattributeQualifier- the attribute qualifier- Returns:
- the attribute value
-
getDirtyAttributeValue
Internal method forDefaultModelServicewhen inspecting dirty attributes during save! -
getLocalizedAttributeValue
Description copied from interface:ModelConverterGeneric attribute value getter to read attribute data from arbitrary model instances.- Specified by:
getLocalizedAttributeValuein interfaceModelConverter- Parameters:
model- the model instanceattributeQualifier- the attribute qualifierlocale- Locale for which value will be returned- Returns:
- the attribute value
-
getDirtyLocalizedAttributeValue
public Object getDirtyLocalizedAttributeValue(Object model, String attributeQualifier, Locale locale) Internal method forDefaultModelServicewhen inspecting dirty attributes during save! -
setAttributeValue
Description copied from interface:ModelConverterGeneric attribute value setter to modify attribute data on arbitrary model instances.- Specified by:
setAttributeValuein interfaceModelConverter- Parameters:
model- the modelattributeQualifier- the attribute qualifiervalue- the attribute value
-
getModelClass
Gets the class used for instantiation of related models.- Returns:
- model class
-
getDefaultType
-
getType
Description copied from interface:ModelConverterReturns the exact type of the given model.- Specified by:
getTypein interfaceModelConverter
-
getTypeFromModelContext
-
getComposedType
-
readSingleAttribute
Performs reading for a single item attribute.- Parameters:
dataLoc- the locale to be useditem- the item to read fromqualifier- the attribute qualifier- Returns:
- the attribute value
-
jaloOnlyAttribute
-
calculateTypeAttributeInfos
protected Collection<ItemModelConverter.TypeAttributeInfo> calculateTypeAttributeInfos(ComposedType ct) Calculates attribute meta info objects for a given composed type. -
isReferenceAttribute
Tells whether an attribute is a reference to an item (including Collection and Maps referencing items). -
assembleMetaInfo
protected ItemModelConverter.TypeMetaInfo assembleMetaInfo(ComposedType ct, Collection<ItemModelConverter.TypeAttributeInfo> attributes) InitializesItemModelConverter.TypeMetaInfo._modelAttributesAllandItemModelConverter.TypeMetaInfo._modelAttributesInitialusing attribute meta infos. Must be called with active write lock!! -
createModelAttributeInfo
protected ItemModelConverter.ModelAttributeInfo createModelAttributeInfo(ItemModelConverter.TypeAttributeInfo info) This method is an internal API and is subject to change without further notice -
readAttributes
protected ItemModelConverter.LoadedAttributeValues readAttributes(Locale dataLoc, PersistenceObject item, Set<String> qualifiers) Performs bulk-reading of item attribute values,- Parameters:
item- the item to read fromqualifiers- the attributes to load values for
-
storeAttributes
Performs storing of item attribute values.- Parameters:
item- the item to write intovalues- the (jalo compatible!) values to store
-
isNonLazyLoadedAttribute
Tells whether or not a attribute should be lazy loaded.- Parameters:
desc- the attribute descriptor
-
mustConvert
Tells whether or not values of the given type require conversion before using them as model attribute values. Here all instances of composed types and other types containing composed types are considered to require conversion.- Parameters:
type- the type to check- See Also:
-
getStdLocale
-
createNewItem
protected PersistenceObject createNewItem(AbstractItemModel model, ItemModelConverter.ModifiedAttributeValues values) Performs item creation. Override this method if you have to provide sophisticated functionality to create a new Item (more than justComposedType.newInstance(Map).- Parameters:
values- the attributes for the new item- Returns:
- the new Item instance
-
toString
-
fillModel
protected void fillModel(AbstractItemModel model, ItemModelConverter.AttributesToLoad attributesToLoad, PersistenceObject item) Loads and fills model instance with loaded attribute values.- Parameters:
model- the model instance to fill values intoattributesToLoad- the attributes to be loaded
-
setLocAttributeLoadError
protected void setLocAttributeLoadError(AbstractItemModel model, String qualifier, Locale loc, ModelLoadingException error) -
setAttributeLoadError
protected void setAttributeLoadError(AbstractItemModel model, String qualifier, ModelLoadingException error) -
preLoadLocAttribute
-
clearLocAttribute
-
preLoadAttribute
-
clearAttribute
-
convertModelsToObjects
Checks if the given object is a model, a collection of models, or a map with models and tries to convert them to related source object.- Parameters:
value- model, collection of models or map of models to convert- Returns:
- converted source object/s or original if no conversion is possible
-
convertObjectsToModels
Tries to convert given object, collection of objects or map of objects to related model instances.- Parameters:
value- object, collection of objects or map of objects to convert to model/s- Returns:
- converted model or original if no conversion is possible
-
isEnumValue
-
getAttributesToReload
protected ItemModelConverter.AttributesToLoad getAttributesToReload(AbstractItemModel model, ItemModelConverter.ModifiedAttributeValues savedValues, boolean forCreation) -
init
Description copied from interface:ModelConverterCalled once before using the converter the first time.- Specified by:
initin interfaceModelConverter- Parameters:
registry- the registry holding this converter.
-
getModelService
-
getI18nService
-
getCommonI18NService
-
getLocaleProvider
-
getDynamicAttributesProvider
-
getGetter
-
getLocalizedGetter
-
getSetter
-
getLocalizedSetter
protected Method getLocalizedSetter(Object model, ItemModelConverter.ModelAttributeInfo info, Object value) -
getSerializationStrategy
-
getFieldValue
-
getDirtyAttributes
Description copied from interface:ModelConverterReturns a map with all modified attribute qualifiers for the given model. The keys are all localized and non-localized attribute qualifier which were modified. If the map value isnullthe key is a non-localized attribute qualifier else the map value contains the locale in which language the localized attribute was modified.- Specified by:
getDirtyAttributesin interfaceModelConverter- Parameters:
model- the new (not saved) model- Returns:
- a map with all modified attribute qualifier.
-
getUniqueAttributes
-
getMandatoryAttributes
-
getMandatoryAttributesForCreation
-
typeSystemChanged
public void typeSystemChanged()Description copied from interface:TypeSystemAwareModelConverterType system was changed and converter will respect this changes.- Specified by:
typeSystemChangedin interfaceTypeSystemAwareModelConverter
-
equals
-
hashCode
public int hashCode()
-
getPersistenceSource(Object)