0

I'm writing a small Groovy script for Hybris.

For reasons unknown when I attempt to call .getCronExpression() Groovy tries to get a property named getCron.

Script execution has failed [reason: groovy.lang.MissingPropertyException: No such property: getCron for class: de.hybris.platform.cronjob.model.TriggerModel]

The getter which I try to call exists on the class.

def methods = TriggerModel.declaredMethods.findAll { !it.synthetic }.name
println methods;
//[getDaysOfWeek, getWeekInterval, setDaysOfWeek, setWeekInterval, getRelative, getJob, setActivationTime, setMaxAcceptableDelay, getTimeTable, setActive, setJob, getCronJob, getActivationTime, setDateRange, getDateRange, getMaxAcceptableDelay, getCronExpression, setCronExpression, setCronJob, getActive, setRelative, setDay, setHour, setMinute, setSecond, getHour, getMinute, getSecond, getYear, getMonth, setYear, setMonth, getDay]

Code part:

def currentDate = new Date();
def query = new FlexibleSearchQuery("SELECT {pk} FROM {cronjob} WHERE {active} IS true");
def result = flexibleSearchService.search(query).getResult();
for (cj in result) {
  def activeTriggers = cj.getTriggers().stream().filter{p -> p.getActive()}.collect();

  if (activeTriggers){
    def at = activeTriggers.get(0);
    def activationTS = at.getActivationTime(); // works
    if (activationTS.before(currentDate)){
      println cj.code + " has invalid next activation date set: " + activationTS; 
    }

    def x = at.getCronExpression(); // error
  }

Update: the project uses hybris 5.7

Andrew
  • 95
  • 2
  • 12

2 Answers2

1

Replacing the getter with the variable name fixed the issue.

Replace-

def x = at.getCronExpression(); // error

with

def xam = at.cronExpression; // working

Complete working groovy-

import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;

def currentDate = new Date();
def query = new FlexibleSearchQuery("SELECT {pk} FROM {cronjob} WHERE {active}='1'");
def result = flexibleSearchService.search(query).getResult();
for (cj in result) {
  def activeTriggers = cj.getTriggers().stream().filter{p -> p.getActive()}.collect();

  if (activeTriggers){
    def at = activeTriggers.get(0);
    def activationTS = at.getActivationTime(); // works
    if (activationTS.before(currentDate)){
      println cj.code + " has invalid next activation date set: " + activationTS; 
    }

    def xam = at.cronExpression; // working
  }
}
Farrukh Chishti
  • 7,652
  • 10
  • 36
  • 60
  • Yes, this solves it. But does not answer why Groovy is not able to call the getter properly. – Andrew Nov 18 '19 at 08:17
  • Having the same issue "No such property: setCron" when using setCronExpression(). Same for get. Hybris 2011. I bet that it is related to upgrading Groovy in Hybris. In Groovy 2.5 there were some major changes like e.g. groovy.sql.Sql.newInstance(). Maybe there were also issues with former Groovy updates in Hybris. – reichhart Mar 29 '22 at 05:58
0

Can you share your groovy part where you are loading activeTriggers?

I created a sample groovy to load Triggers and print CronExpression for 1st object and it worked like a charm.

import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;

flexibleQuery = new FlexibleSearchQuery("select {pk} from {Trigger}");
flexibleSearchService = spring.getBean("flexibleSearchService")
activeTriggers = flexibleSearchService.search(flexibleQuery).getResult();

def at = activeTriggers.get(0);
def x = at.getCronExpression(); 

OUTPUT

0 0 0/4 * * ? *

AFTER PO EDITED THE QUESTION I still don't see getCronExpression error, I believe you were calling the function on a different object then TriggerModel. Though there were some syntax errors in your groovy, a working version of the copy is as below. (Tested on 1905 version)

import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;

def currentDate = new Date();
def query = new FlexibleSearchQuery("SELECT {pk} FROM {cronjob} WHERE {active} = true");
def result = flexibleSearchService.search(query).getResult();
for (cj in result) {
  def activeTriggers = cj.getTriggers().stream().filter{p -> p.getActive()}.collect();

  if (activeTriggers){
    def at = activeTriggers.get(0);
    def activationTS = at.getActivationTime(); // works
    if (activationTS!=null && activationTS.before(currentDate)){
      println cj.code + " has invalid next activation date set: " + activationTS; 
    }

    def x = at.getCronExpression(); // error
    println x;
  }

}
www.hybriscx.com
  • 1,129
  • 4
  • 22