Search in sources :

Example 1 with TEntityType

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

the class PropertiesResource method getProperties.

/**
 * Gets the defined properties. If no properties are defined, an empty JSON object is returned. If k/v properties
 * are defined, then a JSON is returned. Otherwise an XML is returned.
 *
 * @return Key/Value map in the case of Winery WPD mode - else instance of XML Element in case of non-key/value
 * properties
 */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
@NonNull
public Response getProperties() {
    TEntityType tempType = RepositoryFactory.getRepository().getTypeForTemplate(this.template);
    WinerysPropertiesDefinition wpd = tempType.getWinerysPropertiesDefinition();
    TEntityTemplate.Properties props = this.template.getProperties();
    if (wpd == null) {
        // These can be null resulting in 200 No Content at the caller
        if (props == null) {
            return Response.ok().entity("{}").type(MediaType.APPLICATION_JSON).build();
        } else {
            @Nullable final Object any = props.getAny();
            if (any == null) {
                LOGGER.debug("XML properties expected, but none found. Returning empty JSON.");
                return Response.ok().entity("{}").type(MediaType.APPLICATION_JSON).build();
            }
            try {
                @ADR(6) String xmlAsString = BackendUtils.getXMLAsString(TEntityTemplate.Properties.class, props, true);
                return Response.ok().entity(xmlAsString).type(MediaType.TEXT_XML).build();
            } catch (Exception e) {
                throw new WebApplicationException(e);
            }
        }
    } else {
        Map<String, String> kvProperties = this.template.getProperties().getKVProperties();
        return Response.ok().entity(kvProperties).type(MediaType.APPLICATION_JSON).build();
    }
}
Also used : TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TEntityType(org.eclipse.winery.model.tosca.TEntityType) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) Nullable(org.eclipse.jdt.annotation.Nullable) ADR(io.github.adr.embedded.ADR) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 2 with TEntityType

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

the class IWineryRepositoryCommon method getTypeForTemplate.

/**
 * Returns the stored type for the given template
 *
 * @param template the template to determine the type for
 * @throws NullPointerException if template.getType() returns null
 */
// we suppress "unchecked" as we use Class.forName
@SuppressWarnings("unchecked")
default TEntityType getTypeForTemplate(TEntityTemplate template) {
    QName type = template.getType();
    Objects.requireNonNull(type);
    // Possibilities:
    // a) try all possibly types whether an appropriate QName exists
    // b) derive type class from template class. Determine appropriate template element afterwards.
    // We go for b)
    String instanceResourceClassName = template.getClass().toString();
    int idx = instanceResourceClassName.lastIndexOf('.');
    // get everything from ".T", where "." is the last dot
    instanceResourceClassName = instanceResourceClassName.substring(idx + 2);
    // strip off "Template"
    instanceResourceClassName = instanceResourceClassName.substring(0, instanceResourceClassName.length() - "Template".length());
    // add "Type"
    instanceResourceClassName += "Type";
    // an id is required to instantiate the resource
    String idClassName = "org.eclipse.winery.common.ids.definitions." + instanceResourceClassName + "Id";
    org.slf4j.Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    LOGGER.debug("idClassName: {}", idClassName);
    // Get instance of id class having "type" as id
    Class<? extends DefinitionsChildId> idClass;
    try {
        idClass = (Class<? extends DefinitionsChildId>) Class.forName(idClassName);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Could not determine id class", e);
    }
    Constructor<? extends DefinitionsChildId> idConstructor;
    try {
        idConstructor = idClass.getConstructor(QName.class);
    } catch (NoSuchMethodException | SecurityException e) {
        throw new IllegalStateException("Could not get QName id constructor", e);
    }
    DefinitionsChildId typeId;
    try {
        typeId = idConstructor.newInstance(type);
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new IllegalStateException("Could not instantiate type", e);
    }
    final Definitions definitions = this.getDefinitions(typeId);
    return (TEntityType) definitions.getElement();
}
Also used : DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) QName(javax.xml.namespace.QName) TEntityType(org.eclipse.winery.model.tosca.TEntityType) Definitions(org.eclipse.winery.model.tosca.Definitions) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with TEntityType

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

