Search in sources :

Example 21 with ModelId

use of org.eclipse.vorto.model.ModelId in project vorto by eclipse.

the class AbstractModelParser method parseModelIdFromFileName.

private ModelId parseModelIdFromFileName() {
    String pureFileName = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.lastIndexOf("."));
    ModelId modelId = new ModelId();
    try {
        modelId.setNamespace(pureFileName.substring(0, pureFileName.lastIndexOf(".")));
        modelId.setName(pureFileName.substring(pureFileName.lastIndexOf(".") + 1, pureFileName.indexOf("_")));
        String version = pureFileName.substring(pureFileName.indexOf("_") + 1);
        version = version.replaceAll("_", ".");
        modelId.setVersion(version.substring(0, 5));
    } catch (Throwable t) {
        return new ModelId(pureFileName, "", "0.0.0");
    }
    return modelId;
}
Also used : ModelId(org.eclipse.vorto.model.ModelId)

Example 22 with ModelId

use of org.eclipse.vorto.model.ModelId in project vorto by eclipse.

the class DefaultResolver method doResolve.

@Override
protected ModelId doResolve(String tenantId, ModelInfo mappingModelResource, ResolveQuery query) {
    ModelFileContent content = getRepositoryFactory().getRepository(tenantId).getModelContent(mappingModelResource.getId(), false);
    MappingModel mappingModel = (MappingModel) content.getModel();
    Optional<MappingRule> objectRule = mappingModel.getRules().stream().filter(rule -> rule.getTarget() instanceof StereoTypeTarget && ((StereoTypeTarget) rule.getTarget()).getName().equals(query.getStereoType())).findFirst();
    if (objectRule.isPresent()) {
        Optional<Attribute> objectIdAttribute = ((StereoTypeTarget) objectRule.get().getTarget()).getAttributes().stream().filter(attribute -> attribute.getName().equals(query.getAttributeId())).findFirst();
        if (objectIdAttribute.isPresent() && objectIdAttribute.get().getValue().equals(query.getAttributeValue())) {
            return ModelId.fromReference(mappingModel.getReferences().get(0).getImportedNamespace(), mappingModel.getReferences().get(0).getVersion());
        }
    }
    return null;
}
Also used : ModelFileContent(org.eclipse.vorto.repository.core.ModelFileContent) ResolveQuery(org.eclipse.vorto.repository.web.core.dto.ResolveQuery) Service(org.springframework.stereotype.Service) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Optional(java.util.Optional) MappingRule(org.eclipse.vorto.core.api.model.mapping.MappingRule) StereoTypeTarget(org.eclipse.vorto.core.api.model.mapping.StereoTypeTarget) ModelId(org.eclipse.vorto.model.ModelId) Attribute(org.eclipse.vorto.core.api.model.mapping.Attribute) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelFileContent(org.eclipse.vorto.repository.core.ModelFileContent) Attribute(org.eclipse.vorto.core.api.model.mapping.Attribute) MappingRule(org.eclipse.vorto.core.api.model.mapping.MappingRule) StereoTypeTarget(org.eclipse.vorto.core.api.model.mapping.StereoTypeTarget) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel)

Example 23 with ModelId

use of org.eclipse.vorto.model.ModelId in project vorto by eclipse.

the class BulkUploadHelper method trytoCreateModelFromCorruptFile.

// TODO: try to guess the modelinfo based on the content of the file, instead of the filename
private ModelInfo trytoCreateModelFromCorruptFile(String fileName) {
    try {
        final String modelName = fileName.substring(0, fileName.lastIndexOf("."));
        final ModelType type = ModelType.fromFileName(fileName);
        return new ModelInfo(new ModelId(modelName, "unknown", "unknown"), type);
    } catch (Throwable t) {
        return null;
    }
}
Also used : ModelType(org.eclipse.vorto.model.ModelType) ModelId(org.eclipse.vorto.model.ModelId)

Example 24 with ModelId

use of org.eclipse.vorto.model.ModelId in project vorto by eclipse.

the class MetadataIntegrityDiagnostic method checkModelType.

private Optional<Diagnostic> checkModelType(final Node node) {
    ModelId modelId = null;
    try {
        modelId = NodeDiagnosticUtils.getModelId(node.getPath()).orElseThrow(() -> new NodeDiagnosticException("Cannot get modelId of node"));
        ModelType fileModelType = ModelType.create(node.getName());
        ModelType nodeModelType = ModelType.valueOf(node.getProperty("vorto:type").getString());
        if (!fileModelType.equals(nodeModelType)) {
            String message = new StringBuilder("The model type should be '").append(fileModelType.name()).append("' but is '").append(nodeModelType.name()).append("'").toString();
            return Optional.of(new Diagnostic(modelId, message));
        }
        return Optional.empty();
    } catch (RepositoryException e) {
        return Optional.of(new Diagnostic(modelId, "Got exception while checking node 'vorto:type' : " + e.getMessage()));
    }
}
Also used : ModelType(org.eclipse.vorto.core.api.model.model.ModelType) Diagnostic(org.eclipse.vorto.repository.core.Diagnostic) NodeDiagnostic(org.eclipse.vorto.repository.core.impl.RepositoryDiagnostics.NodeDiagnostic) RepositoryException(javax.jcr.RepositoryException) ModelId(org.eclipse.vorto.model.ModelId)

Example 25 with ModelId

use of org.eclipse.vorto.model.ModelId in project vorto by eclipse.

the class ModelRepositoryTest2 method testReferencedByApi.

@Test
public void testReferencedByApi() {
    IUserContext creator = createUserContext("creator", "playground");
    importModel("Color.type", creator);
    importModel("Color4.type", creator);
    importModel("Colorlight.fbmodel", creator);
    importModel("Colorlight2.fbmodel", creator);
    importModel("Colorlight3.fbmodel", creator);
    List<ModelInfo> referencedBy = repositoryFactory.getRepository(creator).getModelsReferencing(new ModelId("Color", "org.eclipse.vorto.examples.type", "1.0.0"));
    assertEquals(2, referencedBy.size());
    assertTrue(referencedBy.stream().anyMatch(model -> model.getId().getName().equals("ColorLight")));
    assertTrue(referencedBy.stream().anyMatch(model -> model.getId().getName().equals("ColorLight2")));
}
Also used : List(java.util.List) Assert.assertTrue(org.junit.Assert.assertTrue) UnitTestBase(org.eclipse.vorto.repository.UnitTestBase) Test(org.junit.Test) ModelId(org.eclipse.vorto.model.ModelId) Assert.assertEquals(org.junit.Assert.assertEquals) ModelId(org.eclipse.vorto.model.ModelId) Test(org.junit.Test)

Aggregations

ModelId (org.eclipse.vorto.model.ModelId)124 Test (org.junit.Test)48 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)29 IOException (java.io.IOException)28 ClassPathResource (org.springframework.core.io.ClassPathResource)25 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)19 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ResponseEntity (org.springframework.http.ResponseEntity)14 IModelRepository (org.eclipse.vorto.repository.core.IModelRepository)13 ModelType (org.eclipse.vorto.model.ModelType)12 IUserContext (org.eclipse.vorto.repository.core.IUserContext)12 Autowired (org.springframework.beans.factory.annotation.Autowired)12 GetMapping (org.springframework.web.bind.annotation.GetMapping)11 Optional (java.util.Optional)10 List (java.util.List)9 IOUtils (org.apache.commons.io.IOUtils)9 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)9 ModelLink (org.eclipse.vorto.repository.web.api.v1.dto.ModelLink)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 ApiOperation (io.swagger.annotations.ApiOperation)8