Search in sources :

Example 1 with UserProperty

use of com.thinkbiganalytics.feedmgr.rest.model.UserProperty in project kylo by Teradata.

the class FeedModelTransform method domainToFeedMetadata.

/**
 * Transforms the specified Metadata feed to a Feed Manager feed.
 *
 * @param domain       the Metadata feed
 * @param userFieldMap cache map from category to user-defined fields, or {@code null}
 * @return the Feed Manager feed
 */
@Nonnull
private FeedMetadata domainToFeedMetadata(@Nonnull final Feed domain, @Nullable final Map<Category, Set<UserFieldDescriptor>> userFieldMap) {
    FeedMetadata feed = deserializeFeedMetadata(domain, false);
    feed.setId(domain.getId().toString());
    feed.setFeedId(domain.getId().toString());
    feed.setFeedName(domain.getDisplayName());
    feed.setSystemFeedName(domain.getName());
    feed.setDescription(domain.getDescription());
    feed.setAllowIndexing(domain.isAllowIndexing());
    feed.setHistoryReindexingStatus(domain.getCurrentHistoryReindexingStatus().getHistoryReindexingState().toString());
    feed.setOwner(domain.getOwner() != null ? new User(domain.getOwner().getName()) : null);
    if (domain.getCreatedTime() != null) {
        feed.setCreateDate(domain.getCreatedTime().toDate());
    }
    if (domain.getModifiedTime() != null) {
        feed.setUpdateDate(domain.getModifiedTime().toDate());
    }
    FeedManagerTemplate template = domain.getTemplate();
    if (template != null) {
        RegisteredTemplate registeredTemplate = templateModelTransform.DOMAIN_TO_REGISTERED_TEMPLATE.apply(template);
        feed.setRegisteredTemplate(registeredTemplate);
        feed.setTemplateId(registeredTemplate.getId());
        feed.setTemplateName(registeredTemplate.getTemplateName());
    }
    Category category = domain.getCategory();
    if (category != null) {
        feed.setCategory(categoryModelTransform.domainToFeedCategorySimple(category));
    }
    feed.setState(domain.getState() != null ? domain.getState().name() : null);
    feed.setVersionName(domain.getVersionName() != null ? domain.getVersionName() : null);
    // Set user-defined properties
    final Set<UserFieldDescriptor> userFields;
    if (userFieldMap == null) {
        userFields = getUserFields(category);
    } else if (userFieldMap.containsKey(category)) {
        userFields = userFieldMap.get(category);
    } else {
        userFields = getUserFields(category);
        userFieldMap.put(category, userFields);
    }
    @SuppressWarnings("unchecked") final Set<UserProperty> userProperties = UserPropertyTransform.toUserProperties(domain.getUserProperties(), userFields);
    feed.setUserProperties(userProperties);
    // Convert JCR securitygroup to DTO
    List<com.thinkbiganalytics.feedmgr.rest.model.HadoopSecurityGroup> restSecurityGroups = new ArrayList<>();
    if (domain.getSecurityGroups() != null && domain.getSecurityGroups().size() > 0) {
        for (Object group : domain.getSecurityGroups()) {
            HadoopSecurityGroup hadoopSecurityGroup = (HadoopSecurityGroup) group;
            com.thinkbiganalytics.feedmgr.rest.model.HadoopSecurityGroup restSecurityGroup = new com.thinkbiganalytics.feedmgr.rest.model.HadoopSecurityGroup();
            restSecurityGroup.setDescription(hadoopSecurityGroup.getDescription());
            restSecurityGroup.setId(hadoopSecurityGroup.getGroupId());
            restSecurityGroup.setName(hadoopSecurityGroup.getName());
            restSecurityGroups.add(restSecurityGroup);
        }
    }
    feed.setSecurityGroups(restSecurityGroups);
    feed.setTags(domain.getTags().stream().map(name -> new DefaultTag(name)).collect(Collectors.toList()));
    if (domain.getUsedByFeeds() != null) {
        final List<FeedSummary> usedByFeeds = domain.getUsedByFeeds().stream().map(this::domainToFeedSummary).collect(Collectors.toList());
        feed.setUsedByFeeds(usedByFeeds);
    }
    // add in access control items
    securityTransform.applyAccessControl(domain, feed);
    return feed;
}
Also used : User(com.thinkbiganalytics.security.rest.model.User) Category(com.thinkbiganalytics.metadata.api.category.Category) FeedCategory(com.thinkbiganalytics.feedmgr.rest.model.FeedCategory) UserFieldDescriptor(com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) ArrayList(java.util.ArrayList) HadoopSecurityGroup(com.thinkbiganalytics.metadata.api.security.HadoopSecurityGroup) JcrHadoopSecurityGroup(com.thinkbiganalytics.metadata.modeshape.security.JcrHadoopSecurityGroup) UserProperty(com.thinkbiganalytics.feedmgr.rest.model.UserProperty) RegisteredTemplate(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate) FeedManagerTemplate(com.thinkbiganalytics.metadata.api.template.FeedManagerTemplate) DefaultTag(com.thinkbiganalytics.discovery.model.DefaultTag) FeedSummary(com.thinkbiganalytics.feedmgr.rest.model.FeedSummary) Nonnull(javax.annotation.Nonnull)

