Search in sources :

Example 1 with ProjectImportRequest

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;
}
Also used : ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ArrayList(java.util.ArrayList) ProjectImportRequest(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest) Pair(org.apache.commons.lang3.tuple.Pair)

Example 2 with ProjectImportRequest

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;
}
Also used : ProjectExportTaskMonitor(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportTaskMonitor) ZipFile(java.util.zip.ZipFile) ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) ProjectExportRequest(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportRequest) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) ProjectImportRequest(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest)

Example 3 with ProjectImportRequest

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)));
}
Also used : RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) UnsupportedFormatException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.exception.UnsupportedFormatException) ZipFile(java.util.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile) MultipartFile(org.springframework.web.multipart.MultipartFile) ProjectImportRequest(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest) RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RProject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ProjectImportRequest

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;
}
Also used : ExportedSourceDocument(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedSourceDocument) ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) ExportedSourceDocument(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedSourceDocument) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ArrayList(java.util.ArrayList) ZipFile(java.util.zip.ZipFile) File(java.io.File) ProjectImportRequest(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest) Pair(org.apache.commons.lang3.tuple.Pair)

Example 5 with ProjectImportRequest

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);
    }
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ProjectImportRequest(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest) IOException(java.io.IOException) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) ZipFile(java.util.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File) IFeedback(org.apache.wicket.feedback.IFeedback) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload)

Aggregations

ProjectImportRequest (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest)5 ZipFile (java.util.zip.ZipFile)4 ExportedProject (de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject)3 File (java.io.File)3 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)2 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)2 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)2 BufferedInputStream (java.io.BufferedInputStream)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 Pair (org.apache.commons.lang3.tuple.Pair)2 ProjectExportRequest (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportRequest)1 ProjectExportTaskMonitor (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportTaskMonitor)1 ExportedSourceDocument (de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedSourceDocument)1 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)1 UnsupportedFormatException (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.exception.UnsupportedFormatException)1 RProject (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RProject)1 ApiOperation (io.swagger.annotations.ApiOperation)1