use of org.eclipse.winery.model.ids.definitions.EntityTemplateId in project winery by eclipse.
the class ConsistencyChecker method checkPropertiesValidation.
private void checkPropertiesValidation(DefinitionsChildId id) {
if (!(id instanceof EntityTemplateId)) {
return;
}
TEntityTemplate entityTemplate;
try {
// TEntityTemplate is abstract. IRepository does not offer getElement for abstract ids
// Therefore, we have to use the detour through getDefinitions
entityTemplate = (TEntityTemplate) configuration.getRepository().getDefinitions(id).getElement();
} catch (IllegalStateException e) {
LOGGER.debug("Illegal State Exception during reading of id {}", id.toReadableString(), e);
printAndAddError(id, "Reading error " + e.getMessage());
return;
} catch (ClassCastException e) {
LOGGER.error("Something wrong in the consistency between Ids and the TOSCA data model. See http://eclipse.github.io/winery/dev/id-system.html for more information on the ID system.");
printAndAddError(id, "Critical error at analysis: " + e.getMessage());
return;
}
if (Objects.isNull(entityTemplate.getType())) {
// no printing necessary; type consistency is checked at other places
return;
}
TEntityType entityType;
try {
entityType = configuration.getRepository().getTypeForTemplate(entityTemplate);
} catch (IllegalStateException e) {
LOGGER.debug("Illegal State Exception during getting type for template {}", entityTemplate.getId(), e);
printAndAddError(id, "Reading error " + e.getMessage());
return;
}
TEntityTemplate.Properties definedProps = entityTemplate.getProperties();
if (requiresProperties(entityType) && definedProps == null) {
printAndAddError(id, "Properties required, but no properties defined");
return;
} else if (!requiresProperties(entityType) && definedProps != null) {
printAndAddError(id, "No properties required by type, but properties were defined on template");
return;
} else if (definedProps == null) {
// no properties required and none defined
return;
}
if (definedProps instanceof TEntityTemplate.XmlProperties) {
// check defined properties any against the xml schema
@Nullable final Object any = ((TEntityTemplate.XmlProperties) definedProps).getAny();
if (any == null) {
printAndAddError(id, "Properties required, but no XmlProperties were empty (any case)");
return;
}
TEntityType.PropertiesDefinition def = entityType.getProperties();
if (def == null) {
printAndAddError(id, "XmlProperties were given, but no XmlPropertiesDefinition was specified");
return;
}
if (def instanceof TEntityType.XmlElementDefinition) {
final QName element = ((TEntityType.XmlElementDefinition) def).getElement();
final Map<String, RepositoryFileReference> mapFromLocalNameToXSD = configuration.getRepository().getXsdImportManager().getMapFromLocalNameToXSD(new Namespace(element.getNamespaceURI(), false), false);
final RepositoryFileReference repositoryFileReference = mapFromLocalNameToXSD.get(element.getLocalPart());
if (repositoryFileReference == null) {
printAndAddError(id, "No Xml Schema definition found for " + element);
return;
}
validate(repositoryFileReference, any, id);
}
} else if (definedProps instanceof TEntityTemplate.WineryKVProperties) {
final WinerysPropertiesDefinition winerysPropertiesDefinition = entityType.getWinerysPropertiesDefinition();
Map<String, String> kvProperties = ((TEntityTemplate.WineryKVProperties) definedProps).getKVProperties();
if (kvProperties.isEmpty()) {
printAndAddError(id, "Properties required, but no properties set (kvproperties case)");
return;
}
for (PropertyDefinitionKV propertyDefinitionKV : winerysPropertiesDefinition.getPropertyDefinitions()) {
String key = propertyDefinitionKV.getKey();
if (kvProperties.get(key) == null) {
printAndAddError(id, "Property " + key + " required, but not set.");
} else {
// removeNamespaceProperties the key from the map to enable checking below whether a property is defined which not requried by the property definition
kvProperties.remove(key);
}
}
// If any key is left, this is a key not defined at the schema
for (Object o : kvProperties.keySet()) {
printAndAddError(id, "Property " + o + " set, but not defined at schema.");
}
} else if (definedProps instanceof TEntityTemplate.YamlProperties) {
// FIXME todo
LOGGER.debug("YAML Properties checking is not yet implemented!");
}
}
use of org.eclipse.winery.model.ids.definitions.EntityTemplateId in project winery by eclipse.
the class AbstractComponentsWithTypeReferenceResource method onJsonPost.
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response onJsonPost(QNameWithTypeApiData jsonData) {
// only check for type parameter as namespace and name are checked in super.onPost
if (StringUtils.isEmpty(jsonData.type)) {
return Response.status(Status.BAD_REQUEST).build();
}
ResourceResult creationResult = super.onPost(jsonData.namespace, jsonData.localname);
if (!creationResult.isSuccess()) {
return creationResult.getResponse();
}
if (creationResult.getStatus().equals(Status.CREATED)) {
final DefinitionsChildId id = (DefinitionsChildId) creationResult.getId();
final TDefinitions definitions = requestRepository.getDefinitions(id);
final TExtensibleElements element = definitions.getElement();
((HasType) element).setType(jsonData.type);
if (id instanceof EntityTemplateId) {
BackendUtils.initializeProperties(requestRepository, (TEntityTemplate) element);
}
try {
BackendUtils.persist(requestRepository, id, definitions);
} catch (IOException e) {
throw new WebApplicationException(e);
}
}
return creationResult.getResponse();
}
Aggregations