use of org.eclipse.winery.model.tosca.Definitions in project winery by eclipse.
the class BackendUtils method getTArtifactTemplate.
/**
* @param directoryId DirectoryID of the TArtifactTemplate that should be returned.
* @return The TArtifactTemplate corresponding to the directoryId.
*/
public static TArtifactTemplate getTArtifactTemplate(DirectoryId directoryId) {
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions((ArtifactTemplateId) directoryId.getParent());
try (InputStream is = RepositoryFactory.getRepository().newInputStream(ref)) {
Unmarshaller u = JAXBSupport.createUnmarshaller();
Definitions defs = ((Definitions) u.unmarshal(is));
for (TExtensibleElements elem : defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation()) {
if (elem instanceof TArtifactTemplate) {
return (TArtifactTemplate) elem;
}
}
} catch (IOException e) {
LOGGER.error("Error reading definitions of " + directoryId.getParent() + " at " + ref.getFileName(), e);
} catch (JAXBException e) {
LOGGER.error("Error in XML in " + ref.getFileName(), e);
}
return null;
}
use of org.eclipse.winery.model.tosca.Definitions in project winery by eclipse.
the class BackendUtils method createWrapperDefinitionsAndInitialEmptyElement.
public static Definitions createWrapperDefinitionsAndInitialEmptyElement(IRepository repository, DefinitionsChildId id) {
final Definitions definitions = createWrapperDefinitions(id);
HasIdInIdOrNameField element;
if (id instanceof RelationshipTypeImplementationId) {
element = new TRelationshipTypeImplementation();
} else if (id instanceof NodeTypeImplementationId) {
element = new TNodeTypeImplementation();
} else if (id instanceof RequirementTypeId) {
element = new TRequirementType();
} else if (id instanceof NodeTypeId) {
element = new TNodeType();
} else if (id instanceof RelationshipTypeId) {
element = new TRelationshipType();
} else if (id instanceof CapabilityTypeId) {
element = new TCapabilityType();
} else if (id instanceof ArtifactTypeId) {
element = new TArtifactType();
} else if (id instanceof PolicyTypeId) {
element = new TPolicyType();
} else if (id instanceof PolicyTemplateId) {
element = new TPolicyTemplate();
} else if (id instanceof ServiceTemplateId) {
element = new TServiceTemplate();
} else if (id instanceof ArtifactTemplateId) {
element = new TArtifactTemplate();
} else if (id instanceof XSDImportId) {
// TImport has no id; thus directly generating it without setting an id
TImport tImport = new TImport();
definitions.setElement(tImport);
return definitions;
} else {
throw new IllegalStateException("Unhandled id branch. Could happen for XSDImportId");
}
copyIdToFields(element, id);
definitions.setElement((TExtensibleElements) element);
return definitions;
}
use of org.eclipse.winery.model.tosca.Definitions in project winery by eclipse.
the class IGenericRepository method setElement.
/**
* Updates the element belonging to the given DefinitionsChildId Regenerates wrapper definitions; thus all
* extensions at the wrapper definitions are lost
*
* @param id the DefinitionsChildId to update
* @param element the element to set
* @throws IOException if persisting went wrong
*/
default void setElement(DefinitionsChildId id, TExtensibleElements element) throws IOException {
// default implementation on the server side
// the client side has to use the REST method
Definitions definitions = BackendUtils.createWrapperDefinitions(id);
definitions.setElement(element);
BackendUtils.persist(id, definitions);
}
use of org.eclipse.winery.model.tosca.Definitions in project winery by eclipse.
the class ImportUtils method getTheImport.
/**
* SIDE EFFECT: persists the import when something is changed
*/
public static Optional<TImport> getTheImport(GenericImportId id) {
Objects.requireNonNull(id);
TImport theImport;
boolean needsPersistence = false;
final IRepository repository = RepositoryFactory.getRepository();
final Definitions 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(id, definitions);
} catch (IOException e) {
LOGGER.error("Could not persist changes", e);
}
}
return Optional.of(theImport);
}
use of org.eclipse.winery.model.tosca.Definitions in project winery by eclipse.
the class FilebasedRepository method rename.
@Override
public void rename(DefinitionsChildId oldId, DefinitionsChildId newId) throws IOException {
Objects.requireNonNull(oldId);
Objects.requireNonNull(newId);
if (oldId.equals(newId)) {
// we do not do anything - even not throwing an error
return;
}
Definitions definitions = this.getDefinitions(oldId);
RepositoryFileReference oldRef = BackendUtils.getRefOfDefinitions(oldId);
RepositoryFileReference newRef = BackendUtils.getRefOfDefinitions(newId);
// oldRef points to the definitions file,
// getParent() returns the directory
// we need toFile(), because we rely on FileUtils.moveDirectoryToDirectory
File oldDir = this.id2AbsolutePath(oldRef.getParent()).toFile();
File newDir = this.id2AbsolutePath(newRef.getParent()).toFile();
org.apache.commons.io.FileUtils.moveDirectory(oldDir, newDir);
// Update definitions and store it
// This also updates the definitions of componentInstanceResource
BackendUtils.updateWrapperDefinitions(newId, definitions);
// This works, because the definitions object here is the same as the definitions object treated at copyIdToFields
// newId has to be passed, because the id is final at AbstractComponentInstanceResource
BackendUtils.copyIdToFields((HasIdInIdOrNameField) definitions.getElement(), newId);
try {
BackendUtils.persist(definitions, newRef, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
} catch (InvalidPathException e) {
LOGGER.debug("Invalid path during write", e);
// QUICK FIX
// Somewhere, the first letter is deleted --> /odetypes/http%3A%2F%2Fwww.example.org%2F05/
// We just ignore it for now
}
}
Aggregations