Custom rule 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 rule code example:


import com.workbrain2.platform.publ.api.LocalizationPublService
import com.workbrain2.platform.publ.api.SystemPublicServiceAccess
import com.workbrain2.ta.publ.api.rules.ctx.ApplyRateCtxPublService
import com.workbrain2.ta.publ.api.rules.RCContext
import com.workbrain2.ta.publ.api.rules.RuleScriptable
import com.workbrain2.ta.publ.api.rules.TARulesPublicServiceAccess
import com.workbrain2.ta.publ.api.domain.rules.RCParameters
import com.workbrain2.ta.publ.api.rules.ParameterInfo
import com.workbrain2.platform.publ.api.exceptions.ScriptExecutionException
 
public class CustomApplyPayRateRule extends RuleScriptable {
   
    public final String PARAM_USE_RATE_TYPE = "USERATETYPE"
    public final String PARAM_USE_RATE_MODE = "USERATEMODE"
    public final String PARAM_APPLY_HTYPE_MULTIPLE_TO_EMP_BASE_RATE = "APPLYHTYPEMULTIPLETOEMPBASERATE"
   
    public final String RATE_TYPE_BASE = "BASE"
    public final String RATE_TYPE_PIECE = "PIECE"
    public final String RATE_TYPE_JOB = "JOB"
    public final String RATE_TYPE_TABLE = "TABLE"
   
    LocalizationPublService localizationService = SystemPublicServiceAccess.getLocalizationService()
    ApplyRateCtxPublService applyRateService = TARulesPublicServiceAccess.getApplyRateCtxService()
   
    ParameterInfo getParameterInfo() {
        ParameterInfo pi = new ParameterInfo()
        pi.addParameter(PARAM_USE_RATE_TYPE, ParameterInfo.STRING_TYPE, false)
        pi.addParameter(PARAM_USE_RATE_MODE, ParameterInfo.STRING_TYPE, true)
        pi.addTrueFalseChoiceParameter(PARAM_APPLY_HTYPE_MULTIPLE_TO_EMP_BASE_RATE, true)
        return pi
    }
   
    void execute(final RCContext context, final RCParameters parameters) throws Exception {
   
        String rateType = parameters.getParameter(PARAM_USE_RATE_TYPE)
        String rateMode = parameters.getParameter(PARAM_USE_RATE_MODE , "GREATEST")
        boolean isApplyHtypeToBaseRate = Boolean.valueOf(parameters.getParameter(PARAM_APPLY_HTYPE_MULTIPLE_TO_EMP_BASE_RATE , 
        "true")).booleanValue()
   
        if (!rateType.equals(RATE_TYPE_BASE)) {
            if (rateType.equals(RATE_TYPE_PIECE)) {
                applyRateService.applyPieceRate(context, rateMode, isApplyHtypeToBaseRate)
            } else if (rateType.equals(RATE_TYPE_JOB)) {
                applyRateService.applyJobRate(context, rateMode)
            } else if (rateType.equals(RATE_TYPE_TABLE)) {
                throw new ScriptExecutionException(
                        localizationService.getLocalizedErrorMsg("APPLY_PAY_RATE_RULE_RATE_TYPE_NOT_SUPPORTED",
                                "Apply Pay Rate Rule: Rate type '{0}' not supported.",
                                rateType
                        ))
            } else {
                throw new RuntimeException(
                        localizationService.getLocalizedErrorMsg("APPLY_PAY_RATE_RULE_RATE_TYPE_NOT_SUPPORTED",
                                "Apply Pay Rate Rule: Rate type '{0}' not supported.",
                                rateType
                        ))
            }
        }
    }
    
    @Override
    public boolean conditionSetExecutionIsMutuallyExclusive() {
        return false
    }
   
}