Hybris Interview Q&A

Q. How can we override out of the box @RequestMapping in Hybris?

Ans. We can use @RequestMappingOverride to override an identical @RequestMapping. However, it does not work correctly when original request mapping supports two HTTP methods and we try to override only one of them. For example:

RequestMapping for original method: @RequestMapping(value=”/foo/bar/{id}”, method= {RequestMethod.PUT, RequestMethod.POST})

Overriding request mapping: @RequestMapping(value = ” /foo/bar/{id} “, method = RequestMethod.PUT )

If we try to override as shown above, method will not be overridden and error “Ambiguous handler methods…” will appear during request (not during platform start up).

For details, please refer this page.

Q. For a failed synchronization job, how the CSV dump file is interpreted?

Ans. The synchronization csv dump file is useful to find the problematic attribute. The column in the csv file are ordered as per the table below.

Source Item PkTarget Item PKitemsynctimestamp PKPending attribute qualifier
879609305511887960930878868796093055595product

Q. Explain life cycle of a model.

Ans.

Q. Explain interceptor in Hybris.

Ans. Interceptor checks whether constraints set for the behaviour of life cycle of models are fulfilled. It is used to make sure that model data is handled properly and only correct data is saved or deleted. Following are the interceptor developed to intercept the behaviour life cycles of model.

Interceptor NameDescriptionInterface To Be Implemented
LoadInterceptorThe Load Interceptor is called whenever a model is loaded from the database. This interceptor is used if we want to change values of the model after load. An exception raised during execution prevents the model from being loaded.LoadInterceptor interface (de.hybris.platform.servicelayer.interceptor package).
Example:
public interface LoadInterceptor extends Interceptor
{
void onLoad(Object model, InterceptorContext ctx) throws InterceptorException;
}
InitDefaultsInterceptorThe Init Defaults Interceptor is called when a model is filled with its default values. This happens either when it is created via the modelService.create method or when the modelService.initDefaults method is called. We can use this interceptor to fill the model with additional default values, apart from the values defined in the items.xml file. InitDefaultsInterceptor interface (de.hybris.platform.servicelayer.interceptor package).
Example:
public interface InitDefaultsInterceptor extends Interceptor
{ void onInitDefaults(Object model, InterceptorContext ctx) throws InterceptorException; }
PrepareInterceptorThe Prepare Interceptor is called before a model is saved to the database before it is validated by Validate interceptors. This is used to add values to the model or modify existing ones before they are saved. An exception raised during execution prevents the model from being saved.PrepareInterceptor interface (de.hybris.platform.servicelayer.interceptor package).
Example:
public interface PrepareInterceptor extends Interceptor
{
void onPrepare(Object model, InterceptorContext ctx) throws InterceptorException;
}
ValidateInterceptorThe Validate Interceptor is called before a model is saved to the database after is been prepared by the Prepare interceptors. We can use Validate Interceptors to validate values of the model and raise an InterceptorException if any values are not valid.ValidateInterceptor interface (de.hybris.platform.servicelayer.interceptor package).
Example:
public interface ValidateInterceptor extends Interceptor
{
void onValidate(Object model, InterceptorContext ctx) throws InterceptorException;
}
RemoveInterceptorThe Remove Interceptor is called before a model is removed from the database. We can use this interceptor, for example:
1. To remove models that are related to the model but are not in the model context.
2. To prevent the removal of the model by raising an InterceptorException.
RemoveInterceptor interface (de.hybris.platform.servicelayer.interceptor package).
Example:
public interface RemoveInterceptor extends Interceptor
{
void onRemove(Object model, InterceptorContext ctx) throws InterceptorException;
}

Q. Explain common structure of items.xml.

Ans.

<items 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="items.xsd">
    <atomictypes>
     ...
    </atomictypes>

    <collectiontypes>
     ...
    </collectiontypes>

    <enumtypes>
     ...
    </enumtypes>

    <maptypes>
     ...
    </maptypes>

    <relations>
     ...
    </relations>

    <itemtypes>
     ...
    </itemtypes>
</items>

Q. What is “Null Value Decorators”?

Ans. Null decorator expression is a ServiceLayer feature that allows to customize the behaviour of a getter method. Useing ‘Null Value Decorator’ we can specify a code fragment in an item definition (items.xml) that computes a result instead of returning null. This ensures that the method never returns a null value.

We use <nullDecorator> tag to specify the expression as below.

