onurkat's blog

my journey in software development

Understanding and Configuring Cronjob Triggers with Cron Expressions in SAP Hybris

In Hybris, cronjobs are essential for automating various backend processes, such as database cleanups, order processing, or generating reports. A crucial part of setting up a cronjob is configuring its cron expression. In this post, I will guide you through the process of setting up cron expressions and explain how they control the scheduling of cronjobs in Hybris.


What is a Cron Expression?

A cron expression is a string that represents a schedule for executing a task. It is widely used in UNIX-based systems and is also implemented in Hybris to define how often and when a cronjob should run.


Structure of a Cron Expression

A cron expression in Hybris consists of six fields, each representing a specific part of the schedule:

1
2
3
4
5
6
7
8
* * * * * *
| | | | | |
| | | | | +--- Day of the week (0 - 7) (Sunday = 0 or 7)
| | | | +----- Month (1 - 12)
| | | +------- Day of the month (1 - 31)
| | +--------- Hour (0 - 23)
| +----------- Minute (0 - 59)
+------------- Second (0 - 59)
Read more »

Dynamic attributes in Hybris (SAP Commerce) allow you to add flexibility to your models without having to modify the database schema. This guide will walk you through the complete cycle of creating and using dynamic attributes.

What is a Dynamic Attribute?

A dynamic attribute is a calculated property on a model that isn’t stored in the database but is computed at runtime. These attributes are useful when you need to provide values based on business logic without altering the model structure.

The Full Cycle of Creating a Dynamic Attribute in Hybris

Step 1: Create the Business Logic with a DynamicAttributeHandler

You need to write the logic that will be used to compute the dynamic attribute’s value. You can achieve this by implementing a DynamicAttributeHandler class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class DiscountedPriceAttributeHandler implements DynamicAttributeHandler<Double, ProductModel> {

@Override
public Double get(final ProductModel product) {
// Business logic to calculate the dynamic attribute
double price = product.getPrice();
double discount = 0.1; // 10% discount
return price * (1 - discount);
}

@Override
public void set(final ProductModel product, final Double discountedPrice) {
throw new UnsupportedOperationException("Cannot set discountedPrice directly");
}
}

Step 2: Register the Handler in Spring Configuration

You must register the DiscountedPriceAttributeHandler in your Spring XML configuration file. This step connects the handler to your model’s dynamic attribute.

1
<bean id="discountedPriceHandler" class="com.mycompany.core.handler.DiscountedPriceAttributeHandler" />

Step 3: Define the New Attribute

You need to define the model in which you want to introduce the dynamic attribute. For this example, let’s add a dynamic attribute to ProductModel.

1
2
3
4
5
6
7
8
9
10
11
12
<itemtype code="Product" autocreate="false" generate="false">
<attributes>
<!-- Standard attributes -->
<attribute qualifier="code" type="java.lang.String" />

<!-- Dynamic attribute example -->
<attribute qualifier="discountedPrice" type="java.lang.Double">
<persistence type="dynamic" attributeHandler="discountedPriceHandler"/>
<modifiers write="false" />
</attribute>
</attributes>
</itemtype>

Step 4: Rebuild the Project

Once you’ve updated your files, rebuild the project to apply your changes:

1
ant clean all

This will compile the code, generate the dynamic model classes, and deploy them into your Hybris environment.

Step 5: Access the Dynamic Attribute in Java

You can now access the dynamic attribute in your business logic. Simply use the getter for the dynamic attribute, as if it were a standard model attribute:

1
2
3
ProductModel product = productService.getProductForCode("someProductCode");
Double discountedPrice = product.getDiscountedPrice();
LOG.info("Discounted Price: " + discountedPrice);

Step 6: Testing the Dynamic Attribute

Test the dynamic attribute by either writing unit tests or using the Hybris Backoffice to check if the dynamic attribute is computed as expected.

Key Considerations

  • Performance: Dynamic attributes are computed at runtime, so be cautious about adding complex logic.
  • Immutability: In most cases, dynamic attributes should be read-only to avoid unnecessary complexity.
  • Testing: Since the logic is part of the attribute handler, make sure to write unit tests to cover all business scenarios.

Final Thoughts

Creating a dynamic attribute in Hybris can add flexibility and power to your models, allowing you to compute values on the fly without modifying your database. This approach is particularly useful when dealing with calculated data that is not frequently used or changes often.

By following this full cycle from point A to Z, you’ll have a solid understanding of how to implement and work with dynamic attributes in Hybris.

Happy Coding!

Hybris, SAP Commerce

Hybris is an e-commerce platform that was originally developed by a company called Hybris AG, which was later acquired by SAP in 2013. The platform is now known as SAP Commerce, but is still commonly referred to as Hybris.

Features

SAP Commerce/Hybris is a robust e-commerce platform that offers a wide range of features to help businesses sell their products online. Some of the key features of the platform include:

  • Product management: SAP Commerce/Hybris makes it easy to manage product catalogs and inventory levels, as well as create and manage product promotions.

  • Order management: The platform provides tools to manage the entire order process, from order creation to fulfillment and delivery.

  • Customer management: SAP Commerce/Hybris allows businesses to manage customer profiles and preferences, as well as provide personalized experiences based on customer data.

  • Marketing and personalization: The platform includes tools to create targeted marketing campaigns and personalized experiences for customers.

  • Multi-channel support: SAP Commerce/Hybris supports multiple channels, including web, mobile, and in-store, allowing businesses to reach customers wherever they are.

Read more »

1- Validating Email Addresses

Regex: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$

This regular expression can be used to validate email addresses in a form. It checks for a string of characters before the @ symbol, followed by a domain name and a top-level domain extension.

2- Matching URLs:

Regex: (http|https)://[a-zA-Z0-9./?=_-]+

This regular expression can be used to match URLs in a text file. It matches both http and https URLs and includes characters such as slashes, question marks, equal signs, and underscores.

3- Extracting Phone Numbers:

Regex: \d{3}-\d{3}-\d{4}

This regular expression can be used to extract phone numbers from a text file or a database. It matches a pattern of three digits, followed by a hyphen, three more digits, another hyphen, and four more digits.

Read more »

Sometimes computers freeze during development usually because our physical RAM isn’t enough for local development environment and there is a way to support our physical RAM with virtual RAM for Linux too.

By creating swap files, you can automatically move inactive pages in your RAM to these swap files when your RAM is almost full.
Since only the inactive pages are moved to this swap file, you will not lose your read-write speed by continuing to actively use your physical RAM and your computer will not freeze.

Simply put, swap files host unnecessary data from your RAM on your disk until it’s needed.

Read more »

Cool RegEx Examples

What is RegEx?

Regular expressions are useful in search and replace operations. The typical use case is to look for a sub-string that matches a pattern and replace it with something else.

Sometimes, learning how to write RegEx’es before you even need them can save you a lot of time.

Read more »

Hello world!

Who am I?

  • Onur Kat, a computer engineer from Istanbul, Turkey.

  • Currently, I’m working at Customertimes as Senior SAP CX (Hybris) Backend Developer.

Read more »
0%