Skip to content

Commit

Permalink
add get project entities by classifier API
Browse files Browse the repository at this point in the history
  • Loading branch information
YannanGao-gs committed Sep 18, 2024
1 parent e32c361 commit e204243
Show file tree
Hide file tree
Showing 8 changed files with 2,651 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ public EtagBuilder withProtocolVersion(String clientProtocolVersion)
return this;
}

public EtagBuilder withClassifier(String classifier)
{
if (classifier == null)
{
this.constantParams = false;
}
else
{
params.add(classifier);
}
return this;
}

public String build()
{
if (this.constantParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public interface EntitiesService<T extends StoredEntity>

List<Entity> getEntities(String groupId, String artifactId, String versionId);

List<Entity> getEntitiesByClassifier(String groupId, String artifactId, String versionId, String classifier);

Optional<Entity> getEntity(String groupId, String artifactId, String versionId, String entityPath);

List<Entity> getEntityFromDependencies(String groupId, String artifactId, String versionId, List<String> entityPaths, boolean includeOrigin);
Expand All @@ -39,8 +41,15 @@ public interface EntitiesService<T extends StoredEntity>

List<ProjectVersionEntities> getDependenciesEntities(List<ProjectVersion> projectDependencies, boolean transitive, boolean includeOrigin);

List<ProjectVersionEntities> getDependenciesEntitiesByClassifier(List<ProjectVersion> projectDependencies, String classifier, boolean transitive, boolean includeOrigin);

default List<ProjectVersionEntities> getDependenciesEntities(String groupId, String artifactId, String versionId, boolean transitive, boolean includeOrigin)
{
return getDependenciesEntities(Arrays.asList(new ProjectVersion(groupId, artifactId, versionId)), transitive, includeOrigin);
}

default List<ProjectVersionEntities> getDependenciesEntitiesByClassifier(String groupId, String artifactId, String versionId, String classifier, boolean transitive, boolean includeOrigin)
{
return getDependenciesEntitiesByClassifier(Arrays.asList(new ProjectVersion(groupId, artifactId, versionId)), classifier, transitive, includeOrigin);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
public class EntitiesDependenciesResource extends TracingResource
{
private final EntitiesService entitiesService;
private final String VALID_CLASSIFIERS = "meta::pure::metamodel::function::ConcreteFunctionDefinition\n" +
"meta::pure::metamodel::dataSpace::DataSpace";

@Inject
public EntitiesDependenciesResource(EntitiesService entitiesService)
Expand All @@ -73,6 +75,24 @@ public Response getEntitiesFromDependencies(@PathParam("groupId") String groupId
return handle(GET_VERSION_DEPENDENCY_ENTITIES, () -> this.entitiesService.getDependenciesEntities(groupId, artifactId, versionId, transitive, includeOrigin), request, () -> EtagBuilder.create().withGAV(groupId, artifactId, versionId).build());
}

@GET
@Path("/projects/{groupId}/{artifactId}/versions/{versionId}/classifier/{classifier}/dependencies")
@ApiOperation(GET_VERSION_DEPENDENCY_ENTITIES)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response getEntitiesFromDependenciesByClassifier(@PathParam("groupId") String groupId,
@PathParam("artifactId") String artifactId,
@PathParam("versionId") @ApiParam(value = VersionValidator.VALID_VERSION_ID_TXT) String versionId,
@PathParam("classifier") @ApiParam(value = VALID_CLASSIFIERS) String classifier,
@QueryParam("transitive") @DefaultValue("false")
@ApiParam("Whether to return transitive dependencies") boolean transitive,
@QueryParam("includeOrigin") @DefaultValue("false")
@ApiParam("Whether to return start of dependency tree") boolean includeOrigin,
@Context Request request)
{
return handle(GET_VERSION_DEPENDENCY_ENTITIES, () -> this.entitiesService.getDependenciesEntitiesByClassifier(groupId, artifactId, versionId, classifier, transitive, includeOrigin), request, () -> EtagBuilder.create().withGAV(groupId, artifactId, versionId).withClassifier(classifier).build());
}

@POST
@Path("/projects/{groupId}/{artifactId}/versions/{versionId}/dependencies/paths")
@ApiOperation(value = GET_VERSION_ENTITY_FROM_DEPENDENCIES, hidden = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
public class EntitiesResource extends TracingResource
{
private final EntitiesService entitiesService;
private final String VALID_CLASSIFIERS = "meta::pure::metamodel::function::ConcreteFunctionDefinition\n" +
"meta::pure::metamodel::dataSpace::DataSpace";

@Inject
public EntitiesResource(EntitiesService entitiesService)
Expand All @@ -64,7 +66,19 @@ public Response getEntities(@PathParam("groupId") String groupId,
return handle(GET_VERSION_ENTITIES, () -> this.entitiesService.getEntities(groupId, artifactId, versionId), request, () -> EtagBuilder.create().withGAV(groupId, artifactId, versionId).build());
}


@GET
@Path("/projects/{groupId}/{artifactId}/versions/{versionId}/classifier/{classifier}")
@ApiOperation(GET_VERSION_ENTITIES)
@Produces(MediaType.APPLICATION_JSON)
public Response getEntitiesByClassifier(@PathParam("groupId") String groupId,
@PathParam("artifactId") String artifactId,
@PathParam("versionId") @ApiParam(value = VersionValidator.VALID_VERSION_ID_TXT) String versionId,
@PathParam("classifier") @ApiParam(value = VALID_CLASSIFIERS) String classifier,
@Context Request request)
{
return handle(GET_VERSION_ENTITIES, () -> this.entitiesService.getEntitiesByClassifier(groupId, artifactId, versionId, classifier), request, () -> EtagBuilder.create().withGAV(groupId, artifactId, versionId).withClassifier(classifier).build());
}

@GET
@Path("/projects/{groupId}/{artifactId}/versions/{versionId}/entities/{path}")
@ApiOperation(GET_VERSION_ENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public List<Entity> getEntities(String groupId, String artifactId, String versio
return entities.getAllEntities(groupId, artifactId, version);
}

@Override
public List<Entity> getEntitiesByClassifier(String groupId, String artifactId, String versionId, String classifier)
{
String version = this.projects.resolveAliasesAndCheckVersionExists(groupId, artifactId, versionId);
return entities.findEntitiesByClassifier(groupId, artifactId, version, classifier);
}

@Override
public Optional<Entity> getEntity(String groupId, String artifactId, String versionId, String entityPath)
{
Expand Down Expand Up @@ -89,6 +96,12 @@ public List<Entity> getEntitiesByPackage(String groupId, String artifactId, Stri

@Override
public List<ProjectVersionEntities> getDependenciesEntities(List<ProjectVersion> projectDependencies, boolean transitive, boolean includeOrigin)
{
return getDependenciesEntitiesByClassifier(projectDependencies, null, transitive, includeOrigin);
}

@Override
public List<ProjectVersionEntities> getDependenciesEntitiesByClassifier(List<ProjectVersion> projectDependencies, String classifier, boolean transitive, boolean includeOrigin)
{
Set<ProjectVersion> dependencies = (Set<ProjectVersion>) executeWithTrace(CALCULATE_PROJECT_DEPENDENCIES, () ->
{
Expand All @@ -110,7 +123,15 @@ public List<ProjectVersionEntities> getDependenciesEntities(List<ProjectVersion>
ParallelIterate.forEach(dependencies, dep ->
{
String version = this.projects.resolveAliasesAndCheckVersionExists(dep.getGroupId(), dep.getArtifactId(), dep.getVersionId());
List<Entity> deps = (List<Entity>) entities.getAllEntities(dep.getGroupId(), dep.getArtifactId(), version).stream().collect(Collectors.toList());
List<Entity> deps;
if (classifier != null)
{
deps = (List<Entity>) entities.findEntitiesByClassifier(dep.getGroupId(), dep.getArtifactId(), version, classifier).stream().collect(Collectors.toList());
}
else
{
deps = (List<Entity>) entities.getAllEntities(dep.getGroupId(), dep.getArtifactId(), version).stream().collect(Collectors.toList());
}
depEntities.add(new ProjectVersionEntities(dep.getGroupId(), dep.getArtifactId(), version, deps));
totalEntities.addAndGet(deps.size());
TracerFactory.get().log(String.format("Total [%s-%s-%s]: [%s] entities",dep.getGroupId(), dep.getArtifactId(), dep.getVersionId(),deps.size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;

import static org.finos.legend.depot.domain.version.VersionValidator.BRANCH_SNAPSHOT;
Expand Down Expand Up @@ -244,6 +243,35 @@ public void canGetClassifiers()
Assert.assertEquals(entities.size(), 3);
}

@Test
public void canGetEntitiesForProjectAndVersionByClassifier()
{
projectsVersionsStore.createOrUpdate(new StoreProjectVersionData("example.services.test","test","2.0.2"));
entityUtils.loadEntities("PROD-C", "2.0.2");
List<Entity> entity = entitiesService.getEntitiesByClassifier("example.services.test", "test", "2.0.2", "meta::pure::metamodel::function::ConcreteFunctionDefinition");
Assert.assertNotNull(entity);
Assert.assertEquals(1, entity.size());
}

@Test
public void canGetDependencyEntitiesForProjectAndVersionByClassifier()
{
StoreProjectVersionData project = new StoreProjectVersionData("examples.metadata", "test-dependencies", "1.0.1");
ProjectVersion pv = new ProjectVersion("example.services.test", "test", "2.0.2");
project.getVersionData().addDependency(pv);
projectsVersionsStore.createOrUpdate(project);
projectsVersionsStore.createOrUpdate(new StoreProjectVersionData("example.services.test","test","2.0.2"));
entityUtils.loadEntities("PROD-B", "1.0.1");
entityUtils.loadEntities("PROD-C", "2.0.2");
List<Entity> entity = entitiesService.getDependenciesEntitiesByClassifier("examples.metadata", "test-dependencies", "1.0.1", "meta::pure::metamodel::function::ConcreteFunctionDefinition", true, true);
Assert.assertNotNull(entity);
Assert.assertEquals(2, entity.size());

List<Entity> entity1 = entitiesService.getDependenciesEntitiesByClassifier("examples.metadata", "test-dependencies", "1.0.1", "meta::pure::metamodel::function::ConcreteFunctionDefinition", true, false);
Assert.assertNotNull(entity1);
Assert.assertEquals(1, entity1.size());
}

@Test
public void canGetEntityFromDependencies()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"_type": "entityData",
"versionId": "1.0.1",
"groupId": "examples.metadata",
"artifactId": "test-dependencies",
"versionedEntity": false,
"entityAttributes": {
"path": "test::testFunction__String_1_",
"classifierPath": "meta::pure::metamodel::function::ConcreteFunctionDefinition",
"package": "test"
},
"entity": {
"classifierPath": "meta::pure::metamodel::function::ConcreteFunctionDefinition",
"path": "test::testFunction__String_1_",
"content": {
"_type": "function",
"body": [
{
"_type": "string",
"value": ""
}
],
"name": "testFunction__String_1_",
"package": "test",
"parameters": [],
"postConstraints": [],
"preConstraints": [],
"returnMultiplicity": {
"lowerBound": 1,
"upperBound": 1
},
"returnType": "String"
}
},
"id": ""
}
]
Loading

0 comments on commit e204243

Please sign in to comment.