Custom entitlement action
This section contains sample code for the custom entitlement action.
import com.workbrain2.platform.publ.api.exceptions.PublException
import com.workbrain2.ta.publ.api.TimeCodePublService
import com.workbrain2.ta.publ.api.domain.EntitlementDetailPubl
import com.workbrain2.ta.publ.api.domain.EntitlementPubl
import com.workbrain2.ta.publ.api.domain.TimeCodePubl
import com.workbrain2.ta.publ.api.domain.WorkDetailPubl
import com.workbrain2.ta.publ.api.entitlements.EntitlementActionScriptable
import com.workbrain2.ta.publ.api.rules.RCContext
import com.workbrain2.ta.publ.api.rules.TARulesPublicServiceAccess
import com.workbrain2.ta.publ.api.rules.ctx.EmployeeBalanceCtxPublService
import com.workbrain2.ta.publ.api.rules.ctx.EmployeeCtxPublService
import com.workbrain2.ta.publ.api.rules.ctx.WorkDetailCtxPublService
// Note: Not all methods need to be implemented. Default implementation is available for all Entitlement Actions.
class CustomEntitlementAction extends EntitlementActionScriptable{
EmployeeCtxPublService employeeCtxPublService = TARulesPublicServiceAccess.getEmployeeCtxService()
EmployeeBalanceCtxPublService employeeBalanceCtxPublService = TARulesPublicServiceAccess.getEmployeeBalanceCtxService()
TimeCodePublService timeCodePublService = TARulesPublicServiceAccess.getTimeCodeService()
WorkDetailCtxPublService workDetailCtxPublService = TARulesPublicServiceAccess.getWorkDetailCtxService()
public boolean execute(RCContext context, EntitlementPubl entitlement, EntitlementDetailPubl entitlementDetail) throws PublException{
// It is possible to simply execute the default execute implementation
return super.execute(context,entitlement, entitlementDetail)
}
public boolean test(RCContext context, EntitlementPubl entitlement, EntitlementDetailPubl entitlementDetail) throws PublException{
// If employee is not active, fail the entitlement condition
if (!employeeCtxPublService.getEmployeeStatus(context).equals("A")) {
return false
}
return super.test(context, entitlement, entitlementDetail)
}
public double getRatio(RCContext context, EntitlementPubl entitlement, EntitlementDetailPubl entitlementDetail) throws PublException{
// Update Ratio based on different conditions
if (employeeCtxPublService.getEmployeeAge(context) > 50){
return 2
}else{
return 1
}
}
public void executeOnMaxOut(RCContext context, EntitlementPubl entitlement, EntitlementDetailPubl entitlementDetail) throws PublException{
// Set balance to maximum on max out
long balId = entitlementDetail.getBalanceId()
double delta = entitlementDetail.getMaximumBalanceValue() - employeeBalanceCtxPublService.getEmployeeBalanceValue(balId,context)
employeeBalanceCtxPublService.addEmployeeBalanceValue(balId,delta,"Set balance to max", context)
}
public void executeOnMinOut(RCContext context, EntitlementPubl entitlement, EntitlementDetailPubl entitlementDetail) throws PublException{
// Set balance to minimum on min out
long balId = entitlementDetail.getBalanceId()
double delta = entitlementDetail.getMinimumBalanceValue() - employeeBalanceCtxPublService.getEmployeeBalanceValue(balId,context)
employeeBalanceCtxPublService.addEmployeeBalanceValue(balId,delta,"Set balance to min", context)
}
public int getSumGood(RCContext context, EntitlementPubl entitlement, EntitlementDetailPubl entitlementDetail) throws PublException{
List<Long> goodTcodeIds = entitlementDetail.getGoodTimeCodeIds()
String unit = entitlementDetail.getMinimumGoodUnit()
List<TimeCodePubl> tcodes = timeCodePublService.getTimeCodesByIds(goodTcodeIds)
List<WorkDetailPubl> workDetails = workDetailCtxPublService.getWorkDetails(context)
if (unit.equals("M")){
int sum = 0
for (TimeCodePubl tcode : tcodes){
for (WorkDetailPubl workDetail : workDetails){
if (goodTcodeIds.contains(workDetail.getTimeCodeId())){
sum = sum + workDetail.getMinutes()
}
}
}
return sum
}else{
// sum based on other units...
}
return 0
}
public int getSumBad(RCContext context, EntitlementPubl entitlement, EntitlementDetailPubl entitlementDetail) throws PublException{
List<Long> badTcodeIds = entitlementDetail.getBadTimeCodeIds()
String unit = entitlementDetail.getMinimumBadUnit()
List<TimeCodePubl> tcodes = timeCodePublService.getTimeCodesByIds(badTcodeIds)
List<WorkDetailPubl> workDetails = workDetailCtxPublService.getWorkDetails(context)
if (unit.equals("M")){
int sum = 0
for (TimeCodePubl tcode : tcodes){
for (WorkDetailPubl workDetail : workDetails){
if (badTcodeIds.contains(workDetail.getTimeCodeId())){
sum = sum + workDetail.getMinutes()
}
}
}
return sum
}else{
// sum based on other units...
}
return 0
}
}