Merge pull request #7 from lmann99/exclude-non-bound-resources-from-notifications

Resolve NullPointerExceptions thrown by email notification scheduled …
This commit is contained in:
lmann99 2017-01-22 10:04:10 -05:00 committed by GitHub
commit f95977c986
7 changed files with 71 additions and 40 deletions

View file

@ -122,8 +122,11 @@ public interface IWorkerDAO extends IIntegrationEntityDAO<Worker> {
public List<Worker> findByFirstNameSecondNameAnotherTransaction( public List<Worker> findByFirstNameSecondNameAnotherTransaction(
String firstname, String secondname); String firstname, String secondname);
public Worker getCurrentWorker(Long resourceID);
/** /**
* Return the list of {@link Worker Workers} bound to any {@link User}. * Return the list of {@link Worker Workers} bound to any {@link User}.
*/ */
List<Worker> getBound(); List<Worker> getBound();
} }

View file

@ -198,4 +198,18 @@ public class WorkerDAO extends IntegrationEntityDAO<Worker>
criteria.add(Restrictions.isNotNull("user")); criteria.add(Restrictions.isNotNull("user"));
return criteria.list(); return criteria.list();
} }
}
@Override
@Transactional(readOnly = true)
public Worker getCurrentWorker(Long resourceID) {
List<Worker> workerList = getWorkers();
for (Worker worker : workerList) {
if (worker.getId().equals(resourceID)) {
return worker;
}
}
return null;
}
}

View file

