Search in sources :

Example 51 with ModelInfo

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

the class MappingModelParser method getReferences.

/**
 * Adds inherited types of references
 * Why? Because mapping models can also define mapping rules of Function Blocks / or Entities for inherited properties
 */
@Override
protected Collection<ModelId> getReferences(Model model) {
    List<ModelId> result = new ArrayList<>();
    model.getReferences().stream().map(modelRef -> ModelId.fromReference(modelRef.getImportedNamespace(), modelRef.getVersion())).collect(Collectors.toList()).stream().forEach(modelId -> {
        result.add(modelId);
        ModelInfo _model = this.modelRepoFactory.getRepositoryByModel(modelId).getById(modelId);
        if (_model != null) {
            result.addAll(_model.getReferences());
        }
    });
    return result;
}
Also used : Injector(com.google.inject.Injector) List(java.util.List) Collection(java.util.Collection) MappingStandaloneSetup(org.eclipse.vorto.editor.mapping.MappingStandaloneSetup) Collectors(java.util.stream.Collectors) ModelId(org.eclipse.vorto.model.ModelId) IModelRepositoryFactory(org.eclipse.vorto.repository.core.IModelRepositoryFactory) ArrayList(java.util.ArrayList) Model(org.eclipse.vorto.core.api.model.model.Model) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ArrayList(java.util.ArrayList) ModelId(org.eclipse.vorto.model.ModelId)

Example 52 with ModelInfo

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

the class AbstractModelImporter method sortAndSaveToRepository.

private List<ModelInfo> sortAndSaveToRepository(List<ModelResource> resources, FileUpload extractedFile, Context context) {
    final IUserContext user = context.getUser();
    List<ModelInfo> savedModels = new ArrayList<>();
    DependencyManager dm = new DependencyManager();
    for (ModelResource resource : resources) {
        dm.addResource(resource);
    }
    dm.getSorted().forEach(resource -> {
        try {
            IModelRepository modelRepository = modelRepoFactory.getRepositoryByModel(resource.getId());
            ModelInfo importedModel = modelRepository.save((ModelResource) resource, user);
            savedModels.add(importedModel);
            postProcessImportedModel(importedModel, new FileContent(extractedFile.getFileName(), extractedFile.getContent()), user);
        } catch (Exception e) {
            logger.error("Problem importing model", e);
            throw new ModelImporterException("Problem importing model", e);
        }
    });
    return savedModels;
}
Also used : IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) FileContent(org.eclipse.vorto.repository.core.FileContent) IUserContext(org.eclipse.vorto.repository.core.IUserContext) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelResource(org.eclipse.vorto.repository.core.ModelResource) ArrayList(java.util.ArrayList) DependencyManager(org.eclipse.vorto.repository.core.impl.utils.DependencyManager) IOException(java.io.IOException) BulkUploadException(org.eclipse.vorto.repository.web.core.exceptions.BulkUploadException)

Example 53 with ModelInfo

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

the class AbstractModelImporter method doImport.

@Override
public List<ModelInfo> doImport(String uploadHandleId, Context context) {
    StorageItem uploadedItem = this.uploadStorage.get(uploadHandleId);
    if (uploadedItem == null) {
        throw new ModelImporterException(String.format("No uploaded file found for handleId '%s'", uploadHandleId));
    }
    List<ModelInfo> importedModels = new ArrayList<>();
    try {
        if (handleZipUploads() && isZipFile(uploadedItem.getValue())) {
            getUploadedFilesFromZip(uploadedItem.getValue().getContent()).stream().forEach(extractedFile -> {
                List<ModelResource> resources = this.convert(extractedFile, context);
                importedModels.addAll(sortAndSaveToRepository(resources, extractedFile, context));
            });
        } else {
            List<ModelResource> resources = this.convert(uploadedItem.getValue(), context);
            importedModels.addAll(sortAndSaveToRepository(resources, uploadedItem.getValue(), context));
        }
    } finally {
        this.uploadStorage.remove(uploadHandleId);
    }
    return importedModels;
}
Also used : ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelResource(org.eclipse.vorto.repository.core.ModelResource) ArrayList(java.util.ArrayList) StorageItem(org.eclipse.vorto.repository.core.impl.StorageItem)

