Custom condition code example

The code for the custom rule in this example is not formatted and does not contain syntax highlighting as would be the case with your preferred code editor. You may need to copy this code into your preferred code editor and then format.

The code examples in this document are valid examples that will work in WFM as is. However, they are just examples and will need to be modified to suit your business requirements.

Custom condition code example:


import com.workbrain.server.data.AccessException
import com.workbrain2.platform.publ.api.exceptions.ScriptExecutionException
import com.workbrain2.ta.publ.api.rules.ConditionScriptable
import com.workbrain2.ta.publ.api.rules.RCContext
import com.workbrain2.ta.publ.api.domain.rules.RCParameters
import com.workbrain2.ta.publ.api.rules.ParameterInfo
  
/**
 * Checks whether the current month is one of the given months
 */
class CustomIsSpecificMonthCondition extends ConditionScriptable {
   
    public final String PARAM_MONTHS_OF_YEAR = "MonthsOfYear"
    public final String PARAM_INCLUSIVE = "Inclusive"
  
    public final String MONTH_ERR_MSG = "Months must be subset of (JAN, " +
            "FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV ,DEC)"
  
    @Override
    ParameterInfo getParameterInfo() {
        ParameterInfo parameterInfo = new ParameterInfo()
        parameterInfo.addParameter(PARAM_MONTHS_OF_YEAR, ParameterInfo.STRING_TYPE, false)
        parameterInfo.addParameter(PARAM_INCLUSIVE, ParameterInfo.STRING_TYPE)
        return parameterInfo
    }
  
    @Override
    boolean evaluate(final RCContext context, final RCParameters parameters) throws Exception {
        String monthsYear = parameters.getParameter(PARAM_MONTHS_OF_YEAR)
        boolean inclusive = ParameterInfo.CHOICE_YES.equalsIgnoreCase(parameters.getParameter("Inclusive"))
  
        if (!monthsYear) {
            throw new ScriptExecutionException (MONTH_ERR_MSG)
        }
  
        List<String> months = (monthsYear ?: "").split(",")*.trim()
        List<Integer> monthsCal = new ArrayList<>()
        for (String month : months) {
            monthsCal.add(getCalendarMonth(month))
        }
        Calendar workDate = Calendar.getInstance()
        workDate.setTime(context.getDate())
        org.codehaus.groovy.runtime.DateGroovyMethods.clearTime(workDate)
        boolean res = monthsCal.contains(workDate.get(Calendar.MONTH)) ==  inclusive
        return res
    }
  
    @Override
    boolean validateParameters(RCParameters parameters, Map<String, String> errorMessages) throws AccessException {
        String monthsOfYear = parameters.getParameter(PARAM_MONTHS_OF_YEAR)
        if (!monthsOfYear) {
            errorMessages.put(PARAM_MONTHS_OF_YEAR, localizationService.localizeText("VALUE_IS_REQUIRED", 
            "Value is required."))
        } else {
            try {
                List<String> months = (monthsOfYear ?: "").split(",")*.trim()
                for (int i = 0; i < months.size(); i++) {
                    getCalendarMonth(months.get(0))
                }
            }
            catch (IllegalArgumentException e) {
                errorMessages.put(PARAM_MONTHS_OF_YEAR, localizationService.localizeText("INVALID_MONTHS", e.getMessage()))
            }
        }
  
        return errorMessages.size() == 0
    }
  
    /**
     * Returns the int value for month of the yeat.
     *
     *@param   month String representing a month
     *@return      month of year as in Calendar class
     */
    public int getCalendarMonth(String month){
        month = month.toUpperCase()
        if (month.indexOf("JAN") == 0 ) {
            return Calendar.JANUARY
        } else if (month.indexOf("FEB") == 0) {
            return Calendar.FEBRUARY
        } else if (month.indexOf("MAR") == 0) {
            return Calendar.MARCH
        } else if (month.indexOf("APR") == 0) {
            return Calendar.APRIL
        } else if (month.indexOf("MAY") == 0) {
            return Calendar.MAY
        } else if (month.indexOf("JUN") == 0) {
            return Calendar.JUNE
        } else if (month.indexOf("JUL") == 0) {
            return Calendar.JULY
        } else if (month.indexOf("AUG") == 0) {
            return Calendar.AUGUST
        } else if (month.indexOf("SEP") == 0) {
            return Calendar.SEPTEMBER
        } else if (month.indexOf("OCT") == 0) {
            return Calendar.OCTOBER
        } else if (month.indexOf("NOV") == 0) {
            return Calendar.NOVEMBER
        } else if (month.indexOf("DEC") == 0) {
            return Calendar.DECEMBER
        } else {
            throw new IllegalArgumentException(MONTH_ERR_MSG)
        }
    }
}