@ -27,6 +27,7 @@ import org.libreplan.business.common.entities.ConnectorProperty;
import org.libreplan.business.email.entities.EmailNotification; import org.libreplan.business.email.entities.EmailNotification;
import org.libreplan.business.email.entities.EmailTemplate; import org.libreplan.business.email.entities.EmailTemplate;
import org.libreplan.business.email.entities.EmailTemplateEnum; 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.Resource;
import org.libreplan.business.resources.entities.Worker; import org.libreplan.business.resources.entities.Worker;
import org.libreplan.business.settings.entities.Language; import org.libreplan.business.settings.entities.Language;
@ -102,7 +103,7 @@ import static org.libreplan.web.I18nHelper._;
public class ComposeMessage { public class ComposeMessage {
@Autowired @Autowired
private IWorkerModel workerModel; private IWorkerDAO workerDAO;
@Autowired @Autowired
private IEmailTemplateModel emailTemplateModel; private IEmailTemplateModel emailTemplateModel;
@ -131,11 +132,11 @@ public class ComposeMessage {
Resource resource = notification.getResource(); Resource resource = notification.getResource();
EmailTemplateEnum type = notification.getType(); EmailTemplateEnum type = notification.getType();
Locale locale; Locale locale;
Worker currentWorker = getCurrentWorker(resource.getId()); Worker currentWorker = workerDAO.getCurrentWorker(resource.getId());
UserRole currentUserRole = getCurrentUserRole(notification.getType()); 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)) { if (currentWorker.getUser().getApplicationLanguage().equals(Language.BROWSER_LANGUAGE)) {
locale = new Locale(System.getProperty("user.language")); locale = new Locale(System.getProperty("user.language"));
} else { } else {
@ -197,18 +198,6 @@ public class ComposeMessage {
return false; return false;
} }
private Worker getCurrentWorker(Long resourceID) {
List<Worker> workerList = workerModel.getWorkers();
for (Worker aWorkerList : workerList) {
if (aWorkerList.getId().equals(resourceID)) {
return aWorkerList;
}
}
return null;
}
private EmailTemplate findCurrentEmailTemplate(EmailTemplateEnum templateEnum, Locale locale) { private EmailTemplate findCurrentEmailTemplate(EmailTemplateEnum templateEnum, Locale locale) {
List<EmailTemplate> emailTemplates; List<EmailTemplate> emailTemplates;
emailTemplates = emailTemplateModel.getAll(); emailTemplates = emailTemplateModel.getAll();

View file

@ -112,10 +112,6 @@ public class SendEmailOnMilestoneReached implements IEmailNotificationJob {
} }
private void sendEmailNotificationToManager(TaskElement item) { private void sendEmailNotificationToManager(TaskElement item) {
emailNotificationModel.setNewObject();
emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_MILESTONE_REACHED);
emailNotificationModel.setUpdated(new Date());
String responsible = ""; String responsible = "";
if ( item.getParent().getOrderElement().getOrder().getResponsible() != null ) { if ( item.getParent().getOrderElement().getOrder().getResponsible() != null ) {
responsible = item.getParent().getOrderElement().getOrder().getResponsible(); 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); user.isInRole(UserRole.ROLE_SUPERUSER) || user.isInRole(UserRole.ROLE_EMAIL_MILESTONE_REACHED);
if ( user.getWorker() != null && userHasNeededRoles ) { if ( user.getWorker() != null && userHasNeededRoles ) {
emailNotificationModel.setNewObject();
emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_MILESTONE_REACHED);
emailNotificationModel.setUpdated(new Date());
emailNotificationModel.setResource(user.getWorker()); emailNotificationModel.setResource(user.getWorker());
emailNotificationModel.setTask(item); emailNotificationModel.setTask(item);
emailNotificationModel.setProject(item.getParent()); emailNotificationModel.setProject(item.getParent());

View file

@ -28,11 +28,15 @@ import org.libreplan.business.email.entities.EmailTemplateEnum;
import org.libreplan.business.planner.daos.ITaskElementDAO; import org.libreplan.business.planner.daos.ITaskElementDAO;
import org.libreplan.business.planner.entities.ResourceAllocation; import org.libreplan.business.planner.entities.ResourceAllocation;
import org.libreplan.business.planner.entities.TaskElement; 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.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.ComposeMessage;
import org.libreplan.importers.notifications.EmailConnectionValidator; import org.libreplan.importers.notifications.EmailConnectionValidator;
import org.libreplan.importers.notifications.IEmailNotificationJob; import org.libreplan.importers.notifications.IEmailNotificationJob;
import org.libreplan.web.email.IEmailNotificationModel; 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.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
@ -68,6 +72,9 @@ public class SendEmailOnTaskShouldFinish implements IEmailNotificationJob {
@Autowired @Autowired
private EmailConnectionValidator emailConnectionValidator; private EmailConnectionValidator emailConnectionValidator;
@Autowired
private IWorkerDAO workerDAO;
/** /**
* Transactional here is needed because without this annotation we are getting * Transactional here is needed because without this annotation we are getting
* "LazyInitializationException: could not initialize proxy - no Session" error, * "LazyInitializationException: could not initialize proxy - no Session" error,
@ -122,7 +129,6 @@ public class SendEmailOnTaskShouldFinish implements IEmailNotificationJob {
} }
} }
private void sendEmailNotificationAboutTaskShouldFinish(TaskElement item){ private void sendEmailNotificationAboutTaskShouldFinish(TaskElement item){
List<ResourceAllocation<?>> resourceAllocations = new ArrayList<>(item.getAllResourceAllocations()); List<ResourceAllocation<?>> resourceAllocations = new ArrayList<>(item.getAllResourceAllocations());
@ -131,13 +137,17 @@ public class SendEmailOnTaskShouldFinish implements IEmailNotificationJob {
resources.add(allocation.getAssociatedResources().get(0)); resources.add(allocation.getAssociatedResources().get(0));
for (Resource resourceItem : resources){ for (Resource resourceItem : resources){
emailNotificationModel.setNewObject(); Worker currentWorker = workerDAO.getCurrentWorker(resourceItem.getId());
emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_TODAY_TASK_SHOULD_FINISH);
emailNotificationModel.setUpdated(new Date()); if (currentWorker != null && (currentWorker.getUser() != null) && currentWorker.getUser().isInRole(UserRole.ROLE_EMAIL_TASK_SHOULD_FINISH)) {
emailNotificationModel.setResource(resourceItem); emailNotificationModel.setNewObject();
emailNotificationModel.setTask(item); emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_TODAY_TASK_SHOULD_FINISH);
emailNotificationModel.setProject(item.getParent()); emailNotificationModel.setUpdated(new Date());
emailNotificationModel.confirmSave(); emailNotificationModel.setResource(resourceItem);
emailNotificationModel.setTask(item);
emailNotificationModel.setProject(item.getParent());
emailNotificationModel.confirmSave();
}
} }
} }

View file

@ -28,16 +28,22 @@ import org.libreplan.business.email.entities.EmailTemplateEnum;
import org.libreplan.business.planner.daos.ITaskElementDAO; import org.libreplan.business.planner.daos.ITaskElementDAO;
import org.libreplan.business.planner.entities.ResourceAllocation; import org.libreplan.business.planner.entities.ResourceAllocation;
import org.libreplan.business.planner.entities.TaskElement; 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.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.ComposeMessage;
import org.libreplan.importers.notifications.EmailConnectionValidator; import org.libreplan.importers.notifications.EmailConnectionValidator;
import org.libreplan.importers.notifications.IEmailNotificationJob; import org.libreplan.importers.notifications.IEmailNotificationJob;
import org.libreplan.web.email.IEmailNotificationModel; 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.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.zkoss.zkplus.spring.SpringUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@ -68,6 +74,9 @@ public class SendEmailOnTaskShouldStart implements IEmailNotificationJob {
@Autowired @Autowired
private EmailConnectionValidator emailConnectionValidator; private EmailConnectionValidator emailConnectionValidator;
@Autowired
private IWorkerDAO workerDAO;
/** /**
* Transactional here is needed because without this annotation we are getting * Transactional here is needed because without this annotation we are getting
* "LazyInitializationException: could not initialize proxy - no Session" error, * "LazyInitializationException: could not initialize proxy - no Session" error,
@ -130,13 +139,17 @@ public class SendEmailOnTaskShouldStart implements IEmailNotificationJob {
resources.add(allocation.getAssociatedResources().get(0)); resources.add(allocation.getAssociatedResources().get(0));
for (Resource resourceItem : resources) { for (Resource resourceItem : resources) {
emailNotificationModel.setNewObject(); Worker currentWorker = workerDAO.getCurrentWorker(resourceItem.getId());
emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_TODAY_TASK_SHOULD_START);
emailNotificationModel.setUpdated(new Date()); if (currentWorker != null && (currentWorker.getUser() != null) && currentWorker.getUser().isInRole(UserRole.ROLE_EMAIL_TASK_SHOULD_START)) {
emailNotificationModel.setResource(resourceItem); emailNotificationModel.setNewObject();
emailNotificationModel.setTask(item); emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_TODAY_TASK_SHOULD_START);
emailNotificationModel.setProject(item.getParent()); emailNotificationModel.setUpdated(new Date());
emailNotificationModel.confirmSave(); emailNotificationModel.setResource(resourceItem);
emailNotificationModel.setTask(item);
emailNotificationModel.setProject(item.getParent());
emailNotificationModel.confirmSave();
}
} }
} }

View file

@ -33,6 +33,7 @@ import org.libreplan.business.resources.entities.Resource;
import org.libreplan.business.resources.entities.Worker; import org.libreplan.business.resources.entities.Worker;
import org.libreplan.business.users.entities.User; 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.EffortDuration;
import org.libreplan.business.workingday.IntraDayDate; import org.libreplan.business.workingday.IntraDayDate;
import org.libreplan.business.workreports.daos.IWorkReportDAO; import org.libreplan.business.workreports.daos.IWorkReportDAO;
@ -159,11 +160,13 @@ public class SendEmailOnTimesheetDataMissing implements IEmailNotificationJob {
private void addRowsToNotificationTable(List<User> users){ private void addRowsToNotificationTable(List<User> users){
for (User user : users){ for (User user : users){
emailNotificationModel.setNewObject(); if ( user.isInRole(UserRole.ROLE_EMAIL_TIMESHEET_DATA_MISSING) ) {
emailNotificationModel.setResource(user.getWorker()); emailNotificationModel.setNewObject();
emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_ENTER_DATA_IN_TIMESHEET); emailNotificationModel.setResource(user.getWorker());
emailNotificationModel.setUpdated(new Date()); emailNotificationModel.setType(EmailTemplateEnum.TEMPLATE_ENTER_DATA_IN_TIMESHEET);
emailNotificationModel.confirmSave(); emailNotificationModel.setUpdated(new Date());
emailNotificationModel.confirmSave();
}
} }
} }