From c73031b572e2b6ace0c3f9abe26cbb57b027d1d1 Mon Sep 17 00:00:00 2001 From: lmann99 Date: Sun, 22 Jan 2017 10:00:56 -0500 Subject: [PATCH] Resolve NullPointerExceptions thrown by email notification scheduled jobs Added null checks. Moved getCurrentWorker to DAO as now being used by multiple routines. Fixes #6 --- .../business/resources/daos/IWorkerDAO.java | 3 +++ .../business/resources/daos/WorkerDAO.java | 16 ++++++++++- .../notifications/ComposeMessage.java | 19 +++---------- .../SendEmailOnMilestoneReached.java | 7 +++-- .../SendEmailOnTaskShouldFinish.java | 26 ++++++++++++------ .../SendEmailOnTaskShouldStart.java | 27 ++++++++++++++----- .../SendEmailOnTimesheetDataMissing.java | 13 +++++---- 7 files changed, 71 insertions(+), 40 deletions(-) diff --git a/libreplan-business/src/main/java/org/libreplan/business/resources/daos/IWorkerDAO.java b/libreplan-business/src/main/java/org/libreplan/business/resources/daos/IWorkerDAO.java index 913ed8702..65208663b 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/resources/daos/IWorkerDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/resources/daos/IWorkerDAO.java @@ -122,8 +122,11 @@ public interface IWorkerDAO extends IIntegrationEntityDAO { public List findByFirstNameSecondNameAnotherTransaction( String firstname, String secondname); + public Worker getCurrentWorker(Long resourceID); + /** * Return the list of {@link Worker Workers} bound to any {@link User}. */ List getBound(); + } diff --git a/libreplan-business/src/main/java/org/libreplan/business/resources/daos/WorkerDAO.java b/libreplan-business/src/main/java/org/libreplan/business/resources/daos/WorkerDAO.java index 341fb01e6..0162f94f2 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/resources/daos/WorkerDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/resources/daos/WorkerDAO.java @@ -198,4 +198,18 @@ public class WorkerDAO extends IntegrationEntityDAO criteria.add(Restrictions.isNotNull("user")); return criteria.list(); } -} \ No newline at end of file + + @Override + @Transactional(readOnly = true) + public Worker getCurrentWorker(Long resourceID) { + List workerList = getWorkers(); + + for (Worker worker : workerList) { + if (worker.getId().equals(resourceID)) { + return worker; + } + } + + return null; + } +} diff --git a/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/ComposeMessage.java b/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/ComposeMessage.java index c2fd7a015..31897184c 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/ComposeMessage.java +++ b/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/ComposeMessage.java @@ -27,6 +27,7 @@ import org.libreplan.business.common.entities.ConnectorProperty; import org.libreplan.business.email.entities.EmailNotification; import org.libreplan.business.email.entities.EmailTemplate; import org.libreplan.business.email.entities.EmailTemplateEnum; +import org.libreplan.business.resources.daos.IWorkerDAO; import org.libreplan.business.resources.entities.Resource; import org.libreplan.business.resources.entities.Worker; import org.libreplan.business.settings.entities.Language; @@ -102,7 +103,7 @@ import static org.libreplan.web.I18nHelper._; public class ComposeMessage { @Autowired - private IWorkerModel workerModel; + private IWorkerDAO workerDAO; @Autowired private IEmailTemplateModel emailTemplateModel; @@ -131,11 +132,11 @@ public class ComposeMessage { Resource resource = notification.getResource(); EmailTemplateEnum type = notification.getType(); Locale locale; - Worker currentWorker = getCurrentWorker(resource.getId()); + Worker currentWorker = workerDAO.getCurrentWorker(resource.getId()); UserRole currentUserRole = getCurrentUserRole(notification.getType()); - if (currentWorker != null && currentWorker.getUser().isInRole(currentUserRole)) { + if (currentWorker != null && (currentWorker.getUser() != null) && currentWorker.getUser().isInRole(currentUserRole)) { if (currentWorker.getUser().getApplicationLanguage().equals(Language.BROWSER_LANGUAGE)) { locale = new Locale(System.getProperty("user.language")); } else { @@ -197,18 +198,6 @@ public class ComposeMessage { return false; } - private Worker getCurrentWorker(Long resourceID) { - List workerList = workerModel.getWorkers(); - - for (Worker aWorkerList : workerList) { - if (aWorkerList.getId().equals(resourceID)) { - return aWorkerList; - } - } - - return null; - } - private EmailTemplate findCurrentEmailTemplate(EmailTemplateEnum templateEnum, Locale locale) { List emailTemplates; emailTemplates = emailTemplateModel.getAll(); diff --git a/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnMilestoneReached.java b/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnMilestoneReached.java index 39904a40b..54d024033 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnMilestoneReached.java +++ b/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnMilestoneReached.java @@ -112,10 +112,6 @@ public class SendEmailOnMilestoneReached implements IEmailNotificationJob { } private void sendEmailNotificationToManager(TaskElement item) { - emailNotificationModel.setNewObject(); - emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_MILESTONE_REACHED); - emailNotificationModel.setUpdated(new Date()); - String responsible = ""; if ( item.getParent().getOrderElement().getOrder().getResponsible() != null ) { responsible = item.getParent().getOrderElement().getOrder().getResponsible(); @@ -129,6 +125,9 @@ public class SendEmailOnMilestoneReached implements IEmailNotificationJob { user.isInRole(UserRole.ROLE_SUPERUSER) || user.isInRole(UserRole.ROLE_EMAIL_MILESTONE_REACHED); if ( user.getWorker() != null && userHasNeededRoles ) { + emailNotificationModel.setNewObject(); + emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_MILESTONE_REACHED); + emailNotificationModel.setUpdated(new Date()); emailNotificationModel.setResource(user.getWorker()); emailNotificationModel.setTask(item); emailNotificationModel.setProject(item.getParent()); diff --git a/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnTaskShouldFinish.java b/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnTaskShouldFinish.java index de57411a7..79f517488 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnTaskShouldFinish.java +++ b/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnTaskShouldFinish.java @@ -28,11 +28,15 @@ import org.libreplan.business.email.entities.EmailTemplateEnum; import org.libreplan.business.planner.daos.ITaskElementDAO; import org.libreplan.business.planner.entities.ResourceAllocation; import org.libreplan.business.planner.entities.TaskElement; +import org.libreplan.business.resources.daos.IWorkerDAO; import org.libreplan.business.resources.entities.Resource; +import org.libreplan.business.resources.entities.Worker; +import org.libreplan.business.users.entities.UserRole; import org.libreplan.importers.notifications.ComposeMessage; import org.libreplan.importers.notifications.EmailConnectionValidator; import org.libreplan.importers.notifications.IEmailNotificationJob; import org.libreplan.web.email.IEmailNotificationModel; +import org.libreplan.web.resources.worker.IWorkerModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Scope; @@ -68,6 +72,9 @@ public class SendEmailOnTaskShouldFinish implements IEmailNotificationJob { @Autowired private EmailConnectionValidator emailConnectionValidator; + @Autowired + private IWorkerDAO workerDAO; + /** * Transactional here is needed because without this annotation we are getting * "LazyInitializationException: could not initialize proxy - no Session" error, @@ -122,7 +129,6 @@ public class SendEmailOnTaskShouldFinish implements IEmailNotificationJob { } } - private void sendEmailNotificationAboutTaskShouldFinish(TaskElement item){ List> resourceAllocations = new ArrayList<>(item.getAllResourceAllocations()); @@ -131,13 +137,17 @@ public class SendEmailOnTaskShouldFinish implements IEmailNotificationJob { resources.add(allocation.getAssociatedResources().get(0)); for (Resource resourceItem : resources){ - emailNotificationModel.setNewObject(); - emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_TODAY_TASK_SHOULD_FINISH); - emailNotificationModel.setUpdated(new Date()); - emailNotificationModel.setResource(resourceItem); - emailNotificationModel.setTask(item); - emailNotificationModel.setProject(item.getParent()); - emailNotificationModel.confirmSave(); + Worker currentWorker = workerDAO.getCurrentWorker(resourceItem.getId()); + + if (currentWorker != null && (currentWorker.getUser() != null) && currentWorker.getUser().isInRole(UserRole.ROLE_EMAIL_TASK_SHOULD_FINISH)) { + emailNotificationModel.setNewObject(); + emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_TODAY_TASK_SHOULD_FINISH); + emailNotificationModel.setUpdated(new Date()); + emailNotificationModel.setResource(resourceItem); + emailNotificationModel.setTask(item); + emailNotificationModel.setProject(item.getParent()); + emailNotificationModel.confirmSave(); + } } } diff --git a/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnTaskShouldStart.java b/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnTaskShouldStart.java index 075007d41..169374500 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnTaskShouldStart.java +++ b/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnTaskShouldStart.java @@ -28,16 +28,22 @@ import org.libreplan.business.email.entities.EmailTemplateEnum; import org.libreplan.business.planner.daos.ITaskElementDAO; import org.libreplan.business.planner.entities.ResourceAllocation; import org.libreplan.business.planner.entities.TaskElement; +import org.libreplan.business.resources.daos.IWorkerDAO; import org.libreplan.business.resources.entities.Resource; +import org.libreplan.business.resources.entities.Worker; +import org.libreplan.business.users.entities.User; +import org.libreplan.business.users.entities.UserRole; import org.libreplan.importers.notifications.ComposeMessage; import org.libreplan.importers.notifications.EmailConnectionValidator; import org.libreplan.importers.notifications.IEmailNotificationJob; import org.libreplan.web.email.IEmailNotificationModel; +import org.libreplan.web.resources.worker.IWorkerModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; +import org.zkoss.zkplus.spring.SpringUtil; import java.util.ArrayList; import java.util.Date; @@ -68,6 +74,9 @@ public class SendEmailOnTaskShouldStart implements IEmailNotificationJob { @Autowired private EmailConnectionValidator emailConnectionValidator; + @Autowired + private IWorkerDAO workerDAO; + /** * Transactional here is needed because without this annotation we are getting * "LazyInitializationException: could not initialize proxy - no Session" error, @@ -130,13 +139,17 @@ public class SendEmailOnTaskShouldStart implements IEmailNotificationJob { resources.add(allocation.getAssociatedResources().get(0)); for (Resource resourceItem : resources) { - emailNotificationModel.setNewObject(); - emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_TODAY_TASK_SHOULD_START); - emailNotificationModel.setUpdated(new Date()); - emailNotificationModel.setResource(resourceItem); - emailNotificationModel.setTask(item); - emailNotificationModel.setProject(item.getParent()); - emailNotificationModel.confirmSave(); + Worker currentWorker = workerDAO.getCurrentWorker(resourceItem.getId()); + + if (currentWorker != null && (currentWorker.getUser() != null) && currentWorker.getUser().isInRole(UserRole.ROLE_EMAIL_TASK_SHOULD_START)) { + emailNotificationModel.setNewObject(); + emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_TODAY_TASK_SHOULD_START); + emailNotificationModel.setUpdated(new Date()); + emailNotificationModel.setResource(resourceItem); + emailNotificationModel.setTask(item); + emailNotificationModel.setProject(item.getParent()); + emailNotificationModel.confirmSave(); + } } } diff --git a/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnTimesheetDataMissing.java b/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnTimesheetDataMissing.java index f2cfa0e44..09d469dca 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnTimesheetDataMissing.java +++ b/libreplan-webapp/src/main/java/org/libreplan/importers/notifications/realization/SendEmailOnTimesheetDataMissing.java @@ -33,6 +33,7 @@ import org.libreplan.business.resources.entities.Resource; import org.libreplan.business.resources.entities.Worker; import org.libreplan.business.users.entities.User; +import org.libreplan.business.users.entities.UserRole; import org.libreplan.business.workingday.EffortDuration; import org.libreplan.business.workingday.IntraDayDate; import org.libreplan.business.workreports.daos.IWorkReportDAO; @@ -159,11 +160,13 @@ public class SendEmailOnTimesheetDataMissing implements IEmailNotificationJob { private void addRowsToNotificationTable(List users){ for (User user : users){ - emailNotificationModel.setNewObject(); - emailNotificationModel.setResource(user.getWorker()); - emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_ENTER_DATA_IN_TIMESHEET); - emailNotificationModel.setUpdated(new Date()); - emailNotificationModel.confirmSave(); + if ( user.isInRole(UserRole.ROLE_EMAIL_TIMESHEET_DATA_MISSING) ) { + emailNotificationModel.setNewObject(); + emailNotificationModel.setResource(user.getWorker()); + emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_ENTER_DATA_IN_TIMESHEET); + emailNotificationModel.setUpdated(new Date()); + emailNotificationModel.confirmSave(); + } } }