Search in sources :

Example 1 with ChangeSet

use of org.eclipse.vorto.model.refactor.ChangeSet 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 ChangeSet

use of org.eclipse.vorto.model.refactor.ChangeSet 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 ChangeSet

use of org.eclipse.vorto.model.refactor.ChangeSet in project vorto by eclipse.

the class RemoteImporter method convert.

@Override
protected List<ModelResource> convert(FileUpload fileUpload, Context context) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    body.add("file", new FileContentResource(fileUpload));
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
    ResponseEntity<byte[]> conversionResult = restTemplate.postForEntity(this.endpointUrl + "/api/2/plugins/importers/{pluginkey}/file_conversion", requestEntity, byte[].class, this.info.getKey());
    IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(conversionResult.getBody()))).read();
    List<ModelResource> resources = new ArrayList<>();
    ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels(context.getTargetNamespace().get()).execute();
    for (Model model : changeSet.get()) {
        resources.add(new ModelResource(model));
    }
    return resources;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ArrayList(java.util.ArrayList) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) ZipInputStream(java.util.zip.ZipInputStream) ModelResource(org.eclipse.vorto.repository.core.ModelResource) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(org.eclipse.vorto.core.api.model.model.Model) FileContentResource(org.eclipse.vorto.repository.plugin.FileContentResource) ChangeSet(org.eclipse.vorto.model.refactor.ChangeSet) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 4 with ChangeSet

use of org.eclipse.vorto.model.refactor.ChangeSet in project vorto by eclipse.

the class ModelRepository method rename.

@Override
public ModelInfo rename(ModelId oldModelId, ModelId newModelId, IUserContext user) {
    Namespace namespace = namespaceService.findNamespaceByWorkspaceId(getWorkspaceId());
    if (getById(newModelId) != null) {
        throw new ModelAlreadyExistsException();
    } else if (!newModelId.getNamespace().startsWith(namespace.getName())) {
        throw new NewNamespacesNotSupersetException();
    }
    ModelInfo oldModel = getById(oldModelId);
    ChangeSet changeSet = refactorModelWithNewId(oldModel, newModelId);
    saveChangeSetIntoRepository(changeSet, user);
    ModelInfo newModel = getById(newModelId);
    newModel = copy(oldModel, newModel, user);
    removeModel(oldModel.getId());
    return newModel;
}
Also used : NewNamespacesNotSupersetException(org.eclipse.vorto.repository.tenant.NewNamespacesNotSupersetException) ChangeSet(org.eclipse.vorto.model.refactor.ChangeSet) Namespace(org.eclipse.vorto.repository.domain.Namespace)

Aggregations

ChangeSet (org.eclipse.vorto.model.refactor.ChangeSet)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ModelResource (org.eclipse.vorto.repository.core.ModelResource)3 IModelWorkspace (org.eclipse.vorto.utilities.reader.IModelWorkspace)3 Model (org.eclipse.vorto.core.api.model.model.Model)2 ArrayList (java.util.ArrayList)1 ZipInputStream (java.util.zip.ZipInputStream)1 Namespace (org.eclipse.vorto.repository.domain.Namespace)1 FileContentResource (org.eclipse.vorto.repository.plugin.FileContentResource)1 NewNamespacesNotSupersetException (org.eclipse.vorto.repository.tenant.NewNamespacesNotSupersetException)1 ModelWorkspaceReader (org.eclipse.vorto.utilities.reader.ModelWorkspaceReader)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 MultiValueMap (org.springframework.util.MultiValueMap)1