Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
Bug 1439343 - Overload expression to accept a 'baseResource' type to …
Browse files Browse the repository at this point in the history
…use instead

of the topmost ancestor.
  • Loading branch information
josejulio authored and spinder committed Aug 24, 2017
1 parent 729ebdf commit ba77f34
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 19 deletions.
Expand Up @@ -91,6 +91,58 @@ public static Resource getBaseServerOrService(Resource resource) {
return current;
}

/**
* Returns first ancestor whose type is parentType.
*
* @param resource
* @param parentType Type of the ancestor needed.
* @return The ancestor of resource whose type is parentType.
* @throws IllegalArgumentException if <code>resource</code> or <code>parentType</code> is null
*/
public static Resource getAncestorResourceOfType(Resource resource, String parentType) {
if (resource == null) {
throw new IllegalArgumentException("resource is null");
}
if (parentType == null) {
throw new IllegalArgumentException("parentType is null");
}
while (true) {
if (resource.getResourceType().getName().equals(parentType)) {
return resource;
}
resource = resource.getParentResource();
if (resource == null) {
return null;
}
}
}

/**
* Returns true if <code>possibleAncestor</code> is an ancestor of <code>resource</code> or itself.
* false in other case.
*
* @param resource
* @param possibleAncestor possible ancestor of <code>resource</code>.
* @return true if <code>possibleAncestor</code> is an ancestor of <code>resource</code> or itself, false in other case.
*/
public static boolean isAncestor(Resource resource, Resource possibleAncestor) {
if (resource == null) {
throw new IllegalArgumentException("resource is null");
}
if (possibleAncestor == null) {
throw new IllegalArgumentException("possibleAncestor is null");
}
while(true) {
if (resource.equals(possibleAncestor)) {
return true;
}
resource = resource.getParentResource();
if (resource == null) {
return false;
}
}
}

