Professional Documents
Culture Documents
§ Installed and initialized using a recipe with at least one b2c shop
- e.g. cx (OOTB) or live_sessions_dev (provided by trainer)
2
Interceptors: Overview
3
Lifecycle of a Model
Model Instantiate a Model
Runtime
Create new Model
modelService.create(XyzModel.class);
Modify Model Values
InitDefaultsInterceptor
modelService.initDefaults();
ModelService
modelService.remove()
modelService.get() modelService.save()
RemoveInterceptor
Database 4
Implementing Interceptors
Implementing an Interceptor
6
Implementation: Registering an Interceptor
7
Implementation – mapping the interceptor
<bean id="myLoadMapping"
class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
One important function: register a model instance with the interceptor for operation to be performed
ctx.registerElementFor(modelA, PersistenceOperation.SAVE)
ctx.registerElementFor(modelA, PersistenceOperation.DELETE)
@Override
public void onRemove(UserModel user, InterceptorContext ctx)
throws InterceptorException
{
final UserAuditEntryModel userAuditEntry =
ctx.getModelService().create( UserAuditEntryModel.class );
userAuditEntry.setChangeTimestamp( new Date() );
userAuditEntry.setDisplayName( user.getDisplayName() );
userAuditEntry.setName( user.getName() );
userAuditEntry.setUid( user.getUid() );
//register item for SAVE operation
ctx.registerElementFor(userAuditEntry, PersistenceOperation.SAVE);
}
}
Registering an item for a persistence operation is preferred over to modelService.save()
n Because the involved models can be tracked within the intercepting process
11
InterceptorContext: Additional Functions
§ Check whether the given model is already registered with a certain operation
E.g. to see if a modelA is already registered for deletion:
ctx.contains(modelA, PersistenceOperation.DELETE);
ctx.getElementsRegisteredFor( PersistenceOperation.DELETE );
12
Disabling Interceptor
Disabling Interceptors
§ This option can be used for relaxing certain constraints imposed on a data model in data
integration scenarios, or for performance reasons.
§ There are 3 system properties that allow you to disable specific interceptors
- disable.interceptor.beans
(takes comma-separated list of Spring bean IDs)
- disable.interceptor.types
(takes comma-separated list of InterceptorType Enum values:
INIT_DEFAULTS, LOAD, PREPARE, REMOVE, VALIDATE)
- disable.UniqueAttributesValidator.for.types
(takes comma-separated list of Commerce item types)
15
Disabling Interceptors: ImpEx
INSERT_UPDATE
Currency[disable.interceptor.beans='validateCurrencyDataInterceptor'];isocode[unique=true];symbol;digits;
;bitcoin ;$ ;-2 ;
16
Interceptor Best Practices
Interceptors: Best Practice I
Naming Conventions
18
Interceptors: Best Practice II
§ Avoid multiple interceptors have same order value
▫ If you know interceptor logic are not needed during that CRUD operation
19
Interceptors Exercise
References
Introduction:
Disabling Interceptors:
21
Thank you.
© 2023 SAP SE or an SAP affiliate company. All rights reserved. See Legal Notice on www.sap.com/legal-notice for use terms, disclaimers, disclosures, or restrictions related to SAP Materials for general audiences.