Search in sources :

Example 1 with ModelResource

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

the class VortoModelImporter method refactor.

private FileUpload refactor(FileUpload fileUpload, String targetNamespace) {
    IModelWorkspace workspace = IModelWorkspace.newReader().addFile(new ByteArrayInputStream(fileUpload.getContent()), ModelType.fromFileName(fileUpload.getFileName())).read();
    ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels(targetNamespace).execute();
    ModelResource resource = new ModelResource(changeSet.get().get(0));
    return FileUpload.create(fileUpload.getFileName(), resource.toDSL());
}
Also used : ModelResource(org.eclipse.vorto.repository.core.ModelResource) ByteArrayInputStream(java.io.ByteArrayInputStream) ChangeSet(org.eclipse.vorto.model.refactor.ChangeSet) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace)

Example 2 with ModelResource

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

the class VortoModelImporter method preValidation.

/**
 * changes the namespace of the uploaded vorto model(s) , if target namespace is specified
 */
@Override
protected FileUpload preValidation(FileUpload fileUpload, Context context) {
    if (context.getTargetNamespace().isPresent()) {
        if (fileUpload.getFileExtension().endsWith(EXTENSION_ZIP)) {
            ModelWorkspaceReader reader = IModelWorkspace.newReader();
            getUploadedFilesFromZip(fileUpload.getContent()).stream().filter(this::isSupported).forEach(extractedFile -> {
                reader.addFile(new ByteArrayInputStream(addVortolangIfMissing(extractedFile).getContent()), ModelType.fromFileName(extractedFile.getFileExtension()));
            });
            IModelWorkspace workspace = reader.read();
            ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels(context.getTargetNamespace().get()).execute();
            ZipUploadFile zipFile = new ZipUploadFile(fileUpload.getFileName());
            for (Model model : changeSet.get()) {
                ModelResource resource = new ModelResource(model);
                zipFile.addToZip(FileUpload.create(resource.getId().getPrettyFormat().replace("\\.", "_") + resource.getType().getExtension(), resource.toDSL()));
            }
            return zipFile.getFileUpload();
        } else {
            return refactor(addVortolangIfMissing(fileUpload), context.getTargetNamespace().get());
        }
    } else {
        return addVortolangIfMissing(fileUpload);
    }
}
Also used : ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) ModelResource(org.eclipse.vorto.repository.core.ModelResource) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(org.eclipse.vorto.core.api.model.model.Model) ChangeSet(org.eclipse.vorto.model.refactor.ChangeSet) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace)

Example 3 with ModelResource

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

the class VortoModelImporter method convert.

@Override
protected List<ModelResource> convert(FileUpload fileUpload, Context context) {
    List<ModelResource> result = new ArrayList<>();
    if (fileUpload.getFileExtension().equalsIgnoreCase(EXTENSION_ZIP)) {
        Collection<FileContent> fileContents = getFileContents(fileUpload);
        result.addAll(fileContents.stream().map(fileContent -> (ModelResource) getModelParserFactory().getParser(fileContent.getFileName()).parse(new ByteArrayInputStream(fileContent.getContent()))).collect(Collectors.toList()));
    } else {
        final ModelResource modelInfo = (ModelResource) getModelParserFactory().getParser(fileUpload.getFileExtension()).parse(new ByteArrayInputStream(fileUpload.getContent()));
        result.add(modelInfo);
    }
    return Collections.unmodifiableList(result);
}
Also used : FileContent(org.eclipse.vorto.repository.core.FileContent) ModelResource(org.eclipse.vorto.repository.core.ModelResource) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList)

Example 4 with ModelResource

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

the class AbstractModelImporter method parseDSL.

protected ModelResource parseDSL(String fileName, byte[] content, Collection<FileContent> fileReferences) {
    IModelParser modelParser = modelParserFactory.getParser(fileName);
    modelParser.setWorkspace(new LocalModelWorkspace(modelRepoFactory, fileReferences));
    modelParser.enableValidation();
    return (ModelResource) modelParser.parse(new ByteArrayInputStream(content));
}
Also used : LocalModelWorkspace(org.eclipse.vorto.repository.core.impl.parser.LocalModelWorkspace) IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) ModelResource(org.eclipse.vorto.repository.core.ModelResource) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 5 with ModelResource

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

the class TypeImportValidation method validate.

@Override
public void validate(ModelInfo modelResource, InvocationContext context) throws ValidationException {
    if (!(modelResource instanceof ModelResource)) {
        // do not check types if passed resource is not a parse emf model resource
        return;
    }
    Collection<String> unImportedReferences = Lists.newArrayList();
    ModelResource emfModel = (ModelResource) modelResource;
    Model model = emfModel.getModel();
    if (model == null)
        return;
    if (model instanceof Entity) {
        unImportedReferences.addAll(getUnimportedProperties(((Entity) model).getProperties(), model.getReferences()));
    } else if (model instanceof FunctionblockModel) {
        unImportedReferences.addAll(validateFunctionBlock((FunctionblockModel) model));
    } else if (model instanceof InformationModel) {
        unImportedReferences.addAll(getUnimportedFunctionblocks(((InformationModel) model).getProperties(), model.getReferences()));
    }
    unImportedReferences.forEach(ref -> System.out.println("Missing : " + ref));
    if (!unImportedReferences.isEmpty()) {
        throw new ValidationException(errorMessage(unImportedReferences), modelResource);
    }
}
Also used : Entity(org.eclipse.vorto.core.api.model.datatype.Entity) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) ModelResource(org.eclipse.vorto.repository.core.ModelResource) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) Model(org.eclipse.vorto.core.api.model.model.Model) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel)

Aggregations

ModelResource (org.eclipse.vorto.repository.core.ModelResource)12 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ArrayList (java.util.ArrayList)4 Model (org.eclipse.vorto.core.api.model.model.Model)4 ModelId (org.eclipse.vorto.model.ModelId)3 ChangeSet (org.eclipse.vorto.model.refactor.ChangeSet)3 IUserContext (org.eclipse.vorto.repository.core.IUserContext)3 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)3 IModelWorkspace (org.eclipse.vorto.utilities.reader.IModelWorkspace)3 ApiOperation (io.swagger.annotations.ApiOperation)2 FileContent (org.eclipse.vorto.repository.core.FileContent)2 IModelRepository (org.eclipse.vorto.repository.core.IModelRepository)2 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)2 ResponseEntity (org.springframework.http.ResponseEntity)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 Authentication (org.springframework.security.core.Authentication)2 Injector (com.google.inject.Injector)1 IOException (java.io.IOException)1 ZipInputStream (java.util.zip.ZipInputStream)1 Resource (org.eclipse.emf.ecore.resource.Resource)1