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; }
@SuppressWarnings("unchecked") public List<Experiment> getMyExperiments(User user) { String queryString = "from Experiment e where e.owner.username=:username"; Query query = dao.getEntityManager().createQuery(queryString); query.setParameter("username", user.getUsername()); List<Experiment> experiments = query.getResultList(); for (Experiment experiment : experiments) { experiment.setNumDataSets(getNumDataSets(experiment)); } return experiments; }
public void permitted(User user, Integer experimentId) throws SecurityException { Experiment experiment = (Experiment) dao.get(Experiment.class.getName(), "id", experimentId); if (experiment.getOwner().equals(user) || experiment.userAllowed(user)) { user.setCurrentExperiment(experiment); User loadedUser = (User) dao.get(User.class.getName(), "username", user.getUsername()); loadedUser.setCurrentExperiment(experiment); dao.update(loadedUser); } else { throw new SecurityException( "You do not have permission to view the experiment: " + experiment.getName()); } }