package com.workbrain2.platform.publ.api.alerts;
/**
* A relatively advanced Groovy script for alerts.
* It has used various whitelisted publ APIs for this purpose.
* It is related to what we have implemented in UnauthorizedTimesheetAlertSource with same level of complexity.
* @author Reza Ahmadi
*/
class AuthorizedTimesheetAlertsScriptable extends AlertsRowSourceScriptable {
private static final String COL_EMP_ID = "EMP_ID";
private static final String COL_EMP_NAME = "EMP_NAME";
private static final String COL_EMP_FULLNAME = "EMP_FULLNAME";
private static final String COL_WRKS_WORK_DATE = "WRKS_WORK_DATE";
private static final String COL_TIMESHEET_LINK = "timesheet";
private List<DataRowPubl> getResultSet() {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
List<DataRowPubl> res = new ArrayList<>();
List<String> empArray = getEmployeeIdsParam();
List<String> calcGroupArray = getCalcGroupsParam();
List<String> payGroupArray = getPayGroupsParam();
List<String> teamsArray = getTeamsParam();
EmployeePubl empInfo = SystemPublicServiceAccess.getEmployeeService().getEmployeeByName("DEPT_1.1.1.6_EMP");
//empid is 83
Date startDate = sdf.parse("01/01/2007");
Date endDate = sdf.parse("02/04/2026");
Map<Long, WorkSummaryPubl> workSummaryMap = [:]
List<WorkSummaryPubl> workSummaries = TAPublicServiceAccess
.getWorkSummaryPublService()
.getWorkSummaries(empInfo.getId(), startDate, endDate, 0, 1000)
for (WorkSummaryPubl ws : workSummaries) {
if (ws.isAuthorized()) {
workSummaryMap[ws.employeeId] = ws
}
}
Map<Long, EmployeeTeamPubl> teamsMap = [:]
List<EmployeeTeamPubl> employeeTeams = SystemPublicServiceAccess
.getEmployeeTeamService()
.getEmployeeTeamsByEmpIdAndDate(empInfo.getId(), startDate, endDate)
for (EmployeeTeamPubl team : employeeTeams) {
if (team.homeTeam && (teamsArray ? teamsArray.contains(team.id) : true)) {
teamsMap[team.employeeId] = team
}
}
List<EmployeeHistoryPubl> allEmpHistories = TAPublicServiceAccess
.getEmployeeHistoryPublService()
.getEmployeeHistoryByDateRange(empInfo.getId(), startDate, endDate)
List<EmployeeHistoryPubl> empHistories = []
for (EmployeeHistoryPubl eh : allEmpHistories) {
if (workSummaryMap.containsKey(eh.getEmployeeId()) && teamsMap.containsKey(eh.getEmployeeId())) {
empHistories.add(eh)
}
}
List<EmployeeHistoryPubl> filteredEmpHistories = []
for (EmployeeHistoryPubl eh : empHistories) {
boolean include = true
if (empArray != null && empArray.size() > 0 && !empArray.contains(eh.getEmployeeId())) {
include = false
}
if (calcGroupArray != null && calcGroupArray.size() > 0 && !calcGroupArray.contains(eh.getCalcGroupId())) {
include = false
}
if (payGroupArray != null && payGroupArray.size() > 0 && !payGroupArray.contains(eh.getPayGroupId())) {
include = false
}
if (include) {
filteredEmpHistories.add(eh)
}
}
empHistories = filteredEmpHistories
for (EmployeeHistoryPubl eh : empHistories) {
WorkSummaryPubl ws = workSummaryMap[eh.employeeId]
//new row
DataColumnPubl empIdColumn = new DataColumnPubl(COL_EMP_ID, String.valueOf(eh.employeeId));
DataColumnPubl empNameColumn = new DataColumnPubl(COL_EMP_NAME, eh.name);
DataColumnPubl empFullNameColumn = new DataColumnPubl(COL_EMP_FULLNAME, eh.fullName);
DataColumnPubl empWorkDateColumn = new DataColumnPubl(COL_WRKS_WORK_DATE, ws.workDate.toString());
// create timesheet link
String params = "&DATE_SELECT=7&AUTH_SELECT=0&VIEW_SELECT=0&SUBMIT_PARAMS=T&ORDER_SELECT=0";
String tmpParams = params.concat("&INC_EMP_ID_0=" + eh.employeeId);
String tsLink = "";
tsLink+="<a href=\"";
String timesheetLink = "fakelink";
if (timesheetLink.indexOf("document.location=' contextPath") != -1) {
timesheetLink = timesheetLink.replaceAll("document.location=' contextPath", "#contextPath#");
timesheetLink = timesheetLink.substring(0, timesheetLink.length() - 2);
}
tsLink+=timesheetLink;
tsLink+="\">";
tsLink+="</a>";
DataColumnPubl timeSheetLinkColumn = new DataColumnPubl(COL_TIMESHEET_LINK, tsLink);
DataRowPubl newRow = new DataRowPubl(Arrays.asList(empIdColumn, empNameColumn, empFullNameColumn, empWorkDateColumn, timeSheetLinkColumn));
res.add(newRow)
}
return res;
}
@Override
public List<DataRowPubl> queryAllForScripts() {
return getResultSet();
}
}