SAP Commerce 1811
Impex -
INSERT_UPDATE Calendar ; code[unique=true] ; name[lang=en] ; year ; active[default=false]
; 10001 ; 2021 Public Holiday ; 2021 ; true
; 10002 ; 2021 Holiday ; 2021 ; true
I have created one validate interceptor which will make sure that only one calendar can be active at a time. It means we can't make more than two Calendar active for the same year.
final CalendarModel cal = calendarService.getActiveCalendar(calendar.getYear());
if (cal != null && !cal.equals(calendar))
{
throw new InterceptorException(
String.format("Only one Calendar can be active at a time for year %s", calendar.getYear()));
}
In this Impex, I am inserting two Calendars with active=true, and expecting to give validation exception for one of the entry.
Since in this use case, one entry depends on other, it won't work in case of multi threading (because order will not defined).
If Max. threads is set to more than 1, and I run the impex, validation is not working. I tried importing this impex with 1 thread, then only validation is working.
Is there any way to solve this issue ?
You cannot solve this problem with an Interceptor. Interceptors are simply not threadsafe. They are not built to validate if an object already exists or not. The interceptors are meant to validate the object that will be saved, independently of other objects.
This is a problem that needs to be fixed on the database, not in your code. You need to add an SQL constraint
(more specifically an SQL CHECK
Constraint) on the table of your CalendarModel. This constraint should validate your requirement on 1 active item per year.
How to write that constraint will depend on your database. To add it, you can either add the constraint directly in the database, though an update system might remove it again (I did not validate this, but it occurs for indexes). Alternativly, you can run an alter table statement during initialization to create the constraint.
With this constraint, a database error will be thrown on the illegal item. This will be visibile in the result when running the impex