Search in sources :

Example 26 with DefinitionsChildId

use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.

the class AbstractComponentsResource method onPost.

/**
 * Creates a new component instance in the given namespace
 *
 * @param namespace plain namespace
 * @param name      the name; used as id
 */
protected ResourceCreationResult onPost(String namespace, String name) {
    ResourceCreationResult res;
    if (StringUtils.isEmpty(namespace) || StringUtils.isEmpty(name)) {
        res = new ResourceCreationResult(Status.BAD_REQUEST);
    } else {
        String id = RestUtils.createXMLidAsString(name);
        DefinitionsChildId tcId;
        try {
            tcId = this.getDefinitionsChildId(namespace, id, false);
            res = this.createComponentInstance(tcId);
            // in case the resource additionally supports a name attribute, we set the original name
            if (res.getStatus().equals(Status.CREATED)) {
                if ((tcId instanceof ServiceTemplateId) || (tcId instanceof ArtifactTemplateId) || (tcId instanceof PolicyTemplateId)) {
                    // these three types have an additional name (instead of a pure id)
                    // we store the name
                    IHasName resource = (IHasName) AbstractComponentsResource.getComponentInstaceResource(tcId);
                    resource.setName(name);
                }
            }
        } catch (Exception e) {
            AbstractComponentsResource.LOGGER.debug("Could not create id instance", e);
            res = new ResourceCreationResult(Status.INTERNAL_SERVER_ERROR);
        }
    }
    return res;
}
Also used : DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) PolicyTemplateId(org.eclipse.winery.common.ids.definitions.PolicyTemplateId) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) NotFoundException(com.sun.jersey.api.NotFoundException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 27 with DefinitionsChildId

use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.

the class AbstractComponentsResource method getListOfAllIds.

/**
 * Used by org.eclipse.winery.repository.repository.client and by the artifactcreationdialog.tag. Especially the
 * "name" field is used there at the UI
 *
 * @param grouped if given, the JSON output is grouped by namespace
 * @return A list of all ids of all instances of this component type. Format: <code>[({"namespace":
 * "[namespace]", "id": "[id]"},)* ]</code>.
 * <p>
 * If grouped is set, the list will be grouped by namespace.
 * <code>[{"id": "[namsepace encoded]", "test": "[namespace decoded]", "children":[{"id": "[qName]", "text":
 * "[id]"}]}]</code>
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getListOfAllIds(@QueryParam("grouped") String grouped, @QueryParam("full") @ApiParam("If set, the full information of the definition's child is returned. E.g., in the case of node types, the same result as a GET on {ns}/{id] is returned. Works only in the case of grouped.") String full) {
    Class<? extends DefinitionsChildId> idClass = RestUtils.getComponentIdClassForComponentContainer(this.getClass());
    boolean supportsNameAttribute = Util.instanceSupportsNameAttribute(idClass);
    final IRepository repository = RepositoryFactory.getRepository();
    SortedSet<? extends DefinitionsChildId> allDefinitionsChildIds = repository.getAllDefinitionsChildIds(idClass);
    JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.setCodec(BackendUtils.mapper);
    StringWriter sw = new StringWriter();
    try {
        JsonGenerator jg = jsonFactory.createGenerator(sw);
        // Refactoring could move this class to common and fill it here
        if (grouped == null) {
            jg.writeStartArray();
            for (DefinitionsChildId id : allDefinitionsChildIds) {
                jg.writeStartObject();
                jg.writeStringField("namespace", id.getNamespace().getDecoded());
                jg.writeStringField("id", id.getXmlId().getDecoded());
                if (supportsNameAttribute) {
                    AbstractComponentInstanceResource componentInstaceResource = AbstractComponentsResource.getComponentInstaceResource(id);
                    String name = ((IHasName) componentInstaceResource).getName();
                    jg.writeStringField("name", name);
                } else {
                    // used for winery-qNameSelector to avoid an if there
                    jg.writeStringField("name", id.getXmlId().getDecoded());
                }
                jg.writeStringField("qName", id.getQName().toString());
                if (full != null) {
                    try {
                        jg.writeFieldName("full");
                        jg.writeObject(BackendUtils.getDefinitionsHavingCorrectImports(repository, id));
                    } catch (Exception e) {
                        throw new WebApplicationException(e);
                    }
                }
                jg.writeEndObject();
            }
            jg.writeEndArray();
        } else {
            jg.writeStartArray();
            Map<Namespace, ? extends List<? extends DefinitionsChildId>> groupedIds = allDefinitionsChildIds.stream().collect(Collectors.groupingBy(id -> id.getNamespace()));
            groupedIds.keySet().stream().sorted().forEach(namespace -> {
                try {
                    jg.writeStartObject();
                    jg.writeStringField("id", namespace.getEncoded());
                    jg.writeStringField("text", namespace.getDecoded());
                    jg.writeFieldName("children");
                    jg.writeStartArray();
                    groupedIds.get(namespace).forEach(id -> {
                        try {
                            jg.writeStartObject();
                            String text;
                            if (supportsNameAttribute) {
                                AbstractComponentInstanceResource componentInstaceResource = AbstractComponentsResource.getComponentInstaceResource(id);
                                text = ((IHasName) componentInstaceResource).getName();
                            } else {
                                text = id.getXmlId().getDecoded();
                            }
                            jg.writeStringField("id", id.getQName().toString());
                            jg.writeStringField("text", text);
                            if (full != null) {
                                try {
                                    jg.writeFieldName("full");
                                    jg.writeObject(BackendUtils.getDefinitionsHavingCorrectImports(repository, id));
                                } catch (Exception e) {
                                    throw new WebApplicationException(e);
                                }
                            }
                            jg.writeEndObject();
                        } catch (IOException e) {
                            AbstractComponentsResource.LOGGER.error("Could not create JSON", e);
                        }
                    });
                    jg.writeEndArray();
                    jg.writeEndObject();
                } catch (IOException e) {
                    AbstractComponentsResource.LOGGER.error("Could not create JSON", e);
                }
            });
            jg.writeEndArray();
        }
        jg.close();
    } catch (Exception e) {
        AbstractComponentsResource.LOGGER.error(e.getMessage(), e);
        return "[]";
    }
    return sw.toString();
}
Also used : java.util(java.util) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) LoggerFactory(org.slf4j.LoggerFactory) ApiParam(io.swagger.annotations.ApiParam) StringUtils(org.apache.commons.lang3.StringUtils) Constructor(java.lang.reflect.Constructor) NotFoundException(com.sun.jersey.api.NotFoundException) MediaType(javax.ws.rs.core.MediaType) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) Status(javax.ws.rs.core.Response.Status) PolicyTemplateId(org.eclipse.winery.common.ids.definitions.PolicyTemplateId) Logger(org.slf4j.Logger) StringWriter(java.io.StringWriter) IOException(java.io.IOException) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) Util(org.eclipse.winery.common.Util) Collectors(java.util.stream.Collectors) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException) JsonFactory(com.fasterxml.jackson.core.JsonFactory) javax.ws.rs(javax.ws.rs) IRepository(org.eclipse.winery.repository.backend.IRepository) RestUtils(org.eclipse.winery.repository.rest.RestUtils) Namespace(org.eclipse.winery.common.ids.Namespace) QName(javax.xml.namespace.QName) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) JsonFactory(com.fasterxml.jackson.core.JsonFactory) IOException(java.io.IOException) NotFoundException(com.sun.jersey.api.NotFoundException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Namespace(org.eclipse.winery.common.ids.Namespace) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IRepository(org.eclipse.winery.repository.backend.IRepository)

Example 28 with DefinitionsChildId

use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId 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)

Example 29 with DefinitionsChildId

use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.

the class AvailableSuperclassesApiData method generateList.

private void generateList(Class<? extends DefinitionsChildId> clazz, DefinitionsChildId classToExclude) {
    SortedSet<? extends DefinitionsChildId> allDefinitionsChildIds = RepositoryFactory.getRepository().getAllDefinitionsChildIds(clazz);
    if (classToExclude != null) {
        allDefinitionsChildIds.remove(classToExclude);
    }
    this.classes = new ArrayList<>();
    for (DefinitionsChildId id : allDefinitionsChildIds) {
        NameAndQNameApiData q = new NameAndQNameApiData(id);
        this.classes.add(q);
    }
}
Also used : DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId)

Example 30 with DefinitionsChildId

use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.

the class EntityTypeResource method getListOfAllInstances.

/**
 * Used by children to implement getListOfAllInstances()
 */
protected SortedSet<Select2OptGroup> getListOfAllInstances(Class<? extends DefinitionsChildId> clazz) throws RepositoryCorruptException {
    Select2DataWithOptGroups data = new Select2DataWithOptGroups();
    Collection<? extends DefinitionsChildId> instanceIds = RepositoryFactory.getRepository().getAllElementsReferencingGivenType(clazz, this.id.getQName());
    for (DefinitionsChildId instanceId : instanceIds) {
        String groupText = instanceId.getNamespace().getDecoded();
        String text = BackendUtils.getName(instanceId);
        data.add(groupText, instanceId.getQName().toString(), text);
    }
    return data.asSortedSet();
}
Also used : DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) Select2DataWithOptGroups(org.eclipse.winery.repository.rest.datatypes.select2.Select2DataWithOptGroups)

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