Example 2 with UserProperty

use of com.thinkbiganalytics.feedmgr.rest.model.UserProperty in project kylo by Teradata.

the class UserPropertyTransform method toUserProperties.

/**
 * Transforms the specified Metadata user-defined properties to Feed Manager user-defined properties.
 *
 * @param properties map of property names to values
 * @param userFields the user-defined field descriptors
 * @return the set of Feed Manager user-defined properties
 */
@Nonnull
@SafeVarargs
public static Set<UserProperty> toUserProperties(@Nonnull final Map<String, String> properties, @Nonnull final Set<UserFieldDescriptor>... userFields) {
    // Map field names and order
    int order = 0;
    int size = Arrays.stream(userFields).collect(Collectors.summingInt(Set::size));
    final Map<String, UserFieldDescriptor> userFieldMap = Maps.newHashMapWithExpectedSize(size);
    final Map<String, Integer> userFieldOrder = Maps.newHashMapWithExpectedSize(size);
    for (final Set<UserFieldDescriptor> set : userFields) {
        final Iterator<UserFieldDescriptor> sorted = set.stream().sorted((f1, f2) -> Integer.compare(f1.getOrder(), f2.getOrder())).iterator();
        while (sorted.hasNext()) {
            final UserFieldDescriptor field = sorted.next();
            if (!userFieldMap.containsKey(field.getSystemName())) {
                userFieldMap.put(field.getSystemName(), field);
                userFieldOrder.put(field.getSystemName(), order++);
            }
        }
    }
    // Convert from Metadata to Feed Manager format
    final Stream<UserProperty> newProperties = userFieldMap.values().stream().filter(field -> !properties.containsKey(field.getSystemName())).map(field -> {
        final UserProperty property = new UserProperty();
        property.setDescription(field.getDescription());
        property.setDisplayName(field.getDisplayName());
        property.setLocked(true);
        property.setOrder(userFieldOrder.get(field.getSystemName()));
        property.setRequired(field.isRequired());
        property.setSystemName(field.getSystemName());
        return property;
    });
    final Stream<UserProperty> existingProperties = properties.entrySet().stream().map(entry -> {
        // Create the Feed Manager property
        final UserProperty property = new UserProperty();
        property.setLocked(false);
        property.setSystemName(entry.getKey());
        property.setValue(entry.getValue());
        // Set additional Metadata attributes
        final UserFieldDescriptor field = userFieldMap.get(entry.getKey());
        if (field != null) {
            property.setDescription(field.getDescription());
            property.setDisplayName(field.getDisplayName());
            property.setLocked(true);
            property.setOrder(userFieldOrder.get(entry.getKey()));
            property.setRequired(field.isRequired());
        }
        // Return the Feed Manager property
        return property;
    });
    return Stream.concat(newProperties, existingProperties).collect(Collectors.toCollection(LinkedHashSet::new));
}
Also used : Arrays(java.util.Arrays) UserField(com.thinkbiganalytics.feedmgr.rest.model.UserField) Iterator(java.util.Iterator) MoreObjects(com.google.common.base.MoreObjects) Set(java.util.Set) UserFieldDescriptor(com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) UserProperty(com.thinkbiganalytics.feedmgr.rest.model.UserProperty) Objects(java.util.Objects) Stream(java.util.stream.Stream) Map(java.util.Map) Nonnull(javax.annotation.Nonnull) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) UserProperty(com.thinkbiganalytics.feedmgr.rest.model.UserProperty) UserFieldDescriptor(com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor) Nonnull(javax.annotation.Nonnull)

