Useful Groovy Scripts. Part 6. – hybrismart | SAP hybris under the hood

Useful Groovy Scripts. Part 6.


Previous parts:

Executing an SQL Query. For hybris 6.0+

In the latest versions of SAP Hybris the classes from HAC are no longer available in Groovy. The code I published before won’t work anymore.

sql = "select pk, p_isocode from languages";
result = jdbcTemplate.queryForList(sql);
result[0].keySet().forEach { print it+"\t"; }
println "";
result.forEach
{
it.values().forEach { print it+"\t"; }
println "";
}

The result can be copied to Excel for further processing (Paste as text; don’t forget to change the cell types to Text to avoid automatic cell value type detection in Excel)

Accessing the file system and fetching a text file via HAC

For example, the following one-liner shows the contents of localextensions.xml.

"cat ../../../../config/localextensions.xml".execute().text

Invalidating a cache for a particular item

cv = catalogVersionService.getCatalogVersion('mycatalog', 'Online')
p = productService.getProductForCode(cv, '123456789')

de.hybris.platform.util.Utilities.invalidateCache(p.pk)

modelService.refresh(p)
"" + p.pk + ":" + p.name

Executing the multi-column flexibleSearch

import de.hybris.platform.jalo.JaloSession;
query = "select {isocode}, {name} from {Language} ";
jaloSession = JaloSession.getCurrentSession();
List result = jaloSession.getFlexibleSearch().search(
query,
Collections.EMPTY_MAP,
Arrays.asList(
//types of arguments in Select
String.class,
String.class
//String.class, String.class
//
),
true,
true,
0, -1).getResult();
result.forEach {
it.forEach { print it + "\t"; }
println "";
}

The result can be copied to Excel for further processing (Paste as text; don’t forget to change the cell types to Text to avoid automatic cell value type detection in Excel)

Executing a long/resource-intensive groovy script

There is a common problem that some scripts need more time that tomcat/hybris allows. After a couple minutes the system kills the script. In order to overcome this problem, you need to create a cronjob with this script and launch it. However, remember that if your script is resource intensive, you may face to the lack of resources, and it is not fixable: you won’t be able to stop the script after it is launched.

There are two options. The first one is creating a thread, the second one is creating a cronjob.

Thread

import org.apache.logging.log4j.LogManager;
def log(String s) {
    println(s)
    LogManager.getLogger().info(s)
}
def thread = Thread.start {
  sleep(2000)
  log "new thread - this line will be in the hybris log only after 2 sec"
  sleep(20000)
  log "new thread 20sec finished - this line will be in the hybris log only after 20 sec"
  sleep(20000)
  log "new thread last 20sec finished - this line will be in the hybris log only after 40 sec"
}
log "immediate message in the groovy console and hybris log"

Cronjob

First, you need to create a groovy script and save it in HAC as “test1”. This name of the script to run is specified in the script below. Of course, you can use any name instead of “test1”, just replace it with your script name in the 4th line in the script below.

As an example of the long-lasting script, let’s take one that shows the files from the hybris media folder.

def p = ['/bin/bash', '-c', 'ls -lR ../../../../data/media/sys_master/* > /tmp/media-folder-size.tmp'].execute()
p.waitFor()
println p.text
import de.hybris.platform.servicelayer.internal.model.ScriptingJobModel;
import de.hybris.platform.cronjob.model.CronJobModel;

scriptName = "test1"
CRONJOBNAME="myHacScriptCronJob1";
MYHACSCRIPTJOBNAME = "myHacScriptJob1"
def scriptingJob = null;
def myHacScriptDynamicCronJob = null;
scriptingJobResults = flexibleSearchService.search("select {pk} from {ScriptingJob} where {code} = '"+MYHACSCRIPTJOBNAME+"'").getResult();
if (scriptingJobResults.size()>0) { scriptingJob = scriptingJobResults.get(0) }
else {
scriptingJob = modelService.create(ScriptingJobModel.class);
scriptingJob.setCode(MYHACSCRIPTJOBNAME);
scriptingJob.setScriptURI("model://"+scriptName);
modelService.save(scriptingJob);
}

try {
myHacScriptDynamicCronJob = cronJobService.getCronJob(CRONJOBNAME);
} catch (de.hybris.platform.servicelayer.exceptions.UnknownIdentifierException e) {
myHacScriptDynamicCronJob = modelService.create(CronJobModel.class);
myHacScriptDynamicCronJob.setCode(CRONJOBNAME);
myHacScriptDynamicCronJob.setJob(scriptingJob);
myHacScriptDynamicCronJob.setActive(true);
myHacScriptDynamicCronJob.setSingleExecutable(true);
myHacScriptDynamicCronJob.setLogToFile(true);
modelService.save(myHacScriptDynamicCronJob);
}
dynamicCJ = cronJobService.getCronJob(CRONJOBNAME)
cronJobService.performCronJob(dynamicCJ,true)

After the cronjob is over, open the temporary file from /tmp/media-folder-size.tmp to see the results:

"cat /tmp/media-folder-size.tmp".execute()

For this particular case, the large size of the file can be an issue. This file can be preprocessed in groovy before getting delivered.  For example, all non-relevant stuff (such as file flags, a user id and group id) can be removed.

Leave a Reply