Skip to content

Commit

Permalink
SED-540 Filtering plans and kw according to tenant selection at export
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromecomte committed Apr 15, 2021
1 parent 5292296 commit 3159c3d
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 127 deletions.
Expand Up @@ -68,6 +68,14 @@
import step.resources.ResourceManager;

public class ExportManagerTest {

private ObjectPredicate objectPredicate = new ObjectPredicate() {

@Override
public boolean test(Object t) {
return true;
}
};

public static GlobalContext createGlobalContext() throws InstantiationException, IllegalAccessException, ClassNotFoundException, CircularDependencyException {
GlobalContext context = createGlobalContext_();
Expand Down Expand Up @@ -127,7 +135,7 @@ public void testExportPlanById() throws Exception {
metadata.put("export-time" , "1589542872475");
metadata.put("user", "admin");
ExportConfiguration exportConfig = new ExportConfiguration(outputStream, metadata, dummyObjectPredicate(), "plans", false, null);
exportManager.exportById(exportConfig, plan.getId().toString());
exportManager.exportById(exportConfig, plan.getId().toString(), objectPredicate);
Assert.assertTrue(FileHelper.isArchive(testExportFile));

//DEBUG
Expand Down Expand Up @@ -167,7 +175,7 @@ public void testExportKeywordById() throws Exception {
metadata.put("export-time" , "1589542872475");
metadata.put("user", "admin");
ExportConfiguration exportConfig = new ExportConfiguration(outputStream, metadata, dummyObjectPredicate(), EntityManager.functions, true, null);
exportManager.exportById(exportConfig, function.getId().toString());
exportManager.exportById(exportConfig, function.getId().toString(), objectPredicate);
Assert.assertTrue(FileHelper.isArchive(testExportFile));

//DEBUG
Expand Down Expand Up @@ -506,7 +514,7 @@ public void testExportPlanByIdWithParameters() throws Exception {
List<String> additionalEntities = new ArrayList<String>();
additionalEntities.add(ParameterManagerPlugin.entityName);
ExportConfiguration exportConfig = new ExportConfiguration(outputStream, metadata, dummyObjectPredicate(), "plans", false, additionalEntities);
exportManager.exportById(exportConfig, plan.getId().toString());
exportManager.exportById(exportConfig, plan.getId().toString(), objectPredicate);
Assert.assertTrue(FileHelper.isArchive(testExportFile));

//create a new context to test the import
Expand Down Expand Up @@ -542,7 +550,7 @@ public void testExportPlanByWithCustomFields() throws Exception {
metadata.put("export-time" , "1589542872475");
metadata.put("user", "admin");
ExportConfiguration exportConfig = new ExportConfiguration(outputStream, metadata, dummyObjectPredicate(), "plans", false, null);
exportManager.exportById(exportConfig, plan.getId().toString());
exportManager.exportById(exportConfig, plan.getId().toString(), objectPredicate);

//DEBUG
/*try (BufferedReader br = new BufferedReader(
Expand Down Expand Up @@ -598,7 +606,7 @@ private void testExportPlansRecursively(boolean overwrite) throws Exception {
Map<String,String> metadata = new HashMap<String,String>();
metadata.put("version", c.getCurrentVersion().toString());
ExportConfiguration exportConfig = new ExportConfiguration(outputStream, metadata, dummyObjectPredicate(), "plans", true, null);
exportManager.exportById(exportConfig, plan2.getId().toString());
exportManager.exportById(exportConfig, plan2.getId().toString(), objectPredicate);

/*DEBUG
try (BufferedReader br = new BufferedReader(
Expand Down Expand Up @@ -674,7 +682,7 @@ private void testExportPlansWithCompoFct(boolean overwrite) throws Exception {
Map<String,String> metadata = new HashMap<String,String>();
metadata.put("version", c.getCurrentVersion().toString());
ExportConfiguration exportConfig = new ExportConfiguration(outputStream, metadata, dummyObjectPredicate(), "plans", true, null);
exportManager.exportById(exportConfig, plan.getId().toString());
exportManager.exportById(exportConfig, plan.getId().toString(), objectPredicate);

//create a new context to test the import
c = createGlobalContext();
Expand Down Expand Up @@ -744,7 +752,7 @@ public void testExportPlansWithResourceFct(boolean overwrite) throws Exception {
Map<String,String> metadata = new HashMap<String,String>();
metadata.put("version", c.getCurrentVersion().toString());
ExportConfiguration exportConfig = new ExportConfiguration(outputStream, metadata, dummyObjectPredicate(), "plans", true, null);
exportManager.exportById(exportConfig, plan.getId().toString());
exportManager.exportById(exportConfig, plan.getId().toString(), objectPredicate);
//delete created resource
File resourceFile = resourceManager.getResourceFile(resource.getId().toHexString()).getResourceFile();
resourceManager.deleteResource(resource.getId().toHexString());
Expand Down
Expand Up @@ -127,7 +127,7 @@ public Function lookupCallFunction(CallFunction callFunction) {
Function function = null;
try {
ObjectPredicate objectPredicate = objectPredicateFactory.getObjectPredicate(getSession());
function = functionLocator.getFunction(callFunction, objectPredicate);
function = functionLocator.getFunction(callFunction, objectPredicate, null);
} catch (RuntimeException e) {}
return function;
}
Expand Down
Expand Up @@ -56,6 +56,7 @@
import step.core.execution.model.ExecutionStatus;
import step.core.imports.GenericDBImporter;
import step.core.imports.PlanImporter;
import step.core.objectenricher.ObjectPredicate;
import step.core.plans.Plan;
import step.core.plans.PlanAccessor;
import step.core.plugins.ControllerInitializationPlugin;
Expand Down Expand Up @@ -174,7 +175,7 @@ private void initContext() throws Exception {
context.setEntityManager(new EntityManager(context));
DynamicJsonObjectResolver dynamicJsonObjectResolver = new DynamicJsonObjectResolver(new DynamicJsonValueResolver(getContext().getExpressionHandler()));
SelectorHelper selectorHelper = new SelectorHelper(dynamicJsonObjectResolver);
PlanLocator planLocator = new PlanLocator(null,getContext().getPlanAccessor(),selectorHelper);
PlanLocator planLocator = new PlanLocator(getContext().getPlanAccessor(), selectorHelper);
context.getEntityManager()
.register( new Entity<Execution, ExecutionAccessor>(
EntityManager.executions, context.getExecutionAccessor(), Execution.class,
Expand All @@ -187,9 +188,9 @@ public boolean shouldExport(AbstractIdentifiableObject a) {
return ((Plan) a).isVisible();
}
@Override
public String resolve(Object artefact) {
public String resolve(Object artefact, ObjectPredicate objectPredicate) {
if (artefact instanceof CallPlan) {
return planLocator.selectPlan((CallPlan) artefact).getId().toHexString();
return planLocator.selectPlan((CallPlan) artefact, objectPredicate, null).getId().toHexString();
} else {
return null;
}
Expand Down
Expand Up @@ -24,7 +24,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipOutputStream;

import org.slf4j.Logger;
Expand All @@ -40,7 +39,7 @@
import step.core.entities.Entity;
import step.core.entities.EntityManager;
import step.core.entities.EntityReferencesMap;
import step.core.objectenricher.ObjectEnricher;
import step.core.objectenricher.ObjectPredicate;
import step.resources.ResourceManager;

public class ExportManager {
Expand All @@ -54,11 +53,11 @@ public ExportManager(GlobalContext context) {
this.context = context;
}

public void exportById(ExportConfiguration exportConfig, String id) throws FileNotFoundException, IOException {
public void exportById(ExportConfiguration exportConfig, String id, ObjectPredicate objectPredicate) throws FileNotFoundException, IOException {
EntityReferencesMap refs = new EntityReferencesMap();
refs.addElementTo(exportConfig.getEntityType(), id);
if (exportConfig.isRecursively()) {
context.getEntityManager().getAllEntities(exportConfig.getEntityType(), id, refs);
context.getEntityManager().getAllEntities(exportConfig.getEntityType(), id, objectPredicate, refs);
}
List<String> additionalEntities = exportConfig.getAdditionalEntities();
if (additionalEntities != null && additionalEntities.size()>0) {
Expand Down
Expand Up @@ -23,7 +23,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.inject.Singleton;
Expand All @@ -38,10 +37,8 @@
import step.core.deployment.AbstractServices;
import step.core.deployment.Secured;
import step.core.deployment.Session;
import step.core.entities.EntityManager;
import step.core.export.ExportTaskManager.ExportRunnable;
import step.core.export.ExportTaskManager.ExportStatus;
import step.core.objectenricher.ObjectEnricher;
import step.core.objectenricher.ObjectHookRegistry;
import step.core.objectenricher.ObjectPredicateFactory;
import step.core.plans.PlanAccessor;
Expand Down Expand Up @@ -88,7 +85,7 @@ public Resource runExport() throws IOException {
ResourceRevisionContainer resourceContainer = getResourceManager().createResourceContainer(ResourceManager.RESOURCE_TYPE_TEMP, filename);
ExportConfiguration exportConfig = new ExportConfiguration(resourceContainer.getOutputStream(), metadata,objectPredicateFactory.getObjectPredicate(session),
entity, recursively, additionalEntities);
exportManager.exportById(exportConfig, id);
exportManager.exportById(exportConfig, id, objectPredicateFactory.getObjectPredicate(session));
status.setWarnings(exportConfig.getMessages());
resourceContainer.save(null);
return resourceContainer.getResource();
Expand Down
Expand Up @@ -179,10 +179,10 @@ public Plan lookupPlan(@PathParam("id") String id, @PathParam("artefactid") Stri
CallPlan artefact = (CallPlan) planNavigator.findArtefactById(artefactId);
DynamicJsonObjectResolver dynamicJsonObjectResolver = new DynamicJsonObjectResolver(new DynamicJsonValueResolver(getContext().getExpressionHandler()));
SelectorHelper selectorHelper = new SelectorHelper(dynamicJsonObjectResolver);
PlanLocator planLocator = new PlanLocator(null,getContext().getPlanAccessor(),selectorHelper);
PlanLocator planLocator = new PlanLocator(getContext().getPlanAccessor(), selectorHelper);
ObjectPredicate objectPredicate = objectPredicateFactory.getObjectPredicate(getSession());
try {
result = planLocator.selectPlan(artefact, objectPredicate);
result = planLocator.selectPlan(artefact, objectPredicate, null);
} catch (RuntimeException e) {}
return result;
}
Expand Down
Expand Up @@ -40,6 +40,7 @@
import step.core.entities.Entity;
import step.core.entities.EntityManager;
import step.core.imports.GenericDBImporter;
import step.core.objectenricher.ObjectPredicate;
import step.core.plugins.AbstractControllerPlugin;
import step.core.plugins.Plugin;
import step.functions.Function;
Expand Down Expand Up @@ -91,9 +92,9 @@ public void executionControllerStart(GlobalContext context) throws Exception {
EntityManager.functions, (FunctionAccessorImpl) functionAccessor, Function.class,
new GenericDBImporter<Function,FunctionAccessorImpl>(context)) {
@Override
public String resolve(Object artefact) {
public String resolve(Object artefact, ObjectPredicate objectPredicate) {
if (artefact instanceof CallFunction) {
return functionLocator.getFunction((CallFunction) artefact).getId().toHexString();
return functionLocator.getFunction((CallFunction) artefact, objectPredicate, null).getId().toHexString();
} else {
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion step-core/src/main/java/step/core/entities/Entity.java
Expand Up @@ -26,6 +26,7 @@
import step.core.accessors.AbstractIdentifiableObject;
import step.core.accessors.CRUDAccessor;
import step.core.imports.Importer;
import step.core.objectenricher.ObjectPredicate;

public class Entity<A extends AbstractIdentifiableObject, T extends CRUDAccessor<A>> {

Expand Down Expand Up @@ -102,7 +103,7 @@ public void setByPassObjectPredicate(boolean byPassObjectPredicate) {
this.byPassObjectPredicate = byPassObjectPredicate;
}

public String resolve(Object artefact) {
public String resolve(Object artefact, ObjectPredicate objectPredicate) {
return null;
}

Expand Down
26 changes: 14 additions & 12 deletions step-core/src/main/java/step/core/entities/EntityManager.java
Expand Up @@ -96,7 +96,7 @@ public void getEntitiesReferences(String entityType, ObjectPredicate objectPredi
if ((entity.isByPassObjectPredicate() || objectPredicate.test(a)) && entity.shouldExport(a)) {
refs.addElementTo(entityType, a.getId().toHexString());
if (recursively) {
getAllEntities(entityType, a.getId().toHexString(), refs);
getAllEntities(entityType,a.getId().toHexString(), objectPredicate, refs);
}
}
});
Expand All @@ -108,7 +108,7 @@ public void getEntitiesReferences(String entityType, ObjectPredicate objectPredi
* @param id the id of the entity
* @param references the map of references to be populated
*/
public void getAllEntities (String entityName, String id, EntityReferencesMap references) {
public void getAllEntities (String entityName, String id, ObjectPredicate objectPredicate, EntityReferencesMap references) {
Entity<?, ?> entity = getEntityByName(entityName);
if (entity == null) {
logger.error("Entities of type '" + entityName + "' are not supported");
Expand All @@ -121,8 +121,8 @@ public void getAllEntities (String entityName, String id, EntityReferencesMap re
references.addReferenceNotFoundWarning("Referenced entity with id '" + id + "' and type '" + entityName + "' is missing");
//keep reference in map to avoid to resolve it multiple times
} else {
resolveReferences(a, references);
entity.getReferencesHook().forEach(h->h.accept(a,references));
resolveReferences(a, objectPredicate, references);
entity.getReferencesHook().forEach(h->h.accept(a, objectPredicate, references));
}
}

Expand All @@ -131,7 +131,7 @@ public void getAllEntities (String entityName, String id, EntityReferencesMap re
* @param object to be introspected
* @param references map to be populated
*/
private void resolveReferences(Object object, EntityReferencesMap references) {
private void resolveReferences(Object object, ObjectPredicate objectPredicate, EntityReferencesMap references) {
if(object!=null) {

Class<?> clazz = object.getClass();
Expand Down Expand Up @@ -164,24 +164,24 @@ private void resolveReferences(Object object, EntityReferencesMap references) {
//No actual references, but need to process the field recursively
if (value instanceof Collection) {
Collection<?> c = (Collection<?>) value;
c.forEach(o->resolveReferences(o, references));
c.forEach(o->resolveReferences(o, objectPredicate, references));
} else {
resolveReferences(value, references);
resolveReferences(value, objectPredicate, references);
}
}
else {
if (value instanceof Collection) {
Collection<?> c = (Collection<?>) value;
c.forEach(r->resolveReference(r, references, entityType, r.getClass()));
c.forEach(r->resolveReference(r, objectPredicate, references, entityType, r.getClass()));
} else {
if (value == null) {
try {
value = getEntityByName(entityType).resolve(object);
value = getEntityByName(entityType).resolve(object, objectPredicate);
} catch (Exception e) {
references.addReferenceNotFoundWarning("Unable to resolve reference of type " + entityType + " from artefact " + object.getClass().getCanonicalName() + ". Error: " + e.getMessage());
}
}
resolveReference(value, references, entityType, method.getReturnType());
resolveReference(value, objectPredicate, references, entityType, method.getReturnType());
}
}
}
Expand All @@ -197,7 +197,7 @@ private void resolveReferences(Object object, EntityReferencesMap references) {
* @param entityType
* @param type
*/
private void resolveReference(Object value, EntityReferencesMap references, String entityType, Class<?> type) {
private void resolveReference(Object value, ObjectPredicate objectPredicate, EntityReferencesMap references, String entityType, Class<?> type) {
FileResolver fileResolver = context.getFileResolver();
String refId = null;
if (type.equals(DynamicValue.class) && value!=null) {
Expand Down Expand Up @@ -226,7 +226,7 @@ private void resolveReference(Object value, EntityReferencesMap references, Stri
}
//avoid circular references issue
if (added) {
getAllEntities(entityType, refId, references);
getAllEntities(entityType, refId, objectPredicate, references);
}
}
}
Expand Down Expand Up @@ -342,7 +342,9 @@ private Object getNewValue_(Object value, Class<?> returnType, Map<String, Strin

private Object getNewValue(Object value, Class<?> returnType, Map<String, String> references, String entityType) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (value instanceof Collection) {
@SuppressWarnings("unchecked")
Collection<Object> c = (Collection<Object>) value;
@SuppressWarnings("unchecked")
Collection<Object> newCol = c.getClass().getConstructor().newInstance();
c.forEach(e-> {
newCol.add(getNewValue_(e,e.getClass(),references, entityType));
Expand Down
Expand Up @@ -18,25 +18,19 @@
******************************************************************************/
package step.core.entities;

import java.util.function.BiConsumer;
import step.core.objectenricher.ObjectPredicate;

public class ResolveReferencesHook implements BiConsumer<Object, EntityReferencesMap> {
public class ResolveReferencesHook {

protected EntityManager entityManager;



public ResolveReferencesHook(EntityManager entityManager) {
super();
this.entityManager = entityManager;
}



@Override
public void accept(Object t, EntityReferencesMap u) {
public void accept(Object t, ObjectPredicate objectPredicate, EntityReferencesMap u) {
throw new RuntimeException("Not implemented");

}

}
Expand Up @@ -41,6 +41,7 @@
import step.core.dynamicbeans.DynamicJsonObjectResolver;
import step.core.dynamicbeans.DynamicJsonValueResolver;
import step.core.execution.ExecutionContext;
import step.core.execution.ExecutionContextBindings;
import step.core.json.JsonProviderCache;
import step.core.miscellaneous.ReportNodeAttachmentManager;
import step.core.miscellaneous.ReportNodeAttachmentManager.AttachmentQuotaException;
Expand Down Expand Up @@ -85,7 +86,7 @@ public void init(ExecutionContext context) {
reportNodeAttachmentManager = new ReportNodeAttachmentManager(context);
dynamicJsonObjectResolver = new DynamicJsonObjectResolver(new DynamicJsonValueResolver(context.getExpressionHandler()));
this.selectorHelper = new SelectorHelper(dynamicJsonObjectResolver);
this.functionLocator = new FunctionLocator(functionAccessor, selectorHelper, context);
this.functionLocator = new FunctionLocator(functionAccessor, selectorHelper);
}

@Override
Expand Down Expand Up @@ -201,7 +202,8 @@ private void validateInput(FunctionInput<JsonObject> input, Function function) {
}

private Function getFunction(CallFunction testArtefact) {
return functionLocator.getFunction(testArtefact);
return functionLocator.getFunction(testArtefact, context.getObjectPredicate(),
ExecutionContextBindings.get(context));
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit 3159c3d

Please sign in to comment.