use of org.apache.commons.lang3.StringUtils.substringAfterLast in project cas by apereo.
the class ConfigurationMetadataGenerator method processHints.
private static Set<ConfigurationMetadataHint> processHints(final Collection<ConfigurationMetadataProperty> props, final Collection<ConfigurationMetadataProperty> groups) {
var hints = new LinkedHashSet<ConfigurationMetadataHint>(0);
val allValidProps = props.stream().filter(p -> p.getDeprecation() == null || !Deprecation.Level.ERROR.equals(p.getDeprecation().getLevel())).collect(Collectors.toList());
for (val entry : allValidProps) {
try {
val propName = StringUtils.substringAfterLast(entry.getName(), ".");
val groupName = StringUtils.substringBeforeLast(entry.getName(), ".");
val grp = groups.stream().filter(g -> g.getName().equalsIgnoreCase(groupName)).findFirst().orElseThrow(() -> new IllegalArgumentException("Cant locate group " + groupName));
val matcher = PATTERN_GENERICS.matcher(grp.getType());
val className = matcher.find() ? matcher.group(1) : grp.getType();
val clazz = ClassUtils.getClass(className);
val hint = new ConfigurationMetadataHint();
hint.setName(entry.getName());
val annotation = Arrays.stream(clazz.getAnnotations()).filter(a -> a.annotationType().equals(RequiresModule.class)).findFirst().map(RequiresModule.class::cast).orElseThrow(() -> new RuntimeException(clazz.getCanonicalName() + " is missing @RequiresModule"));
val valueHint = new ValueHint();
valueHint.setValue(toJson(Map.of("module", annotation.name(), "automated", annotation.automated())));
valueHint.setDescription(RequiresModule.class.getName());
hint.getValues().add(valueHint);
val grpHint = new ValueHint();
grpHint.setValue(toJson(Map.of("owner", clazz.getCanonicalName())));
grpHint.setDescription(PropertyOwner.class.getName());
hint.getValues().add(grpHint);
val names = RelaxedPropertyNames.forCamelCase(propName);
names.getValues().forEach(Unchecked.consumer(name -> {
val f = ReflectionUtils.findField(clazz, name);
if (f != null && f.isAnnotationPresent(RequiredProperty.class)) {
val propertyHint = new ValueHint();
propertyHint.setValue(toJson(Map.of("owner", clazz.getName())));
propertyHint.setDescription(RequiredProperty.class.getName());
hint.getValues().add(propertyHint);
}
if (f != null && f.isAnnotationPresent(DurationCapable.class)) {
val propertyHint = new ValueHint();
propertyHint.setDescription(DurationCapable.class.getName());
propertyHint.setValue(toJson(List.of(DurationCapable.class.getName())));
hint.getValues().add(propertyHint);
}
if (f != null && f.isAnnotationPresent(ExpressionLanguageCapable.class)) {
val propertyHint = new ValueHint();
propertyHint.setDescription(ExpressionLanguageCapable.class.getName());
propertyHint.setValue(toJson(List.of(ExpressionLanguageCapable.class.getName())));
hint.getValues().add(propertyHint);
}
}));
if (!hint.getValues().isEmpty()) {
hints.add(hint);
}
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
return hints;
}
use of org.apache.commons.lang3.StringUtils.substringAfterLast in project ddf by codice.
the class DocumentationTest method testBrokenAnchorsPresent.
@Test
public void testBrokenAnchorsPresent() throws IOException, URISyntaxException {
List<Path> docs = Files.list(getPath()).filter(f -> f.toString().endsWith(HTML_DIRECTORY)).collect(Collectors.toList());
Set<String> links = new HashSet<>();
Set<String> anchors = new HashSet<>();
for (Path path : docs) {
Document doc = Jsoup.parse(path.toFile(), "UTF-8", EMPTY_STRING);
String thisDoc = StringUtils.substringAfterLast(path.toString(), File.separator);
Elements elements = doc.body().getAllElements();
for (Element element : elements) {
if (!element.toString().contains(":") && StringUtils.substringBetween(element.toString(), HREF_ANCHOR, CLOSE) != null) {
links.add(thisDoc + "#" + StringUtils.substringBetween(element.toString(), HREF_ANCHOR, CLOSE));
}
anchors.add(thisDoc + "#" + StringUtils.substringBetween(element.toString(), ID, CLOSE));
}
}
links.removeAll(anchors);
assertThat("Anchors missing section reference: " + links.toString(), links.isEmpty());
}
use of org.apache.commons.lang3.StringUtils.substringAfterLast in project kylo by Teradata.
the class DefaultServiceLevelAgreementService method saveAndScheduleSla.
/**
* In order to Save an SLA if it is related to a Feed(s) the user needs to have EDIT_DETAILS permission on the Feed(s)
*
* @param serviceLevelAgreement the sla to save
* @param feed an option Feed to relate to this SLA. If this is not present the related feeds are also embedded in the SLA policies. The Feed is a pointer access to the current
* feed the user is editing if they are creating an SLA from the Feed Details page. If creating an SLA from the main SLA page the feed property will not be populated.
*/
private ServiceLevelAgreement saveAndScheduleSla(ServiceLevelAgreementGroup serviceLevelAgreement, FeedMetadata feed) {
// ensure user has permissions to edit the SLA
if (serviceLevelAgreement != null) {
ServiceLevelAgreementMetricTransformerHelper transformer = new ServiceLevelAgreementMetricTransformerHelper();
// Read the feeds on the SLA as a Service. Then verify the current user has access to edit these feeds
List<String> feedsOnSla = metadataAccess.read(() -> {
List<String> feedIds = new ArrayList<>();
// all referencing Feeds
List<String> systemCategoryAndFeedNames = transformer.getCategoryFeedNames(serviceLevelAgreement);
for (String categoryAndFeed : systemCategoryAndFeedNames) {
// fetch and update the reference to the sla
String categoryName = StringUtils.trim(StringUtils.substringBefore(categoryAndFeed, "."));
String feedName = StringUtils.trim(StringUtils.substringAfterLast(categoryAndFeed, "."));
Feed feedEntity = feedProvider.findBySystemName(categoryName, feedName);
if (feedEntity != null) {
feedIds.add(feedEntity.getId().toString());
}
}
return feedIds;
}, MetadataAccess.SERVICE);
boolean allowedToEdit = feedsOnSla.isEmpty() ? true : feedsOnSla.stream().allMatch(feedId -> feedManagerFeedService.checkFeedPermission(feedId, FeedAccessControl.EDIT_DETAILS));
if (allowedToEdit) {
return metadataAccess.commit(() -> {
// Re read back in the Feeds for this session
Set<Feed> slaFeeds = new HashSet<Feed>();
Set<Feed.ID> slaFeedIds = new HashSet<Feed.ID>();
feedsOnSla.stream().forEach(feedId -> {
Feed feedEntity = feedProvider.findById(feedProvider.resolveId(feedId));
if (feedEntity != null) {
slaFeeds.add(feedEntity);
slaFeedIds.add(feedEntity.getId());
}
});
if (feed != null) {
feedManagerFeedService.checkFeedPermission(feed.getId(), FeedAccessControl.EDIT_DETAILS);
}
if (feed != null) {
transformer.applyFeedNameToCurrentFeedProperties(serviceLevelAgreement, feed.getCategory().getSystemName(), feed.getSystemFeedName());
}
ServiceLevelAgreement sla = transformer.getServiceLevelAgreement(serviceLevelAgreement);
ServiceLevelAgreementBuilder slaBuilder = null;
com.thinkbiganalytics.metadata.sla.api.ServiceLevelAgreement.ID existingId = null;
if (StringUtils.isNotBlank(sla.getId())) {
existingId = slaProvider.resolve(sla.getId());
}
if (existingId != null) {
slaBuilder = slaProvider.builder(existingId);
} else {
slaBuilder = slaProvider.builder();
}
slaBuilder.name(sla.getName()).description(sla.getDescription());
for (com.thinkbiganalytics.metadata.rest.model.sla.ObligationGroup group : sla.getGroups()) {
ObligationGroupBuilder groupBuilder = slaBuilder.obligationGroupBuilder(ObligationGroup.Condition.valueOf(group.getCondition()));
for (Obligation o : group.getObligations()) {
groupBuilder.obligationBuilder().metric(o.getMetrics()).description(o.getDescription()).build();
}
groupBuilder.build();
}
com.thinkbiganalytics.metadata.sla.api.ServiceLevelAgreement savedSla = slaBuilder.build();
List<ServiceLevelAgreementActionConfiguration> actions = transformer.getActionConfigurations(serviceLevelAgreement);
// now assign the sla checks
slaProvider.slaCheckBuilder(savedSla.getId()).removeSlaChecks().actionConfigurations(actions).build();
// relate them
Set<Feed.ID> feedIds = new HashSet<>();
FeedServiceLevelAgreementRelationship feedServiceLevelAgreementRelationship = feedSlaProvider.relateFeeds(savedSla, slaFeeds);
if (feedServiceLevelAgreementRelationship != null && feedServiceLevelAgreementRelationship.getFeeds() != null) {
feedIds = feedServiceLevelAgreementRelationship.getFeeds().stream().map(f -> f.getId()).collect(Collectors.toSet());
}
Set<VelocityTemplate.ID> velocityTemplates = findVelocityTemplates(serviceLevelAgreement);
// Update the JPA mapping in Ops Manager for this SLA and its related Feeds
serviceLevelAgreementDescriptionProvider.updateServiceLevelAgreement(savedSla.getId(), savedSla.getName(), savedSla.getDescription(), feedIds, velocityTemplates);
com.thinkbiganalytics.metadata.rest.model.sla.FeedServiceLevelAgreement restModel = serviceLevelAgreementTransform.toModel(savedSla, slaFeeds, true);
// schedule it
serviceLevelAgreementScheduler.scheduleServiceLevelAgreement(savedSla);
return restModel;
});
}
}
return null;
}
use of org.apache.commons.lang3.StringUtils.substringAfterLast in project kylo by Teradata.
the class DerivedDatasourceFactory method ensureDataTransformationSourceDatasources.
/**
* Builds the list of data sources for the specified data transformation feed.
*
* @param feed the feed
* @return the list of data sources
* @throws NullPointerException if the feed has no data transformation
*/
@Nonnull
private Set<Datasource.ID> ensureDataTransformationSourceDatasources(@Nonnull final FeedMetadata feed) {
final Set<String> dataSetIds = new HashSet<>();
final Set<Datasource.ID> datasources = new HashSet<>();
if (feed.getSourceDataSets() != null) {
List<String> datasetIds = feed.getSourceDataSets().stream().map(ds -> ds.getId()).collect(Collectors.toList());
dataSetIds.addAll(datasetIds);
}
final List<String> catalogSources = feed.getDataTransformation().getCatalogDataSourceIds() != null ? feed.getDataTransformation().getCatalogDataSourceIds() : new ArrayList<>();
// Extract nodes in chart view model
@SuppressWarnings("unchecked") final Stream<Map<String, Object>> nodes = Optional.ofNullable(feed.getDataTransformation().getChartViewModel()).map(model -> (List<Map<String, Object>>) model.get("nodes")).map(Collection::stream).orElse(Stream.empty());
// Create a data source for each node
final DatasourceDefinition hiveDefinition = datasourceDefinitionProvider.findByProcessorType(DATA_TRANSFORMATION_HIVE_DEFINITION);
final DatasourceDefinition jdbcDefinition = datasourceDefinitionProvider.findByProcessorType(DATA_TRANSFORMATION_JDBC_DEFINITION);
nodes.forEach(node -> {
// Filter data sets
if (!StringUtils.equalsAnyIgnoreCase((String) node.get("datasourceId"), null, "HIVE") && node.get("dataset") != null && !Objects.equals(node.get("datasetMatchesUserDataSource"), Boolean.TRUE)) {
dataSetIds.add((String) node.get("datasourceId"));
return;
}
// Extract properties from node
DatasourceDefinition datasourceDefinition = null;
final Map<String, String> properties = new HashMap<>();
String userDatasourceId = (String) node.get("datasourceId");
if ((userDatasourceId == null && node.get("dataset") == null) || (userDatasourceId != null && userDatasourceId.equalsIgnoreCase("HIVE"))) {
final String name = (String) node.get("name");
datasourceDefinition = hiveDefinition;
properties.put(HIVE_SCHEMA_KEY, StringUtils.trim(StringUtils.substringBefore(name, ".")));
properties.put(HIVE_TABLE_KEY, StringUtils.trim(StringUtils.substringAfterLast(name, ".")));
} else if (userDatasourceId != null) {
final Datasource datasource = datasourceProvider.getDatasource(datasourceProvider.resolve(userDatasourceId));
if (datasource != null) {
datasourceDefinition = jdbcDefinition;
properties.put(JDBC_CONNECTION_KEY, datasource.getName());
properties.put(JDBC_TABLE_KEY, (String) node.get("name"));
properties.putAll(parseDataTransformControllerServiceProperties(datasourceDefinition, datasource.getName()));
}
}
if (datasourceDefinition != null) {
// Create the derived data source
final String identityString = propertyExpressionResolver.resolveVariables(datasourceDefinition.getIdentityString(), properties);
final String title = datasourceDefinition.getTitle() != null ? propertyExpressionResolver.resolveVariables(datasourceDefinition.getTitle(), properties) : identityString;
final String desc = propertyExpressionResolver.resolveVariables(datasourceDefinition.getDescription(), properties);
final DerivedDatasource datasource = datasourceProvider.ensureDerivedDatasource(datasourceDefinition.getDatasourceType(), identityString, title, desc, new HashMap<>(properties));
datasources.add(datasource.getId());
}
});
// Build the data sources from the data source ids
if (dataSetIds.isEmpty()) {
final List<String> datasourceIds = Optional.ofNullable(feed.getDataTransformation()).map(FeedDataTransformation::getDatasourceIds).orElse(Collections.emptyList());
datasourceIds.stream().filter(id -> !dataSetIds.contains(id) && !catalogSources.contains(id)).map(datasourceProvider::resolve).forEach(datasources::add);
}
return datasources;
}
use of org.apache.commons.lang3.StringUtils.substringAfterLast in project data-prep by Talend.
the class ResourceLoaderContentCache method getResource.
private DeletableResource getResource(ContentCacheKey key) {
try {
final DeletableResource[] patternMatches = resolver.getResources(CACHE_PREFIX + key.getKey() + "*");
final DeletableResource[] directMatches = resolver.getResources(CACHE_PREFIX + key.getKey());
final DeletableResource[] resources = new DeletableResource[patternMatches.length + directMatches.length];
System.arraycopy(patternMatches, 0, resources, 0, patternMatches.length);
System.arraycopy(directMatches, 0, resources, patternMatches.length, directMatches.length);
if (resources.length <= 0) {
return null;
} else {
// resources.length > 0
final Optional<DeletableResource> reduce = stream(resources).reduce(maxBy((r1, r2) -> {
final String suffix1 = substringAfterLast(r1.getFilename(), ".");
final String suffix2 = substringAfterLast(r2.getFilename(), ".");
if (StringUtils.isEmpty(suffix1) || StringUtils.isEmpty(suffix2)) {
return 0;
}
final long i1 = parseLong(suffix1);
final long i2 = parseLong(suffix2);
return Long.compare(i1, i2);
}));
return reduce.filter(r -> {
if (!r.exists()) {
return false;
}
final String suffix = StringUtils.substringAfterLast(r.getFilename(), ".");
if (NumberUtils.isCreatable(suffix)) {
final long time = parseLong(suffix);
return time > System.currentTimeMillis();
} else {
return true;
}
}).orElse(null);
}
} catch (IOException e) {
throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
}
}
Aggregations