Example 54 with ModelInfo

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

the class ElasticSearchService method fromSearchHit.

private ModelInfo fromSearchHit(SearchHit searchHit) {
    ModelInfo modelInfo = new ModelInfo();
    Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
    modelInfo.setId(ModelId.fromPrettyFormat((String) sourceAsMap.get(BasicIndexFieldExtractor.MODEL_ID)));
    modelInfo.setType(ModelType.valueOf((String) sourceAsMap.get(BasicIndexFieldExtractor.MODEL_TYPE)));
    modelInfo.setState((String) sourceAsMap.get(BasicIndexFieldExtractor.STATE));
    modelInfo.setVisibility((String) sourceAsMap.get(BasicIndexFieldExtractor.VISIBILITY));
    modelInfo.setAuthor((String) sourceAsMap.get(BasicIndexFieldExtractor.AUTHOR));
    modelInfo.setDescription((String) sourceAsMap.get(BasicIndexFieldExtractor.DESCRIPTION));
    modelInfo.setDisplayName((String) sourceAsMap.get(BasicIndexFieldExtractor.DISPLAY_NAME));
    modelInfo.setHasImage(Boolean.parseBoolean((String) sourceAsMap.get(BasicIndexFieldExtractor.MODEL_HASIMAGE)));
    String createdOn = (String) sourceAsMap.get(BasicIndexFieldExtractor.MODEL_CREATIONDATE);
    modelInfo.setCreationDate(new Date(Long.parseLong(createdOn)));
    return modelInfo;
}
Also used : ModelInfo(org.eclipse.vorto.repository.core.ModelInfo)

Example 55 with ModelInfo

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

the class ElasticSearchService method reindexAllModels.

@Override
public IndexingResult reindexAllModels() {
    IndexingResult result = new IndexingResult();
    // (1) Delete all models in the index
    deleteAllModels(VORTO_INDEX);
    // (2) Index all models in all the tenants
    namespaceRepository.findAll().stream().forEach(namespace -> {
        IModelRepository repo = this.repositoryFactory.getRepository(namespace.getWorkspaceId());
        List<ModelInfo> modelsToIndex = repo.search("");
        if (!modelsToIndex.isEmpty()) {
            BulkRequest bulkRequest = new BulkRequest();
            modelsToIndex.forEach(model -> {
                bulkRequest.add(createIndexRequest(model, repo.getWorkspaceId()));
            });
            try {
                BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
                // temporary fix: getting namespace name instead of tenant ID here
                // in the long run, once the tenant service is gone we can normalize
                result.addIndexedNamespace(namespace.getName(), modelsToIndex.size());
                logger.info(String.format("Received %d replies for workspace '%s' with %d models", bulkResponse.getItems().length, repo.getWorkspaceId(), modelsToIndex.size()));
            } catch (IOException e) {
                throw new IndexingException(String.format("Error trying to index all models in '%s' workspace.", repo.getWorkspaceId()), e);
            }
        }
    });
    return result;
}
Also used : IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IOException(java.io.IOException)

Aggregations

ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)89 Test (org.junit.Test)33 IUserContext (org.eclipse.vorto.repository.core.IUserContext)32 ModelId (org.eclipse.vorto.model.ModelId)28 IModelRepository (org.eclipse.vorto.repository.core.IModelRepository)21 FileContent (org.eclipse.vorto.repository.core.FileContent)11 ClassPathResource (org.springframework.core.io.ClassPathResource)11 IOException (java.io.IOException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 ModelNotFoundException (org.eclipse.vorto.repository.core.ModelNotFoundException)8 WorkflowException (org.eclipse.vorto.repository.workflow.WorkflowException)8 BeforeClass (org.junit.BeforeClass)7 ResponseEntity (org.springframework.http.ResponseEntity)7 List (java.util.List)6 Optional (java.util.Optional)6 ModelAlreadyExistsException (org.eclipse.vorto.repository.core.ModelAlreadyExistsException)6 NotAuthorizedException (org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)6 DependencyManager (org.eclipse.vorto.repository.core.impl.utils.DependencyManager)5