From a32451167d5e22cbadd0099756e4331baf98cff0 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Thu, 24 Jul 2014 15:51:51 +0200 Subject: [PATCH] =?UTF-8?q?Optimalizace=20na=C4=8D=C3=ADt=C3=A1n=C3=AD=20d?= =?UTF-8?q?at=20z=20datab=C3=A1ze.=20Joinovan=C3=A9=20kolekce=20lze=20ozna?= =?UTF-8?q?=C4=8Dit=20jako=20lazy.=20Do=20servisn=C3=AD=20vrstvy=20se=20pa?= =?UTF-8?q?k=20implementuje=20transak=C4=8Dn=C3=AD=20metoda=20s=20anotac?= =?UTF-8?q?=C3=AD=20LazyLoader,=20kter=C3=A1=20dona=C4=8Dte=20kolekci.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../info/bukova/isspst/AppInitListener.java | 1 + .../java/info/bukova/isspst/SessionData.java | 59 ++++++++++++++ .../java/info/bukova/isspst/data/Member.java | 2 +- .../java/info/bukova/isspst/data/User.java | 2 +- .../info/bukova/isspst/data/Workgroup.java | 10 ++- .../isspst/security/LoginSuccessHandler.java | 59 ++++++++++++++ .../isspst/services/AbstractOwnedService.java | 4 + .../isspst/services/AbstractService.java | 39 ++++++++++ .../isspst/services/LazyLoadException.java | 12 +++ .../bukova/isspst/services/LazyLoader.java | 12 +++ .../info/bukova/isspst/services/Service.java | 2 + .../RequirementBaseServiceImpl.java | 51 ++++++++++--- .../TripRequirementServiceImpl.java | 25 ++++-- .../isspst/services/users/UserService.java | 1 + .../services/users/UserServiceImpl.java | 25 +++++- .../services/workgroups/WorkgroupService.java | 2 + .../workgroups/WorkgroupServiceImpl.java | 76 ++++++++++++++++--- .../info/bukova/isspst/ui/ListViewModel.java | 4 +- .../isspst/ui/dashboard/DashBoardVM.java | 17 ++--- .../ui/requirement/RequirementSubpage.java | 7 -- .../requirement/TripRequirementListAll.java | 5 -- .../TripRequirementListCentre.java | 5 -- .../TripRequirementListWorkgroup.java | 7 +- .../isspst/ui/workgroups/WorkgroupForm.java | 10 +++ .../isspst/ui/workgroups/WorkgroupList.java | 1 + .../ui/workgroups/WorkgroupTreeFactory.java | 9 +++ .../webapp/WEB-INF/spring/root-context.xml | 11 ++- 27 files changed, 392 insertions(+), 66 deletions(-) create mode 100644 src/main/java/info/bukova/isspst/SessionData.java create mode 100644 src/main/java/info/bukova/isspst/security/LoginSuccessHandler.java create mode 100644 src/main/java/info/bukova/isspst/services/LazyLoadException.java create mode 100644 src/main/java/info/bukova/isspst/services/LazyLoader.java diff --git a/src/main/java/info/bukova/isspst/AppInitListener.java b/src/main/java/info/bukova/isspst/AppInitListener.java index d18c9188..f2bf42c1 100644 --- a/src/main/java/info/bukova/isspst/AppInitListener.java +++ b/src/main/java/info/bukova/isspst/AppInitListener.java @@ -171,6 +171,7 @@ public class AppInitListener implements ServletContextListener { boolean adminExists = false; for (User u : userService.getAll()) { + userService.loadAuthorities(u); if (userService.hasRole(u, Constants.ROLE_ADMIN)) { adminExists = true; break; diff --git a/src/main/java/info/bukova/isspst/SessionData.java b/src/main/java/info/bukova/isspst/SessionData.java new file mode 100644 index 00000000..65feebf2 --- /dev/null +++ b/src/main/java/info/bukova/isspst/SessionData.java @@ -0,0 +1,59 @@ +package info.bukova.isspst; + +import info.bukova.isspst.data.Role; +import info.bukova.isspst.data.User; +import info.bukova.isspst.data.Workgroup; + +import java.io.Serializable; +import java.util.List; +import java.util.Map; + +public class SessionData implements Serializable { + + /** + * + */ + private static final long serialVersionUID = -764426911263559758L; + + private List userCentres; + private List userWorkgroups; + private User currentUser; + private Map> workgroupRoles; + + public SessionData() { + + } + + public List getUserCentres() { + return userCentres; + } + + public void setUserCentres(List userCentres) { + this.userCentres = userCentres; + } + + public List getUserWorkgroups() { + return userWorkgroups; + } + + public void setUserWorkgroups(List userWorkgroups) { + this.userWorkgroups = userWorkgroups; + } + + public User getCurrentUser() { + return currentUser; + } + + public void setCurrentUser(User currentUser) { + this.currentUser = currentUser; + } + + public Map> getWorkgroupRoles() { + return workgroupRoles; + } + + public void setWorkgroupRoles(Map> workgroupRoles) { + this.workgroupRoles = workgroupRoles; + } + +} diff --git a/src/main/java/info/bukova/isspst/data/Member.java b/src/main/java/info/bukova/isspst/data/Member.java index fcf28e6b..ad976e3d 100644 --- a/src/main/java/info/bukova/isspst/data/Member.java +++ b/src/main/java/info/bukova/isspst/data/Member.java @@ -31,7 +31,7 @@ public abstract class Member implements DataModel { @Column(name = "MODIFIED") private Date modified; @ManyToMany - @LazyCollection(LazyCollectionOption.FALSE) + @LazyCollection(LazyCollectionOption.TRUE) @JoinTable(name="MEMBER_PARENT", joinColumns={@JoinColumn(name="MEMBER_ID")}, inverseJoinColumns={@JoinColumn(name="PARENT_ID")}) private List parents; @Transient diff --git a/src/main/java/info/bukova/isspst/data/User.java b/src/main/java/info/bukova/isspst/data/User.java index 934c9bba..40e3d03d 100644 --- a/src/main/java/info/bukova/isspst/data/User.java +++ b/src/main/java/info/bukova/isspst/data/User.java @@ -38,7 +38,7 @@ public class User extends Member implements UserDetails, DataModel { private String email; @Column(name="NOTIFY") private boolean notify; - @ManyToMany(fetch=FetchType.EAGER) + @ManyToMany(fetch=FetchType.LAZY) @JoinTable(name="USER_ROLE", joinColumns={@JoinColumn(name="USER_ID")}, inverseJoinColumns={@JoinColumn(name="ROLE_ID")}) private List authorities; diff --git a/src/main/java/info/bukova/isspst/data/Workgroup.java b/src/main/java/info/bukova/isspst/data/Workgroup.java index 210e6837..4e5dccd8 100644 --- a/src/main/java/info/bukova/isspst/data/Workgroup.java +++ b/src/main/java/info/bukova/isspst/data/Workgroup.java @@ -1,5 +1,6 @@ package info.bukova.isspst.data; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; @@ -19,14 +20,19 @@ import org.hibernate.annotations.LazyCollectionOption; @Entity @Table(name = "WORKGROUP") -public class Workgroup extends Member implements OwnedDataModel { +public class Workgroup extends Member implements OwnedDataModel, Serializable { + + /** + * + */ + private static final long serialVersionUID = -5039169434782289101L; @Column(name = "CODE") private String code; @Column(name = "NAME") private String name; @ManyToMany(cascade = {CascadeType.ALL}) - @LazyCollection(LazyCollectionOption.FALSE) + @LazyCollection(LazyCollectionOption.TRUE) @JoinTable(name="WORKGROUP_MEMBER", joinColumns={@JoinColumn(name="WORKGROUP_ID")}, inverseJoinColumns={@JoinColumn(name="JOBMAPPING_ID")}) private List members; @Column(name = "CENTRE") diff --git a/src/main/java/info/bukova/isspst/security/LoginSuccessHandler.java b/src/main/java/info/bukova/isspst/security/LoginSuccessHandler.java new file mode 100644 index 00000000..3e0c0927 --- /dev/null +++ b/src/main/java/info/bukova/isspst/security/LoginSuccessHandler.java @@ -0,0 +1,59 @@ +package info.bukova.isspst.security; + +import info.bukova.isspst.SessionData; +import info.bukova.isspst.data.Role; +import info.bukova.isspst.data.User; +import info.bukova.isspst.data.Workgroup; +import info.bukova.isspst.services.users.UserService; +import info.bukova.isspst.services.workgroups.WorkgroupService; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.Authentication; +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; + +public class LoginSuccessHandler implements AuthenticationSuccessHandler { + + @Autowired + private SessionData sessionData; + @Autowired + private WorkgroupService workgroupService; + @Autowired + private UserService userService; + + private static final Logger logger = LoggerFactory.getLogger(LoginSuccessHandler.class); + + @Override + public void onAuthenticationSuccess(HttpServletRequest request, + HttpServletResponse response, Authentication auth) throws IOException, + ServletException { + + User u = (User) userService.loadUserByUsername(((User)auth.getPrincipal()).getUsername()); + + logger.info("User " + u.getUsername() + " logged in"); + + sessionData.setUserCentres(workgroupService.getUserCentres((User) auth.getPrincipal())); + sessionData.setUserWorkgroups(workgroupService.getUserWorkgroups((User) auth.getPrincipal())); + workgroupService.loadParents(u); + sessionData.setCurrentUser(u); + + Map> wgRoles = new HashMap>(); + for (Workgroup w : u.getParents()) { + wgRoles.put(w.getId(), workgroupService.getUserWorkgroupRoles(w, u)); + } + sessionData.setWorkgroupRoles(wgRoles); + + response.sendRedirect("app/"); + } + +} diff --git a/src/main/java/info/bukova/isspst/services/AbstractOwnedService.java b/src/main/java/info/bukova/isspst/services/AbstractOwnedService.java index af1051d0..af7951ce 100644 --- a/src/main/java/info/bukova/isspst/services/AbstractOwnedService.java +++ b/src/main/java/info/bukova/isspst/services/AbstractOwnedService.java @@ -36,6 +36,10 @@ public class AbstractOwnedService extends AbstractServ @Transactional protected User getLoggedInUser() { + if (sessionData.getCurrentUser() != null) { + return sessionData.getCurrentUser(); + } + try { String query = "from User where USERNAME = '" + ((UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername() + "'"; Query q = dao.getQuery(query); diff --git a/src/main/java/info/bukova/isspst/services/AbstractService.java b/src/main/java/info/bukova/isspst/services/AbstractService.java index 32c8d7b4..469cdc05 100644 --- a/src/main/java/info/bukova/isspst/services/AbstractService.java +++ b/src/main/java/info/bukova/isspst/services/AbstractService.java @@ -3,6 +3,7 @@ package info.bukova.isspst.services; import static ch.lambdaj.Lambda.filter; import info.bukova.isspst.Constants; import info.bukova.isspst.Module; +import info.bukova.isspst.SessionData; import info.bukova.isspst.dao.BaseDao; import info.bukova.isspst.data.DataModel; import info.bukova.isspst.data.NumberSeries; @@ -10,6 +11,9 @@ import info.bukova.isspst.filters.Filter; import info.bukova.isspst.reporting.Report; import info.bukova.isspst.services.numberseries.NumberSeriesService; +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Arrays; import java.util.Date; import java.util.List; @@ -20,6 +24,7 @@ import javax.validation.Validator; import org.hibernate.NonUniqueResultException; import org.hibernate.Query; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; @@ -27,6 +32,8 @@ public abstract class AbstractService implements Service protected BaseDao dao; private Validator validator; + @Autowired + protected SessionData sessionData; private NumberSeriesService numberSeriesService; @@ -183,4 +190,36 @@ public abstract class AbstractService implements Service return currentNumber; } + + @Override + @Transactional + public void loadLazyData(T entity) { + loadLazyData("", entity); + } + + @Override + @Transactional + public void loadLazyData(String group, T entity) { + Method[] methods = this.getClass().getMethods(); + + for (Method m : methods) { + Annotation[] annotations = m.getAnnotations(); + for (Annotation a : annotations) { + if (a instanceof LazyLoader) { + String[] values = ((LazyLoader) a).value(); + if (group.isEmpty() || values.length == 0 || Arrays.asList(values).contains(group)) { + try { + m.invoke(this, entity); + } catch (IllegalArgumentException e) { + throw new LazyLoadException(e.getCause()); + } catch (IllegalAccessException e) { + throw new LazyLoadException(e.getCause()); + } catch (InvocationTargetException e) { + throw new LazyLoadException(e.getCause()); + } + } + } + } + } + } } diff --git a/src/main/java/info/bukova/isspst/services/LazyLoadException.java b/src/main/java/info/bukova/isspst/services/LazyLoadException.java new file mode 100644 index 00000000..d8de78ab --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/LazyLoadException.java @@ -0,0 +1,12 @@ +package info.bukova.isspst.services; + + +public class LazyLoadException extends RuntimeException { + + private static final long serialVersionUID = -2251676786418660140L; + + public LazyLoadException(Throwable cause) { + super(cause); + } + +} diff --git a/src/main/java/info/bukova/isspst/services/LazyLoader.java b/src/main/java/info/bukova/isspst/services/LazyLoader.java new file mode 100644 index 00000000..755a17ce --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/LazyLoader.java @@ -0,0 +1,12 @@ +package info.bukova.isspst.services; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface LazyLoader { + String[] value() default ""; +} diff --git a/src/main/java/info/bukova/isspst/services/Service.java b/src/main/java/info/bukova/isspst/services/Service.java index b0f22709..cac39c05 100644 --- a/src/main/java/info/bukova/isspst/services/Service.java +++ b/src/main/java/info/bukova/isspst/services/Service.java @@ -13,6 +13,8 @@ public interface Service { public void update(T entity); public void delete(T entity); public void validate(T entity) throws ValidationException; + public void loadLazyData(T entity); + public void loadLazyData(String group, T entity); public List getAll(); public List execQuery(String query); public T selectSingle(String query); 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 15aedb2c..7a05daa4 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java @@ -9,11 +9,13 @@ 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 info.bukova.isspst.services.LazyLoader; import java.util.ArrayList; import java.util.Date; import java.util.List; +import org.hibernate.LazyInitializationException; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; @@ -69,27 +71,58 @@ public abstract class RequirementBaseServiceImpl exte @Override @Transactional + @LazyLoader({"grid", "form"}) public void loadAuthItems(T entity) { - T e = dao.getById(entity.getId()); - e.getAuthorization().size(); - entity.setAuthorization(e.getAuthorization()); + try { + if (entity.getAuthorization() == null) { + throw new LazyInitializationException(""); + } + entity.getAuthorization().size(); + } catch (LazyInitializationException ex) { + T e = dao.getById(entity.getId()); + e.getAuthorization().size(); + entity.setAuthorization(e.getAuthorization()); + } } @Override @Transactional + @LazyLoader("grid") public void loadType(T data) { - T e = dao.getById(data.getId()); - e.getType().getType(); - data.setType(e.getType()); + try { + if (data.getType() == null) { + throw new LazyInitializationException(""); + } + data.getType().getType(); + } catch (LazyInitializationException ex) { + T e = dao.getById(data.getId()); + + if (e.getType() == null) { + return; + } + + e.getType().getType(); + data.setType(e.getType()); + } } @Override @Transactional + @LazyLoader({"grid", "form"}) public void loadWorkflow(T data) { - T e = dao.getById(data.getId()); - e.getWorkflow().size(); - data.setWorkflow(e.getWorkflow()); + try { + if (data.getWorkflow() == null) { + throw new LazyInitializationException(""); + } + data.getWorkflow().size(); + } catch (LazyInitializationException ex) { + T e = dao.getById(data.getId()); + e.getWorkflow().size(); + e.getOwnedBy().getFullName(); + data.setWorkflow(e.getWorkflow()); + data.setOwnedBy(e.getOwnedBy()); + } } @Override 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 15496793..28fd66e8 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/TripRequirementServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/TripRequirementServiceImpl.java @@ -4,11 +4,13 @@ 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.LazyLoader; import info.bukova.isspst.services.workgroups.WorkgroupService; import java.util.Date; import java.util.List; +import org.hibernate.LazyInitializationException; import org.hibernate.Query; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PostFilter; @@ -66,7 +68,7 @@ public class TripRequirementServiceImpl extends RequirementBaseServiceImpl getCentreReq() { List wgList = workgroupService.getUserCentres(getLoggedInUser()); - Query q = dao.getQuery("select tr from TripRequirement tr join tr.centre c where tr.state != :state and c in (:wgList)"); + Query q = dao.getQuery("select tr from TripRequirement tr join fetch tr.ownedBy join tr.centre c where tr.state != :state and c in (:wgList) order by tr.numser"); q.setParameterList("wgList", wgList); q.setParameter("state", RequirementState.APPROVED); return q.list(); @@ -79,25 +81,36 @@ public class TripRequirementServiceImpl extends RequirementBaseServiceImpl getWorkgroupReq() { List wgList = workgroupService.getUserWorkgroups(getLoggedInUser()); - Query q = dao.getQuery("select tr from TripRequirement tr join tr.workgroup w where tr.state != :state and w in (:wgList)"); + Query q = dao.getQuery("select tr from TripRequirement tr join fetch tr.ownedBy join tr.workgroup w where tr.state != :state and w in (:wgList) order by tr.numser"); q.setParameterList("wgList", wgList); q.setParameter("state", RequirementState.APPROVED); return q.list(); } + @SuppressWarnings("unchecked") @Override @Transactional @PreAuthorize("hasPermission(this, 'PERM_SHOW_ALL_REQ')") public List getFromAll() { - return super.getAll(); + Query q = dao.getQuery("from TripRequirement as tr join fetch tr.ownedBy order by tr.numser"); + return q.list(); } @Override @Transactional + @LazyLoader("form") public void loadPassangers(TripRequirement entity) { - TripRequirement e = dao.getById(entity.getId()); - e.getPassengers().size(); - entity.setPassengers(e.getPassengers()); + try { + if (entity.getPassengers() == null) { + throw new LazyInitializationException(""); + } + + entity.getPassengers().size(); + } catch (LazyInitializationException ex) { + TripRequirement e = dao.getById(entity.getId()); + e.getPassengers().size(); + entity.setPassengers(e.getPassengers()); + } } } diff --git a/src/main/java/info/bukova/isspst/services/users/UserService.java b/src/main/java/info/bukova/isspst/services/users/UserService.java index 182a4b46..5b913f35 100644 --- a/src/main/java/info/bukova/isspst/services/users/UserService.java +++ b/src/main/java/info/bukova/isspst/services/users/UserService.java @@ -18,5 +18,6 @@ public interface UserService extends UserDetailsService, Service { public String encodePassword(User user, String plain); public void grantAdmin(); public void removeAccess(); + public void loadAuthorities(User user); } diff --git a/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java b/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java index 9a3df4cc..4e3f713c 100644 --- a/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java @@ -4,6 +4,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import org.hibernate.LazyInitializationException; import org.hibernate.Query; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -18,6 +19,7 @@ import info.bukova.isspst.Constants; import info.bukova.isspst.data.Role; import info.bukova.isspst.data.User; import info.bukova.isspst.services.AbstractService; +import info.bukova.isspst.services.LazyLoader; public class UserServiceImpl extends AbstractService implements UserService { @@ -34,7 +36,7 @@ public class UserServiceImpl extends AbstractService implements UserServic public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - Query q = dao.getQuery("from User user where user.username = :username"); + Query q = dao.getQuery("from User user join fetch user.authorities where user.username = :username"); q.setString("username", username); User u = (User) q.uniqueResult(); @@ -73,6 +75,10 @@ public class UserServiceImpl extends AbstractService implements UserServic @Override @Transactional public User getCurrent() { + if (sessionData.getCurrentUser() != null) { + return sessionData.getCurrentUser(); + } + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (auth != null && auth.getPrincipal() != null) { @@ -128,5 +134,22 @@ public class UserServiceImpl extends AbstractService implements UserServic return u; } + @Override + @Transactional + @LazyLoader({"grid", "form"}) + public void loadAuthorities(User user) { + try { + if (user.getAuthorities() == null) { + throw new LazyInitializationException(""); + } + + user.getAuthorities().size(); + } catch (LazyInitializationException ex) { + User u = dao.getById(user.getId()); + u.getAuthorities().size(); + user.setAuthorities(u.getAuthorities()); + } + } + } diff --git a/src/main/java/info/bukova/isspst/services/workgroups/WorkgroupService.java b/src/main/java/info/bukova/isspst/services/workgroups/WorkgroupService.java index 229b25dc..b9420b1c 100644 --- a/src/main/java/info/bukova/isspst/services/workgroups/WorkgroupService.java +++ b/src/main/java/info/bukova/isspst/services/workgroups/WorkgroupService.java @@ -20,5 +20,7 @@ public interface WorkgroupService extends Service { public List getUserWorkgroups(User user); public List getUserWorkgroupRoles(Workgroup workgroup, User user); public List getMembers(Workgroup workgroup); + public void loadMembers(Workgroup workgroup); + public void loadParents(Member member); } diff --git a/src/main/java/info/bukova/isspst/services/workgroups/WorkgroupServiceImpl.java b/src/main/java/info/bukova/isspst/services/workgroups/WorkgroupServiceImpl.java index abdafbde..7687ed94 100644 --- a/src/main/java/info/bukova/isspst/services/workgroups/WorkgroupServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/workgroups/WorkgroupServiceImpl.java @@ -7,10 +7,12 @@ import info.bukova.isspst.data.Role; import info.bukova.isspst.data.User; import info.bukova.isspst.data.Workgroup; import info.bukova.isspst.services.AbstractOwnedService; +import info.bukova.isspst.services.LazyLoader; import java.util.ArrayList; import java.util.List; +import org.hibernate.LazyInitializationException; import org.hibernate.Query; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; @@ -66,7 +68,13 @@ public class WorkgroupServiceImpl extends AbstractOwnedService implem } @Override + @Transactional public boolean isMember(Workgroup workgroup, Member member) { + if (member.equals(getLoggedInUser()) && sessionData.getCurrentUser() != null) { + return member.getParents().contains(workgroup); + } + + loadMembers(workgroup); for (JobMapping jm : workgroup.getMembers()) { if (jm.getMember().equals(member)) { return true; @@ -98,6 +106,10 @@ public class WorkgroupServiceImpl extends AbstractOwnedService implem @Override @Transactional public List getUserCentres(User user) { + if (user.equals(getLoggedInUser()) && sessionData.getUserCentres() != null) { + return sessionData.getUserCentres(); + } + List parents = getParents(user); if (parents == null) { @@ -112,6 +124,7 @@ public class WorkgroupServiceImpl extends AbstractOwnedService implem } for (Workgroup w : getUserWorkgroups(user)) { + loadParents(w); for (Workgroup centre : w.getParents()) { if (!centres.contains(centre)) { centres.add(centre); @@ -124,6 +137,10 @@ public class WorkgroupServiceImpl extends AbstractOwnedService implem @Override @Transactional public List getUserWorkgroups(User user) { + if (user.equals(getLoggedInUser()) && sessionData.getUserWorkgroups() != null) { + return sessionData.getUserWorkgroups(); + } + List parents = getParents(user); if (parents == null) { @@ -141,15 +158,9 @@ public class WorkgroupServiceImpl extends AbstractOwnedService implem } private List getParents(User user) { - List parents; - - if (user.getParents() == null) { - parents = loadUser(user).getParents(); - } else { - parents = user.getParents(); - } - - return parents; + loadParents(user); + + return user.getParents(); } private User loadUser(User user) { @@ -186,6 +197,10 @@ public class WorkgroupServiceImpl extends AbstractOwnedService implem @Override @Transactional public List getUserWorkgroupRoles(Workgroup workgroup, User user) { + if (user.equals(getLoggedInUser()) && sessionData.getWorkgroupRoles() != null) { + return sessionData.getWorkgroupRoles().get(workgroup.getId()); + } + List roles = new ArrayList(); User u; @@ -199,6 +214,8 @@ public class WorkgroupServiceImpl extends AbstractOwnedService implem return null; } + loadMembers(workgroup); + for (JobMapping jm : workgroup.getMembers()) { if (jm.getMember().equals(u)) { roles.add(jm.getRole()); @@ -212,7 +229,9 @@ public class WorkgroupServiceImpl extends AbstractOwnedService implem @Transactional @PreAuthorize("hasPermission(this, 'PERM_DELETE')") public void delete(Workgroup entity) { + loadMembers(entity); for (JobMapping m : entity.getMembers()) { + loadParents(m.getMember()); m.getMember().removeParent(entity); dao.getSession().merge(m.getMember()); } @@ -221,6 +240,7 @@ public class WorkgroupServiceImpl extends AbstractOwnedService implem @Override public List getMembers(Workgroup workgroup) { + loadMembers(workgroup); List jmList = workgroup.getMembers(); List members = new ArrayList(); @@ -231,4 +251,42 @@ public class WorkgroupServiceImpl extends AbstractOwnedService implem return members; } + @Override + @Transactional + @LazyLoader({"grid", "form"}) + public void loadMembers(Workgroup workgroup) { + try { + if (workgroup.getMembers() == null) { + throw new LazyInitializationException(""); + } + workgroup.getMembers().size(); + } catch (LazyInitializationException e) { + Workgroup w = dao.getById(workgroup.getId()); + w.getMembers().size(); + workgroup.setMembers(w.getMembers()); + } + } + + @Override + @Transactional + @LazyLoader("form") + public void loadParents(Member member) { + try { + if (member.getParents() == null) { + throw new LazyInitializationException(""); + } + member.getParents().size(); + } catch (LazyInitializationException e) { + if (member.isPerson()) { + User u = loadUser((User) member); + u.getParents().size(); + member.setParents(u.getParents()); + } else { + Workgroup w = dao.getById(member.getId()); + w.getParents().size(); + member.setParents(w.getParents()); + } + } + } + } diff --git a/src/main/java/info/bukova/isspst/ui/ListViewModel.java b/src/main/java/info/bukova/isspst/ui/ListViewModel.java index 2357d2d7..4a1093e8 100644 --- a/src/main/java/info/bukova/isspst/ui/ListViewModel.java +++ b/src/main/java/info/bukova/isspst/ui/ListViewModel.java @@ -61,7 +61,7 @@ public class ListViewModel { } protected void loadLazyData(T data) { - + service.loadLazyData("grid", data); } public void setDataFilter(Filter dataFilter) { @@ -164,7 +164,7 @@ public class ListViewModel { } protected void loadLazyDataForEdit(T data) { - + service.loadLazyData("form", data); } @Command diff --git a/src/main/java/info/bukova/isspst/ui/dashboard/DashBoardVM.java b/src/main/java/info/bukova/isspst/ui/dashboard/DashBoardVM.java index b7e1c02e..ae4784bc 100644 --- a/src/main/java/info/bukova/isspst/ui/dashboard/DashBoardVM.java +++ b/src/main/java/info/bukova/isspst/ui/dashboard/DashBoardVM.java @@ -1,17 +1,16 @@ package info.bukova.isspst.ui.dashboard; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import info.bukova.isspst.data.Role; import info.bukova.isspst.data.User; import info.bukova.isspst.data.Workgroup; import info.bukova.isspst.services.users.UserService; import info.bukova.isspst.services.workgroups.WorkgroupService; -import org.springframework.security.core.context.SecurityContextHolder; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.zkoss.bind.annotation.Init; import org.zkoss.zk.ui.select.annotation.WireVariable; @@ -26,11 +25,7 @@ public class DashBoardVM { @Init public void init() { - user = User.class.cast(SecurityContextHolder.getContext().getAuthentication().getPrincipal()); - - if (user.getParents() == null) { // try reload from DB - user = userService.getCurrent(); - } + user = userService.getCurrent(); groupRoles = new HashMap>(); diff --git a/src/main/java/info/bukova/isspst/ui/requirement/RequirementSubpage.java b/src/main/java/info/bukova/isspst/ui/requirement/RequirementSubpage.java index 5139ebe9..2f9a723b 100644 --- a/src/main/java/info/bukova/isspst/ui/requirement/RequirementSubpage.java +++ b/src/main/java/info/bukova/isspst/ui/requirement/RequirementSubpage.java @@ -15,13 +15,6 @@ public class RequirementSubpage extends ListViewModel { return (RequirementBaseService) service; } - - @Override - protected void loadLazyData(T data) { - this.getReqService().loadAuthItems(data); - this.getReqService().loadWorkflow(data); - this.getReqService().loadType(data); - } @Command @NotifyChange({"dataBean", "canApprove"}) 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 f6bf7743..463e7f72 100644 --- a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListAll.java +++ b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListAll.java @@ -59,10 +59,5 @@ public class TripRequirementListAll extends RequirementSubpage public void reloadRelated() { this.reload(); } - - @Override - protected void loadLazyDataForEdit(TripRequirement data) { - tripRequirementService.loadPassangers(data); - } } 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 a5b0869c..ba8d87d5 100644 --- a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListCentre.java +++ b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListCentre.java @@ -56,10 +56,5 @@ public class TripRequirementListCentre extends RequirementSubpage { if (!r.getAuthority().equals(Constants.ROLE_USER)) { List users = userService.getUsersWithRole(r); for (User u : users) { + workgroupService.loadParents(u); workgroupService.addMember(getDataBean(), u, r); } } @@ -124,6 +125,8 @@ public class WorkgroupForm extends FormViewModel { return; } + workgroupService.loadMembers(getDataBean()); + workgroupService.loadParents(u.getMember()); workgroupService.removeMember(getDataBean(), u); if (!target.equals("users")) { @@ -139,6 +142,13 @@ public class WorkgroupForm extends FormViewModel { return; } + workgroupService.loadMembers(getDataBean()); + workgroupService.loadParents(u); + + if (!u.isPerson()) { + workgroupService.loadMembers((Workgroup) u); + } + workgroupService.addMember(getDataBean(), u, roleService.getRoleByAuthority(target)); if (!u.isPerson()) { diff --git a/src/main/java/info/bukova/isspst/ui/workgroups/WorkgroupList.java b/src/main/java/info/bukova/isspst/ui/workgroups/WorkgroupList.java index cd3f2d2f..a6bdaecd 100644 --- a/src/main/java/info/bukova/isspst/ui/workgroups/WorkgroupList.java +++ b/src/main/java/info/bukova/isspst/ui/workgroups/WorkgroupList.java @@ -35,6 +35,7 @@ public class WorkgroupList extends ListViewModel { formZul = "workgroupForm.zul"; dataFilter = new WorkgroupFilter(getFilterTemplate()); treeFactory = new WorkgroupTreeFactory(); + treeFactory.setWorkgroupService(workgroupService); boolConverter = new BoolConverter(); } diff --git a/src/main/java/info/bukova/isspst/ui/workgroups/WorkgroupTreeFactory.java b/src/main/java/info/bukova/isspst/ui/workgroups/WorkgroupTreeFactory.java index b63969a8..01106bb2 100644 --- a/src/main/java/info/bukova/isspst/ui/workgroups/WorkgroupTreeFactory.java +++ b/src/main/java/info/bukova/isspst/ui/workgroups/WorkgroupTreeFactory.java @@ -7,6 +7,7 @@ import info.bukova.isspst.Constants; import info.bukova.isspst.data.JobMapping; import info.bukova.isspst.data.Member; import info.bukova.isspst.data.Workgroup; +import info.bukova.isspst.services.workgroups.WorkgroupService; import org.zkoss.zul.DefaultTreeModel; import org.zkoss.zul.TreeModel; @@ -14,6 +15,8 @@ import org.zkoss.zul.TreeNode; public class WorkgroupTreeFactory { + private WorkgroupService workgroupService; + public TreeModel> createTreeModel(Workgroup workgroup) { WorkgroupTreeNode root = new WorkgroupTreeNode(null, generateChildren(workgroup)); @@ -24,6 +27,8 @@ public class WorkgroupTreeFactory { Collection children = new LinkedList(); Workgroup workgroup = Workgroup.class.cast(member); + workgroupService.loadMembers(workgroup); + for (JobMapping jm : workgroup.getMembers()) { if (jm.getRole().getAuthority().equals(Constants.ROLE_USER) && jm.getMember().isPerson()) { children.add(new WorkgroupTreeNode(jm.getMember())); @@ -35,4 +40,8 @@ public class WorkgroupTreeFactory { return children; } + public void setWorkgroupService(WorkgroupService workgroupService) { + this.workgroupService = workgroupService; + } + } diff --git a/src/main/webapp/WEB-INF/spring/root-context.xml b/src/main/webapp/WEB-INF/spring/root-context.xml index c6187b0a..264d4fa1 100644 --- a/src/main/webapp/WEB-INF/spring/root-context.xml +++ b/src/main/webapp/WEB-INF/spring/root-context.xml @@ -49,6 +49,7 @@ ${jdbc.dialect} false update + @@ -81,7 +82,8 @@ + authentication-failure-handler-ref="loginFail" + authentication-success-handler-ref="loginSuccess"/> @@ -101,6 +103,8 @@ + + @@ -109,6 +113,11 @@ + + + + +