Search in sources :

Example 6 with DefinitionsChildId

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);
    }
}
Also used : DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) QName(javax.xml.namespace.QName) InvocationTargetException(java.lang.reflect.InvocationTargetException) HasInheritance(org.eclipse.winery.model.tosca.HasInheritance) HasType(org.eclipse.winery.model.tosca.HasType)

Example 7 with DefinitionsChildId

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);
}
Also used : DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId)

Example 8 with DefinitionsChildId

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;
}
Also used : ToscaElementId(org.eclipse.winery.common.ids.elements.ToscaElementId) ResourceCreationResult(org.eclipse.winery.repository.rest.resources._support.ResourceCreationResult) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) URI(java.net.URI)

Example 9 with DefinitionsChildId

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();
}
Also used : DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId)

Example 10 with DefinitionsChildId

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();
}
Also used : QNameApiData(org.eclipse.winery.repository.rest.resources.apiData.QNameApiData) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) ArrayList(java.util.ArrayList) QNameConverter(org.eclipse.winery.repository.rest.resources.apiData.converter.QNameConverter) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)30 QName (javax.xml.namespace.QName)12 ArrayList (java.util.ArrayList)9 NodeTypeId (org.eclipse.winery.common.ids.definitions.NodeTypeId)7 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)6 IOException (java.io.IOException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)5 CapabilityTypeId (org.eclipse.winery.common.ids.definitions.CapabilityTypeId)5 TExtensibleElements (org.eclipse.winery.model.tosca.TExtensibleElements)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 ArtifactTypeId (org.eclipse.winery.common.ids.definitions.ArtifactTypeId)4 RelationshipTypeId (org.eclipse.winery.common.ids.definitions.RelationshipTypeId)4 ServiceTemplateId (org.eclipse.winery.common.ids.definitions.ServiceTemplateId)4 TOSCAModelHelper.createNodeTypeId (org.eclipse.winery.compliance.TOSCAModelHelper.createNodeTypeId)4 TOSCAModelHelper.createTNodeTemplate (org.eclipse.winery.compliance.TOSCAModelHelper.createTNodeTemplate)4 TOSCAModelHelper.createTNodeType (org.eclipse.winery.compliance.TOSCAModelHelper.createTNodeType)4 TComplianceRule (org.eclipse.winery.model.tosca.TComplianceRule)4 TNodeType (org.eclipse.winery.model.tosca.TNodeType)4