@Override public EndpointGroup save(EndpointGroup object) { LOG.debug("Saving endpoint group {}", object); Set<Topic> topics = object.getTopics(); object = super.save(object); if (topics != null && !topics.isEmpty()) { for (Topic topic : topics) { addTopicToEndpointGroup(object, topic.getStringId()); } } if (LOG.isTraceEnabled()) { LOG.trace("Saving result: {}.", object); } else { LOG.debug("Saving result: {}.", object != null); } return object; }
private EndpointGroup addTopicToEndpointGroup(EndpointGroup endpointGroup, String topicId) { Topic topic = topicDao.findById(topicId); if (topic != null) { topic.getEndpointGroups().add(endpointGroup); } endpointGroup.getTopics().add(save(topic, Topic.class)); return endpointGroup; }
@Override public EndpointGroup removeTopicFromEndpointGroup(String id, String topicId) { LOG.debug("Removing topic [{}] from endpoint group [{}]", topicId, id); EndpointGroup endpointGroup = findById(id); Set<Topic> topics = endpointGroup.getTopics(); Iterator<Topic> it = topics.iterator(); while (it.hasNext()) { Topic topic = it.next(); if (topic.getId() == Long.parseLong(topicId)) { it.remove(); topic.getEndpointGroups().remove(endpointGroup); topicDao.save(topic); break; } } return save(endpointGroup); }
@Override public void removeById(String id) { EndpointGroup endpointGroup = findById(id); if (endpointGroup != null) { Set<Topic> topics = endpointGroup.getTopics(); if (topics != null && !topics.isEmpty()) { for (Topic topic : topics) { topic.getEndpointGroups().remove(endpointGroup); topicDao.save(topic); } } } remove(endpointGroup); LOG.debug("Removed endpoint group by id [{}] ", id); }
@Override public List<TopicDto> findTopicsByEndpointGroupId(String endpointGroupId) { validateId(endpointGroupId, "Can't find topics. Invalid endpoint group id " + endpointGroupId); EndpointGroup endpointGroup = endpointGroupDao.findById(endpointGroupId); Set<Topic> topics = endpointGroup.getTopics(); return convertDtoList(topics); }
@Test public void removeTopicFromEndpointGroup() { Topic first = generateTopic(null, null, "first"); Topic second = generateTopic(first.getApplication(), null, "second"); Topic third = generateTopic(first.getApplication(), null, "third"); Set<Topic> topics = new HashSet<>(); topics.add(first); topics.add(second); topics.add(third); EndpointGroup endpointGroup = generateEndpointGroup(first.getApplication(), topics); Assert.assertEquals(3, endpointGroup.getTopics().size()); endpointGroupDao.removeTopicFromEndpointGroup(endpointGroup.getId().toString(), first.getId().toString()); EndpointGroup group = endpointGroupDao.removeTopicFromEndpointGroup(endpointGroup.getId().toString(), second.getId().toString()); Set<Topic> expected = new HashSet<>(); expected.add(third); Assert.assertEquals(expected, group.getTopics()); }
@Test public void addTopicToEndpointGroup() { Topic first = generateTopic(null, null, "first"); Topic second = generateTopic(first.getApplication(), null, "second"); Topic third = generateTopic(first.getApplication(), null, "third"); Set<Topic> topics = new HashSet<>(); topics.add(first); topics.add(second); topics.add(third); EndpointGroup endpointGroup = generateEndpointGroup(first.getApplication(), topics); endpointGroupDao.addTopicToEndpointGroup(endpointGroup.getId().toString(), first.getId().toString()); endpointGroupDao.addTopicToEndpointGroup(endpointGroup.getId().toString(), second.getId().toString()); EndpointGroup saved = endpointGroupDao.addTopicToEndpointGroup(endpointGroup.getId().toString(), third.getId().toString()); Assert.assertNotNull(saved); Assert.assertEquals(topics, saved.getTopics()); }