use of org.eclipse.winery.repository.rest.resources.apiData.GenerateArtifactApiData in project winery by eclipse.
the class EdmmResource method createPlaceholders.
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Path("create-placeholders-scripts")
public Response createPlaceholders(String componentType, @Context UriInfo uriInfo) throws IOException {
// adding the interface to the component node type
NodeTypeResource nodeTypeResource = new NodeTypesResource().getComponentInstanceResource(EncodingUtil.URLencode(NODE_TYPES), componentType);
// the interfaces will be overridden
nodeTypeResource.getInterfaces().onPost(buildInterfacesForNodeType(LIFECYCLE_NAME, LIFECYCLE));
for (String operation : LIFECYCLE) {
// adding a new artifact template
final String artifactLocalName = componentType + "-" + StringUtils.capitalize(operation);
ArtifactTemplatesResource artifactTemplatesResource = new ArtifactTemplatesResource();
QNameWithTypeApiData qNameWithTypeApiData = new QNameWithTypeApiData(artifactLocalName, ARTIFACT_TEMPLATES, "{" + ARTIFACT_TYPES + "}Script");
artifactTemplatesResource.onJsonPost(qNameWithTypeApiData);
// adding the placeholder file
ArtifactTemplateResource artifactTemplateResource = artifactTemplatesResource.getComponentInstanceResource(EncodingUtil.URLencode(ARTIFACT_TEMPLATES), artifactLocalName);
boolean fileExists = artifactTemplateResource.getFilesResource().getAllFileMetas().stream().anyMatch(fileMeta -> fileMeta.getName().equals(operation + ".sh"));
// if the file exists it will be overridden, but we do not want that.
if (!fileExists) {
artifactTemplateResource.getFilesResource().putContentToFile(operation + ".sh", getPlaceholderFile(operation), "text/x-sh");
artifactTemplateResource.synchronizeReferences();
}
// creating a new node type implementation
String nodeTypeImplementationLocalName = componentType + "-IA";
NodeTypeImplementationsResource nodeTypeImplementationsResource = new NodeTypeImplementationsResource();
qNameWithTypeApiData = new QNameWithTypeApiData(nodeTypeImplementationLocalName, NODE_TYPE_IMPLEMENTATIONS, "{" + NODE_TYPES + "}" + componentType);
nodeTypeImplementationsResource.onJsonPost(qNameWithTypeApiData);
// linking the artifact template to the node type implementation
NodeTypeImplementationResource nodeTypeImplementationResource = nodeTypeImplementationsResource.getComponentInstanceResource(EncodingUtil.URLencode(NODE_TYPE_IMPLEMENTATIONS), nodeTypeImplementationLocalName);
GenerateArtifactApiData artifactApiData = new GenerateArtifactApiData();
artifactApiData.artifactName = operation;
artifactApiData.artifactTemplate = "{" + ARTIFACT_TEMPLATES + "}" + artifactLocalName;
artifactApiData.interfaceName = LIFECYCLE_NAME;
artifactApiData.operationName = operation;
boolean implementationResourceExists = nodeTypeImplementationResource.getImplementationArtifacts().getAllArtifactResources().stream().anyMatch(artifactResource -> {
TImplementationArtifact implementationArtifact = artifactResource.getImplementationArtifact();
return operation.equals(implementationArtifact.getOperationName()) && LIFECYCLE_NAME.equals(implementationArtifact.getInterfaceName()) && implementationArtifact.getArtifactRef() != null && artifactApiData.artifactTemplate.equals(implementationArtifact.getArtifactRef().toString());
});
if (!implementationResourceExists) {
nodeTypeImplementationResource.getImplementationArtifacts().generateArtifact(artifactApiData, uriInfo);
}
}
return Response.status(Response.Status.CREATED).build();
}
Aggregations