Search in sources :

Example 1 with HasType

use of org.eclipse.winery.model.tosca.HasType 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 2 with HasType

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

the class AbstractComponentsWithTypeReferenceResource method onJsonPost.

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response onJsonPost(QNameWithTypeApiData jsonData) {
    // only check for type parameter as namespace and name are checked in super.onPost
    if (StringUtils.isEmpty(jsonData.type)) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    ResourceCreationResult creationResult = super.onPost(jsonData.namespace, jsonData.localname);
    if (!creationResult.isSuccess()) {
        return creationResult.getResponse();
    }
    if (creationResult.getStatus().equals(Status.CREATED)) {
        final DefinitionsChildId id = (DefinitionsChildId) creationResult.getId();
        final IRepository repository = RepositoryFactory.getRepository();
        final Definitions definitions = repository.getDefinitions(id);
        final TExtensibleElements element = definitions.getElement();
        ((HasType) element).setType(jsonData.type);
        if (id instanceof EntityTemplateId) {
            BackendUtils.initializeProperties(repository, (TEntityTemplate) element);
        }
        try {
            BackendUtils.persist(id, definitions);
        } catch (IOException e) {
            throw new WebApplicationException(e);
        }
    }
    return creationResult.getResponse();
}
Also used : EntityTemplateId(org.eclipse.winery.common.ids.definitions.EntityTemplateId) WebApplicationException(javax.ws.rs.WebApplicationException) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) Definitions(org.eclipse.winery.model.tosca.Definitions) HasType(org.eclipse.winery.model.tosca.HasType) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) IOException(java.io.IOException) IRepository(org.eclipse.winery.repository.backend.IRepository) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)2 HasType (org.eclipse.winery.model.tosca.HasType)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 QName (javax.xml.namespace.QName)1 EntityTemplateId (org.eclipse.winery.common.ids.definitions.EntityTemplateId)1 Definitions (org.eclipse.winery.model.tosca.Definitions)1 HasInheritance (org.eclipse.winery.model.tosca.HasInheritance)1 TExtensibleElements (org.eclipse.winery.model.tosca.TExtensibleElements)1 IRepository (org.eclipse.winery.repository.backend.IRepository)1