Search in sources :

Example 6 with Tag

use of com.hortonworks.registries.tag.Tag in project registry by hortonworks.

the class TagCatalogResource method makeTag.

private Tag makeTag(TagDto tagDto) {
    List<Tag> parentTags = new ArrayList<>();
    if (tagDto.getTagIds() != null) {
        for (Long tagId : tagDto.getTagIds()) {
            Tag tag = tagService.getTag(tagId);
            if (tag == null) {
                throw new IllegalArgumentException("Tag with id " + tagId + " does not exist.");
            }
            parentTags.add(tag);
        }
    }
    Tag tag = new Tag();
    tag.setId(tagDto.getId());
    tag.setName(tagDto.getName());
    tag.setDescription(tagDto.getDescription());
    tag.setTimestamp(tagDto.getTimestamp());
    tag.setTags(parentTags);
    return tag;
}
Also used : ArrayList(java.util.ArrayList) Tag(com.hortonworks.registries.tag.Tag)

Example 7 with Tag

use of com.hortonworks.registries.tag.Tag in project registry by hortonworks.

the class CatalogTagService method addTagsForStorable.

@Override
public void addTagsForStorable(TaggedEntity taggedEntity, List<Tag> tags) {
    if (tags != null) {
        for (Tag tag : tags) {
            TagStorableMapping tagStorable = new TagStorableMapping();
            tagStorable.setTagId(tag.getId());
            tagStorable.setStorableNamespace(taggedEntity.getNamespace());
            tagStorable.setStorableId(taggedEntity.getId());
            this.dao.add(tagStorable);
        }
    }
}
Also used : Tag(com.hortonworks.registries.tag.Tag) TagStorableMapping(com.hortonworks.registries.tag.TagStorableMapping)

Example 8 with Tag

use of com.hortonworks.registries.tag.Tag in project registry by hortonworks.

the class CatalogTagService method getTags.

@Override
public List<Tag> getTags(TaggedEntity taggedEntity) {
    List<Tag> tags = new ArrayList<>();
    QueryParam qp1 = new QueryParam(TagStorableMapping.FIELD_STORABLE_ID, String.valueOf(taggedEntity.getId()));
    QueryParam qp2 = new QueryParam(TagStorableMapping.FIELD_STORABLE_NAMESPACE, String.valueOf(taggedEntity.getNamespace()));
    for (TagStorableMapping mapping : listTagStorableMapping(ImmutableList.of(qp1, qp2))) {
        tags.add(getTag(mapping.getTagId()));
    }
    return tags;
}
Also used : QueryParam(com.hortonworks.registries.common.QueryParam) ArrayList(java.util.ArrayList) Tag(com.hortonworks.registries.tag.Tag) TagStorableMapping(com.hortonworks.registries.tag.TagStorableMapping)

Example 9 with Tag

use of com.hortonworks.registries.tag.Tag in project registry by hortonworks.

the class TagRestServiceTest method testTagResource.

