Skip to content

Commit

Permalink
add artifact generations api
Browse files Browse the repository at this point in the history
  • Loading branch information
MauricioUyaguari committed Sep 28, 2022
1 parent b603458 commit 2c1356e
Show file tree
Hide file tree
Showing 30 changed files with 1,362 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.finos.legend.depot.store.artifacts.api.ArtifactsRefreshService;
import org.finos.legend.depot.store.artifacts.api.entities.EntitiesVersionArtifactsHandler;
import org.finos.legend.depot.store.artifacts.api.entities.EntityArtifactsProvider;
import org.finos.legend.depot.store.artifacts.api.generation.FileArtifactGenerationsVersionArtifactsHandler;
import org.finos.legend.depot.store.artifacts.api.generation.artifact.ArtifactGenerationsVersionArtifactsHandler;
import org.finos.legend.depot.store.artifacts.api.generation.file.FileGenerationsProvider;
import org.finos.legend.depot.store.artifacts.api.generation.file.FileGenerationsVersionArtifactsHandler;
import org.finos.legend.depot.store.artifacts.api.status.ManageRefreshStatusService;
Expand All @@ -33,7 +35,9 @@
import org.finos.legend.depot.store.artifacts.services.ArtifactsRefreshServiceImpl;
import org.finos.legend.depot.store.artifacts.services.entities.EntitiesHandlerImpl;
import org.finos.legend.depot.store.artifacts.services.entities.EntityProvider;
import org.finos.legend.depot.store.artifacts.services.file.FileGenerationVersionsHandler;
import org.finos.legend.depot.store.artifacts.services.file.ArtifactGenerationHandler;
import org.finos.legend.depot.store.artifacts.services.file.FileArtifactGenerationVersionsHandler;
import org.finos.legend.depot.store.artifacts.services.file.FileGenerationHandler;
import org.finos.legend.depot.store.artifacts.services.file.FileGenerationsProviderImpl;
import org.finos.legend.depot.store.artifacts.store.mongo.ArtifactsMongo;
import org.finos.legend.depot.store.artifacts.store.mongo.MongoRefreshStatus;
Expand Down Expand Up @@ -62,9 +66,12 @@ protected void configure()
bind(EntitiesVersionArtifactsHandler.class).to(EntitiesHandlerImpl.class);
bind(EntityArtifactsProvider.class).to(EntityProvider.class);

bind(FileGenerationsVersionArtifactsHandler.class).to(FileGenerationVersionsHandler.class);
bind(ArtifactGenerationsVersionArtifactsHandler.class).to(ArtifactGenerationHandler.class);
bind(FileGenerationsVersionArtifactsHandler.class).to(FileGenerationHandler.class);
bind(FileArtifactGenerationsVersionArtifactsHandler.class).to(FileArtifactGenerationVersionsHandler.class);
bind(FileGenerationsProvider.class).to(FileGenerationsProviderImpl.class);


bind(ArtifactsRefreshService.class).to(ArtifactsRefreshServiceImpl.class);

expose(EntityArtifactsProvider.class);
Expand All @@ -73,6 +80,8 @@ protected void configure()
expose(ManageRefreshStatusService.class);
expose(RefreshStatusService.class);
expose(ArtifactsResource.class);
expose(ArtifactGenerationsVersionArtifactsHandler.class);
expose(FileGenerationsVersionArtifactsHandler.class);
}

