Search in sources :

Example 1 with UserFieldDescriptor

use of com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor in project kylo by Teradata.

the class JcrPropertyUtil method setUserProperties.

/**
 * Sets the specified user-defined properties on the specified node.
 *
 * @param node       the target node
 * @param fields     the predefined user fields
 * @param properties the map of user-defined property names to values
 * @throws IllegalStateException       if a property name is encoded incorrectly
 * @throws MetadataRepositoryException if the metadata repository is unavailable
 */
public static void setUserProperties(@Nonnull final Node node, @Nonnull final Set<UserFieldDescriptor> fields, @Nonnull final Map<String, String> properties) {
    // Verify required properties are not empty
    for (final UserFieldDescriptor field : fields) {
        if (field.isRequired() && StringUtils.isEmpty(properties.get(field.getSystemName()))) {
            throw new MissingUserPropertyException("Missing required property: " + field.getSystemName());
        }
    }
    // Set properties on node
    final Set<String> newProperties = new HashSet<>(properties.size());
    final String prefix = JcrMetadataAccess.USR_PREFIX + ":";
    properties.forEach((key, value) -> {
        try {
            final String name = prefix + URLEncoder.encode(key, USER_PROPERTY_ENCODING);
            newProperties.add(name);
            node.setProperty(name, value);
        } catch (AccessDeniedException e) {
            log.debug("Access denied", e);
            throw new AccessControlException(e.getMessage());
        } catch (RepositoryException e) {
            throw new MetadataRepositoryException("Failed to set user property \"" + key + "\" on node: " + node, e);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e.toString(), e);
        }
    });
    // Get node properties
    final PropertyIterator iterator;
    try {
        iterator = node.getProperties();
    } catch (AccessDeniedException e) {
        log.debug("Access denied", e);
        throw new AccessControlException(e.getMessage());
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed to get properties for node: " + node, e);
    }
    // Remove properties from node
    while (iterator.hasNext()) {
        final Property property = iterator.nextProperty();
        try {
            final String name = property.getName();
            if (name.startsWith(prefix) && !newProperties.contains(name)) {
                property.remove();
            }
        } catch (AccessDeniedException e) {
            log.debug("Access denied", e);
            throw new AccessControlException(e.getMessage());
        } catch (RepositoryException e) {
            throw new MetadataRepositoryException("Failed to remove property \"" + property + "\" on node: " + node, e);
        }
    }
}
Also used : MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) AccessDeniedException(javax.jcr.AccessDeniedException) UserFieldDescriptor(com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor) JcrUserFieldDescriptor(com.thinkbiganalytics.metadata.modeshape.extension.JcrUserFieldDescriptor) PropertyIterator(javax.jcr.PropertyIterator) AccessControlException(java.security.AccessControlException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) MissingUserPropertyException(com.thinkbiganalytics.metadata.api.MissingUserPropertyException) Property(javax.jcr.Property) HashSet(java.util.HashSet)

Example 2 with UserFieldDescriptor

use of com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor in project kylo by Teradata.

the class JcrPropertyUtilTest method setUserProperties.

/**
 * Verify setting user properties.
 */
@Test
public void setUserProperties() throws Exception {
    // Mock node
    final Property oldProp = Mockito.when(Mockito.mock(Property.class).getName()).thenReturn("usr:oldProp").getMock();
    final Iterator<Property> delegateIter = ImmutableList.of(oldProp).iterator();
    final PropertyIterator propIter = Mockito.mock(PropertyIterator.class);
    Mockito.when(propIter.hasNext()).thenAnswer(invocation -> delegateIter.hasNext());
    Mockito.when(propIter.nextProperty()).thenAnswer(invocation -> delegateIter.next());
    final Node node = Mockito.mock(Node.class);
    Mockito.when(node.getProperties()).thenReturn(propIter);
    // Mock field
    final UserFieldDescriptor field = Mockito.mock(UserFieldDescriptor.class);
    Mockito.when(field.getSystemName()).thenReturn("testProp");
    Mockito.when(field.isRequired()).thenReturn(true);
    // Test setting user properties
    final Map<String, String> properties = ImmutableMap.of("testProp", "one", "碼標準萬國碼/1.1/?name=%20", "two");
    JcrPropertyUtil.setUserProperties(node, Collections.singleton(field), properties);
    Mockito.verify(node).setProperty("usr:testProp", "one");
    Mockito.verify(node).setProperty("usr:%E7%A2%BC%E6%A8%99%E6%BA%96%E8%90%AC%E5%9C%8B%E7%A2%BC%2F1.1%2F%3Fname%3D%2520", "two");
    Mockito.verify(node).getProperties();
    Mockito.verify(oldProp).remove();
    Mockito.verifyNoMoreInteractions(node);
}
Also used : UserFieldDescriptor(com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property) Test(org.junit.Test)

Example 3 with UserFieldDescriptor

use of com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor in project kylo by Teradata.

