Search in sources :

Example 61 with ModelId

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

the class ModelRepository method getMappingModelsForTargetPlatform.

@Override
public List<ModelInfo> getMappingModelsForTargetPlatform(ModelId modelId, String targetPlatform, Optional<String> version) {
    LOGGER.info("Fetching mapping models for model ID " + modelId.getPrettyFormat() + " and key " + targetPlatform);
    Set<ModelInfo> mappingResources = new HashSet<>();
    ModelInfo modelResource = getBasicInfo(modelId);
    if (modelResource != null) {
        for (ModelInfo referenceeModelInfo : this.getModelsReferencing(modelId)) {
            if (referenceeModelInfo.getType() != ModelType.Mapping || version.isPresent() && !referenceeModelInfo.getId().getVersion().equals(version.get())) {
                continue;
            }
            if (referenceeModelInfo.getTargetPlatformKey() != null) {
                if (targetPlatform.equalsIgnoreCase(referenceeModelInfo.getTargetPlatformKey())) {
                    mappingResources.add(referenceeModelInfo);
                }
            } else if (getEMFResource(referenceeModelInfo.getId()).matchesTargetPlatform(targetPlatform)) {
                mappingResources.add(referenceeModelInfo);
            }
        }
        for (ModelId referencedModelId : modelResource.getReferences()) {
            mappingResources.addAll(this.repositoryFactory.getRepositoryByModel(referencedModelId).getMappingModelsForTargetPlatform(referencedModelId, targetPlatform, version));
        }
    }
    return new ArrayList<>(mappingResources);
}
Also used : ModelId(org.eclipse.vorto.model.ModelId)

Example 62 with ModelId

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

the class AbstractModelParser method parse.

@Override
public ModelInfo parse(InputStream is) {
    Injector injector = getInjector();
    XtextResourceSet resourceSet = workspace.getResourceSet();
    Resource resource = createResource(fileName, getContent(is), resourceSet).orElseThrow(() -> new ValidationException("Xtext is not able to create a resource for this model. Check if you are using the correct parser.", getModelInfoFromFilename()));
    if (resource.getContents().size() <= 0) {
        throw new ValidationException("Xtext is not able to create a model out of this file. Check if the file you are using is correct.", getModelInfoFromFilename());
    }
    Model model = (Model) resource.getContents().get(0);
    // always load direct references to make the model complete
    workspace.loadFromRepository(getReferences(model));
    if (this.isValidationEnabled) {
        /* Execute validators */
        IResourceValidator validator = injector.getInstance(IResourceValidator.class);
        List<Issue> issues = validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
        if (issues.size() > 0) {
            List<ModelId> missingReferences = getMissingReferences(model, issues);
            if (missingReferences.size() > 0) {
                throw new CouldNotResolveReferenceException(getModelInfo(model).orElse(getModelInfoFromFilename()), missingReferences);
            } else {
                Set<ValidationIssue> validationIssues = convertIssues(issues);
                throw new ValidationException(collate(validationIssues), validationIssues, getModelInfo(model).orElse(getModelInfoFromFilename()));
            }
        }
        if (!resource.getErrors().isEmpty()) {
            throw new ValidationException(resource.getErrors().get(0).getMessage(), getModelInfo(model).orElse(getModelInfoFromFilename()));
        }
    }
    return new ModelResource((Model) resource.getContents().get(0));
}
Also used : ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) Issue(org.eclipse.xtext.validation.Issue) IResourceValidator(org.eclipse.xtext.validation.IResourceValidator) CouldNotResolveReferenceException(org.eclipse.vorto.repository.core.impl.validation.CouldNotResolveReferenceException) ModelResource(org.eclipse.vorto.repository.core.ModelResource) Resource(org.eclipse.emf.ecore.resource.Resource) ModelResource(org.eclipse.vorto.repository.core.ModelResource) Injector(com.google.inject.Injector) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) Model(org.eclipse.vorto.core.api.model.model.Model) ModelId(org.eclipse.vorto.model.ModelId)

Example 63 with ModelId

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

the class LocalModelWorkspace method loadFromRepository.

public void loadFromRepository(Collection<ModelId> modelIds) {
    Collection<ModelId> allReferences = modelIds;
    allReferences.removeAll(this.modelIds);
    allReferences.forEach(refModelId -> {
        try {
            repoFactory.getRepositoryByModel(refModelId).getFileContent(refModelId, Optional.empty()).ifPresent(refFile -> {
                createResource(refFile.getFileName(), refFile.getContent(), resourceSet);
            });
        } catch (ModelNotFoundException notFoundException) {
            throw new ValidationException("Could not find reference " + refModelId.getPrettyFormat(), null);
        }
    });
    // add references, so that they are not looked up again
    this.modelIds.addAll(allReferences);
}
Also used : ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) ModelId(org.eclipse.vorto.model.ModelId)

