use of org.eclipse.winery.model.ids.definitions.ArtifactTypeId in project winery by eclipse.
the class ArtifactTypesResource method getArtifactTypeForExtension.
/**
* Returns the first matching ArtifactTypeResource for the given file extension. Returns null if no such
* ArtifactType can be found
* <p>
* The case of the extension is ignored.
* <p>
* This is more a DAO method
*/
public ArtifactTypeResource getArtifactTypeForExtension(String extension) {
SortedSet<ArtifactTypeId> allArtifactTypeIds = RepositoryFactory.getRepository().getAllDefinitionsChildIds(ArtifactTypeId.class);
ArtifactTypeResource res = null;
for (ArtifactTypeId id : allArtifactTypeIds) {
ArtifactTypeResource r = new ArtifactTypeResource(id);
if (extension.equalsIgnoreCase(r.getAssociatedFileExtension())) {
res = r;
break;
}
}
return res;
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTypeId in project winery by eclipse.
the class DASpecification method getArtifactTypeOfDA.
public static TArtifactType getArtifactTypeOfDA(TDeploymentArtifact deploymentArtifact) {
QName DAArtifactTypeQName = deploymentArtifact.getArtifactType();
ArtifactTypeId artifactTypeId = new ArtifactTypeId(DAArtifactTypeQName);
return RepositoryFactory.getRepository().getElement(artifactTypeId);
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTypeId in project winery by eclipse.
the class GenericArtifactsResource method generateArtifact.
/**
* @return TImplementationArtifact | TDeploymentArtifact (XML) | URL of generated IA zip (in case of autoGenerateIA)
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Creates a new implementation/deployment artifact. If an implementation artifact with the same name already exists, it is <em>overridden</em>.")
@SuppressWarnings("unchecked")
public Response generateArtifact(GenerateArtifactApiData apiData, @Context UriInfo uriInfo) {
// we assume that the parent ComponentInstance container exists
final IRepository repository = RepositoryFactory.getRepository();
if (StringUtils.isEmpty(apiData.artifactName)) {
return Response.status(Status.BAD_REQUEST).entity("Empty artifactName").build();
}
if (StringUtils.isEmpty(apiData.artifactType)) {
if (StringUtils.isEmpty(apiData.artifactTemplateName) || StringUtils.isEmpty(apiData.artifactTemplateNamespace)) {
if (StringUtils.isEmpty(apiData.artifactTemplate)) {
return Response.status(Status.BAD_REQUEST).entity("No artifact type given and no template given. Cannot guess artifact type").build();
}
}
}
if (!StringUtils.isEmpty(apiData.autoGenerateIA)) {
if (StringUtils.isEmpty(apiData.javaPackage)) {
return Response.status(Status.BAD_REQUEST).entity("no java package name supplied for IA auto generation.").build();
}
if (StringUtils.isEmpty(apiData.interfaceName)) {
return Response.status(Status.BAD_REQUEST).entity("no interface name supplied for IA auto generation.").build();
}
}
// convert second calling form to first calling form
if (!StringUtils.isEmpty(apiData.artifactTemplate)) {
QName qname = QName.valueOf(apiData.artifactTemplate);
apiData.artifactTemplateName = qname.getLocalPart();
apiData.artifactTemplateNamespace = qname.getNamespaceURI();
}
Document doc = null;
// if invalid, abort and do not create anything
if (!StringUtils.isEmpty(apiData.artifactSpecificContent)) {
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
StringReader sr = new StringReader(apiData.artifactSpecificContent);
is.setCharacterStream(sr);
doc = db.parse(is);
} catch (Exception e) {
// FIXME: currently we allow a single element only. However, the content should be internally wrapped by an (arbitrary) XML element as the content will be nested in the artifact element, too
LOGGER.debug("Invalid content", e);
return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
}
}
// determine artifactTemplate and artifactType
ArtifactTypeId artifactTypeId;
ArtifactTemplateId artifactTemplateId = null;
boolean doAutoCreateArtifactTemplate = !(StringUtils.isEmpty(apiData.autoCreateArtifactTemplate) || apiData.autoCreateArtifactTemplate.equalsIgnoreCase("no") || apiData.autoCreateArtifactTemplate.equalsIgnoreCase("false"));
if (!doAutoCreateArtifactTemplate) {
// no auto creation
if (!StringUtils.isEmpty(apiData.artifactTemplateName) && !StringUtils.isEmpty(apiData.artifactTemplateNamespace)) {
QName artifactTemplateQName = new QName(apiData.artifactTemplateNamespace, apiData.artifactTemplateName);
artifactTemplateId = BackendUtils.getDefinitionsChildId(ArtifactTemplateId.class, artifactTemplateQName);
}
if (StringUtils.isEmpty(apiData.artifactType)) {
// derive the type from the artifact template
if (artifactTemplateId == null) {
return Response.status(Status.NOT_ACCEPTABLE).entity("No artifactTemplate and no artifactType provided. Deriving the artifactType is not possible.").build();
}
@NonNull final QName type = repository.getElement(artifactTemplateId).getType();
artifactTypeId = BackendUtils.getDefinitionsChildId(ArtifactTypeId.class, type);
} else {
// artifactTypeStr is directly given, use that
artifactTypeId = BackendUtils.getDefinitionsChildId(ArtifactTypeId.class, apiData.artifactType);
}
} else {
if (StringUtils.isEmpty(apiData.artifactType)) {
return Response.status(Status.BAD_REQUEST).entity("Artifact template auto creation requested, but no artifact type supplied.").build();
}
artifactTypeId = BackendUtils.getDefinitionsChildId(ArtifactTypeId.class, apiData.artifactType);
// ensure that given type exists
if (!repository.exists(artifactTypeId)) {
LOGGER.debug("Artifact type {} is created", apiData.artifactType);
final TArtifactType element = repository.getElement(artifactTypeId);
try {
repository.setElement(artifactTypeId, element);
} catch (IOException e) {
throw new WebApplicationException(e);
}
}
if (StringUtils.isEmpty(apiData.artifactTemplateName) || StringUtils.isEmpty(apiData.artifactTemplateNamespace)) {
// no explicit name provided
// we use the artifactNameStr as prefix for the
// artifact template name
// we create a new artifact template in the namespace of the parent
// element
Namespace namespace = this.resWithNamespace.getNamespace();
artifactTemplateId = new ArtifactTemplateId(namespace, new XmlId(apiData.artifactName + "artifactTemplate", false));
} else {
QName artifactTemplateQName = new QName(apiData.artifactTemplateNamespace, apiData.artifactTemplateName);
artifactTemplateId = new ArtifactTemplateId(artifactTemplateQName);
}
// even if artifactTemplate does not exist, it is loaded
final TArtifactTemplate artifactTemplate = repository.getElement(artifactTemplateId);
artifactTemplate.setType(artifactTypeId.getQName());
try {
repository.setElement(artifactTemplateId, artifactTemplate);
} catch (IOException e) {
throw new WebApplicationException(e);
}
}
// variable artifactTypeId is set
// variable artifactTemplateId is not null if artifactTemplate has been generated
// we have to generate the DA/IA resource now
// Doing it here instead of doing it at the subclasses is dirty on the
// one hand, but quicker to implement on the other hand
// Create the artifact itself
ArtifactT resultingArtifact;
if (this instanceof ImplementationArtifactsResource) {
TImplementationArtifact.Builder builder = new TImplementationArtifact.Builder(artifactTypeId.getQName()).setName(apiData.artifactName).setInterfaceName(apiData.interfaceName).setOperationName(apiData.operationName);
if (artifactTemplateId != null) {
builder.setArtifactRef(artifactTemplateId.getQName());
}
if (doc != null) {
// the content has been checked for validity at the beginning of the method.
// If this point in the code is reached, the XML has been parsed into doc
// just copy over the dom node. Hopefully, that works...
builder.setAny(Collections.singletonList(doc.getDocumentElement()));
}
resultingArtifact = (ArtifactT) builder.build();
} else {
// for comments see other branch
TDeploymentArtifact.Builder builder = new TDeploymentArtifact.Builder(apiData.artifactName, artifactTypeId.getQName());
if (artifactTemplateId != null) {
builder.setArtifactRef(artifactTemplateId.getQName());
}
if (doc != null) {
builder.setAny(Collections.singletonList(doc.getDocumentElement()));
}
resultingArtifact = (ArtifactT) builder.build();
}
this.list.add(resultingArtifact);
// TODO: Check for error, and in case one found return it
RestUtils.persist(super.res);
if (StringUtils.isEmpty(apiData.autoGenerateIA)) {
// No IA generation
return Response.created(URI.create(EncodingUtil.URLencode(apiData.artifactName))).entity(resultingArtifact).build();
} else {
// after everything was created, we fire up the artifact generation
return this.generateImplementationArtifact(apiData.interfaceName, apiData.javaPackage, uriInfo, artifactTemplateId);
}
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTypeId in project winery by eclipse.
the class IRepository method getReferencedDefinitionsChildIds.
/**
* Determines the referenced definition children Ids. Does NOT return the included files.
*
* @return a collection of referenced definition child Ids
*/
default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(ArtifactTemplateId id) throws RepositoryCorruptException {
Collection<DefinitionsChildId> ids = new ArrayList<>();
final TArtifactTemplate artifactTemplate = this.getElement(id);
// "Export" type
QName type = artifactTemplate.getType();
if (type == null) {
throw new RepositoryCorruptException("Type is null for " + id.toReadableString());
} else {
ids.add(new ArtifactTypeId(type));
}
return ids;
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTypeId in project winery by eclipse.
the class IRepository method getReferencedTOSCAComponentImplementationArtifactIds.
/**
* Helper method
*
* @param ids the list of ids to add the new ids to
* @param implementationArtifacts the implementation artifacts belonging to the given id
* @param id the id to handle
*/
default Collection<DefinitionsChildId> getReferencedTOSCAComponentImplementationArtifactIds(Collection<DefinitionsChildId> ids, List<TImplementationArtifact> implementationArtifacts, DefinitionsChildId id) {
if (implementationArtifacts != null) {
for (TImplementationArtifact ia : implementationArtifacts) {
QName qname;
if ((qname = ia.getArtifactRef()) != null) {
ids.add(new ArtifactTemplateId(qname));
}
ids.add(new ArtifactTypeId(ia.getArtifactType()));
}
}
return ids;
}
Aggregations