@Test
public void testTagResource() throws Exception {
    TagClient tagClient = new TagClient(catalogRootUrl);
    long parentTagId = 10L;
    long childTagId = 11L;
    // create a "parent-tag"
    Tag parent = createTag(parentTagId, "parent-tag");
    Assert.assertTrue(tagClient.addTag(parent).getId() == parentTagId);
    // create a "child-tag" which is tagged under "parent-tag"
    Tag child = createTag(childTagId, "child-tag", ImmutableList.<Tag>of(parent));
    Assert.assertTrue(tagClient.addTag(child).getId() == childTagId);
    // update parent-tag
    parent = createTag(parentTagId, "parent-update-tag");
    Assert.assertTrue(tagClient.addOrUpdateTag(parent).getId() == parentTagId);
    // get a Tag by Id
    Tag tag = tagClient.getTag(parentTagId);
    Assert.assertTrue("Tag Id is different", tag.getId() == parentTagId);
    long unknownTagId = 100L;
    // get an unknown tag by Id
    try {
        tag = tagClient.getTag(unknownTagId);
        Assert.fail("This should have thrown an error as entity" + unknownTagId + " does not exist");
    } catch (Exception e) {
    }
    // add another tag
    Tag testTag = createTag(12L, "to-delete-tag");
    Assert.assertTrue(tagClient.addTag(testTag).getId() == 12L);
    // list all tags
    List<Tag> allTags = tagClient.listTags();
    Assert.assertTrue("tag count mismatch", allTags.size() == 3);
    // list tags with queryParams
    Map<String, Object> queryParams = new HashMap<>();
    queryParams.put("name", "child-tag");
    queryParams.put("description", "child-tag");
    allTags = tagClient.listTags(queryParams);
    Assert.assertTrue("tag count mismatch", allTags.size() == 1);
    // delete a tag
    tagClient.removeTag(12L);
    allTags = tagClient.listTags();
    Assert.assertTrue("count mismatch", allTags.size() == 2);
    // add Tag for Entity
    tagClient.addTagForEntity(new TaggedEntity("Device", 1L), parentTagId);
    tagClient.addTagForEntity(new TaggedEntity("Device", 2L), parentTagId);
    tagClient.addTagForEntity(new TaggedEntity("Device", 3L), parentTagId);
    // get All Entities For Tag
    List<TaggedEntity> allEntities = tagClient.getTaggedEntities(parentTagId);
    Assert.assertTrue("entity count mismatch", allEntities.size() == 3);
    // remove Tag for Entity
    tagClient.removeTagForEntity(new TaggedEntity("Device", 1L), parentTagId);
    allEntities = tagClient.getTaggedEntities(parentTagId);
    Assert.assertTrue("entity count mismatch", allEntities.size() == 2);
    // add new Tag to existing entity
    Tag newTag = createTag(13L, "new-tag");
    Assert.assertTrue(tagClient.addTag(newTag).getId() == 13L);
    tagClient.addTagForEntity(new TaggedEntity("Device", 2L), 13L);
    // get All Tags For a given Entity
    allTags = tagClient.getTags(new TaggedEntity("Device", 2L));
    Assert.assertTrue("tag count mismatch", allTags.size() == 2);
    // try adding unknown tag for a Entity
    try {
        tagClient.addTagForEntity(new TaggedEntity("Device", 1L), unknownTagId);
        Assert.fail("should have thrown error");
    } catch (RuntimeException e) {
    }
    // try removing unknown tag for a Entity
    try {
        tagClient.removeTagForEntity(new TaggedEntity("Device", 1L), unknownTagId);
        Assert.fail("should have thrown error");
    } catch (RuntimeException e) {
    }
}
Also used : TaggedEntity(com.hortonworks.registries.tag.TaggedEntity) TagClient(com.hortonworks.registries.tag.client.TagClient) HashMap(java.util.HashMap) Tag(com.hortonworks.registries.tag.Tag) Test(org.junit.Test) IntegrationTest(com.hortonworks.registries.common.test.IntegrationTest)

Example 10 with Tag

use of com.hortonworks.registries.tag.Tag in project registry by hortonworks.

the class TagRestServiceTest method createTag.

private Tag createTag(Long id, String name, List<Tag> parentTags) {
    Tag newTag = new Tag();
    newTag.setId(id);
    newTag.setName(name);
    newTag.setDescription(name);
    newTag.setTags(parentTags);
    return newTag;
}
Also used : Tag(com.hortonworks.registries.tag.Tag)

Aggregations

Tag (com.hortonworks.registries.tag.Tag)10 ArrayList (java.util.ArrayList)4 TagStorableMapping (com.hortonworks.registries.tag.TagStorableMapping)3 StorableKey (com.hortonworks.registries.storage.StorableKey)2 QueryParam (com.hortonworks.registries.common.QueryParam)1 IntegrationTest (com.hortonworks.registries.common.test.IntegrationTest)1 TaggedEntity (com.hortonworks.registries.tag.TaggedEntity)1 TagClient (com.hortonworks.registries.tag.client.TagClient)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1