use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest in project webanno by webanno.
the class CuratedDocumentsExporterTest method runImportAndFetchDocuments.
private List<Pair<SourceDocument, String>> runImportAndFetchDocuments(ZipFile aZipFile) throws Exception {
ArgumentCaptor<SourceDocument> sourceDocCaptor = ArgumentCaptor.forClass(SourceDocument.class);
ArgumentCaptor<String> usernameCaptor = ArgumentCaptor.forClass(String.class);
// Import the project again
ExportedProject exProject = ProjectExportServiceImpl.loadExportedProject(aZipFile);
ProjectImportRequest importRequest = new ProjectImportRequest(true);
sut.importData(importRequest, project, exProject, aZipFile);
verify(casStorageService, atLeastOnce()).getCasFile(sourceDocCaptor.capture(), usernameCaptor.capture());
List<Pair<SourceDocument, String>> importedCases = new ArrayList<>();
List<SourceDocument> docs = sourceDocCaptor.getAllValues();
List<String> users = usernameCaptor.getAllValues();
for (int i = 0; i < docs.size(); i++) {
importedCases.add(Pair.of(docs.get(i), users.get(i)));
}
return importedCases;
}
use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest in project webanno by webanno.
the class LayerExporterTest method runExportImportAndFetchLayers.
private ArgumentCaptor<AnnotationLayer> runExportImportAndFetchLayers() throws Exception {
// Export the project
ProjectExportRequest exportRequest = new ProjectExportRequest();
ProjectExportTaskMonitor monitor = new ProjectExportTaskMonitor();
exportRequest.setProject(project);
ExportedProject exportedProject = new ExportedProject();
sut.exportData(exportRequest, monitor, exportedProject, workFolder);
// Import the project again
ArgumentCaptor<AnnotationLayer> captor = ArgumentCaptor.forClass(AnnotationLayer.class);
doNothing().when(annotationService).createOrUpdateLayer(captor.capture());
ProjectImportRequest importRequest = new ProjectImportRequest(true);
ZipFile zipFile = mock(ZipFile.class);
sut.importData(importRequest, project, exportedProject, zipFile);
return captor;
}
use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest in project webanno by webanno.
the class AeroRemoteApiController method projectImport.
@ApiOperation(value = "Import a previously exported project")
@//
RequestMapping(//
value = ("/" + PROJECTS + "/" + IMPORT), //
method = RequestMethod.POST, //
consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<RProject>> projectImport(@RequestPart(PARAM_FILE) MultipartFile aFile) throws Exception {
// Get current user - this will throw an exception if the current user does not exit
User user = getCurrentUser();
// Check for the access
assertPermission("User [" + user.getUsername() + "] is not allowed to import projects", userRepository.isAdministrator(user));
Project importedProject;
File tempFile = File.createTempFile("webanno-training", null);
try (InputStream is = new BufferedInputStream(aFile.getInputStream());
OutputStream os = new FileOutputStream(tempFile)) {
if (!ZipUtils.isZipStream(is)) {
throw new UnsupportedFormatException("Invalid ZIP file");
}
IOUtils.copyLarge(is, os);
if (!ImportUtil.isZipValidWebanno(tempFile)) {
throw new UnsupportedFormatException("Incompatible to webanno ZIP file");
}
// importedProject = importService.importProject(tempFile, false);
ProjectImportRequest request = new ProjectImportRequest(false);
importedProject = exportService.importProject(request, new ZipFile(tempFile));
} finally {
tempFile.delete();
}
return ResponseEntity.ok(new RResponse<>(new RProject(importedProject)));
}
use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest in project webanno by webanno.
the class AnnotationDocumentsExporterTest method runImportAndFetchDocuments.
private List<Pair<SourceDocument, String>> runImportAndFetchDocuments(ZipFile aZipFile) throws Exception {
// Import the project again
ExportedProject exProject = ProjectExportServiceImpl.loadExportedProject(aZipFile);
// Provide source documents based on data in the exported project
when(documentService.listSourceDocuments(any())).then(invocation -> {
long i = 1;
List<SourceDocument> docs = new ArrayList<>();
for (ExportedSourceDocument exDoc : exProject.getSourceDocuments()) {
SourceDocument doc = new SourceDocument();
doc.setId(i++);
doc.setName(exDoc.getName());
doc.setProject(project);
docs.add(doc);
}
return docs;
});
ProjectImportRequest importRequest = new ProjectImportRequest(true);
sut.importData(importRequest, project, exProject, aZipFile);
List<Pair<SourceDocument, String>> importedCases = new ArrayList<>();
for (SourceDocument doc : documentService.listSourceDocuments(project)) {
File annFolder = casStorageService.getAnnotationFolder(doc);
for (File serFile : annFolder.listFiles((dir, name) -> name.endsWith(".ser"))) {
importedCases.add(Pair.of(doc, removeExtension(serFile.getName())));
}
}
return importedCases;
}
use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest in project webanno by webanno.
the class ProjectImportPanel method actionImport.
private void actionImport(AjaxRequestTarget aTarget, Form<Preferences> aForm) {
List<FileUpload> exportedProjects = fileUpload.getFileUploads();
User currentUser = userRepository.getCurrentUser();
boolean currentUserIsAdministrator = userRepository.isAdministrator(currentUser);
boolean currentUserIsProjectCreator = userRepository.isProjectCreator(currentUser);
boolean createMissingUsers;
boolean importPermissions;
// Importing of permissions is only allowed if the importing user is an administrator
if (currentUserIsAdministrator) {
createMissingUsers = preferences.getObject().generateUsers;
importPermissions = preferences.getObject().importPermissions;
} else // ... otherwise we force-disable importing of permissions so that the only remaining
// permission for non-admin users is that they become the managers of projects they import.
{
createMissingUsers = false;
importPermissions = false;
}
// If the current user is an administrator and importing of permissions is *DISABLED*, we
// configure the current user as a project manager. But if importing of permissions is
// *ENABLED*, we do not set the admin up as a project manager because we would assume that
// the admin wants to restore a project (maybe one exported from another instance) and in
// that case we want to maintain the permissions the project originally had without adding
// the admin as a manager.
Optional<User> manager = Optional.empty();
if (currentUserIsAdministrator) {
if (!importPermissions) {
manager = Optional.of(currentUser);
}
} else // importing the project for own use, so we add the user as a project manager.
if (currentUserIsProjectCreator) {
manager = Optional.of(currentUser);
}
Project importedProject = null;
for (FileUpload exportedProject : exportedProjects) {
try {
// Workaround for WICKET-6425
File tempFile = File.createTempFile("webanno-training", null);
try (InputStream is = new BufferedInputStream(exportedProject.getInputStream());
OutputStream os = new FileOutputStream(tempFile)) {
if (!ZipUtils.isZipStream(is)) {
throw new IOException("Invalid ZIP file");
}
IOUtils.copyLarge(is, os);
if (!ImportUtil.isZipValidWebanno(tempFile)) {
throw new IOException("ZIP file is not a WebAnno project archive");
}
ProjectImportRequest request = new ProjectImportRequest(createMissingUsers, importPermissions, manager);
importedProject = exportService.importProject(request, new ZipFile(tempFile));
} finally {
tempFile.delete();
}
} catch (Exception e) {
aTarget.addChildren(getPage(), IFeedback.class);
error("Error importing project: " + ExceptionUtils.getRootCauseMessage(e));
LOG.error("Error importing project", e);
}
}
if (importedProject != null) {
selectedModel.setObject(importedProject);
aTarget.add(getPage());
Session.get().setMetaData(CURRENT_PROJECT, importedProject);
}
}
Aggregations