<attribute autocreate=”true” qualifier=”qualifierName” type=”….” generated=”true”>

	<defaultValue>default_value_for_attribute</defaultValue>

	<persistence type=”property”/>

	<modifiers read=”true” write=”true” search=”true” optional=”true” />

	<model>

		<getter default=”true” name=”qualifierName”>

			<nullDecorator>Boolean.valueOf(false)</nullDecorator>

		</getter>

	</model>

</attribute>

Q. How can we create an item type as synchronizing capable (catalog aware item)?

Ans. An item can be made synchronizing capable by using <custom-properties> tag in items.xml file as shown below.

<itemtype code=”MyCustomType” …>

	<custom-properties>

		<!– marking the type as synchronizing capable –>

		<property name=”catalogItemType”>

			<value>java.lang.Boolean.True</value>

		</property>

	<!– define catalog version attribute –>

		<property name=”catalogVersionAttributeQualifier”>

			<value>”catalogVersion”</value>

		</property>

	<!– define unique key attribute, separate multiple attribute qualifier by comma –>

		<property name=”uniqueKeyAttributeQualifier”>

			<value>”code”</value>

		</property>

	</custom-properties>

	<attributes>

		<attribute qualifier=”code” type=”java.lang.String”>

			<modifiers read=”true” write=”true” search=”true” optional=”false”/>

			<persistence type=”property”/>

		</attribute>

		<attribute qualifier=”catalogVersion” type=”CatalogVersion”>

			<modifiers read=”true” write=”true” search=”true” optional=”false”/>

			<persistence type=”property”/>

		</attribute>

	</attributes>

</itemtype>

Q. How to stop(kill) an abortable cronjob if it is not aborting and processing is stuck(blocked)?

Ans. If an abortable cronjob is not aborting and is stuck, we can use following groovy script to kill the java thread for specific instance of cronjob.

To know the java thread id, open Hybris admin console and navigate to Monitoring->Suspend, note the java thread id for corresponding cronjob code. Execute following script from admin console (navigate to hAC -> Console -> Scripting Language).


//Stop a thread with the given Java thread id

long threadId = javaThreadIdForTheCronJob;

ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
Thread[] threads = new Thread[threadGroup.activeCount()];
threadGroup.enumerate(threads);
for(int index=0; index &lt; threads.length; index++)
 { 
	if(threads[index] != null && threads[index.getId()] == threadId)
	{
	threads[index].stop()

	}
 } 

Q. Explain the steps to configure custom interceptor in Hybris.

Ans. Following are the steps to create and configure custom interceptor in Hybris.
1. Define your custom interceptor by implementing respective interface e.g. InitDefaultsInterceptor, LoadInterceptor, PrepareInterceptor, ValidateInterceptor or RemoveInterceptor.

2. In spring.xml of core extension (myprojectcore-spring.xml), define bean as below.
<bean id=”myInterceptor” class=”com.myproject.core.interceptor.MyInterceptorName”/>

3. Create interceptor mapping as below.
<bean id=”myInterceptorMapping” class=”de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping”>
<property name=”interceptor” ref=”myInterceptor”/>
<property name=”typeCode” value=”codeOfItemTypeIntercepted”/>
</bean>

Q. Where can we find the SAP Commerce version?

Ans. The SAP Commerce version is included in the build.number file located in

the <HYBRIS_BIN_DIR>/platform directory.

Q. What are the different phases during Hybris platform build?

Ans. Hybris platform build has three phases as listed below.

  1. Preparation – During this phase, the build program performs following tasks.
    1. Checks the directories specified by the <HYBRIS_DATA_DIR><HYBRIS_TEMP_DIR>, and <HYBRIS_LOG_DIR> SAP Commerce environment variables and creates the directories if they do not exist.
    2. Checks the directory specified by the <HYBRIS_CONFIG_DIR> SAP Commerce environment variable and prompts for a path if the directory does not exist.
    3. Resolves extension dependencies. By specifying the <requires-extension=”myextension” /> tag in an extensioninfo.xml file, we specify that the extension is dependent on myextension.
    4. Prepares the build framework by bootstrapping and copying build.xml files into each extension directory.
    5. Generates source files for all extensions (for example, abstract Jalo Layer classes based on items.xml files)
    6. Generates Models for use with the SAP Commerce ServiceLayer.
  2. Dependency update
  3. Extension building