Example 64 with ModelId

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

the class ModelSequencerOld method execute.

@Override
public boolean execute(Property inputProperty, Node outputNode, Context context) throws Exception {
    Binary binaryValue = inputProperty.getBinary();
    CheckArg.isNotNull(binaryValue, "binary");
    ModelInfo modelResource = null;
    try {
        IModelParser parser = ModelParserFactory.instance().getParser(outputNode.getPath());
        modelResource = parser.parse(binaryValue.getStream());
        outputNode.setProperty("vorto:description", modelResource.getDescription() != null ? modelResource.getDescription() : "");
        outputNode.setProperty("vorto:type", modelResource.getType().name());
        outputNode.setProperty("vorto:displayname", modelResource.getDisplayName());
        outputNode.setProperty("vorto:version", modelResource.getId().getVersion());
        outputNode.setProperty("vorto:namespace", modelResource.getId().getNamespace());
        outputNode.setProperty("vorto:name", modelResource.getId().getName());
        if (outputNode.hasProperty("vorto:references")) {
            // first remove any previous references of the node.
            outputNode.getProperty("vorto:references").remove();
            outputNode.getSession().save();
        }
        ModelReferencesHelper referencesHelper = new ModelReferencesHelper(modelResource.getReferences());
        if (referencesHelper.hasReferences()) {
            List<Value> references = new ArrayList<Value>();
            for (ModelId modelId : referencesHelper.getReferences()) {
                ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
                Node referencedFolder = outputNode.getSession().getNode(modelIdHelper.getFullPath());
                Node reference = referencedFolder.getNodes().nextNode();
                references.add(context.valueFactory().createValue(reference));
            }
            outputNode.setProperty("vorto:references", references.toArray(new Value[references.size()]));
        }
    } catch (Throwable couldNotParse) {
        outputNode.setProperty("vorto:description", "Erronous Model !!!!");
        outputNode.setProperty("vorto:type", ModelType.create(outputNode.getPath()).name());
        outputNode.setProperty("vorto:displayname", "");
        outputNode.setProperty("vorto:version", "1.0.0");
        outputNode.setProperty("vorto:namespace", "erronous");
        outputNode.setProperty("vorto:name", outputNode.getName());
    }
    return true;
}
Also used : ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) ModelReferencesHelper(org.eclipse.vorto.repository.core.impl.utils.ModelReferencesHelper) Node(javax.jcr.Node) Value(javax.jcr.Value) ArrayList(java.util.ArrayList) Binary(javax.jcr.Binary) ModelId(org.eclipse.vorto.model.ModelId)

Example 65 with ModelId

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

the class ReferenceIntegrityDiagnostic method apply.

@Override
public Collection<Diagnostic> apply(Node node) {
    Collection<Diagnostic> diagnostics = Lists.newArrayList();
    try {
        String nodeId = node.getIdentifier();
        ModelId modelId = NodeDiagnosticUtils.getModelId(node.getPath()).orElseThrow(() -> new NodeDiagnosticException("Cannot generate modelId from supplied node with identifier '" + nodeId + "'"));
        if (node.hasProperty(VORTO_REFERENCES)) {
            for (Value value : node.getProperty(VORTO_REFERENCES).getValues()) {
                try {
                    node.getSession().getNodeByIdentifier(value.getString());
                } catch (ItemNotFoundException e) {
                    diagnostics.add(new Diagnostic(modelId, "The model has reference to node '" + value.getString() + "' but it cannot be found in the repository."));
                }
            }
        }
        PropertyIterator propIter = node.getReferences();
        while (propIter.hasNext()) {
            Property prop = propIter.nextProperty();
            try {
                Node referencedByFileNode = prop.getParent();
                ModelIdHelper.fromPath(referencedByFileNode.getParent().getPath());
            } catch (Exception e) {
                diagnostics.add(new Diagnostic(modelId, "The model is being referenced by a stale node."));
            }
        }
    } catch (RepositoryException e) {
        throw new NodeDiagnosticException("Error in accessing some properties of node", e);
    }
    return diagnostics;
}
Also used : Node(javax.jcr.Node) Value(javax.jcr.Value) PropertyIterator(javax.jcr.PropertyIterator) Diagnostic(org.eclipse.vorto.repository.core.Diagnostic) NodeDiagnostic(org.eclipse.vorto.repository.core.impl.RepositoryDiagnostics.NodeDiagnostic) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property) ModelId(org.eclipse.vorto.model.ModelId) ItemNotFoundException(javax.jcr.ItemNotFoundException) RepositoryException(javax.jcr.RepositoryException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

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