Example 3 with UserProperty

use of com.thinkbiganalytics.feedmgr.rest.model.UserProperty in project kylo by Teradata.

the class UserPropertyTransformTest method toUserProperties.

/**
 * Verify transforming property map to user properties.
 */
@Test
public void toUserProperties() {
    // Mock user field descriptors
    final UserFieldDescriptor field1 = Mockito.mock(UserFieldDescriptor.class);
    Mockito.when(field1.getDescription()).thenReturn("Global field 1");
    Mockito.when(field1.getDisplayName()).thenReturn("Test1");
    Mockito.when(field1.getOrder()).thenReturn(0);
    Mockito.when(field1.getSystemName()).thenReturn("field1");
    final UserFieldDescriptor field2 = Mockito.mock(UserFieldDescriptor.class);
    Mockito.when(field2.getDescription()).thenReturn("Global field 2");
    Mockito.when(field2.getDisplayName()).thenReturn("Test2");
    Mockito.when(field2.getOrder()).thenReturn(1);
    Mockito.when(field2.isRequired()).thenReturn(true);
    Mockito.when(field2.getSystemName()).thenReturn("field2");
    final UserFieldDescriptor field3 = Mockito.mock(UserFieldDescriptor.class);
    Mockito.when(field3.getDescription()).thenReturn("Category field 1");
    Mockito.when(field3.getDisplayName()).thenReturn("Overridden");
    Mockito.when(field3.getOrder()).thenReturn(0);
    Mockito.when(field3.isRequired()).thenReturn(true);
    Mockito.when(field3.getSystemName()).thenReturn("field1");
    final UserFieldDescriptor field4 = Mockito.mock(UserFieldDescriptor.class);
    Mockito.when(field4.getDescription()).thenReturn("Category field 2");
    Mockito.when(field4.getDisplayName()).thenReturn("Test4");
    Mockito.when(field4.getOrder()).thenReturn(0);
    Mockito.when(field4.getSystemName()).thenReturn("field4");
    // Verify user properties
    final Map<String, String> properties = ImmutableMap.of("field1", "one", "field2", "two", "field4", "three", "customField", "four");
    final Set<UserProperty> userProperties = UserPropertyTransform.toUserProperties(properties, ImmutableSet.of(field2, field1), ImmutableSet.of(field3, field4));
    Assert.assertEquals(4, userProperties.size());
    final UserProperty[] array = userProperties.toArray(new UserProperty[4]);
    Assert.assertEquals("Global field 1", array[0].getDescription());
    Assert.assertEquals("Test1", array[0].getDisplayName());
    Assert.assertTrue(array[0].isLocked());
    Assert.assertEquals(0, array[0].getOrder().intValue());
    Assert.assertFalse(array[0].isRequired());
    Assert.assertEquals("field1", array[0].getSystemName());
    Assert.assertEquals("one", array[0].getValue());
    Assert.assertEquals("Global field 2", array[1].getDescription());
    Assert.assertEquals("Test2", array[1].getDisplayName());
    Assert.assertTrue(array[1].isLocked());
    Assert.assertEquals(1, array[1].getOrder().intValue());
    Assert.assertTrue(array[1].isRequired());
    Assert.assertEquals("field2", array[1].getSystemName());
    Assert.assertEquals("two", array[1].getValue());
    Assert.assertEquals("Category field 2", array[2].getDescription());
    Assert.assertEquals("Test4", array[2].getDisplayName());
    Assert.assertTrue(array[2].isLocked());
    Assert.assertEquals(2, array[2].getOrder().intValue());
    Assert.assertFalse(array[2].isRequired());
    Assert.assertEquals("field4", array[2].getSystemName());
    Assert.assertEquals("three", array[2].getValue());
    Assert.assertNull(array[3].getDescription());
    Assert.assertNull(array[3].getDisplayName());
    Assert.assertFalse(array[3].isLocked());
    Assert.assertNull(array[3].getOrder());
    Assert.assertNull(array[3].isRequired());
    Assert.assertEquals("customField", array[3].getSystemName());
    Assert.assertEquals("four", array[3].getValue());
}
Also used : UserProperty(com.thinkbiganalytics.feedmgr.rest.model.UserProperty) UserFieldDescriptor(com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor) Test(org.junit.Test)

