use of org.eclipse.winery.model.tosca.TEntityType in project winery by eclipse.
the class PropertiesResource method getProperties.
/**
* Gets the defined properties. If no properties are defined, an empty JSON object is returned. If k/v properties
* are defined, then a JSON is returned. Otherwise an XML is returned.
*
* @return Key/Value map in the case of Winery WPD mode - else instance of XML Element in case of non-key/value
* properties
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
@NonNull
public Response getProperties() {
TEntityType tempType = RepositoryFactory.getRepository().getTypeForTemplate(this.template);
WinerysPropertiesDefinition wpd = tempType.getWinerysPropertiesDefinition();
TEntityTemplate.Properties props = this.template.getProperties();
if (wpd == null) {
// These can be null resulting in 200 No Content at the caller
if (props == null) {
return Response.ok().entity("{}").type(MediaType.APPLICATION_JSON).build();
} else {
@Nullable final Object any = props.getAny();
if (any == null) {
LOGGER.debug("XML properties expected, but none found. Returning empty JSON.");
return Response.ok().entity("{}").type(MediaType.APPLICATION_JSON).build();
}
try {
@ADR(6) String xmlAsString = BackendUtils.getXMLAsString(TEntityTemplate.Properties.class, props, true);
return Response.ok().entity(xmlAsString).type(MediaType.TEXT_XML).build();
} catch (Exception e) {
throw new WebApplicationException(e);
}
}
} else {
Map<String, String> kvProperties = this.template.getProperties().getKVProperties();
return Response.ok().entity(kvProperties).type(MediaType.APPLICATION_JSON).build();
}
}
use of org.eclipse.winery.model.tosca.TEntityType in project winery by eclipse.
the class IWineryRepositoryCommon method getTypeForTemplate.
/**
* Returns the stored type for the given template
*
* @param template the template to determine the type for
* @throws NullPointerException if template.getType() returns null
*/
// we suppress "unchecked" as we use Class.forName
@SuppressWarnings("unchecked")
default TEntityType getTypeForTemplate(TEntityTemplate template) {
QName type = template.getType();
Objects.requireNonNull(type);
// Possibilities:
// a) try all possibly types whether an appropriate QName exists
// b) derive type class from template class. Determine appropriate template element afterwards.
// We go for b)
String instanceResourceClassName = template.getClass().toString();
int idx = instanceResourceClassName.lastIndexOf('.');
// get everything from ".T", where "." is the last dot
instanceResourceClassName = instanceResourceClassName.substring(idx + 2);
// strip off "Template"
instanceResourceClassName = instanceResourceClassName.substring(0, instanceResourceClassName.length() - "Template".length());
// add "Type"
instanceResourceClassName += "Type";
// an id is required to instantiate the resource
String idClassName = "org.eclipse.winery.common.ids.definitions." + instanceResourceClassName + "Id";
org.slf4j.Logger LOGGER = LoggerFactory.getLogger(this.getClass());
LOGGER.debug("idClassName: {}", idClassName);
// Get instance of id class having "type" as id
Class<? extends DefinitionsChildId> idClass;
try {
idClass = (Class<? extends DefinitionsChildId>) Class.forName(idClassName);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Could not determine id class", e);
}
Constructor<? extends DefinitionsChildId> idConstructor;
try {
idConstructor = idClass.getConstructor(QName.class);
} catch (NoSuchMethodException | SecurityException e) {
throw new IllegalStateException("Could not get QName id constructor", e);
}
DefinitionsChildId typeId;
try {
typeId = idConstructor.newInstance(type);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new IllegalStateException("Could not instantiate type", e);
}
final Definitions definitions = this.getDefinitions(typeId);
return (TEntityType) definitions.getElement();
}
use of org.eclipse.winery.model.tosca.TEntityType in project winery by eclipse.
the class PropertiesDefinitionResource method onJsonPost.
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response onJsonPost(PropertiesDefinitionResourceApiData data) {
if (data.selectedValue == PropertiesDefinitionEnum.Element || data.selectedValue == PropertiesDefinitionEnum.Type) {
// first of all, remove Winery's Properties definition (if it exists)
ModelUtilities.removeWinerysPropertiesDefinition(this.getEntityType());
// replace old properties definition by new one
PropertiesDefinition def = new PropertiesDefinition();
if (data.propertiesDefinition.getElement() != null) {
def.setElement(data.propertiesDefinition.getElement());
} else if (data.propertiesDefinition.getType() != null) {
def.setType(data.propertiesDefinition.getType());
} else {
return Response.status(Status.BAD_REQUEST).entity("Wrong data submitted!").build();
}
this.getEntityType().setPropertiesDefinition(def);
List<String> errors = new ArrayList<>();
BackendUtils.deriveWPD(this.getEntityType(), errors);
// currently the errors are just logged
for (String error : errors) {
PropertiesDefinitionResource.LOGGER.debug(error);
}
return RestUtils.persist(this.parentRes);
} else if (data.selectedValue == PropertiesDefinitionEnum.Custom) {
TEntityType et = this.parentRes.getEntityType();
// clear current properties definition
et.setPropertiesDefinition(null);
// create winery properties definition and persist it
ModelUtilities.replaceWinerysPropertiesDefinition(et, data.winerysPropertiesDefinition);
String namespace = data.winerysPropertiesDefinition.getNamespace();
NamespaceManager namespaceManager = RepositoryFactory.getRepository().getNamespaceManager();
if (!namespaceManager.hasPrefix(namespace)) {
namespaceManager.addNamespace(namespace);
}
return RestUtils.persist(this.parentRes);
}
return Response.status(Status.BAD_REQUEST).entity("Wrong data submitted!").build();
}
use of org.eclipse.winery.model.tosca.TEntityType in project winery by eclipse.
the class FilebasedRepositoryTest method getTypeForTemplateReturnsCorrectTypeForMyTinyTestArtifactTemplate.
@Test
public void getTypeForTemplateReturnsCorrectTypeForMyTinyTestArtifactTemplate() throws Exception {
this.setRevisionTo("1374c8c13ec64899360511dbe0414223b88d3b01");
ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId("http://opentosca.org/artifacttemplates", "MyTinyTest", false);
final TArtifactTemplate artifactTemplate = this.repository.getElement(artifactTemplateId);
final TEntityType typeForTemplate = this.repository.getTypeForTemplate(artifactTemplate);
Assert.assertEquals(new QName("http://winery.opentosca.org/test/artifacttypes", "MiniArtifactType"), new QName(typeForTemplate.getTargetNamespace(), typeForTemplate.getName()));
}
use of org.eclipse.winery.model.tosca.TEntityType in project winery by eclipse.
the class TOSCATransformer method addTEntityTypes.
public static void addTEntityTypes(QName nodeTypeQName, TOSCAEntity entity, Class<? extends TEntityType> tEntityTypeClass) {
TEntityType entityType = getEntityType(nodeTypeQName, tEntityTypeClass);
entity.addTEntityType(entityType);
Optional.of(entityType).map(TEntityType::getDerivedFrom).map(TEntityType.DerivedFrom::getTypeRef).ifPresent(qName -> addTEntityTypes(qName, entity, tEntityTypeClass));
}
Aggregations