the class JcrPropertyUtilTest method setUserPropertiesWithMissing.

/**
 * Verify exception if a required property is not set.
 */
@Test(expected = MissingUserPropertyException.class)
public void setUserPropertiesWithMissing() {
    // Mock field
    final UserFieldDescriptor field = Mockito.mock(UserFieldDescriptor.class);
    Mockito.when(field.getSystemName()).thenReturn("requiredProperty");
    Mockito.when(field.isRequired()).thenReturn(true);
    // Test required property
    JcrPropertyUtil.setUserProperties(Mockito.mock(Node.class), Collections.singleton(field), Collections.emptyMap());
}
Also used : UserFieldDescriptor(com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor) Node(javax.jcr.Node) Test(org.junit.Test)

Example 4 with UserFieldDescriptor

use of com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor in project kylo by Teradata.

the class DefaultFeedManagerCategoryService method saveCategory.

@Override
public void saveCategory(final FeedCategory feedCategory) {
    boolean isNew = feedCategory.getId() == null;
    // Perform the rest of the updates as the current user.
    Category.ID updateId = metadataAccess.commit(() -> {
        this.accessController.checkPermission(AccessController.SERVICES, FeedServicesAccessControl.EDIT_CATEGORIES);
        if (feedCategory.getId() != null) {
            final Category.ID domainId = feedCategory.getId() != null ? categoryProvider.resolveId(feedCategory.getId()) : null;
            final FeedCategory oldCategory = getCategoryById(feedCategory.getId());
            if (oldCategory != null && !oldCategory.getSystemName().equalsIgnoreCase(feedCategory.getSystemName())) {
                // system names have changed, only regenerate the system name if there are no related feeds
                if (oldCategory.getRelatedFeeds() == 0) {
                    categoryProvider.renameSystemName(domainId, feedCategory.getSystemName());
                }
            }
        }
        // Update the domain entity
        final Category domainCategory = categoryProvider.update(categoryModelTransform.feedCategoryToDomain(feedCategory));
        // Repopulate identifier
        feedCategory.setId(domainCategory.getId().toString());
        // TODO only do this when modifying the access control
        if (domainCategory.getAllowedActions().hasPermission(CategoryAccessControl.CHANGE_PERMS)) {
            feedCategory.toRoleMembershipChangeList().stream().forEach(roleMembershipChange -> securityService.changeCategoryRoleMemberships(feedCategory.getId(), roleMembershipChange));
            feedCategory.toFeedRoleMembershipChangeList().stream().forEach(roleMembershipChange -> securityService.changeCategoryFeedRoleMemberships(feedCategory.getId(), roleMembershipChange));
        }
        // Update user-defined fields
        final Set<UserFieldDescriptor> userFields = (feedCategory.getUserFields() != null) ? UserPropertyTransform.toUserFieldDescriptors(feedCategory.getUserFields()) : Collections.emptySet();
        categoryProvider.setFeedUserFields(domainCategory.getId(), userFields);
        return domainCategory.getId();
    });
    notifyCategoryChange(updateId, feedCategory.getSystemName(), isNew ? MetadataChange.ChangeType.CREATE : MetadataChange.ChangeType.UPDATE);
}
Also used : Category(com.thinkbiganalytics.metadata.api.category.Category) FeedCategory(com.thinkbiganalytics.feedmgr.rest.model.FeedCategory) FeedCategory(com.thinkbiganalytics.feedmgr.rest.model.FeedCategory) UserFieldDescriptor(com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor)

Example 5 with UserFieldDescriptor

use of com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor 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)

Aggregations

UserFieldDescriptor (com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor)8 FeedCategory (com.thinkbiganalytics.feedmgr.rest.model.FeedCategory)3 UserProperty (com.thinkbiganalytics.feedmgr.rest.model.UserProperty)3 Category (com.thinkbiganalytics.metadata.api.category.Category)3 Nonnull (javax.annotation.Nonnull)3 Test (org.junit.Test)3 DefaultTag (com.thinkbiganalytics.discovery.model.DefaultTag)2 FeedMetadata (com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata)2 RegisteredTemplate (com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate)2 HadoopSecurityGroup (com.thinkbiganalytics.metadata.api.security.HadoopSecurityGroup)2 FeedManagerTemplate (com.thinkbiganalytics.metadata.api.template.FeedManagerTemplate)2 JcrHadoopSecurityGroup (com.thinkbiganalytics.metadata.modeshape.security.JcrHadoopSecurityGroup)2 ArrayList (java.util.ArrayList)2 Node (javax.jcr.Node)2 Property (javax.jcr.Property)2 PropertyIterator (javax.jcr.PropertyIterator)2 MoreObjects (com.google.common.base.MoreObjects)1 Maps (com.google.common.collect.Maps)1 Tag (com.thinkbiganalytics.discovery.schema.Tag)1 FeedSummary (com.thinkbiganalytics.feedmgr.rest.model.FeedSummary)1