Example 4 with UserProperty

use of com.thinkbiganalytics.feedmgr.rest.model.UserProperty in project kylo by Teradata.

the class UserPropertyDeserializer method deserialize.

@Override
public Set<UserProperty> deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
    final ObjectCodec codec = (p.getCodec() != null) ? p.getCodec() : new ObjectMapper();
    final JsonNode node = codec.readTree(p);
    final Set<UserProperty> userProperties;
    if (node.isArray()) {
        userProperties = new HashSet<>(node.size());
        for (final JsonNode child : node) {
            userProperties.add(codec.treeToValue(child, UserProperty.class));
        }
    } else if (node.isObject()) {
        final Iterator<Map.Entry<String, JsonNode>> iter = node.fields();
        userProperties = new HashSet<>(node.size());
        while (iter.hasNext()) {
            final Map.Entry<String, JsonNode> element = iter.next();
            final UserProperty userProperty = new UserProperty();
            userProperty.setSystemName(element.getKey());
            userProperty.setValue(element.getValue().asText());
            userProperties.add(userProperty);
        }
    } else {
        throw new JsonParseException("Unsupported user property node: " + node, p.getCurrentLocation());
    }
    return userProperties;
}
Also used : UserProperty(com.thinkbiganalytics.feedmgr.rest.model.UserProperty) Iterator(java.util.Iterator) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Aggregations

UserProperty (com.thinkbiganalytics.feedmgr.rest.model.UserProperty)4 UserFieldDescriptor (com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor)3 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Nonnull (javax.annotation.Nonnull)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 MoreObjects (com.google.common.base.MoreObjects)1 Maps (com.google.common.collect.Maps)1 DefaultTag (com.thinkbiganalytics.discovery.model.DefaultTag)1 FeedCategory (com.thinkbiganalytics.feedmgr.rest.model.FeedCategory)1 FeedMetadata (com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata)1 FeedSummary (com.thinkbiganalytics.feedmgr.rest.model.FeedSummary)1 RegisteredTemplate (com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate)1 UserField (com.thinkbiganalytics.feedmgr.rest.model.UserField)1 Category (com.thinkbiganalytics.metadata.api.category.Category)1 HadoopSecurityGroup (com.thinkbiganalytics.metadata.api.security.HadoopSecurityGroup)1 FeedManagerTemplate (com.thinkbiganalytics.metadata.api.template.FeedManagerTemplate)1