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);
}
}
}
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);
}
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());
}
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);
}
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;
}
Aggregations