private ResourceUtility() {
// Defensive
}
Expand Down
Expand Up @@ -2570,12 +2570,12 @@ public PageList<GroupPluginConfigurationUpdate> findGroupPluginConfigurationUpda
public ConfigurationDefinition getOptionsForConfigurationDefinition(Subject subject, int resourceId,
int parentResourceId, ConfigurationDefinition def) {

Resource resource = null, baseResource = null;
Resource resource = null, baseResource = null, parentResource = null;
if (resourceId >= 0) {
resource = resourceManager.getResource(subject, resourceId);
}
if (parentResourceId >= 0) {
Resource parentResource = resourceManager.getResource(subject, parentResourceId);
parentResource = resourceManager.getResource(subject, parentResourceId);
baseResource = ResourceUtility.getBaseServerOrService(parentResource);
} else if (resource != null) {
baseResource = ResourceUtility.getBaseServerOrService(resource);
Expand All @@ -2586,19 +2586,19 @@ public ConfigurationDefinition getOptionsForConfigurationDefinition(Subject subj

if (pd instanceof PropertyDefinitionSimple) {
PropertyDefinitionSimple pds = (PropertyDefinitionSimple) pd;
handlePDS(subject, resource, baseResource, pds);
handlePDS(subject, resource, parentResource, baseResource, pds);

} else if (pd instanceof PropertyDefinitionList) {
PropertyDefinitionList pdl = (PropertyDefinitionList) pd;
PropertyDefinition memberDef = pdl.getMemberDefinition();
if (memberDef instanceof PropertyDefinitionSimple) {
PropertyDefinitionSimple pds = (PropertyDefinitionSimple) memberDef;
handlePDS(subject, resource, baseResource, pds);
handlePDS(subject, resource, parentResource, baseResource, pds);
} else if (memberDef instanceof PropertyDefinitionMap) {
PropertyDefinitionMap pdm = (PropertyDefinitionMap) memberDef;
for (PropertyDefinition inner : pdm.getOrderedPropertyDefinitions()) {
if (inner instanceof PropertyDefinitionSimple) {
handlePDS(subject, resource, baseResource, (PropertyDefinitionSimple) inner);
handlePDS(subject, resource, parentResource, baseResource, (PropertyDefinitionSimple) inner);
}
if (LOG.isDebugEnabled()) {
LOG.debug("3 ____[ " + inner.toString() + " in " + pdl.toString()
Expand All @@ -2616,7 +2616,7 @@ public ConfigurationDefinition getOptionsForConfigurationDefinition(Subject subj
PropertyDefinitionMap pdm = (PropertyDefinitionMap) pd;
for (PropertyDefinition inner : pdm.getOrderedPropertyDefinitions()) {
if (inner instanceof PropertyDefinitionSimple) {
handlePDS(subject, resource, baseResource, (PropertyDefinitionSimple) inner);
handlePDS(subject, resource, parentResource, baseResource, (PropertyDefinitionSimple) inner);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("4 ____[ " + inner.toString() + " in " + pdm.toString()
Expand All @@ -2639,7 +2639,8 @@ public ConfigurationDefinition getOptionsForConfigurationDefinition(Subject subj
* @param subject Subject of the caller - may limit search results
* @param pds the PropertyDefinitionSimple to work on
*/
private void handlePDS(final Subject subject, Resource resource, Resource baseResource, PropertyDefinitionSimple pds) {
private void handlePDS(final Subject subject, Resource resource, Resource parentResource,
Resource baseResource, PropertyDefinitionSimple pds) {

if (pds.getOptionsSource() != null) {
// evaluate the source parameters
Expand All @@ -2652,6 +2653,35 @@ private void handlePDS(final Subject subject, Resource resource, Resource baseRe
if (filter != null)
filterPattern = Pattern.compile(filter);

Predicate<Resource> baseResourcePredicate = null;
if (expressionScope == PropertyOptionsSource.ExpressionScope.BASE_RESOURCE) {
int halfCommaIndex = expression.indexOf(";");
if (halfCommaIndex > -1) {
if (resource == null && parentResource == null) {
LOG.warn("Different base resource type requested but resource id and parent_resource_id are not valid."
+ "Option source expression:" + expression);
return;
}
Resource nonNullResource = (resource == null? parentResource : resource);
String ancestorType = expression.substring(
halfCommaIndex + 1, expression.length());
expression = expression.substring(0, halfCommaIndex);
Resource foundBaseResource = ResourceUtility.getAncestorResourceOfType(
nonNullResource, ancestorType);
if (foundBaseResource != null) {
baseResourcePredicate = new isAncestorResourcePredicate(foundBaseResource);
} else {
LOG.warn("Couldn't find base resource of type ["
+ ancestorType + "] for resource [" + nonNullResource.getName() + "]");
return;
}
}
if (baseResourcePredicate == null && baseResource != null) {
baseResourcePredicate = new IsInBaseResourcePredicate(baseResource);
}
}


if (tt == PropertyOptionsSource.TargetType.RESOURCE || tt == PropertyOptionsSource.TargetType.CONFIGURATION) {
ResourceCriteria criteria = new ResourceCriteria();

Expand All @@ -2672,13 +2702,8 @@ public PageList<Resource> execute(ResourceCriteria criteria) {
expr = expr.substring(expr.indexOf(':') + 1);

if (!"self".equals(expr)) {
criteria.setSearchExpression(expr);
foundResources = new CriteriaQuery<Resource, ResourceCriteria>(criteria, queryExecutor);
if (expressionScope == PropertyOptionsSource.ExpressionScope.BASE_RESOURCE
&& baseResource != null) {
foundResources = Iterables.filter(foundResources, new IsInBaseResourcePredicate(
baseResource));
}
foundResources = processSearchExpression(
expression, criteria, queryExecutor, expressionScope, baseResourcePredicate);
} else if (resource != null) {
ArrayList<Resource> resourceList = new ArrayList<Resource>();
resourceList.add(resource);
Expand All @@ -2694,11 +2719,8 @@ public PageList<Resource> execute(ResourceCriteria criteria) {
return;
}
} else {
criteria.setSearchExpression(expression);
foundResources = new CriteriaQuery<Resource, ResourceCriteria>(criteria, queryExecutor);
if (expressionScope == PropertyOptionsSource.ExpressionScope.BASE_RESOURCE && baseResource != null) {
foundResources = Iterables.filter(foundResources, new IsInBaseResourcePredicate(baseResource));
}
foundResources = processSearchExpression(
expression, criteria, queryExecutor, expressionScope, baseResourcePredicate);
}

for (Resource foundResource : foundResources) {
Expand All @@ -2718,6 +2740,18 @@ public PageList<Resource> execute(ResourceCriteria criteria) {

}

private Iterable<Resource> processSearchExpression(String expression, ResourceCriteria criteria,
CriteriaQueryExecutor<Resource, ResourceCriteria> queryExecutor,
PropertyOptionsSource.ExpressionScope expressionScope,
Predicate<Resource> baseResourcePredicate) {
criteria.setSearchExpression(expression);
Iterable<Resource> foundResources = new CriteriaQuery<Resource, ResourceCriteria>(criteria, queryExecutor);
if (expressionScope == PropertyOptionsSource.ExpressionScope.BASE_RESOURCE && baseResourcePredicate != null) {
foundResources = Iterables.filter(foundResources, baseResourcePredicate);
}
return foundResources;
}

private void processPropertyOptionsSource(Resource resource, Resource baseResource, PropertyDefinitionSimple pds,
PropertyOptionsSource.TargetType tt, String expression, Pattern filterPattern, Resource foundResource) {
if (tt == PropertyOptionsSource.TargetType.RESOURCE) {
Expand Down Expand Up @@ -2854,4 +2888,18 @@ public boolean apply(Resource resource) {
return baseResource.equals(baseServerOrService);
}
}

private static final class isAncestorResourcePredicate implements Predicate<Resource> {

private Resource ancestor;

private isAncestorResourcePredicate(Resource ancestor) {
this.ancestor = ancestor;
}

@Override
public boolean apply(Resource resource) {
return ResourceUtility.isAncestor(resource, this.ancestor);
}
}
}

0 comments on commit ba77f34

Please sign in to comment.