the class PropertiesDefinitionResource method onJsonPost.

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response onJsonPost(PropertiesDefinitionResourceApiData data) {
    if (data.selectedValue == PropertiesDefinitionEnum.Element || data.selectedValue == PropertiesDefinitionEnum.Type) {
        // first of all, remove Winery's Properties definition (if it exists)
        ModelUtilities.removeWinerysPropertiesDefinition(this.getEntityType());
        // replace old properties definition by new one
        PropertiesDefinition def = new PropertiesDefinition();
        if (data.propertiesDefinition.getElement() != null) {
            def.setElement(data.propertiesDefinition.getElement());
        } else if (data.propertiesDefinition.getType() != null) {
            def.setType(data.propertiesDefinition.getType());
        } else {
            return Response.status(Status.BAD_REQUEST).entity("Wrong data submitted!").build();
        }
        this.getEntityType().setPropertiesDefinition(def);
        List<String> errors = new ArrayList<>();
        BackendUtils.deriveWPD(this.getEntityType(), errors);
        // currently the errors are just logged
        for (String error : errors) {
            PropertiesDefinitionResource.LOGGER.debug(error);
        }
        return RestUtils.persist(this.parentRes);
    } else if (data.selectedValue == PropertiesDefinitionEnum.Custom) {
        TEntityType et = this.parentRes.getEntityType();
        // clear current properties definition
        et.setPropertiesDefinition(null);
        // create winery properties definition and persist it
        ModelUtilities.replaceWinerysPropertiesDefinition(et, data.winerysPropertiesDefinition);
        String namespace = data.winerysPropertiesDefinition.getNamespace();
        NamespaceManager namespaceManager = RepositoryFactory.getRepository().getNamespaceManager();
        if (!namespaceManager.hasPrefix(namespace)) {
            namespaceManager.addNamespace(namespace);
        }
        return RestUtils.persist(this.parentRes);
    }
    return Response.status(Status.BAD_REQUEST).entity("Wrong data submitted!").build();
}
Also used : NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) TEntityType(org.eclipse.winery.model.tosca.TEntityType) PropertiesDefinition(org.eclipse.winery.model.tosca.TEntityType.PropertiesDefinition) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) ArrayList(java.util.ArrayList)

Example 4 with TEntityType

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

the class FilebasedRepositoryTest method getTypeForTemplateReturnsCorrectTypeForMyTinyTestArtifactTemplate.

@Test
public void getTypeForTemplateReturnsCorrectTypeForMyTinyTestArtifactTemplate() throws Exception {
    this.setRevisionTo("1374c8c13ec64899360511dbe0414223b88d3b01");
    ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId("http://opentosca.org/artifacttemplates", "MyTinyTest", false);
    final TArtifactTemplate artifactTemplate = this.repository.getElement(artifactTemplateId);
    final TEntityType typeForTemplate = this.repository.getTypeForTemplate(artifactTemplate);
    Assert.assertEquals(new QName("http://winery.opentosca.org/test/artifacttypes", "MiniArtifactType"), new QName(typeForTemplate.getTargetNamespace(), typeForTemplate.getName()));
}
Also used : TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TEntityType(org.eclipse.winery.model.tosca.TEntityType) QName(javax.xml.namespace.QName) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) Test(org.junit.Test)

Example 5 with TEntityType

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

the class TOSCATransformer method addTEntityTypes.

public static void addTEntityTypes(QName nodeTypeQName, TOSCAEntity entity, Class<? extends TEntityType> tEntityTypeClass) {
    TEntityType entityType = getEntityType(nodeTypeQName, tEntityTypeClass);
    entity.addTEntityType(entityType);
    Optional.of(entityType).map(TEntityType::getDerivedFrom).map(TEntityType.DerivedFrom::getTypeRef).ifPresent(qName -> addTEntityTypes(qName, entity, tEntityTypeClass));
}
Also used : TEntityType(org.eclipse.winery.model.tosca.TEntityType)

Aggregations

TEntityType (org.eclipse.winery.model.tosca.TEntityType)9 QName (javax.xml.namespace.QName)4 WinerysPropertiesDefinition (org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition)4 Definitions (org.eclipse.winery.model.tosca.Definitions)3 WebResource (com.sun.jersey.api.client.WebResource)2 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)2 TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)2 PropertiesDefinition (org.eclipse.winery.model.tosca.TEntityType.PropertiesDefinition)2 Document (org.w3c.dom.Document)2 ADR (io.github.adr.embedded.ADR)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 JAXBElement (javax.xml.bind.JAXBElement)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)1 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)1 DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)1