@Provides
Expand All @@ -87,7 +96,7 @@ boolean registerEntityRefresh(EntitiesVersionArtifactsHandler versionArtifactsHa
@Provides
@Named("fileGenerationRefresh")
@Singleton
boolean registerFileGenerationRefresh(FileGenerationsVersionArtifactsHandler versionArtifactsHandler)
boolean registerFileGenerationRefresh(FileArtifactGenerationsVersionArtifactsHandler versionArtifactsHandler)
{
ArtifactResolverFactory.registerVersionUpdater(ArtifactType.FILE_GENERATIONS, versionArtifactsHandler);
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2022 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package org.finos.legend.depot.store.artifacts.api.generation;

import org.finos.legend.depot.store.artifacts.api.ProjectVersionArtifactsHandler;

public interface FileArtifactGenerationsVersionArtifactsHandler extends ProjectVersionArtifactsHandler
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2022 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package org.finos.legend.depot.store.artifacts.api.generation.artifact;

import java.util.List;
import org.finos.legend.depot.domain.api.MetadataEventResponse;
import org.finos.legend.depot.domain.generation.artifact.ArtifactGeneration;
import org.finos.legend.depot.domain.project.ProjectData;

public interface ArtifactGenerationsVersionArtifactsHandler
{

MetadataEventResponse refreshProjectVersionArtifacts(ProjectData project, String versionId, List<ArtifactGeneration> fileGenerations);

void delete(String groupId, String artifactId, String versionId);



}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Goldman Sachs
// Copyright 2022 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,9 +15,16 @@

package org.finos.legend.depot.store.artifacts.api.generation.file;

import org.finos.legend.depot.store.artifacts.api.ProjectVersionArtifactsHandler;
import java.util.List;
import org.finos.legend.depot.domain.api.MetadataEventResponse;
import org.finos.legend.depot.domain.generation.file.FileGeneration;
import org.finos.legend.depot.domain.project.ProjectData;

public interface FileGenerationsVersionArtifactsHandler extends ProjectVersionArtifactsHandler
public interface FileGenerationsVersionArtifactsHandler
{
MetadataEventResponse refreshProjectVersionArtifacts(ProjectData project, String versionId, List<FileGeneration> fileGenerations);

void delete(String groupId, String artifactId, String versionId);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2022 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package org.finos.legend.depot.store.artifacts.services.file;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.finos.legend.depot.artifacts.repository.api.ArtifactRepository;
import org.finos.legend.depot.artifacts.repository.domain.ArtifactType;
import org.finos.legend.depot.domain.api.MetadataEventResponse;
import org.finos.legend.depot.domain.generation.artifact.ArtifactGeneration;
import org.finos.legend.depot.domain.generation.artifact.StoredArtifactGeneration;
import org.finos.legend.depot.domain.project.ProjectData;
import org.finos.legend.depot.services.api.generation.artifact.ManageArtifactGenerationsService;
import org.finos.legend.depot.store.artifacts.api.generation.artifact.ArtifactGenerationsVersionArtifactsHandler;
import org.finos.legend.sdlc.domain.model.entity.Entity;
import org.finos.legend.sdlc.serialization.EntityLoader;
import org.slf4j.Logger;

@Singleton
public class ArtifactGenerationHandler implements ArtifactGenerationsVersionArtifactsHandler
{

public static final String VERSIONED_ENTITIES = "versioned-entities";
public static final String PURE_PACKAGE_SEPARATOR = "::";
public static final String PATH = "/";
private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(ArtifactGenerationHandler.class);
protected final ManageArtifactGenerationsService generations;
private final ArtifactRepository repository;

@Inject
public ArtifactGenerationHandler(ArtifactRepository repository, ManageArtifactGenerationsService artifactGenerationService)
{
this.repository = repository;
this.generations = artifactGenerationService;
}


@Override
public MetadataEventResponse refreshProjectVersionArtifacts(ProjectData project, String versionId, List<ArtifactGeneration> artifactGenerations)
{
MetadataEventResponse response = new MetadataEventResponse();

Map<String, Entity> entities = findEntitiesByPath(project.getGroupId(), project.getArtifactId(), versionId);
Set<String> entityPaths = entities.keySet();
String message = String.format(" %s version %s found %s artifact generations", project.getProjectId(), versionId, artifactGenerations.size());
LOGGER.info(message);
response.addMessage(message);
artifactGenerations.forEach(generation ->
{
String generator = null;
Optional<String> entityKey = entityPaths.stream().filter(s -> generation.getPath().startsWith(PATH + s)).findFirst();
if (entityKey.isPresent())
{
generator = entities.get(entityKey.get()).getPath();
}
StoredArtifactGeneration storedArtifactGeneration = new StoredArtifactGeneration(project.getGroupId(), project.getArtifactId(), versionId, generator, generation);
this.generations.createOrUpdate(storedArtifactGeneration);
});
return response;
}

private Map<String, Entity> findEntitiesByPath(String groupId, String artifactId, String versionId)
{
Map<String, Entity> entityMap = new HashMap<>();
Optional<File> entitiesFiles = repository.findFiles(ArtifactType.ENTITIES, groupId, artifactId, versionId).stream().filter(file -> isEntitiesArtifactFile(versionId, file)).findFirst();
entitiesFiles.map(file -> EntityLoader.newEntityLoader(file).getAllEntities().collect(Collectors.toList())).orElse(Collections.emptyList())
.forEach(entity -> entityMap.put(entity.getPath().replace(PURE_PACKAGE_SEPARATOR, PATH), entity));
return entityMap;
}

private boolean isEntitiesArtifactFile(String versionId, File file)
{
String entitiesFileName = "entities-" + versionId;
return file.getName().contains(entitiesFileName) && !file.getName().contains(VERSIONED_ENTITIES);
}

@Override
public void delete(String groupId, String artifactId, String versionId)
{
this.generations.delete(groupId, artifactId, versionId);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2021 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package org.finos.legend.depot.store.artifacts.services.file;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.finos.legend.depot.domain.api.MetadataEventResponse;
import org.finos.legend.depot.domain.generation.artifact.ArtifactGeneration;
import org.finos.legend.depot.domain.generation.file.FileGeneration;
import org.finos.legend.depot.domain.project.ProjectData;
import org.finos.legend.depot.store.artifacts.api.generation.FileArtifactGenerationsVersionArtifactsHandler;
import org.finos.legend.depot.store.artifacts.api.generation.artifact.ArtifactGenerationsVersionArtifactsHandler;
import org.finos.legend.depot.store.artifacts.api.generation.file.FileGenerationsProvider;
import org.finos.legend.depot.store.artifacts.api.generation.file.FileGenerationsVersionArtifactsHandler;


public class FileArtifactGenerationVersionsHandler implements FileArtifactGenerationsVersionArtifactsHandler
{

private final FileGenerationsProvider fileGenerationsProvider;
private final ArtifactGenerationsVersionArtifactsHandler artifactGenerationHandler;
private final FileGenerationsVersionArtifactsHandler fileGenerationHandler;

@Inject
public FileArtifactGenerationVersionsHandler(FileGenerationsProvider fileGenerationsProvider, ArtifactGenerationsVersionArtifactsHandler artifactGenerationHandler, FileGenerationsVersionArtifactsHandler fileGenerationHandler)
{
this.fileGenerationsProvider = fileGenerationsProvider;
this.artifactGenerationHandler = artifactGenerationHandler;
this.fileGenerationHandler = fileGenerationHandler;
}

@Override
public MetadataEventResponse refreshProjectVersionArtifacts(ProjectData project, String versionId, List<File> files)
{
List<FileGeneration> fileGenerations = this.fileGenerationsProvider.loadArtifacts(files);
MetadataEventResponse response = new MetadataEventResponse();
response.combine(this.fileGenerationHandler.refreshProjectVersionArtifacts(project, versionId, fileGenerations));
response.combine(this.artifactGenerationHandler.refreshProjectVersionArtifacts(project, versionId,
fileGenerations.stream().map(e -> new ArtifactGeneration(e.getPath(), e.getContent())).collect(Collectors.toList())));
return response;
}

@Override
public void delete(String groupId, String artifactId, String versionId)
{
this.fileGenerationHandler.delete(groupId, artifactId, versionId);
this.artifactGenerationHandler.delete(groupId, artifactId, versionId);
}
}
Loading

0 comments on commit 2c1356e

Please sign in to comment.