diff --git a/src/main/java/info/bukova/isspst/services/requirement/ReqMaterialServiceImpl.java b/src/main/java/info/bukova/isspst/services/requirement/ReqMaterialServiceImpl.java index d0da386d..cf52e60d 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/ReqMaterialServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/ReqMaterialServiceImpl.java @@ -14,7 +14,7 @@ import org.springframework.security.access.prepost.PostFilter; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; -public class ReqMaterialServiceImpl extends RequirementBaseServiceImpl implements RequirementService, RequirementBaseService +public class ReqMaterialServiceImpl extends RequirementServiceImpl implements RequirementService, RequirementBaseService { @Autowired private RequirementTypeService reqTypeService; diff --git a/src/main/java/info/bukova/isspst/services/requirement/ReqServicesServiceImpl.java b/src/main/java/info/bukova/isspst/services/requirement/ReqServicesServiceImpl.java index a3aa4d22..46d86338 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/ReqServicesServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/ReqServicesServiceImpl.java @@ -14,7 +14,7 @@ import org.springframework.security.access.prepost.PostFilter; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; -public class ReqServicesServiceImpl extends RequirementBaseServiceImpl implements RequirementService, RequirementBaseService +public class ReqServicesServiceImpl extends RequirementServiceImpl implements RequirementService, RequirementBaseService { @Autowired private RequirementTypeService reqTypeService; diff --git a/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java b/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java index 37a29784..0a84ae74 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java @@ -182,10 +182,7 @@ public abstract class RequirementBaseServiceImpl exte } } - @Override - @Transactional - @PreAuthorize("this.canApprove(#entity)") - public void approve(T entity) { + protected void approve(T entity, User user) { T e = (T) dao.getById(entity.getId()); Workflow wf = getNextWorkflow(e); @@ -195,7 +192,7 @@ public abstract class RequirementBaseServiceImpl exte Role role = wf.getRole(); AuthItem auth = new AuthItem(); - auth.setApprover(getLoggedInUser()); + auth.setApprover(user); auth.setRole(role); auth.setAuthDate(new Date()); @@ -211,26 +208,39 @@ public abstract class RequirementBaseServiceImpl exte super.update(e); - this.sendToApprovers(e); - - SettingsData settings = settingsService.getSettings(); - MailMessage message = null; - - if (e.getOwnedBy().getEmail() != null - && !e.getOwnedBy().getEmail().isEmpty() - && e.getOwnedBy().isNotify()) { - if (e.getState() == RequirementState.APPROVED && settings.getConfReqTemplate() != null) { - message = messageBuilder.buildMessage(settings.getConfReqTemplate(), e); - } else if (settings.getAuthReqTemplate() != null) { - message = messageBuilder.buildMessage(settings.getAuthReqTemplate(), e); - } + if (!autoApprove(entity)) { + this.sendToApprovers(e); - if (message != null) { - message.setTo(e.getOwnedBy().getEmail()); - mailer.send(message); + SettingsData settings = settingsService.getSettings(); + MailMessage message = null; + + if (e.getOwnedBy().getEmail() != null + && !e.getOwnedBy().getEmail().isEmpty() + && e.getOwnedBy().isNotify()) { + if (e.getState() == RequirementState.APPROVED && settings.getConfReqTemplate() != null) { + message = messageBuilder.buildMessage(settings.getConfReqTemplate(), e); + } else if (settings.getAuthReqTemplate() != null) { + message = messageBuilder.buildMessage(settings.getAuthReqTemplate(), e); + } + + if (message != null) { + message.setTo(e.getOwnedBy().getEmail()); + mailer.send(message); + } } } } + + @Override + @Transactional + @PreAuthorize("this.canApprove(#entity)") + public void approve(T entity) { + approve(entity, getLoggedInUser()); + } + + protected boolean autoApprove(T entity) { + return false; + } @Override @Transactional @@ -243,7 +253,7 @@ public abstract class RequirementBaseServiceImpl exte return false; } - private Workflow getNextWorkflow(T e) { + protected Workflow getNextWorkflow(T e) { AuthItem authItem = null; if (e.getWorkflow() == null) { return null; diff --git a/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java b/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java index d57b2d85..a34559ab 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java @@ -3,8 +3,11 @@ package info.bukova.isspst.services.requirement; import info.bukova.isspst.Constants; import info.bukova.isspst.data.Requirement; import info.bukova.isspst.data.RequirementState; +import info.bukova.isspst.data.User; +import info.bukova.isspst.data.Workflow; import java.util.Date; +import java.util.List; import org.springframework.beans.factory.annotation.Autowired; @@ -26,4 +29,21 @@ public class RequirementServiceImpl extends return entity; } + @Override + protected boolean autoApprove(Requirement entity) { + List approvers = this.getNextApprover(entity); + Workflow nextWf = this.getNextWorkflow(entity); + + if (approvers == null || approvers.isEmpty() || nextWf == null || nextWf.getLimit() == null) { + return false; + } + + if (entity.getSumTotal().compareTo(nextWf.getLimit()) == -1) { + approve(entity, approvers.get(0)); + return true; + } + + return false; + } + }