Search in sources :

Example 6 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class ModelRepositoryTest method tesUploadValidModel.

@Test
public void tesUploadValidModel() throws IOException {
    UploadModelResult uploadResult = modelRepository.upload(IOUtils.toByteArray(new ClassPathResource("sample_models/Color.type").getInputStream()), "Color.type", "admin");
    assertEquals(true, uploadResult.isValid());
    assertNull(uploadResult.getErrorMessage());
    assertNotNull(uploadResult.getHandleId());
    ModelInfo resource = uploadResult.getModelResource();
    assertEquals("org.eclipse.vorto.examples.type", resource.getId().getNamespace());
    assertEquals("Color", resource.getId().getName());
    assertEquals("1.0.0", resource.getId().getVersion());
    assertEquals(ModelType.Datatype, resource.getType());
    assertEquals(0, resource.getReferences().size());
    assertEquals("Color", resource.getDisplayName());
    assertNull(resource.getDescription());
    assertEquals(0, modelRepository.search("*").size());
}
Also used : UploadModelResult(org.eclipse.vorto.repository.api.upload.UploadModelResult) ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test) AbstractIntegrationTest(org.eclipse.vorto.repository.AbstractIntegrationTest)

Example 7 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class ModelRepositoryTest method testGetReferencedBy.

@Test
public void testGetReferencedBy() throws Exception {
    checkinModel("Color.type");
    checkinModel("Colorlight.fbmodel");
    assertEquals(2, modelRepository.search("*").size());
    ModelInfo result = modelRepository.getById(ModelId.fromReference("org.eclipse.vorto.examples.type.Color", "1.0.0"));
    assertEquals(1, result.getReferencedBy().size());
    assertEquals("org.eclipse.vorto.examples.fb.ColorLight:1.0.0", result.getReferencedBy().get(0).getPrettyFormat());
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) Test(org.junit.Test) AbstractIntegrationTest(org.eclipse.vorto.repository.AbstractIntegrationTest)

Example 8 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class BulkUploadHelper method uploadMultiple.

public List<UploadModelResult> uploadMultiple(byte[] content, String zipFileName, String callerId) {
    if (!isValid(zipFileName)) {
        throw new FatalModelRepositoryException("Filename/type is invalid", null);
    }
    List<UploadModelResult> invalidResult = new ArrayList<UploadModelResult>();
    List<UploadModelResult> validResult = new ArrayList<UploadModelResult>();
    Set<ModelInfo> parsedModels = new HashSet<>();
    ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(content));
    ZipEntry entry = null;
    try {
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                final String fileName = entry.getName();
                if (ModelParserFactory.hasParserFor(fileName)) {
                    try {
                        parsedModels.add(ModelParserFactory.getParser(fileName).parse(new ByteArrayInputStream(copyStream(zis, entry))));
                    } catch (ValidationException grammarProblem) {
                        invalidResult.add(UploadModelResultFactory.create(grammarProblem));
                    }
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    try {
        List<IModelValidator> bulkUploadValidators = constructBulkUploadValidators(parsedModels);
        for (ModelInfo modelResource : parsedModels) {
            try {
                for (IModelValidator validator : bulkUploadValidators) {
                    validator.validate(modelResource, InvocationContext.create(callerId));
                }
                UploadModelResult result = UploadModelResult.valid(modelResource);
                if (!invalidResult.contains(result)) {
                    validResult.add(result);
                }
            } catch (ValidationException validationException) {
                invalidResult.add(UploadModelResultFactory.create(validationException));
            }
        }
        if (invalidResult.isEmpty()) {
            DependencyManager dm = new DependencyManager(parsedModels);
            return safelyUpload(dm.getSorted());
        } else {
            List<UploadModelResult> completeResult = new ArrayList<>();
            completeResult.addAll(invalidResult);
            completeResult.addAll(validResult);
            return completeResult;
        }
    } catch (Exception e) {
        throw new FatalModelRepositoryException("Invalid zip file", e);
    }
}
Also used : UploadModelResult(org.eclipse.vorto.repository.api.upload.UploadModelResult) ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) IModelValidator(org.eclipse.vorto.repository.core.impl.validation.IModelValidator) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) HashSet(java.util.HashSet)

Example 9 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class JcrModelRepository method search.

@Override
public List<ModelInfo> search(final String expression) {
    String queryExpression = expression;
    if (queryExpression == null || queryExpression.isEmpty()) {
        queryExpression = "*";
    }
    try {
        List<ModelInfo> modelResources = new ArrayList<>();
        Query query = modelSearchUtil.createQueryFromExpression(session, queryExpression);
        logger.debug("Searching repository with expression " + query.getStatement());
        QueryResult result = query.execute();
        RowIterator rowIterator = result.getRows();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.nextRow();
            Node currentNode = row.getNode();
            if (currentNode.hasProperty("vorto:type")) {
                try {
                    modelResources.add(createMinimalModelInfo(currentNode));
                } catch (Exception ex) {
                // model is corrupt ,ignoring....
                }
            }
        }
        return modelResources;
    } catch (RepositoryException e) {
        throw new RuntimeException("Could not create query manager", e);
    }
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) RowIterator(javax.jcr.query.RowIterator) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) RepositoryException(javax.jcr.RepositoryException) Row(javax.jcr.query.Row) ModelReferentialIntegrityException(org.eclipse.vorto.repository.core.ModelReferentialIntegrityException) ModelNotFoundException(org.eclipse.vorto.repository.api.exception.ModelNotFoundException) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException)

Example 10 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class JcrModelRepository method getMappingModelsForTargetPlatform.

@Override
public List<ModelInfo> getMappingModelsForTargetPlatform(ModelId modelId, String targetPlatform) {
    List<ModelInfo> mappingResources = new ArrayList<>();
    ModelInfo modelResource = getById(modelId);
    if (modelResource != null) {
        for (ModelId referenceeModelId : modelResource.getReferencedBy()) {
            ModelInfo referenceeModelResources = getById(referenceeModelId);
            if (referenceeModelResources.getType() == ModelType.Mapping && isTargetPlatformMapping(referenceeModelResources, targetPlatform)) {
                mappingResources.add(referenceeModelResources);
            }
        }
        for (ModelId referencedModelId : modelResource.getReferences()) {
            mappingResources.addAll(getMappingModelsForTargetPlatform(referencedModelId, targetPlatform));
        }
    }
    return mappingResources;
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ArrayList(java.util.ArrayList) ModelId(org.eclipse.vorto.repository.api.ModelId)

Aggregations

ModelInfo (org.eclipse.vorto.repository.api.ModelInfo)35 Test (org.junit.Test)12 ModelId (org.eclipse.vorto.repository.api.ModelId)11 IOException (java.io.IOException)9 ModelNotFoundException (org.eclipse.vorto.repository.api.exception.ModelNotFoundException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 AbstractIntegrationTest (org.eclipse.vorto.repository.AbstractIntegrationTest)6 FatalModelRepositoryException (org.eclipse.vorto.repository.core.FatalModelRepositoryException)6 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)6 ArrayList (java.util.ArrayList)5 Node (javax.jcr.Node)5 PathNotFoundException (javax.jcr.PathNotFoundException)5 RepositoryException (javax.jcr.RepositoryException)5 ApiOperation (io.swagger.annotations.ApiOperation)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ZipEntry (java.util.zip.ZipEntry)4 UploadTooLargeException (org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ApiResponses (io.swagger.annotations.ApiResponses)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3