diff --git a/src/main/java/info/bukova/isspst/data/RequirementBase.java b/src/main/java/info/bukova/isspst/data/RequirementBase.java index 00912ddc..399267aa 100644 --- a/src/main/java/info/bukova/isspst/data/RequirementBase.java +++ b/src/main/java/info/bukova/isspst/data/RequirementBase.java @@ -13,6 +13,7 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.MappedSuperclass; import javax.persistence.OneToMany; +import javax.persistence.OrderBy; import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; @@ -29,7 +30,7 @@ public class RequirementBase extends BaseData { private Date reqDate; @Column(name = "DESCRIPTION") private String description; - @ManyToOne(fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "CENTRE_ID") private Workgroup centre; @ManyToOne(fetch = FetchType.EAGER) @@ -37,6 +38,7 @@ public class RequirementBase extends BaseData { private Workgroup workgroup; @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @LazyCollection(LazyCollectionOption.TRUE) + @OrderBy("ID") private List authorization; @Column(name = "STATE") @Enumerated(EnumType.ORDINAL) diff --git a/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseService.java b/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseService.java index b6a8723b..e6fe255b 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseService.java +++ b/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseService.java @@ -1,5 +1,15 @@ package info.bukova.isspst.services.requirement; -public interface RequirementBaseService { +import info.bukova.isspst.data.RequirementBase; +import info.bukova.isspst.data.User; +import java.util.List; + +public interface RequirementBaseService { + + public void loadAuthItems(T data); + public void approve(T entity); + public boolean canApprove(T entity); + public List getNextApprover(T entity); + } diff --git a/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java b/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java new file mode 100644 index 00000000..0f63dc52 --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java @@ -0,0 +1,130 @@ +package info.bukova.isspst.services.requirement; + +import info.bukova.isspst.data.AuthItem; +import info.bukova.isspst.data.JobMapping; +import info.bukova.isspst.data.RequirementBase; +import info.bukova.isspst.data.RequirementState; +import info.bukova.isspst.data.Role; +import info.bukova.isspst.data.User; +import info.bukova.isspst.data.Workflow; +import info.bukova.isspst.data.Workgroup; +import info.bukova.isspst.services.AbstractOwnedService; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.transaction.annotation.Transactional; + +public abstract class RequirementBaseServiceImpl extends + AbstractOwnedService implements RequirementBaseService { + + @Override + @Transactional + public List getNextApprover(T entity) { + T e = (T) dao.getById(entity.getId()); + + + List users = new ArrayList(); + Workgroup wg; + Workflow wf = getNextWorkflow(e); + Role nextRole; + + if (wf == null) { + return null; + } else { + nextRole = wf.getRole(); + } + + if (wf.getCentre() || e.getWorkgroup() == null) { + wg = e.getCentre(); + } else { + wg = e.getWorkgroup(); + } + + for (JobMapping jm : wg.getMembers()) { + if (jm.getRole().equals(nextRole) && jm.getMember().isPerson()) { + users.add((User) jm.getMember()); + } + } + + return users; + } + + @Override + @Transactional + public void loadAuthItems(T entity) { + T e = dao.getById(entity.getId()); + e.getAuthorization().size(); + entity.setAuthorization(e.getAuthorization()); + } + + @Override + @Transactional + @PreAuthorize("this.canApprove(#entity)") + public void approve(T entity) { + T e = (T) dao.getById(entity.getId()); + + Workflow wf = getNextWorkflow(e); + if (wf == null) { + return; + } + + Role role = wf.getRole(); + AuthItem auth = new AuthItem(); + auth.setApprover(getLoggedInUser()); + auth.setRole(role); + auth.setAuthDate(new Date()); + + e.getAuthorization().add(auth); + + if (getNextWorkflow(e) == null) { + e.setState(RequirementState.APPROVED); + } else { + e.setState(RequirementState.PARTIALLY); + } + entity.setState(e.getState()); + entity.getAuthorization().add(auth); + + this.update(e); + } + + @Override + @Transactional + public boolean canApprove(T entity) { + List nextApprovers = getNextApprover(entity); + if (nextApprovers != null && nextApprovers.contains(getLoggedInUser())) { + return true; + } + + return false; + } + + private Workflow getNextWorkflow(T e) { + AuthItem authItem = null; + if (e.getAuthorization().size() > 0) { + authItem = e.getAuthorization().get(e.getAuthorization().size() - 1); + } + + Workflow wf = null; + for (int i = 0; i < e.getType().getWorkflow().size(); i++) { + wf = e.getType().getWorkflow().get(i); + if (authItem != null && wf.getRole().equals(authItem.getRole())) { + if (i + 1 < e.getType().getWorkflow().size()) { + wf = e.getType().getWorkflow().get(i + 1); + break; + } else { + return null; + } + } + + if (authItem == null) { + break; + } + } + + return wf; + } + +} 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 944207c5..b6b40b6a 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java @@ -3,7 +3,6 @@ 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.services.AbstractOwnedService; import java.util.Date; @@ -11,14 +10,15 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; -public class RequirementServiceImpl extends AbstractOwnedService implements RequirementService, RequirementBaseService -{ +public class RequirementServiceImpl extends + RequirementBaseServiceImpl implements RequirementService, + RequirementBaseService { + @Autowired private RequirementTypeService reqTypeService; @Override - protected Requirement createEntity() - { + protected Requirement createEntity() { Requirement entity = new Requirement(); entity.setReqDate(new Date()); @@ -31,8 +31,7 @@ public class RequirementServiceImpl extends AbstractOwnedService im @Override @Transactional @PreAuthorize("hasPermission(this, 'PERM_ADD')") - public void add(Requirement entity) - { + public void add(Requirement entity) { entity.setNumser(this.getNumberSerie()); super.add(entity); } diff --git a/src/main/java/info/bukova/isspst/services/requirement/TripRequirementService.java b/src/main/java/info/bukova/isspst/services/requirement/TripRequirementService.java index 3285cc7b..cf258732 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/TripRequirementService.java +++ b/src/main/java/info/bukova/isspst/services/requirement/TripRequirementService.java @@ -3,6 +3,7 @@ package info.bukova.isspst.services.requirement; import java.util.List; import info.bukova.isspst.data.TripRequirement; +import info.bukova.isspst.data.User; import info.bukova.isspst.services.Service; public interface TripRequirementService extends Service { @@ -10,5 +11,9 @@ public interface TripRequirementService extends Service { public List getCentreReq(); public List getWorkgroupReq(); public List getFromAll(); + public void loadAuthItems(TripRequirement entity); + public List getNextApprover(TripRequirement entity); + public boolean canApprove(TripRequirement entity); + public void approve(TripRequirement entity); } diff --git a/src/main/java/info/bukova/isspst/services/requirement/TripRequirementServiceImpl.java b/src/main/java/info/bukova/isspst/services/requirement/TripRequirementServiceImpl.java index 46b84443..ad384152 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/TripRequirementServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/TripRequirementServiceImpl.java @@ -4,7 +4,6 @@ import info.bukova.isspst.Constants; import info.bukova.isspst.data.RequirementState; import info.bukova.isspst.data.TripRequirement; import info.bukova.isspst.data.Workgroup; -import info.bukova.isspst.services.AbstractOwnedService; import info.bukova.isspst.services.workgroups.WorkgroupService; import java.util.Date; @@ -16,8 +15,8 @@ import org.springframework.security.access.prepost.PostFilter; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; -public class TripRequirementServiceImpl extends AbstractOwnedService - implements TripRequirementService, RequirementBaseService { +public class TripRequirementServiceImpl extends RequirementBaseServiceImpl + implements TripRequirementService, RequirementBaseService { @Autowired private RequirementTypeService reqTypeService; @@ -89,4 +88,5 @@ public class TripRequirementServiceImpl extends AbstractOwnedService { @NotifyChange({"dataBean","ableToDelete"}) public void setDataBean(T data) { + loadLazyData(data); this.dataBean = data; } + + protected void loadLazyData(T data) { + + } public void setDataFilter(Filter dataFilter) { this.dataFilter = dataFilter; diff --git a/src/main/java/info/bukova/isspst/ui/requirement/RequirementSubpage.java b/src/main/java/info/bukova/isspst/ui/requirement/RequirementSubpage.java new file mode 100644 index 00000000..bf6eee07 --- /dev/null +++ b/src/main/java/info/bukova/isspst/ui/requirement/RequirementSubpage.java @@ -0,0 +1,45 @@ +package info.bukova.isspst.ui.requirement; + +import info.bukova.isspst.data.RequirementBase; +import info.bukova.isspst.services.requirement.RequirementBaseService; +import info.bukova.isspst.ui.ListViewModel; + +import org.zkoss.bind.annotation.Command; +import org.zkoss.bind.annotation.NotifyChange; + +public class RequirementSubpage extends ListViewModel { + + protected RequirementBaseService reqService; + + @SuppressWarnings("unchecked") + public void init() { + reqService = (RequirementBaseService) service; + } + + @Override + protected void loadLazyData(T data) { + reqService.loadAuthItems(data); + } + + @Command + @NotifyChange("dataBean") + public void approve() { + reqService.approve(getDataBean()); + } + + @Override + @NotifyChange({ "dataBean", "ableToDelete", "canApprove" }) + public void setDataBean(T data) { + super.setDataBean(data); + } + + public boolean isCanApprove() { + if (getDataBean() != null) { + return reqService.canApprove(getDataBean()); + } + + return false; + } + + +} diff --git a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListAll.java b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListAll.java index 13209f19..0d1262ee 100644 --- a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListAll.java +++ b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListAll.java @@ -1,5 +1,11 @@ package info.bukova.isspst.ui.requirement; +import info.bukova.isspst.data.TripRequirement; +import info.bukova.isspst.data.Workgroup; +import info.bukova.isspst.filters.TripRequirementFilter; +import info.bukova.isspst.services.requirement.TripRequirementService; +import info.bukova.isspst.services.workgroups.WorkgroupService; + import java.util.ArrayList; import java.util.List; @@ -8,22 +14,12 @@ import org.zkoss.bind.BindUtils; import org.zkoss.bind.annotation.Init; import org.zkoss.zk.ui.select.annotation.WireVariable; -import info.bukova.isspst.data.TripRequirement; -import info.bukova.isspst.data.Workgroup; -import info.bukova.isspst.filters.TripRequirementFilter; -import info.bukova.isspst.services.requirement.TripRequirementService; -import info.bukova.isspst.services.users.UserService; -import info.bukova.isspst.services.workgroups.WorkgroupService; -import info.bukova.isspst.ui.ListViewModel; - -public class TripRequirementListAll extends ListViewModel { +public class TripRequirementListAll extends RequirementSubpage { @WireVariable private TripRequirementService tripRequirementService; @WireVariable private WorkgroupService workgroupService; - @WireVariable - private UserService userService; private List allCentres; private List allWorkgroups; @@ -36,6 +32,7 @@ public class TripRequirementListAll extends ListViewModel { allCentres = workgroupService.getCentres(); allWorkgroups = workgroupService.getWorkgroups(); + super.init(); } @Override diff --git a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListCentre.java b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListCentre.java index 6eac2a19..f8833c8a 100644 --- a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListCentre.java +++ b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListCentre.java @@ -1,5 +1,12 @@ package info.bukova.isspst.ui.requirement; +import info.bukova.isspst.data.TripRequirement; +import info.bukova.isspst.data.Workgroup; +import info.bukova.isspst.filters.TripRequirementFilter; +import info.bukova.isspst.services.requirement.TripRequirementService; +import info.bukova.isspst.services.users.UserService; +import info.bukova.isspst.services.workgroups.WorkgroupService; + import java.util.ArrayList; import java.util.List; @@ -8,15 +15,7 @@ import org.zkoss.bind.BindUtils; import org.zkoss.bind.annotation.Init; import org.zkoss.zk.ui.select.annotation.WireVariable; -import info.bukova.isspst.data.TripRequirement; -import info.bukova.isspst.data.Workgroup; -import info.bukova.isspst.filters.TripRequirementFilter; -import info.bukova.isspst.services.requirement.TripRequirementService; -import info.bukova.isspst.services.users.UserService; -import info.bukova.isspst.services.workgroups.WorkgroupService; -import info.bukova.isspst.ui.ListViewModel; - -public class TripRequirementListCentre extends ListViewModel { +public class TripRequirementListCentre extends RequirementSubpage { @WireVariable private TripRequirementService tripRequirementService; @@ -34,6 +33,7 @@ public class TripRequirementListCentre extends ListViewModel { dataFilter = new TripRequirementFilter(getFilterTemplate()); myCentres = workgroupService.getUserCentres(userService.getCurrent()); + super.init(); } @Override @@ -49,5 +49,7 @@ public class TripRequirementListCentre extends ListViewModel { public List getMyCentres() { return myCentres; } + + } diff --git a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListWorkgroup.java b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListWorkgroup.java index 78167bcd..d744e0a1 100644 --- a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListWorkgroup.java +++ b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListWorkgroup.java @@ -1,5 +1,12 @@ package info.bukova.isspst.ui.requirement; +import info.bukova.isspst.data.TripRequirement; +import info.bukova.isspst.data.Workgroup; +import info.bukova.isspst.filters.TripRequirementFilter; +import info.bukova.isspst.services.requirement.TripRequirementService; +import info.bukova.isspst.services.users.UserService; +import info.bukova.isspst.services.workgroups.WorkgroupService; + import java.util.ArrayList; import java.util.List; @@ -8,15 +15,7 @@ import org.zkoss.bind.BindUtils; import org.zkoss.bind.annotation.Init; import org.zkoss.zk.ui.select.annotation.WireVariable; -import info.bukova.isspst.data.TripRequirement; -import info.bukova.isspst.data.Workgroup; -import info.bukova.isspst.filters.TripRequirementFilter; -import info.bukova.isspst.services.requirement.TripRequirementService; -import info.bukova.isspst.services.users.UserService; -import info.bukova.isspst.services.workgroups.WorkgroupService; -import info.bukova.isspst.ui.ListViewModel; - -public class TripRequirementListWorkgroup extends ListViewModel { +public class TripRequirementListWorkgroup extends RequirementSubpage { @WireVariable private TripRequirementService tripRequirementService; @@ -36,6 +35,7 @@ public class TripRequirementListWorkgroup extends ListViewModel myCentres = workgroupService.getUserCentres(userService.getCurrent()); myWorkgroups = workgroupService.getUserWorkgroups(userService.getCurrent()); + super.init(); } @Override diff --git a/src/main/webapp/requirements/approveStatus.zul b/src/main/webapp/requirements/approveStatus.zul new file mode 100644 index 00000000..e18761ca --- /dev/null +++ b/src/main/webapp/requirements/approveStatus.zul @@ -0,0 +1,25 @@ + + + +