Search in sources :

Example 1 with MissingUserPropertyException

use of com.thinkbiganalytics.metadata.api.MissingUserPropertyException 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 MissingUserPropertyException

use of com.thinkbiganalytics.metadata.api.MissingUserPropertyException 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, boolean enforceRequired) {
    // Verify required properties are not empty
    for (final UserFieldDescriptor field : fields) {
        if (enforceRequired && 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)

Aggregations

MissingUserPropertyException (com.thinkbiganalytics.metadata.api.MissingUserPropertyException)2 UserFieldDescriptor (com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor)2 MetadataRepositoryException (com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException)2 JcrUserFieldDescriptor (com.thinkbiganalytics.metadata.modeshape.extension.JcrUserFieldDescriptor)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 AccessControlException (java.security.AccessControlException)2 HashSet (java.util.HashSet)2 AccessDeniedException (javax.jcr.AccessDeniedException)2 Property (javax.jcr.Property)2 PropertyIterator (javax.jcr.PropertyIterator)2 RepositoryException (javax.jcr.RepositoryException)2