Merge pull request #7 from lmann99/exclude-non-bound-resources-from-notifications
Resolve NullPointerExceptions thrown by email notification scheduled …
This commit is contained in:
commit
f95977c986
7 changed files with 71 additions and 40 deletions
|
|
@ -122,8 +122,11 @@ public interface IWorkerDAO extends IIntegrationEntityDAO<Worker> {
|
|||
public List<Worker> findByFirstNameSecondNameAnotherTransaction(
|
||||
String firstname, String secondname);
|
||||
|
||||
public Worker getCurrentWorker(Long resourceID);
|
||||
|
||||
/**
|
||||
* Return the list of {@link Worker Workers} bound to any {@link User}.
|
||||
*/
|
||||
List<Worker> getBound();
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -198,4 +198,18 @@ public class WorkerDAO extends IntegrationEntityDAO<Worker>
|
|||
criteria.add(Restrictions.isNotNull("user"));
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Worker> workerList = workerModel.getWorkers();
|
||||
|
||||
for (Worker aWorkerList : workerList) {
|
||||
if (aWorkerList.getId().equals(resourceID)) {
|
||||
return aWorkerList;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private EmailTemplate findCurrentEmailTemplate(EmailTemplateEnum templateEnum, Locale locale) {
|
||||
List<EmailTemplate> emailTemplates;
|
||||
emailTemplates = emailTemplateModel.getAll();
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
|
|
@ -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<ResourceAllocation<?>> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<User> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue