use of org.eclipse.winery.model.tosca.TImport in project winery by eclipse.
the class ImportUtils method getTheImport.
/**
* SIDE EFFECT: persists the import when something is changed
*/
public static Optional<TImport> getTheImport(IRepository repository, GenericImportId id) {
Objects.requireNonNull(id);
TImport theImport;
boolean needsPersistence = false;
final TDefinitions definitions = repository.getDefinitions(id);
if (!repository.exists(id)) {
return Optional.empty();
}
if (definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().isEmpty()) {
// definitions exist
// we have to manually assign our import right
theImport = definitions.getImport().get(0);
} else {
// someone created a new import
// store it locally
theImport = (TImport) definitions.getElement();
// undo the side effect of adding it at the wrong place at TDefinitions
definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().clear();
// add import at the right place
definitions.getImport().add(theImport);
// Super class has persisted the definitions
// We have to persist the new variant
needsPersistence = true;
}
if (theImport.getLocation() == null) {
// invalid import -- try to synchronize with storage
SortedSet<RepositoryFileReference> containedFiles = repository.getContainedFiles(id);
// we are only interested in the non-.definitions
for (RepositoryFileReference ref : containedFiles) {
if (!ref.getFileName().endsWith(".definitions")) {
// associated file found
// set the filename of the import to the found xsd
// TODO: no more validity checks are done currently. In the case of XSD: targetNamespace matches, not more than one xsd
theImport.setLocation(ref.getFileName());
needsPersistence = true;
break;
}
}
}
if (needsPersistence) {
try {
BackendUtils.persist(repository, id, definitions);
} catch (IOException e) {
LOGGER.error("Could not persist changes", e);
}
}
return Optional.of(theImport);
}
use of org.eclipse.winery.model.tosca.TImport in project winery by eclipse.
the class XSDImportResource method createNewElement.
@Override
protected TExtensibleElements createNewElement() {
TImport imp = new TImport();
imp.setImportType(XMLConstants.W3C_XML_SCHEMA_NS_URI);
return imp;
}
use of org.eclipse.winery.model.tosca.TImport in project winery by eclipse.
the class GenericImportResource method getFile.
@GET
@Path("{filename}")
public Response getFile(@PathParam("filename") @NonNull final String encodedFileName) {
Objects.requireNonNull(encodedFileName);
final Optional<TImport> theImport = ImportUtils.getTheImport(RepositoryFactory.getRepository(), (GenericImportId) id);
if (!theImport.isPresent()) {
return Response.status(Status.NOT_FOUND).build();
}
@Nullable final String location = theImport.get().getLocation();
@NonNull String fileName = EncodingUtil.URLdecode(encodedFileName);
if (!fileName.equals(location)) {
LOGGER.debug("Filename mismatch %s vs %s", fileName, location);
return Response.status(Status.NOT_FOUND).build();
}
RepositoryFileReference ref = new RepositoryFileReference(this.id, location);
return RestUtils.returnRepoPath(ref, null);
}
use of org.eclipse.winery.model.tosca.TImport in project winery by eclipse.
the class ToCanonical method convert.
public TDataType convert(YTDataType node, String id) {
String name = fixNamespaceDuplication(id, node.getMetadata().get("targetNamespace"));
TDataType.Builder builder = new TDataType.Builder(name).addConstraints(convertList(node.getConstraints(), this::convert));
fillEntityTypeProperties(node, builder);
TDataType result = builder.build();
// FIXME need to actually transform the node.getProperties() to an xml schema
// to be able to import it and add a PropertiesDefinition reference to that schema
String namespace = this.namespace;
if (namespace == null) {
// attempt to patch namespace with the definitions' targetNamespace
namespace = result.getTargetNamespace();
}
if (namespace == null) {
LOGGER.warn("Could not determine namespace for DataType {}. Imports may be incorrect!", id);
return result;
}
TImport importDefinition = new TImport.Builder(Namespaces.XML_NS).setLocation(EncodingUtil.URLencode(namespace) + ".xsd").setNamespace(namespace).build();
if (!this.imports.contains(importDefinition)) {
this.imports.add(importDefinition);
}
return result;
}
use of org.eclipse.winery.model.tosca.TImport in project winery by eclipse.
the class YamlToscaExportUtil method processDefinitionsElement.
@Override
protected Collection<DefinitionsChildId> processDefinitionsElement(IRepository repository, DefinitionsChildId tcId, CsarContentProperties definitionsFileProperties) throws RepositoryCorruptException, IOException {
if (!repository.exists(tcId)) {
String error = "Component instance " + tcId.toReadableString() + " does not exist.";
LOGGER.error(error);
throw new RepositoryCorruptException(error);
}
TDefinitions entryDefinitions = repository.getDefinitions(tcId);
this.getPrepareForExport(repository, tcId, entryDefinitions);
Collection<DefinitionsChildId> referencedDefinitionsChildIds = repository.getReferencedDefinitionsChildIds(tcId);
if (!EXPORT_NORMATIVE_TYPES) {
referencedDefinitionsChildIds.removeIf(id -> id.getQName().getNamespaceURI().startsWith("tosca"));
}
// adjust imports: add imports of definitions
Collection<TImport> imports = new ArrayList<>();
Map<String, QName> importDefinitions = new HashMap<>();
for (DefinitionsChildId id : referencedDefinitionsChildIds) {
this.addToImports(repository, id, imports);
String defName = YamlExporter.getDefinitionsName(repository, id).concat(Constants.SUFFIX_TOSCA_DEFINITIONS);
importDefinitions.put(defName, id.getQName());
}
entryDefinitions.getImport().addAll(imports);
entryDefinitions.setImportDefinitions(importDefinitions);
// END: Definitions modification
YAMLDefinitionsBasedCsarEntry entry = new YAMLDefinitionsBasedCsarEntry(repository, entryDefinitions);
// Custom Adjustments for Service Templates
YamlExportAdjustmentsBuilder adjustmentsBuilder = new YamlExportAdjustmentsBuilder(entry);
if (!EXPORT_NORMATIVE_TYPES) {
adjustmentsBuilder.removeNormativeTypeImports();
}
entry = adjustmentsBuilder.setMetadataName(tcId).build();
this.referencesToPathInCSARMap.put(definitionsFileProperties, entry);
return referencedDefinitionsChildIds;
}
Aggregations