Search in sources :

Example 1 with TCapabilityDefinition

use of org.eclipse.winery.model.tosca.TCapabilityDefinition in project winery by eclipse.

the class IGenericRepository method getReferencedDefinitionsChildIds.

default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(NodeTypeId id) {
    Collection<DefinitionsChildId> ids = new ArrayList<>();
    Collection<NodeTypeImplementationId> allNodeTypeImplementations = this.getAllElementsReferencingGivenType(NodeTypeImplementationId.class, id.getQName());
    for (NodeTypeImplementationId ntiId : allNodeTypeImplementations) {
        ids.add(ntiId);
    }
    final TNodeType nodeType = this.getElement(id);
    // add all referenced requirement types
    TNodeType.RequirementDefinitions reqDefsContainer = nodeType.getRequirementDefinitions();
    if (reqDefsContainer != null) {
        List<TRequirementDefinition> reqDefs = reqDefsContainer.getRequirementDefinition();
        for (TRequirementDefinition reqDef : reqDefs) {
            RequirementTypeId reqTypeId = new RequirementTypeId(reqDef.getRequirementType());
            ids.add(reqTypeId);
        }
    }
    // add all referenced capability types
    TNodeType.CapabilityDefinitions capDefsContainer = nodeType.getCapabilityDefinitions();
    if (capDefsContainer != null) {
        List<TCapabilityDefinition> capDefs = capDefsContainer.getCapabilityDefinition();
        for (TCapabilityDefinition capDef : capDefs) {
            CapabilityTypeId capTypeId = new CapabilityTypeId(capDef.getCapabilityType());
            ids.add(capTypeId);
        }
    }
    return ids;
}
Also used : NodeTypeImplementationId(org.eclipse.winery.common.ids.definitions.NodeTypeImplementationId) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) CapabilityTypeId(org.eclipse.winery.common.ids.definitions.CapabilityTypeId) ArrayList(java.util.ArrayList) TNodeType(org.eclipse.winery.model.tosca.TNodeType) RequirementTypeId(org.eclipse.winery.common.ids.definitions.RequirementTypeId) TRequirementDefinition(org.eclipse.winery.model.tosca.TRequirementDefinition) TCapabilityDefinition(org.eclipse.winery.model.tosca.TCapabilityDefinition)

Example 2 with TCapabilityDefinition

use of org.eclipse.winery.model.tosca.TCapabilityDefinition in project winery by eclipse.

the class RequirementOrCapabilityDefinitionsResource method onPost.

@POST
// As there is no supertype of TCapabilityType and TRequirementType containing the common attributes, we have to rely on unchecked casts
@SuppressWarnings("unchecked")
@Consumes(MediaType.APPLICATION_JSON)
public Response onPost(CapabilityDefinitionPostData postData) {
    if (StringUtils.isEmpty(postData.name)) {
        return Response.status(Status.BAD_REQUEST).entity("Name has to be provided").build();
    }
    if (StringUtils.isEmpty(postData.type)) {
        return Response.status(Status.BAD_REQUEST).entity("Type has to be provided").build();
    }
    int lbound = 1;
    if (!StringUtils.isEmpty(postData.lowerBound)) {
        try {
            lbound = Integer.parseInt(postData.lowerBound);
        } catch (NumberFormatException e) {
            return Response.status(Status.BAD_REQUEST).entity("Bad format of lowerbound: " + e.getMessage()).build();
        }
    }
    String ubound = "1";
    if (!StringUtils.isEmpty(postData.upperBound)) {
        ubound = postData.upperBound;
    }
    // we also support replacement of existing requirements
    // therefore, we loop through the existing requirements
    int idx = -1;
    boolean found = false;
    for (ReqDefOrCapDef d : this.list) {
        idx++;
        if (this.getId(d).equals(postData.name)) {
            found = true;
            break;
        }
    }
    QName typeQName = QName.valueOf(postData.type);
    // Create object and put type in it
    ReqDefOrCapDef def;
    if (this instanceof CapabilityDefinitionsResource) {
        def = (ReqDefOrCapDef) new TCapabilityDefinition();
        ((TCapabilityDefinition) def).setCapabilityType(typeQName);
    } else {
        assert (this instanceof RequirementDefinitionsResource);
        def = (ReqDefOrCapDef) new TRequirementDefinition();
        ((TRequirementDefinition) def).setRequirementType(typeQName);
    }
    // copy all other data into object
    AbstractReqOrCapDefResource.invokeSetter(def, "setName", postData.name);
    AbstractReqOrCapDefResource.invokeSetter(def, "setLowerBound", lbound);
    AbstractReqOrCapDefResource.invokeSetter(def, "setUpperBound", ubound);
    if (found) {
        // replace element
        this.list.set(idx, def);
    } else {
        // add new element
        this.list.add(def);
    }
    return RestUtils.persist(this.res);
}
Also used : QName(javax.xml.namespace.QName) TRequirementDefinition(org.eclipse.winery.model.tosca.TRequirementDefinition) TCapabilityDefinition(org.eclipse.winery.model.tosca.TCapabilityDefinition) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

TCapabilityDefinition (org.eclipse.winery.model.tosca.TCapabilityDefinition)2 TRequirementDefinition (org.eclipse.winery.model.tosca.TRequirementDefinition)2 ArrayList (java.util.ArrayList)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 QName (javax.xml.namespace.QName)1 CapabilityTypeId (org.eclipse.winery.common.ids.definitions.CapabilityTypeId)1 DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)1 NodeTypeImplementationId (org.eclipse.winery.common.ids.definitions.NodeTypeImplementationId)1 RequirementTypeId (org.eclipse.winery.common.ids.definitions.RequirementTypeId)1 TNodeType (org.eclipse.winery.model.tosca.TNodeType)1