@Override public void removeSubscriber(User user, Test test) { Test freshTest = testDAO.get(test.getId()); Collection<User> testSubscribers = new ArrayList<>(freshTest.getSubscribers()); for (User testSubscriber : freshTest.getSubscribers()) { if (testSubscriber.getId().equals(user.getId())) { testSubscribers.remove(testSubscriber); } } test.setSubscribers(testSubscribers); testDAO.update(test); }
@Override public boolean isUserSubscribed(User user, Test test) { Test freshTest = testDAO.get(test.getId()); Collection<User> testSubscribers = freshTest.getSubscribers(); for (User testSubscriber : testSubscribers) { if (testSubscriber.getId().equals(user.getId())) { return true; } } return false; }
@Override public void addSubscriber(User user, Test test) { Test freshTest = testDAO.get(test.getId()); User freshUser = userDAO.get(user.getId()); Collection<User> testSubscribers = freshTest.getSubscribers(); if (testSubscribers.contains(freshUser)) { return; } testSubscribers.add(freshUser); testDAO.update(freshTest); }
@Override public Test getFullTest(Long id) { Test test = testDAO.get(id); if (test == null) { return null; } test = test.clone(); // TODO: return by named query, with optimized fetching Collection<Metric> metrics = test.getMetrics(); if (metrics != null) { List<Metric> clonedMetrics = new ArrayList<Metric>(); for (Metric metric : metrics) { clonedMetrics.add(metric); } test.setMetrics(clonedMetrics); } Collection<User> subscribers = test.getSubscribers(); if (subscribers != null) { List<User> subscribersClone = new ArrayList<>(); for (User subscriber : subscribers) { subscribersClone.add(subscriber.clone()); } test.setSubscribers(subscribersClone); } Collection<Alert> alerts = test.getAlerts(); if (alerts != null) { List<Alert> alertsClone = new ArrayList<>(); for (Alert alert : alerts) { List<Tag> tagsClone = new ArrayList<>(); for (Tag tag : alert.getTags()) { tagsClone.add(tag.clone()); } Alert alertClone = alert.clone(); alertClone.setTags(tagsClone); alertsClone.add(alertClone); } test.setAlerts(alertsClone); } return test; }