public List<Experiment> getSharedExperiments(User user) { String queryString = "from User u inner join fetch u.experiments where u.username=:username"; Query query = dao.getEntityManager().createQuery(queryString); query.setParameter("username", user.getUsername()); @SuppressWarnings("unchecked") List<User> users = query.getResultList(); List<Experiment> experiments = new ArrayList<Experiment>(); for (User userSingleE : users) { Object[] userExperiments = userSingleE.getExperiments().toArray(); for (Object o : userExperiments) { Experiment e = (Experiment) o; boolean have = false; for (Experiment currentE : experiments) { if (currentE.getId() == e.getId()) have = true; } if (!have) experiments.add(e); } } // get experiments shared with all queryString = "from Experiment where shareWithAll=:shareWithAll"; query = dao.getEntityManager().createQuery(queryString); query.setParameter("shareWithAll", true); @SuppressWarnings("unchecked") List<Object> experimentsSharedWithAll = query.getResultList(); for (Object o : experimentsSharedWithAll) { experiments.add((Experiment) o); } for (Experiment experiment : experiments) { experiment.setNumDataSets(getNumDataSets(experiment)); } return experiments; }
public void clearSharedList(Experiment experiment) { List<User> userList = new ArrayList<User>(); for (User user : experiment.getSharedList()) { userList.add(user); } for (User user : userList) { if (user.getExperiments() != null) { user.getExperiments().remove(experiment); dao.update(user); dao.getEntityManager().flush(); } } experiment.getSharedList().clear(); dao.update(experiment); updateActivity(experiment); dao.getEntityManager().flush(); }