Search in sources :

Example 1 with NamespaceManager

use of org.eclipse.winery.repository.backend.NamespaceManager in project winery by eclipse.

the class CsarImporter method importNamespacePrefixes.

/**
 * Import namespace prefixes. This is kind of a quick hack. TODO: during the import, the prefixes should be
 * extracted using JAXB and stored in the NamespacesResource
 *
 * @param rootPath the root path of the extracted CSAR
 */
private void importNamespacePrefixes(Path rootPath) {
    NamespaceManager namespaceManager = RepositoryFactory.getRepository().getNamespaceManager();
    Path properties = rootPath.resolve(CsarExporter.PATH_TO_NAMESPACES_PROPERTIES);
    if (Files.exists(properties)) {
        PropertiesConfiguration pconf;
        try {
            pconf = new PropertiesConfiguration(properties.toFile());
        } catch (ConfigurationException e) {
            CsarImporter.LOGGER.debug(e.getMessage(), e);
            return;
        }
        Iterator<String> namespaces = pconf.getKeys();
        while (namespaces.hasNext()) {
            boolean addToStorage = false;
            String namespace = namespaces.next();
            if (namespaceManager.hasPrefix(namespace)) {
                String storedPrefix = namespaceManager.getPrefix(namespace);
                // QUICK HACK to check whether the prefix is a generated one
                // We assume we know the internal generation routine
                Matcher m = CsarImporter.GENERATED_PREFIX_PATTERN.matcher(storedPrefix);
                if (m.matches()) {
                    // the stored prefix is a generated one
                    // replace it by the one stored in the exported properties
                    addToStorage = true;
                }
            } else {
                addToStorage = true;
            }
            if (addToStorage) {
                String prefix = pconf.getString(namespace);
                namespaceManager.setPrefix(namespace, prefix);
            }
        }
    }
}
Also used : NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) ConfigurationException(org.apache.commons.configuration.ConfigurationException) Matcher(java.util.regex.Matcher) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration)

Example 2 with NamespaceManager

use of org.eclipse.winery.repository.backend.NamespaceManager 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)

Aggregations

NamespaceManager (org.eclipse.winery.repository.backend.NamespaceManager)2 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)1 TEntityType (org.eclipse.winery.model.tosca.TEntityType)1 PropertiesDefinition (org.eclipse.winery.model.tosca.TEntityType.PropertiesDefinition)1 WinerysPropertiesDefinition (org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition)1