|
|
@@ -1,783 +0,0 @@ |
|
|
|
package com.ffii.lioner.modules.lioner.service; |
|
|
|
|
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
import java.text.ParseException; |
|
|
|
import java.time.LocalDate; |
|
|
|
import java.time.LocalDateTime; |
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.scheduling.annotation.Scheduled; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import com.ffii.lioner.modules.lioner.entity.Application; |
|
|
|
import com.ffii.lioner.modules.lioner.entity.Event; |
|
|
|
import com.ffii.lioner.modules.lioner.entity.TodoReminder; |
|
|
|
import com.ffii.lioner.modules.lioner.entity.TodoReminderEmailLog; |
|
|
|
import com.ffii.lioner.modules.lioner.entity.TodoReminderRepository; |
|
|
|
import com.ffii.lioner.modules.common.SecurityUtils; |
|
|
|
import com.ffii.lioner.modules.common.SettingNames; |
|
|
|
import com.ffii.lioner.modules.common.mail.pojo.MailRequest; |
|
|
|
import com.ffii.lioner.modules.common.mail.service.MailService; |
|
|
|
import com.ffii.lioner.modules.common.service.AuditLogService; |
|
|
|
import com.ffii.lioner.modules.master.entity.Division; |
|
|
|
import com.ffii.lioner.modules.master.entity.SubDivision; |
|
|
|
import com.ffii.lioner.modules.master.service.DivisionService; |
|
|
|
import com.ffii.lioner.modules.master.service.SubDivisionService; |
|
|
|
import com.ffii.lioner.modules.settings.service.SettingsService; |
|
|
|
import com.ffii.lioner.modules.user.entity.User; |
|
|
|
import com.ffii.lioner.modules.user.service.UserService; |
|
|
|
import com.ffii.core.support.AbstractBaseEntityService; |
|
|
|
import com.ffii.core.support.JdbcDao; |
|
|
|
import com.ffii.core.utils.JsonUtils; |
|
|
|
|
|
|
|
import jakarta.mail.internet.InternetAddress; |
|
|
|
import jakarta.persistence.Table; |
|
|
|
|
|
|
|
@Service |
|
|
|
public class TodoReminderService extends AbstractBaseEntityService<TodoReminder, Long, TodoReminderRepository> { |
|
|
|
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
|
|
|
|
|
|
|
private Integer reminderBefore; |
|
|
|
private Integer reminderInterval; |
|
|
|
private Integer reminderLimit; |
|
|
|
private Integer reminderMaxValue; |
|
|
|
private Integer reminderEventWithin; |
|
|
|
private EventService eventService; |
|
|
|
private MailService mailService; |
|
|
|
private UserService userService; |
|
|
|
private DivisionService divisionService; |
|
|
|
private ApplicationService applicationService; |
|
|
|
private SubDivisionService subDivisionService; |
|
|
|
private AuditLogService auditLogService; |
|
|
|
private SettingsService settingsService; |
|
|
|
|
|
|
|
@Value("${host.url}") |
|
|
|
private String url; |
|
|
|
|
|
|
|
public TodoReminderService(JdbcDao jdbcDao, TodoReminderRepository repository, SettingsService settingsService, |
|
|
|
EventService eventService, MailService mailService, UserService userService, |
|
|
|
DivisionService divisionService, SubDivisionService subDivisionService, |
|
|
|
ApplicationService applicationService, AuditLogService auditLogService) { |
|
|
|
super(jdbcDao, repository); |
|
|
|
this.eventService = eventService; |
|
|
|
this.mailService = mailService; |
|
|
|
this.userService = userService; |
|
|
|
this.divisionService = divisionService; |
|
|
|
this.auditLogService = auditLogService; |
|
|
|
this.subDivisionService = subDivisionService; |
|
|
|
this.applicationService = applicationService; |
|
|
|
this.settingsService = settingsService; |
|
|
|
this.reminderBefore = settingsService.getInt(SettingNames.SYS_REMINDER_BEFORE); |
|
|
|
this.reminderInterval = settingsService.getInt(SettingNames.SYS_REMINDER_INTERVAL); |
|
|
|
this.reminderLimit = settingsService.getInt(SettingNames.SYS_REMINDER_LIMIT); |
|
|
|
this.reminderMaxValue = settingsService.getInt(SettingNames.SYS_REMINDER_LIMIT_MAX); |
|
|
|
this.reminderEventWithin = settingsService.getInt(SettingNames.SYS_REMINDER_WITHIN); |
|
|
|
} |
|
|
|
|
|
|
|
@Scheduled(cron = "0 0 6 * * ?") // Runs at 06:00 AM every day |
|
|
|
public void generateApplicationReminder() throws UnsupportedEncodingException, ParseException { |
|
|
|
logger.info("============= Generate Application Reminder Start ============="); |
|
|
|
LocalDateTime range = LocalDateTime.now().minusYears(reminderEventWithin); |
|
|
|
|
|
|
|
logger.info("Fetch Event From: " + range.toString()); |
|
|
|
|
|
|
|
List<Event> eventList = eventService.getEventByWithinYear(Map.of( |
|
|
|
"yearRange", range)); |
|
|
|
|
|
|
|
logger.info("Fetched Total Number of Event: " + eventList.size()); |
|
|
|
|
|
|
|
for (Event event : eventList) { |
|
|
|
|
|
|
|
logger.info("---------------------------------------------"); |
|
|
|
logger.info("Checking Event: " + event.getName()); |
|
|
|
Boolean isOldEvent = false; |
|
|
|
LocalDate firstIssueDate = null; |
|
|
|
Boolean hasApplicationRecord = eventService.hasApplicationRecord(Map.of("id", event.getId())); |
|
|
|
|
|
|
|
|
|
|
|
LocalDate newEventFirstIssueDate = event.getStartDate().minusDays(event.getReminderThreshold()); |
|
|
|
generateNewEventApplicationReminder(event, newEventFirstIssueDate); |
|
|
|
if (hasApplicationRecord) {{ |
|
|
|
if (event.getNextApplicationDate() == null) |
|
|
|
continue; |
|
|
|
isOldEvent = true; |
|
|
|
|
|
|
|
LocalDate oldEventFirstIssueDate = event.getNextApplicationDate().minusDays(event.getReminderThreshold()); |
|
|
|
generateOldEventApplicationReminder(event, oldEventFirstIssueDate); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
logger.info("============= Generate Application Reminder End ============="); |
|
|
|
} |
|
|
|
|
|
|
|
public void generateOldEventApplicationReminder(Event event, LocalDate firstIssueDate) { |
|
|
|
|
|
|
|
logger.info("=> Checking Old Event Application Reminder"); |
|
|
|
LocalDate now = LocalDate.now(); |
|
|
|
Integer limit = event.getReminderLimit().intValue(); |
|
|
|
Integer interval = event.getReminderInterval().intValue(); |
|
|
|
|
|
|
|
List<LocalDate> reminderDateSchedule = new ArrayList<LocalDate>(); |
|
|
|
for (Integer i = 0; i < limit; i++) { |
|
|
|
reminderDateSchedule.add(firstIssueDate.plusDays(i * interval)); |
|
|
|
} |
|
|
|
|
|
|
|
logger.info("Reminder Dates: " + reminderDateSchedule.stream() |
|
|
|
.map(date -> date.format(dateFormat)).collect(Collectors.joining(", "))); |
|
|
|
|
|
|
|
List<Map<String, Object>> eventSubDivision = eventService |
|
|
|
.getEventDivisionAllData(Map.of("id", event.getId())); |
|
|
|
for (Map<String, Object> subDivision : eventSubDivision) { |
|
|
|
Integer eventSubDivisionId = (Integer) subDivision.get("id"); |
|
|
|
Integer subDivisionId = (Integer) subDivision.get("subDivisionId"); |
|
|
|
Integer reminderCount = (Integer) subDivision.get("oldReminderCount"); |
|
|
|
Boolean isSuppressed = (Boolean) subDivision.get("suppressedOldReminder"); |
|
|
|
if (!isSuppressed) { |
|
|
|
if (reminderCount < reminderDateSchedule.size()) { |
|
|
|
LocalDate date = reminderDateSchedule.get(reminderCount); |
|
|
|
|
|
|
|
if (date.compareTo(now) <= 0) { |
|
|
|
logger.info("*** Reminder Date Matched ***"); |
|
|
|
|
|
|
|
// update event_sub_division reminder count |
|
|
|
jdbcDao.executeUpdate( |
|
|
|
"UPDATE event_sub_division esd" |
|
|
|
+ " SET esd.oldReminderCount = esd.oldReminderCount + 1" |
|
|
|
+ " WHERE id = :eventSubDivisionId", |
|
|
|
Map.of("eventSubDivisionId", eventSubDivisionId)); |
|
|
|
|
|
|
|
// Send email reminder |
|
|
|
List<User> userList = userService.listUserBySubDivision(subDivisionId); |
|
|
|
for (User user : userList) { |
|
|
|
logger.info("Send Email to user: " + user.getUsername() + " (" |
|
|
|
+ user.getEmail() |
|
|
|
+ ")"); |
|
|
|
sendApplicationEmail(event.getId(), subDivisionId.longValue(), |
|
|
|
user.getId(), |
|
|
|
event, user, |
|
|
|
true); |
|
|
|
} |
|
|
|
|
|
|
|
// Update system reminder |
|
|
|
String message = "Please note below event will be opened for application:\\n\\n" |
|
|
|
+ |
|
|
|
" - Event Name: \\t\\t\\t " + event.getName() + "\\n" + |
|
|
|
" - Application Deadline: \\t " |
|
|
|
+ event.getApplicationDeadline(); |
|
|
|
|
|
|
|
TodoReminder instance = new TodoReminder(); |
|
|
|
instance.setEventId(event.getId()); |
|
|
|
instance.setSubDivisionId(subDivisionId.longValue()); |
|
|
|
instance.setTitle("Application Reminder (Old Event)"); |
|
|
|
instance.setMessage(message); |
|
|
|
instance.setReminderType("oldEventApplication"); |
|
|
|
|
|
|
|
// =====GET OLD AUDIT LOG=====// |
|
|
|
String tableName = instance.getClass().getAnnotation(Table.class) |
|
|
|
.name(); |
|
|
|
Map<String, Object> oldValueObject = new HashMap<String, Object>(); |
|
|
|
Map<String, Object> newValueObject = new HashMap<String, Object>(); |
|
|
|
|
|
|
|
if (instance != null && instance.getId() != null |
|
|
|
&& instance.getId() > 0) { |
|
|
|
Map<String, Object> input = Map.of("id", instance.getId()); |
|
|
|
Map<String, Object> logData = getAuditLogObject(input); |
|
|
|
oldValueObject = logData; |
|
|
|
} |
|
|
|
// =====GET OLD AUDIT LOG=====// |
|
|
|
instance = save(instance); |
|
|
|
// =====GET NEW AUDIT LOG=====// |
|
|
|
if (instance != null && instance.getId() != null |
|
|
|
&& instance.getId() > 0) { |
|
|
|
Map<String, Object> input = Map.of("id", instance.getId()); |
|
|
|
Map<String, Object> logData = getAuditLogObject(input); |
|
|
|
logData.put("noted", this.getTodoRead(input)); |
|
|
|
newValueObject = logData; |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
auditLogService.save( |
|
|
|
tableName, |
|
|
|
instance.getId(), |
|
|
|
instance.getTitle(), |
|
|
|
SecurityUtils.getUser().isPresent() |
|
|
|
? SecurityUtils.getUser().get() |
|
|
|
.getId() |
|
|
|
: null, |
|
|
|
new Date(), |
|
|
|
JsonUtils.toJsonString( |
|
|
|
auditLogService.compareMaps( |
|
|
|
newValueObject, |
|
|
|
oldValueObject)), |
|
|
|
JsonUtils.toJsonString( |
|
|
|
auditLogService.compareMaps( |
|
|
|
oldValueObject, |
|
|
|
newValueObject))); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.info("Saving Audit Log Exception:"); |
|
|
|
logger.info(ExceptionUtils.getStackTrace(e)); |
|
|
|
} |
|
|
|
// =====GET NEW AUDIT LOG=====//\ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void generateNewEventApplicationReminder(Event event, LocalDate firstIssueDate) { |
|
|
|
logger.info("=> Checking New Event Application Reminder"); |
|
|
|
|
|
|
|
LocalDate now = LocalDate.now(); |
|
|
|
Integer limit = event.getReminderLimit().intValue(); |
|
|
|
Integer interval = event.getReminderInterval().intValue(); |
|
|
|
|
|
|
|
List<LocalDate> reminderDateSchedule = new ArrayList<LocalDate>(); |
|
|
|
for (Integer i = 0; i < limit; i++) { |
|
|
|
reminderDateSchedule.add(firstIssueDate.plusDays(i * interval)); |
|
|
|
} |
|
|
|
|
|
|
|
logger.info("Reminder Dates: " + reminderDateSchedule.stream() |
|
|
|
.map(date -> date.format(dateFormat)).collect(Collectors.joining(", "))); |
|
|
|
|
|
|
|
logger.info("Application Deadline: " + event.getApplicationDeadline().format(dateFormat)); |
|
|
|
List<Map<String, Object>> eventSubDivision = eventService |
|
|
|
.getEventDivisionAllData(Map.of("id", event.getId())); |
|
|
|
for (Map<String, Object> subDivision : eventSubDivision) { |
|
|
|
Integer eventSubDivisionId = (Integer) subDivision.get("id"); |
|
|
|
Integer subDivisionId = (Integer) subDivision.get("subDivisionId"); |
|
|
|
Integer reminderCount = (Integer) subDivision.get("newReminderCount"); |
|
|
|
Boolean isSuppressed = (Boolean) subDivision.get("suppressedOldReminder"); |
|
|
|
if (!isSuppressed) { |
|
|
|
if (reminderCount < reminderDateSchedule.size()) { |
|
|
|
LocalDate date = reminderDateSchedule.get(reminderCount); |
|
|
|
|
|
|
|
if (date.compareTo(now) <= 0 |
|
|
|
&& now.compareTo(event.getApplicationDeadline()) <= 0) { |
|
|
|
logger.info("*** Reminder Date Matched ***"); |
|
|
|
|
|
|
|
// update event_sub_division reminder count |
|
|
|
jdbcDao.executeUpdate( |
|
|
|
"UPDATE event_sub_division esd" |
|
|
|
+ " SET esd.newReminderCount = esd.newReminderCount + 1" |
|
|
|
+ " WHERE id = :eventSubDivisionId", |
|
|
|
Map.of("eventSubDivisionId", eventSubDivisionId)); |
|
|
|
|
|
|
|
// Send email reminder |
|
|
|
List<User> userList = userService.listUserBySubDivision(subDivisionId); |
|
|
|
for (User user : userList) { |
|
|
|
logger.info("Send Email to user: " + user.getUsername() + " (" |
|
|
|
+ user.getEmail() |
|
|
|
+ ")"); |
|
|
|
sendApplicationEmail(event.getId(), subDivisionId.longValue(), |
|
|
|
user.getId(), |
|
|
|
event, user, |
|
|
|
false); |
|
|
|
} |
|
|
|
|
|
|
|
// Update system reminder |
|
|
|
String message = "Please note below event will be opened for application:\\n\\n" |
|
|
|
+ |
|
|
|
" - Event Name: \\t\\t\\t " + event.getName() + "\\n" + |
|
|
|
" - Application Deadline: \\t " |
|
|
|
+ event.getApplicationDeadline(); |
|
|
|
|
|
|
|
TodoReminder instance = new TodoReminder(); |
|
|
|
instance.setEventId(event.getId()); |
|
|
|
instance.setSubDivisionId(subDivisionId.longValue()); |
|
|
|
instance.setTitle( |
|
|
|
"Application Reminder (New Event)"); |
|
|
|
instance.setMessage(message); |
|
|
|
instance.setReminderType("newEventApplication"); |
|
|
|
|
|
|
|
// =====GET OLD AUDIT LOG=====// |
|
|
|
String tableName = instance.getClass().getAnnotation(Table.class) |
|
|
|
.name(); |
|
|
|
Map<String, Object> oldValueObject = new HashMap<String, Object>(); |
|
|
|
Map<String, Object> newValueObject = new HashMap<String, Object>(); |
|
|
|
|
|
|
|
if (instance != null && instance.getId() != null |
|
|
|
&& instance.getId() > 0) { |
|
|
|
Map<String, Object> input = Map.of("id", instance.getId()); |
|
|
|
Map<String, Object> logData = getAuditLogObject(input); |
|
|
|
oldValueObject = logData; |
|
|
|
} |
|
|
|
// =====GET OLD AUDIT LOG=====// |
|
|
|
instance = save(instance); |
|
|
|
// =====GET NEW AUDIT LOG=====// |
|
|
|
if (instance != null && instance.getId() != null |
|
|
|
&& instance.getId() > 0) { |
|
|
|
Map<String, Object> input = Map.of("id", instance.getId()); |
|
|
|
Map<String, Object> logData = getAuditLogObject(input); |
|
|
|
logData.put("noted", this.getTodoRead(input)); |
|
|
|
newValueObject = logData; |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
auditLogService.save( |
|
|
|
tableName, |
|
|
|
instance.getId(), |
|
|
|
instance.getTitle(), |
|
|
|
SecurityUtils.getUser().isPresent() |
|
|
|
? SecurityUtils.getUser().get() |
|
|
|
.getId() |
|
|
|
: null, |
|
|
|
new Date(), |
|
|
|
JsonUtils.toJsonString( |
|
|
|
auditLogService.compareMaps( |
|
|
|
newValueObject, |
|
|
|
oldValueObject)), |
|
|
|
JsonUtils.toJsonString( |
|
|
|
auditLogService.compareMaps( |
|
|
|
oldValueObject, |
|
|
|
newValueObject))); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.info("Saving Audit Log Exception:"); |
|
|
|
logger.info(ExceptionUtils.getStackTrace(e)); |
|
|
|
} |
|
|
|
// =====GET NEW AUDIT LOG=====//\ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Scheduled(cron = "0 30 6 * * ?") // Runs at 06:30 AM every day |
|
|
|
public void generateApplicationAnnouncement() throws UnsupportedEncodingException, ParseException { |
|
|
|
logger.info("============= Generate Announcement Reminder Start ============="); |
|
|
|
LocalDateTime range = LocalDateTime.now().minusYears(reminderEventWithin); |
|
|
|
|
|
|
|
logger.info("Fetch Event From: " + range.toString()); |
|
|
|
|
|
|
|
List<Event> eventList = eventService.getEventByWithinYear(Map.of( |
|
|
|
"yearRange", range, "announcementDate", LocalDate.now())); |
|
|
|
|
|
|
|
logger.info("Fetched Total Number of Event: " + eventList.size()); |
|
|
|
|
|
|
|
for (Event event : eventList) { |
|
|
|
logger.info("---------------------------------------------"); |
|
|
|
logger.info("Event's award to be announced: " + event.getName()); |
|
|
|
List<Application> applications = applicationService |
|
|
|
.getApplicationByEventId(Map.of("eventId", event.getId())); |
|
|
|
String applicationString = ""; |
|
|
|
for (Application application : applications) { |
|
|
|
// get application name list |
|
|
|
logger.info("Send Email for Application: " + application.getName()); |
|
|
|
if (applicationString.length() > 0) { |
|
|
|
applicationString = applicationString + |
|
|
|
"\\t\\t\\t\\t\\t\\t" + |
|
|
|
application.getName() + "\\n"; |
|
|
|
} else { |
|
|
|
applicationString = application.getName() + "\\n"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for (Application application : applications) { |
|
|
|
// send email in application level |
|
|
|
List<Integer> appSubDivisionIdList = applicationService |
|
|
|
.getApplicationSubDivision(Map.of("id", application.getId())); |
|
|
|
for (Integer subDivisionId : appSubDivisionIdList) { |
|
|
|
List<User> userList = userService.listUserBySubDivision(subDivisionId); |
|
|
|
for (User user : userList) { |
|
|
|
logger.info("Send Email to user: " + user.getUsername() + " (" |
|
|
|
+ user.getEmail() |
|
|
|
+ ")"); |
|
|
|
sendAnnouncementEmail(event.getId(), subDivisionId.longValue(), |
|
|
|
user.getId(), event, user); |
|
|
|
} |
|
|
|
String message = //"Result announcement details:\\n\\n" + |
|
|
|
//" - Event Title: \\t\\t\\t" + event.getName() + "\\n" + |
|
|
|
//" - Application : \\t\\t\\t" + applicationString + "\\n" + |
|
|
|
" Announcement Date: \\t" + event.getAnnouncementDate(); |
|
|
|
|
|
|
|
TodoReminder instance = new TodoReminder(); |
|
|
|
instance.setEventId(event.getId()); |
|
|
|
instance.setSubDivisionId(subDivisionId.longValue()); |
|
|
|
instance.setTitle("Result Announcement for " + event.getName()); |
|
|
|
instance.setMessage(message); |
|
|
|
instance.setReminderType("announcement"); |
|
|
|
// =====GET OLD AUDIT LOG=====// |
|
|
|
String tableName = instance.getClass().getAnnotation(Table.class) |
|
|
|
.name(); |
|
|
|
Map<String, Object> oldValueObject = new HashMap<String, Object>(); |
|
|
|
Map<String, Object> newValueObject = new HashMap<String, Object>(); |
|
|
|
|
|
|
|
if (instance != null && instance.getId() != null |
|
|
|
&& instance.getId() > 0) { |
|
|
|
Map<String, Object> input = Map.of("id", instance.getId()); |
|
|
|
Map<String, Object> logData = getAuditLogObject(input); |
|
|
|
oldValueObject = logData; |
|
|
|
} |
|
|
|
// =====GET OLD AUDIT LOG=====// |
|
|
|
instance = save(instance); |
|
|
|
// =====GET NEW AUDIT LOG=====// |
|
|
|
if (instance != null && instance.getId() != null |
|
|
|
&& instance.getId() > 0) { |
|
|
|
Map<String, Object> input = Map.of("id", instance.getId()); |
|
|
|
Map<String, Object> logData = getAuditLogObject(input); |
|
|
|
logData.put("noted", this.getTodoRead(input)); |
|
|
|
newValueObject = logData; |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
auditLogService.save( |
|
|
|
tableName, |
|
|
|
instance.getId(), |
|
|
|
instance.getTitle(), |
|
|
|
SecurityUtils.getUser().isPresent() |
|
|
|
? SecurityUtils.getUser().get().getId() |
|
|
|
: null, |
|
|
|
new Date(), |
|
|
|
JsonUtils.toJsonString(auditLogService.compareMaps( |
|
|
|
newValueObject, oldValueObject)), |
|
|
|
JsonUtils.toJsonString(auditLogService.compareMaps( |
|
|
|
oldValueObject, newValueObject))); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.info("Saving Audit Log Exception:"); |
|
|
|
logger.info(ExceptionUtils.getStackTrace(e)); |
|
|
|
} |
|
|
|
// =====GET NEW AUDIT LOG=====// |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
logger.info("============= Generate Announcement Reminder End ============="); |
|
|
|
} |
|
|
|
|
|
|
|
public Map<String, Object> getAuditLogObject(Map<String, Object> req) { |
|
|
|
StringBuilder sql = new StringBuilder("SELECT" |
|
|
|
+ " tr.id, " |
|
|
|
+ " tr.created, " |
|
|
|
+ " tr.createdBy AS createdById, " |
|
|
|
+ " u1.name AS createdBy, " |
|
|
|
+ " tr.version, " |
|
|
|
+ " tr.modified, " |
|
|
|
+ " tr.modifiedBy AS modifiedById, " |
|
|
|
+ " tr.deleted, " |
|
|
|
+ " u2.name AS modifiedBy, " |
|
|
|
+ " e.name, " |
|
|
|
+ " sd.name, " |
|
|
|
+ " tr.reminderType, " |
|
|
|
+ " tr.title, " |
|
|
|
+ " tr.message, " |
|
|
|
+ " tr.suppressedBy, " |
|
|
|
+ " tr.suppressedDate " |
|
|
|
+ " FROM todo_reminder tr " |
|
|
|
+ " LEFT JOIN `user` u1 ON u1.id = tr.createdBy " |
|
|
|
+ " LEFT JOIN `user` u2 ON u2.id = tr.modifiedBy " |
|
|
|
+ " JOIN event e ON e.id = tr.eventId " |
|
|
|
+ " JOIN sub_division sd ON sd.id = tr.subDivisionId " |
|
|
|
+ " WHERE tr.id = :id "); |
|
|
|
|
|
|
|
return jdbcDao.queryForMap(sql.toString(), req).get(); |
|
|
|
} |
|
|
|
|
|
|
|
public void sendTestEmail(Map<String, Object> req) |
|
|
|
throws UnsupportedEncodingException, ParseException { |
|
|
|
|
|
|
|
String receiver = (String) req.get("receiver"); |
|
|
|
String templateName = (String) req.get("templateName"); |
|
|
|
Locale locale = Locale.ENGLISH; |
|
|
|
Boolean isAnnouncement = templateName.contains("announcement"); |
|
|
|
|
|
|
|
logger.info(templateName); |
|
|
|
String templateContent = settingsService.getString(templateName); |
|
|
|
Long tempId = (long) -1; |
|
|
|
Map<String, Object> dummyAnnouncementData = Map.of( |
|
|
|
"subject", "Reminder: Upcoming Event for Enrolment", |
|
|
|
"receiver", "RECEIVER", |
|
|
|
"eventName", "EVENT NAME", |
|
|
|
"applicationDetail", "APPLICATION DETAIL", |
|
|
|
"announcementDate", "ANNOUNCEMENT DATE", |
|
|
|
"subDivision", "SUBDIVISION", |
|
|
|
"division", "DIVISION", |
|
|
|
"sender", "SENDER", |
|
|
|
"arsLink", url + "/award"); |
|
|
|
|
|
|
|
Map<String, Object> dummyApplicationData = Map.of( |
|
|
|
"subject", "Result Announcement for EVENT NAME", |
|
|
|
"receiver", "RECEIVER", |
|
|
|
"eventName", "EVENT NAME", |
|
|
|
"applicationDeadLine", "APPLICATION DEAD LINE", |
|
|
|
"subDivision", "SUBDIVISION", |
|
|
|
"division", "DIVISION", |
|
|
|
"sender", "SENDER", |
|
|
|
"arsLink", url + "/application"); |
|
|
|
|
|
|
|
MailRequest temp = MailRequest.builder() |
|
|
|
.subject(isAnnouncement ? dummyAnnouncementData.get("subject").toString() |
|
|
|
: dummyApplicationData.get("subject").toString()) |
|
|
|
// .template("mail/applicationAnnouncement") |
|
|
|
.templateContent(templateContent) |
|
|
|
.args(isAnnouncement ? dummyAnnouncementData : dummyApplicationData) |
|
|
|
.addTo(new InternetAddress( |
|
|
|
// "[email protected]", "Mable Ng")) |
|
|
|
// user.getEmail(), user.getFullname())) |
|
|
|
receiver, "RECEIVER")) |
|
|
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
List<MailRequest> temp2 = new ArrayList<MailRequest>(); |
|
|
|
temp2.add(temp); |
|
|
|
mailService.sendARS(temp2, locale, tempId, tempId, tempId, |
|
|
|
isAnnouncement ? "announcement" : "application"); |
|
|
|
} |
|
|
|
|
|
|
|
public void sendApplicationEmail(Long eventId, Long subDivisionId, Long userId, Event event, User user, |
|
|
|
Boolean isOldEvent) { |
|
|
|
try { |
|
|
|
Locale locale = Locale.ENGLISH; |
|
|
|
SubDivision subDivision = subDivisionService.find(subDivisionId).get(); |
|
|
|
Division division = divisionService.find(subDivision.getDivisionId()).get(); |
|
|
|
String templateContent = isOldEvent |
|
|
|
? settingsService.getString(SettingNames.SYS_EMAIL_TEMPLATE_REMINDER_OLDEVENT) |
|
|
|
: settingsService.getString(SettingNames.SYS_EMAIL_TEMPLATE_REMINDER_NEWEVENT); |
|
|
|
|
|
|
|
MailRequest temp = MailRequest.builder() |
|
|
|
.subject("Reminder: Upcoming Event for Enrolment") |
|
|
|
// .template("mail/applicationReminder") |
|
|
|
.templateContent(templateContent) |
|
|
|
.args(Map.of( |
|
|
|
"receiver", user.getFullname(), |
|
|
|
"eventName", event.getName(), |
|
|
|
"applicationDeadLine", |
|
|
|
isOldEvent ? event.getNextApplicationDate() |
|
|
|
: event.getApplicationDeadline(), |
|
|
|
"subDivision", subDivision.getName(), |
|
|
|
"division", division.getName(), |
|
|
|
"sender", "Electrical and Mechanical Services Department", |
|
|
|
"arsLink", url + "/event/maintain/" + event.getId())) |
|
|
|
.addTo(new InternetAddress( |
|
|
|
// "[email protected]", "Mabel Ng")) |
|
|
|
user.getEmail(), user.getFullname())) |
|
|
|
// "[email protected]", "Jason Lam")) |
|
|
|
|
|
|
|
.build(); |
|
|
|
List<MailRequest> temp2 = new ArrayList<MailRequest>(); |
|
|
|
temp2.add(temp); |
|
|
|
mailService.sendARS(temp2, locale, eventId, subDivisionId, userId, "application"); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.info(ExceptionUtils.getStackTrace(e)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void sendAnnouncementEmail(Long eventId, Long subDivisionId, Long userId, Event event, User user) { |
|
|
|
|
|
|
|
try { |
|
|
|
Locale locale = Locale.ENGLISH; |
|
|
|
List<Application> applications = applicationService |
|
|
|
.getApplicationByEventId(Map.of("eventId", event.getId())); |
|
|
|
SubDivision subDivision = subDivisionService.find(subDivisionId).get(); |
|
|
|
Division division = divisionService.find(subDivision.getDivisionId()).get(); |
|
|
|
String templateContent = settingsService |
|
|
|
.getString(SettingNames.SYS_EMAIL_TEMPLATE_ANNOUNCEMENT); |
|
|
|
|
|
|
|
String applicationString = ""; |
|
|
|
for (Application application : applications) { |
|
|
|
applicationString = applicationString + "- " + application.getName() + "<br>"; |
|
|
|
} |
|
|
|
|
|
|
|
MailRequest temp = MailRequest.builder() |
|
|
|
.subject("Reminder: Result Announcement for " + event.getName()) |
|
|
|
// .template("mail/applicationAnnouncement") |
|
|
|
.templateContent(templateContent) |
|
|
|
.args(Map.of( |
|
|
|
"receiver", user.getFullname(), |
|
|
|
"eventName", event.getName(), |
|
|
|
"applicationDetail", applicationString, |
|
|
|
"announcementDate", event.getAnnouncementDate(), |
|
|
|
"subDivision", subDivision.getName(), |
|
|
|
"division", division.getName(), |
|
|
|
"sender", "Electrical and Mechanical Services Department", |
|
|
|
"arsLink", url + "/event/maintain/" + event.getId())) |
|
|
|
.addTo(new InternetAddress( |
|
|
|
// "[email protected]", "Mable Ng")) |
|
|
|
user.getEmail(), user.getFullname())) |
|
|
|
// "[email protected]", "Jason Lam")) |
|
|
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
List<MailRequest> temp2 = new ArrayList<MailRequest>(); |
|
|
|
temp2.add(temp); |
|
|
|
mailService.sendARS(temp2, locale, eventId, subDivisionId, userId, "announcement"); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.info(ExceptionUtils.getStackTrace(e)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public List<Map<String, Object>> list(Map<String, Object> args) { |
|
|
|
StringBuilder sql = new StringBuilder("SELECT" |
|
|
|
+ " DISTINCT tr.id, " |
|
|
|
+ " tr.eventId, " |
|
|
|
+ " tr.subDivisionId, " |
|
|
|
+ " tr.reminderType, " |
|
|
|
+ " tr.title, " |
|
|
|
+ " tr.message, " |
|
|
|
+ " tr.suppressedBy, " |
|
|
|
+ " trn.userId " |
|
|
|
+ " FROM `user` u " |
|
|
|
+ " LEFT JOIN sub_division sd ON u.subDivisionId = sd.id " |
|
|
|
// + " LEFT JOIN division d ON sd.divisionId = d.id " |
|
|
|
// + " LEFT JOIN sub_division sd2 ON sd2.divisionId = d.id " |
|
|
|
+ " LEFT JOIN todo_reminder tr ON tr.subDivisionId = sd.id " |
|
|
|
+ " LEFT JOIN todo_reminder_noted trn ON trn.todoReminderId = tr.id " |
|
|
|
+ " WHERE tr.deleted = FALSE " |
|
|
|
+ " AND u.id = :userId " |
|
|
|
); |
|
|
|
if (args != null) { |
|
|
|
if (args.containsKey("reminderType")) |
|
|
|
sql.append(" AND tr.reminderType= :reminderType "); |
|
|
|
else |
|
|
|
sql.append(" AND tr.reminderType != \"announcement\" "); |
|
|
|
} |
|
|
|
|
|
|
|
sql.append( |
|
|
|
" AND IF(:read, trn.userId IS NOT NULL AND trn.userId = :userId , trn.userId IS NULL) "+ |
|
|
|
" ORDER BY tr.id DESC" |
|
|
|
); |
|
|
|
|
|
|
|
return jdbcDao.queryForList(sql.toString(), args); |
|
|
|
} |
|
|
|
|
|
|
|
public Integer userRead(Long id, Long userId) { |
|
|
|
TodoReminder instance = this.find(id).get(); |
|
|
|
|
|
|
|
Map<String, Object> value = Map.of("todoReminderId", id, "userId", userId, "notedDate", |
|
|
|
LocalDateTime.now()); |
|
|
|
// =====GET OLD AUDIT LOG=====// |
|
|
|
String tableName = instance.getClass().getAnnotation(Table.class).name(); |
|
|
|
StringBuilder sql = new StringBuilder("SELECT * FROM " + tableName + " WHERE id = :id"); |
|
|
|
Map<String, Object> oldValueObject = new HashMap<String, Object>(); |
|
|
|
Map<String, Object> newValueObject = new HashMap<String, Object>(); |
|
|
|
|
|
|
|
if (instance != null && instance.getId() != null && instance.getId() > 0) { |
|
|
|
Map<String, Object> input = Map.of("id", instance.getId()); |
|
|
|
Map<String, Object> logData = getAuditLogObject(input); |
|
|
|
oldValueObject = logData; |
|
|
|
} |
|
|
|
// =====GET OLD AUDIT LOG=====// |
|
|
|
jdbcDao.executeUpdate( |
|
|
|
"INSERT IGNORE INTO todo_reminder_noted (todoReminderId, userId, notedDate)" |
|
|
|
+ " VALUE (:todoReminderId, :userId, :notedDate)", |
|
|
|
value); |
|
|
|
// =====GET NEW AUDIT LOG=====// |
|
|
|
if (instance != null && instance.getId() != null && instance.getId() > 0) { |
|
|
|
Map<String, Object> input = Map.of("id", instance.getId()); |
|
|
|
Map<String, Object> logData = getAuditLogObject(input); |
|
|
|
logData.put("noted", this.getTodoRead(input).get("notedDate")); |
|
|
|
newValueObject = logData; |
|
|
|
} |
|
|
|
auditLogService.save( |
|
|
|
tableName, |
|
|
|
instance.getId(), |
|
|
|
instance.getTitle(), |
|
|
|
SecurityUtils.getUser() != null ? SecurityUtils.getUser().get().getId() : null, |
|
|
|
new Date(), |
|
|
|
JsonUtils.toJsonString(auditLogService.compareMaps(newValueObject, oldValueObject)), |
|
|
|
JsonUtils.toJsonString(auditLogService.compareMaps(oldValueObject, newValueObject))); |
|
|
|
// =====GET NEW AUDIT LOG=====// |
|
|
|
|
|
|
|
return 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public Map<String, Object> getTodoRead(Map<String, Object> req) { |
|
|
|
StringBuilder sql = new StringBuilder("SELECT" |
|
|
|
+ " trn.notedDate " |
|
|
|
+ " FROM todo_reminder_noted trn " |
|
|
|
+ " WHERE trn.todoReminderId =:id "); |
|
|
|
|
|
|
|
return jdbcDao.queryForMap(sql.toString(), req).orElse(null); |
|
|
|
} |
|
|
|
|
|
|
|
public List<TodoReminderEmailLog> getFailEmailRecord(Map<String, Object> req) { |
|
|
|
StringBuilder sql = new StringBuilder("SELECT" |
|
|
|
+ " * " |
|
|
|
+ " FROM todo_reminder_email_log trel " |
|
|
|
+ " WHERE trel.resendSuccess=0 " |
|
|
|
+ " AND trel.success=0 "); |
|
|
|
|
|
|
|
return jdbcDao.queryForList(sql.toString(), req, TodoReminderEmailLog.class); |
|
|
|
} |
|
|
|
|
|
|
|
public Long suppressTodo(Long id, Long userId) { |
|
|
|
TodoReminder instance = find(id).get(); |
|
|
|
instance.setSuppressedBy(userId); |
|
|
|
instance.setSuppressedDate(LocalDateTime.now()); |
|
|
|
|
|
|
|
// =====GET OLD AUDIT LOG=====// |
|
|
|
String tableName = instance.getClass().getAnnotation(Table.class).name(); |
|
|
|
StringBuilder sql = new StringBuilder("SELECT * FROM " + tableName + " WHERE id = :id"); |
|
|
|
Map<String, Object> oldValueObject = new HashMap<String, Object>(); |
|
|
|
Map<String, Object> newValueObject = new HashMap<String, Object>(); |
|
|
|
|
|
|
|
if (instance != null && instance.getId() != null && instance.getId() > 0) { |
|
|
|
Map<String, Object> input = Map.of("id", instance.getId()); |
|
|
|
Map<String, Object> logData = getAuditLogObject(input); |
|
|
|
oldValueObject = logData; |
|
|
|
} |
|
|
|
// =====GET OLD AUDIT LOG=====// |
|
|
|
instance = save(instance); |
|
|
|
|
|
|
|
Map<String, Object> value = Map.of( |
|
|
|
"eventId", instance.getEventId(), |
|
|
|
"subDivisionId", instance.getSubDivisionId()); |
|
|
|
|
|
|
|
logger.info(instance.getReminderType()); |
|
|
|
if (instance.getReminderType().equals("newEventApplication")) { |
|
|
|
logger.info("here"); |
|
|
|
jdbcDao.executeUpdate( |
|
|
|
"UPDATE event_sub_division esd" |
|
|
|
+ " SET esd.suppressedNewReminder = TRUE" |
|
|
|
+ " WHERE eventId = :eventId " |
|
|
|
+ " AND subDivisionId = :subDivisionId", |
|
|
|
value); |
|
|
|
} else if (instance.getReminderType().equals("oldEventApplication")) { |
|
|
|
logger.info("here2"); |
|
|
|
jdbcDao.executeUpdate( |
|
|
|
"UPDATE event_sub_division esd" |
|
|
|
+ " SET esd.suppressedOldReminder = TRUE" |
|
|
|
+ " WHERE eventId = :eventId " |
|
|
|
+ " AND subDivisionId = :subDivisionId", |
|
|
|
value); |
|
|
|
} |
|
|
|
|
|
|
|
Map<String, Object> value2 = Map.of("todoReminderId", id, "userId", userId, "notedDate", |
|
|
|
LocalDateTime.now()); |
|
|
|
|
|
|
|
if (this.getTodoRead(Map.of("id", instance.getId())) == null) { |
|
|
|
jdbcDao.executeUpdate( |
|
|
|
"INSERT IGNORE INTO todo_reminder_noted (todoReminderId, userId, notedDate)" |
|
|
|
+ " VALUE (:todoReminderId, :userId, :notedDate)", |
|
|
|
value2); |
|
|
|
} |
|
|
|
|
|
|
|
// =====GET NEW AUDIT LOG=====// |
|
|
|
if (instance != null && instance.getId() != null && instance.getId() > 0) { |
|
|
|
Map<String, Object> input = Map.of("id", instance.getId()); |
|
|
|
Map<String, Object> logData = getAuditLogObject(input); |
|
|
|
logData.put("noted", this.getTodoRead(input).get("notedDate")); |
|
|
|
newValueObject = logData; |
|
|
|
} |
|
|
|
auditLogService.save( |
|
|
|
tableName, |
|
|
|
instance.getId(), |
|
|
|
instance.getTitle(), |
|
|
|
SecurityUtils.getUser() != null ? SecurityUtils.getUser().get().getId() : null, |
|
|
|
new Date(), |
|
|
|
JsonUtils.toJsonString(auditLogService.compareMaps(newValueObject, oldValueObject)), |
|
|
|
JsonUtils.toJsonString(auditLogService.compareMaps(oldValueObject, newValueObject))); |
|
|
|
// =====GET NEW AUDIT LOG=====// |
|
|
|
return instance.getId(); |
|
|
|
} |
|
|
|
|
|
|
|
} |