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());
}
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);
}
}
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;
}
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;
}
Aggregations