use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class IGenericRepository method getDefinitionsChildIdOfParent.
/**
* Determines the id of the parent in the inheritance hierarchy (if exists).
*
* @return the id of the parent class
*/
default Optional<DefinitionsChildId> getDefinitionsChildIdOfParent(HasInheritanceId id) {
final HasInheritance element = (HasInheritance) this.getDefinitions(id).getElement();
final HasType derivedFrom = element.getDerivedFrom();
QName derivedFromType = null;
if (derivedFrom != null) {
derivedFromType = derivedFrom.getTypeAsQName();
}
if (derivedFromType == null) {
return Optional.empty();
} else {
// Instantiate an id with the same class as the current id
DefinitionsChildId parentId;
Constructor<? extends DefinitionsChildId> constructor;
try {
constructor = id.getClass().getConstructor(QName.class);
} catch (NoSuchMethodException | SecurityException e1) {
throw new IllegalStateException("Could get constructor to instantiate parent id", e1);
}
try {
parentId = constructor.newInstance(derivedFromType);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new IllegalStateException("Could not instantiate id for parent", e);
}
return Optional.of(parentId);
}
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class FilebasedRepository method forceDelete.
public void forceDelete(Class<? extends DefinitionsChildId> definitionsChildIdClazz, Namespace namespace) {
// instantiate new definitions child id with "ID" as id
// this is used to get the absolute path
DefinitionsChildId id = BackendUtils.getDefinitionsChildId(definitionsChildIdClazz, namespace.getEncoded(), "ID", true);
Path path = this.id2AbsolutePath(id);
// do not delete the id, delete the complete namespace
// patrent folder is the namespace folder
path = path.getParent();
FileUtils.forceDelete(path);
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class RestUtils method create.
/**
* Generates given TOSCA element and returns appropriate response code <br />
* <p>
* In the case of an existing resource, the other possible return code is 302. This code has no Status constant,
* therefore we use Status.CONFLICT, which is also possible.
*
* @return <ul> <li> <ul> <li>Status.CREATED (201) if the resource has been created,</li> <li>Status.CONFLICT if the
* resource already exists,</li> <li>Status.INTERNAL_SERVER_ERROR (500) if something went wrong</li> </ul> </li>
* <li>URI: the absolute URI of the newly created resource</li> </ul>
*/
public static ResourceCreationResult create(GenericId id) {
ResourceCreationResult res = new ResourceCreationResult();
if (RepositoryFactory.getRepository().exists(id)) {
// res.setStatus(302);
res.setStatus(Status.CONFLICT);
} else {
if (RepositoryFactory.getRepository().flagAsExisting(id)) {
res.setStatus(Status.CREATED);
// @formatter:off
// This method is a generic method
// We cannot return an "absolute" URL as the URL is always
// relative to the caller
// Does not work: String path = Environment.getUrlConfiguration().getRepositoryApiUrl()
// + "/" +
// Utils.getUrlPathForPathInsideRepo(id.getPathInsideRepo());
// We distinguish between two cases: DefinitionsChildId and
// TOSCAelementId
// @formatter:on
String path;
if (id instanceof DefinitionsChildId) {
// here, we return namespace + id, as it is only possible to
// post on the definition child*s* resource to create an
// instance of a definition child
DefinitionsChildId tcId = (DefinitionsChildId) id;
path = tcId.getNamespace().getEncoded() + "/" + tcId.getXmlId().getEncoded() + "/";
} else {
assert (id instanceof ToscaElementId);
// We just return the id as we assume that only the parent
// of this id may create sub elements
path = id.getXmlId().getEncoded() + "/";
}
// we have to encode it twice to get correct URIs
path = Util.getUrlPath(path);
URI uri = RestUtils.createURI(path);
res.setUri(uri);
res.setId(id);
} else {
res.setStatus(Status.INTERNAL_SERVER_ERROR);
}
}
return res;
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class RestUtils method getType.
/**
* This is a quick helper method. The code should be refactored to use HasType on the element directly instead of
* going through the resource. The method was implemented, because it is not that easy to get the id of the element
* belonging to a resource.
*
* @param res a resource, where the associated element has a type. Example: EntityTypeImplementationResource
* @return the QName of the associated type
*/
public static QName getType(IPersistable res) {
DefinitionsChildId id = (DefinitionsChildId) res.getRepositoryFileReference().getParent();
final HasType element = (HasType) RepositoryFactory.getRepository().getDefinitions(id).getElement();
return element.getTypeAsQName();
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class TemplatesOfOneType method getJSON.
/**
* returns a list of all implementations of a type using the getAllImplementations method
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getJSON() {
Collection<? extends DefinitionsChildId> allImplementations = this.getAllImplementations();
List<QNameApiData> res = new ArrayList<>(allImplementations.size());
QNameConverter adapter = new QNameConverter();
for (DefinitionsChildId id : allImplementations) {
res.add(adapter.marshal(id.getQName()));
}
return Response.ok().entity(res).build();
}
Aggregations