Search in sources :

Example 1 with INITIAL_CAS_PSEUDO_USER

use of de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.INITIAL_CAS_PSEUDO_USER in project webanno by webanno.

the class AnnotationDocumentExporter method exportAnnotationDocumentContents.

private void exportAnnotationDocumentContents(ProjectExportRequest aRequest, ProjectExportTaskMonitor aMonitor, ExportedProject aExProject, File aStage) throws UIMAException, ClassNotFoundException, IOException {
    Project project = aRequest.getProject();
    // The export process may store project-related information in this context to ensure it
    // is looked up only once during the bulk operation and the DB is not hit too often.
    Map<Pair<Project, String>, Object> bulkOperationContext = new HashMap<>();
    List<SourceDocument> documents = documentService.listSourceDocuments(project);
    int i = 1;
    int initProgress = aMonitor.getProgress();
    // Create a map containing the annotation documents for each source document. Doing this
    // as one DB access before the main processing to avoid hammering the DB in the loops
    // below.
    Map<SourceDocument, List<AnnotationDocument>> srcToAnnIdx = documentService.listAnnotationDocuments(project).stream().collect(groupingBy(doc -> doc.getDocument(), toList()));
    // Cache user lookups to avoid constantly hitting the database
    LoadingCache<String, User> usersCache = Caffeine.newBuilder().build(key -> userRepository.get(key));
    for (SourceDocument srcDoc : documents) {
        try (CasStorageSession session = CasStorageSession.openNested()) {
            // If the initial CAS does not exist yet, it must be created before export.
            if (!documentService.existsInitialCas(srcDoc)) {
                documentService.createOrReadInitialCas(srcDoc);
            }
            File targetDir = new File(aStage, ANNOTATION_CAS_FOLDER + srcDoc.getName());
            forceMkdir(targetDir);
            File initialCasFile = documentService.getCasFile(srcDoc, INITIAL_CAS_PSEUDO_USER);
            copyFileToDirectory(initialCasFile, targetDir);
            log.info("Exported annotation document content for user [" + INITIAL_CAS_PSEUDO_USER + "] for source document [" + srcDoc.getId() + "] in project [" + project.getName() + "] with id [" + project.getId() + "]");
            // 
            // Export per-user annotation document
            // 
            // Determine which format to use for export
            String formatId = FORMAT_AUTO.equals(aRequest.getFormat()) ? srcDoc.getFormat() : aRequest.getFormat();
            FormatSupport format = importExportService.getWritableFormatById(formatId).orElseGet(() -> {
                FormatSupport fallbackFormat = new WebAnnoTsv3FormatSupport();
                aMonitor.addMessage(LogMessage.warn(this, "Annotation: [%s] No writer " + "found for original format [%s] - exporting as [%s] " + "instead.", srcDoc.getName(), formatId, fallbackFormat.getName()));
                return fallbackFormat;
            });
            // Export annotations from regular users
            for (AnnotationDocument annDoc : srcToAnnIdx.computeIfAbsent(srcDoc, key -> emptyList())) {
                // annotation document is not NEW/IGNORE
                if (usersCache.get(annDoc.getUser()) != null && !annDoc.getState().equals(AnnotationDocumentState.NEW) && !annDoc.getState().equals(AnnotationDocumentState.IGNORE)) {
                    File annSerDir = new File(aStage.getAbsolutePath() + ANNOTATION_CAS_FOLDER + srcDoc.getName());
                    File annDocDir = new File(aStage.getAbsolutePath() + ANNOTATION_ORIGINAL_FOLDER + srcDoc.getName());
                    forceMkdir(annSerDir);
                    forceMkdir(annDocDir);
                    File annSerFile = documentService.getCasFile(srcDoc, annDoc.getUser());
                    File annFile = null;
                    if (annSerFile.exists()) {
                        annFile = importExportService.exportAnnotationDocument(srcDoc, annDoc.getUser(), format, annDoc.getUser(), ANNOTATION, false, bulkOperationContext);
                    }
                    if (annSerFile.exists()) {
                        copyFileToDirectory(annSerFile, annSerDir);
                        copyFileToDirectory(annFile, annDocDir);
                        forceDelete(annFile);
                    }
                    log.info("Exported annotation document content for user [" + annDoc.getUser() + "] for source document [" + srcDoc.getId() + "] in project [" + project.getName() + "] with id [" + project.getId() + "]");
                }
            }
            // correction type projects.
            if (PROJECT_TYPE_AUTOMATION.equals(project.getMode()) || PROJECT_TYPE_CORRECTION.equals(project.getMode())) {
                File corrSerFile = documentService.getCasFile(srcDoc, CORRECTION_USER);
                if (corrSerFile.exists()) {
                    // Copy CAS - this is used when importing the project again
                    // Util WebAnno 3.4.x, the CORRECTION_USER CAS was exported to 'curation'
                    // and 'curation_ser'.
                    // Since WebAnno 3.5.x, the CORRECTION_USER CAS is exported to 'annotation'
                    // and 'annotation_ser'.
                    File curationSerDir = new File(aStage + ANNOTATION_AS_SERIALISED_CAS + srcDoc.getName());
                    forceMkdir(curationSerDir);
                    copyFileToDirectory(corrSerFile, curationSerDir);
                    // Copy secondary export format for convenience - not used during import
                    File curationDir = new File(aStage + ANNOTATION_ORIGINAL_FOLDER + srcDoc.getName());
                    forceMkdir(curationDir);
                    File corrFile = importExportService.exportAnnotationDocument(srcDoc, CORRECTION_USER, format, CORRECTION_USER, CORRECTION);
                    copyFileToDirectory(corrFile, curationDir);
                    forceDelete(corrFile);
                }
            }
        }
        aMonitor.setProgress(initProgress + (int) ceil(((double) i) / documents.size() * 80.0));
        i++;
    }
}
Also used : Enumeration(java.util.Enumeration) PROJECT_TYPE_AUTOMATION(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.PROJECT_TYPE_AUTOMATION) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ProjectExportTaskMonitor(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportTaskMonitor) ANNOTATION(de.tudarmstadt.ukp.clarin.webanno.model.Mode.ANNOTATION) RepositoryProperties(de.tudarmstadt.ukp.clarin.webanno.api.RepositoryProperties) FastIOUtils.copy(de.tudarmstadt.ukp.clarin.webanno.support.io.FastIOUtils.copy) Collectors.toMap(java.util.stream.Collectors.toMap) Pair(org.apache.commons.lang3.tuple.Pair) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) FileUtils.forceMkdir(org.apache.commons.io.FileUtils.forceMkdir) ZipFile(java.util.zip.ZipFile) WebAnnoTsv3FormatSupport(de.tudarmstadt.ukp.clarin.webanno.tsv.WebAnnoTsv3FormatSupport) FORMAT_AUTO(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportRequest.FORMAT_AUTO) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) UIMAException(org.apache.uima.UIMAException) CasStorageSession(de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession) PROJECT_TYPE_CORRECTION(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.PROJECT_TYPE_CORRECTION) LoadingCache(com.github.benmanes.caffeine.cache.LoadingCache) Collections.emptyList(java.util.Collections.emptyList) PROJECT_FOLDER(de.tudarmstadt.ukp.clarin.webanno.api.ProjectService.PROJECT_FOLDER) UserDao(de.tudarmstadt.ukp.clarin.webanno.security.UserDao) Set(java.util.Set) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) Math.ceil(java.lang.Math.ceil) AnnotationDocumentState(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocumentState) FileUtils.copyFileToDirectory(org.apache.commons.io.FileUtils.copyFileToDirectory) ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) CORRECTION(de.tudarmstadt.ukp.clarin.webanno.model.Mode.CORRECTION) List(java.util.List) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) Function.identity(java.util.function.Function.identity) FilenameUtils(org.apache.commons.io.FilenameUtils) CORRECTION_USER(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.CORRECTION_USER) System.currentTimeMillis(java.lang.System.currentTimeMillis) FormatSupport(de.tudarmstadt.ukp.clarin.webanno.api.format.FormatSupport) LogMessage(de.tudarmstadt.ukp.clarin.webanno.support.logging.LogMessage) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) HashMap(java.util.HashMap) FileUtils.forceDelete(org.apache.commons.io.FileUtils.forceDelete) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) ProjectExporter(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExporter) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) INITIAL_CAS_PSEUDO_USER(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.INITIAL_CAS_PSEUDO_USER) ProjectExportRequest(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportRequest) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) DocumentService(de.tudarmstadt.ukp.clarin.webanno.api.DocumentService) Logger(org.slf4j.Logger) Files(java.nio.file.Files) IOException(java.io.IOException) ANNOTATION_FOLDER(de.tudarmstadt.ukp.clarin.webanno.api.ProjectService.ANNOTATION_FOLDER) File(java.io.File) Collectors.toList(java.util.stream.Collectors.toList) Component(org.springframework.stereotype.Component) ExportedAnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedAnnotationDocument) DurationFormatUtils(org.apache.commons.lang3.time.DurationFormatUtils) DOCUMENT_FOLDER(de.tudarmstadt.ukp.clarin.webanno.api.ProjectService.DOCUMENT_FOLDER) ProjectImportRequest(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest) ImportExportService(de.tudarmstadt.ukp.clarin.webanno.api.ImportExportService) WebAnnoTsv3FormatSupport(de.tudarmstadt.ukp.clarin.webanno.tsv.WebAnnoTsv3FormatSupport) FormatSupport(de.tudarmstadt.ukp.clarin.webanno.api.format.FormatSupport) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) HashMap(java.util.HashMap) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) WebAnnoTsv3FormatSupport(de.tudarmstadt.ukp.clarin.webanno.tsv.WebAnnoTsv3FormatSupport) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) ExportedAnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedAnnotationDocument) ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) Arrays.asList(java.util.Arrays.asList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) CasStorageSession(de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession) ZipFile(java.util.zip.ZipFile) File(java.io.File) Pair(org.apache.commons.lang3.tuple.Pair)

Example 2 with INITIAL_CAS_PSEUDO_USER

use of de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.INITIAL_CAS_PSEUDO_USER in project webanno by webanno.

the class AnnotationDocumentsExporterTest method thatImportingCorrectionProjectWorks_3_6_1.

@Test
public void thatImportingCorrectionProjectWorks_3_6_1() throws Exception {
    project.setMode(PROJECT_TYPE_CORRECTION);
    // Export the project and import it again
    List<Pair<SourceDocument, String>> imported = runImportAndFetchDocuments(new ZipFile("src/test/resources/exports/Export+Test+-+Curated+correction+project_3_6_1.zip"));
    // Check that the curation for the document in the project is imported
    assertThat(imported).extracting(p -> p.getKey().getName()).containsExactlyInAnyOrder("example_sentence.txt", "example_sentence.txt", "example_sentence.txt");
    // Since WebAnno 3.5.x, the CORRECTION_USER CAS is stored with the annotations
    assertThat(imported).extracting(Pair::getValue).containsExactlyInAnyOrder(INITIAL_CAS_PSEUDO_USER, "admin", CORRECTION_USER);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) CORRECTION_USER(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.CORRECTION_USER) Mock(org.mockito.Mock) ImportExportServiceImpl(de.tudarmstadt.ukp.clarin.webanno.api.dao.ImportExportServiceImpl) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CasStorageService(de.tudarmstadt.ukp.clarin.webanno.api.CasStorageService) PROJECT_TYPE_ANNOTATION(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.PROJECT_TYPE_ANNOTATION) XmiFormatSupport(de.tudarmstadt.ukp.clarin.webanno.xmi.XmiFormatSupport) ArrayList(java.util.ArrayList) AnnotationSchemaService(de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService) RepositoryProperties(de.tudarmstadt.ukp.clarin.webanno.api.RepositoryProperties) Pair(org.apache.commons.lang3.tuple.Pair) Arrays.asList(java.util.Arrays.asList) ZipFile(java.util.zip.ZipFile) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) INITIAL_CAS_PSEUDO_USER(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.INITIAL_CAS_PSEUDO_USER) CasStorageServiceImpl(de.tudarmstadt.ukp.clarin.webanno.api.dao.CasStorageServiceImpl) Before(org.junit.Before) PROJECT_TYPE_CORRECTION(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.PROJECT_TYPE_CORRECTION) DocumentService(de.tudarmstadt.ukp.clarin.webanno.api.DocumentService) Test(org.junit.Test) ExportedSourceDocument(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedSourceDocument) Mockito.when(org.mockito.Mockito.when) ProjectExportServiceImpl(de.tudarmstadt.ukp.clarin.webanno.api.dao.export.ProjectExportServiceImpl) BackupProperties(de.tudarmstadt.ukp.clarin.webanno.api.dao.BackupProperties) File(java.io.File) ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) FilenameUtils.removeExtension(org.apache.commons.io.FilenameUtils.removeExtension) List(java.util.List) Rule(org.junit.Rule) ProjectImportRequest(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ImportExportService(de.tudarmstadt.ukp.clarin.webanno.api.ImportExportService) TemporaryFolder(org.junit.rules.TemporaryFolder) ZipFile(java.util.zip.ZipFile) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 3 with INITIAL_CAS_PSEUDO_USER

use of de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.INITIAL_CAS_PSEUDO_USER in project webanno by webanno.

the class AnnotationDocumentsExporterTest method thatImportingCorrectionProjectWorks_3_4_x.

@Test
public void thatImportingCorrectionProjectWorks_3_4_x() throws Exception {
    project.setMode(PROJECT_TYPE_CORRECTION);
    // Export the project and import it again
    List<Pair<SourceDocument, String>> imported = runImportAndFetchDocuments(new ZipFile("src/test/resources/exports/Export+Test+-+Curated+correction+project_3_4_8.zip"));
    // Check that the curation for the document in the project is imported
    assertThat(imported).extracting(p -> p.getKey().getName()).containsExactlyInAnyOrder("example_sentence.txt", "example_sentence.txt");
    // Before WebAnno 3.5.x, the CORRECTION_USER CAS was stored with the curations
    assertThat(imported).extracting(Pair::getValue).containsExactlyInAnyOrder(INITIAL_CAS_PSEUDO_USER, "admin");
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) CORRECTION_USER(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.CORRECTION_USER) Mock(org.mockito.Mock) ImportExportServiceImpl(de.tudarmstadt.ukp.clarin.webanno.api.dao.ImportExportServiceImpl) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CasStorageService(de.tudarmstadt.ukp.clarin.webanno.api.CasStorageService) PROJECT_TYPE_ANNOTATION(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.PROJECT_TYPE_ANNOTATION) XmiFormatSupport(de.tudarmstadt.ukp.clarin.webanno.xmi.XmiFormatSupport) ArrayList(java.util.ArrayList) AnnotationSchemaService(de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService) RepositoryProperties(de.tudarmstadt.ukp.clarin.webanno.api.RepositoryProperties) Pair(org.apache.commons.lang3.tuple.Pair) Arrays.asList(java.util.Arrays.asList) ZipFile(java.util.zip.ZipFile) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) INITIAL_CAS_PSEUDO_USER(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.INITIAL_CAS_PSEUDO_USER) CasStorageServiceImpl(de.tudarmstadt.ukp.clarin.webanno.api.dao.CasStorageServiceImpl) Before(org.junit.Before) PROJECT_TYPE_CORRECTION(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.PROJECT_TYPE_CORRECTION) DocumentService(de.tudarmstadt.ukp.clarin.webanno.api.DocumentService) Test(org.junit.Test) ExportedSourceDocument(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedSourceDocument) Mockito.when(org.mockito.Mockito.when) ProjectExportServiceImpl(de.tudarmstadt.ukp.clarin.webanno.api.dao.export.ProjectExportServiceImpl) BackupProperties(de.tudarmstadt.ukp.clarin.webanno.api.dao.BackupProperties) File(java.io.File) ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) FilenameUtils.removeExtension(org.apache.commons.io.FilenameUtils.removeExtension) List(java.util.List) Rule(org.junit.Rule) ProjectImportRequest(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ImportExportService(de.tudarmstadt.ukp.clarin.webanno.api.ImportExportService) TemporaryFolder(org.junit.rules.TemporaryFolder) ZipFile(java.util.zip.ZipFile) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 4 with INITIAL_CAS_PSEUDO_USER

use of de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.INITIAL_CAS_PSEUDO_USER in project webanno by webanno.

the class AnnotationDocumentsExporterTest method thatImportingAnnotationProjectWorks_3_6_1.

@Test
public void thatImportingAnnotationProjectWorks_3_6_1() throws Exception {
    project.setMode(PROJECT_TYPE_ANNOTATION);
    // Export the project and import it again
    List<Pair<SourceDocument, String>> imported = runImportAndFetchDocuments(new ZipFile("src/test/resources/exports/Export+Test+-+Curated+annotation+project_3_6_1.zip"));
    // Check that the curation for the document in the project is imported
    assertThat(imported).extracting(p -> p.getKey().getName()).containsExactlyInAnyOrder("example_sentence.txt", "example_sentence.txt");
    assertThat(imported).extracting(Pair::getValue).containsExactlyInAnyOrder(INITIAL_CAS_PSEUDO_USER, "admin");
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) CORRECTION_USER(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.CORRECTION_USER) Mock(org.mockito.Mock) ImportExportServiceImpl(de.tudarmstadt.ukp.clarin.webanno.api.dao.ImportExportServiceImpl) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CasStorageService(de.tudarmstadt.ukp.clarin.webanno.api.CasStorageService) PROJECT_TYPE_ANNOTATION(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.PROJECT_TYPE_ANNOTATION) XmiFormatSupport(de.tudarmstadt.ukp.clarin.webanno.xmi.XmiFormatSupport) ArrayList(java.util.ArrayList) AnnotationSchemaService(de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService) RepositoryProperties(de.tudarmstadt.ukp.clarin.webanno.api.RepositoryProperties) Pair(org.apache.commons.lang3.tuple.Pair) Arrays.asList(java.util.Arrays.asList) ZipFile(java.util.zip.ZipFile) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) INITIAL_CAS_PSEUDO_USER(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.INITIAL_CAS_PSEUDO_USER) CasStorageServiceImpl(de.tudarmstadt.ukp.clarin.webanno.api.dao.CasStorageServiceImpl) Before(org.junit.Before) PROJECT_TYPE_CORRECTION(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.PROJECT_TYPE_CORRECTION) DocumentService(de.tudarmstadt.ukp.clarin.webanno.api.DocumentService) Test(org.junit.Test) ExportedSourceDocument(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedSourceDocument) Mockito.when(org.mockito.Mockito.when) ProjectExportServiceImpl(de.tudarmstadt.ukp.clarin.webanno.api.dao.export.ProjectExportServiceImpl) BackupProperties(de.tudarmstadt.ukp.clarin.webanno.api.dao.BackupProperties) File(java.io.File) ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) FilenameUtils.removeExtension(org.apache.commons.io.FilenameUtils.removeExtension) List(java.util.List) Rule(org.junit.Rule) ProjectImportRequest(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ImportExportService(de.tudarmstadt.ukp.clarin.webanno.api.ImportExportService) TemporaryFolder(org.junit.rules.TemporaryFolder) ZipFile(java.util.zip.ZipFile) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 5 with INITIAL_CAS_PSEUDO_USER

use of de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.INITIAL_CAS_PSEUDO_USER in project webanno by webanno.

the class ProjectCasDoctorPanel method actionRepair.

private void actionRepair(AjaxRequestTarget aTarget, Form<?> aForm) throws IOException, UIMAException, ClassNotFoundException {
    CasDoctor casDoctor = new CasDoctor();
    casDoctor.setApplicationContext(ApplicationContextProvider.getApplicationContext());
    casDoctor.setFatalChecks(false);
    casDoctor.setRepairClasses(formModel.repairs);
    Project project = getModelObject();
    formModel.messageSets = new ArrayList<>();
    for (SourceDocument sd : documentService.listSourceDocuments(project)) {
        // Repair INITIAL CAS
        {
            LogMessageSet messageSet = new LogMessageSet(sd.getName() + " [INITIAL]");
            try {
                casStorageService.forceActionOnCas(sd, INITIAL_CAS_PSEUDO_USER, (doc, user) -> createOrReadInitialCasWithoutSaving(doc, messageSet), // 
                (cas) -> casDoctor.repair(project, cas, messageSet.messages), true);
            } catch (Exception e) {
                messageSet.messages.add(new LogMessage(getClass(), LogLevel.ERROR, "Error repairing initial CAS for [" + sd.getName() + "]: " + e.getMessage()));
                LOG.error("Error repairing initial CAS for [{}]", sd.getName(), e);
            }
            noticeIfThereAreNoMessages(messageSet);
            formModel.messageSets.add(messageSet);
        }
        // Repair CORRECTION_USER CAS if necessary
        if (PROJECT_TYPE_CORRECTION.equals(project.getMode())) {
            LogMessageSet messageSet = new LogMessageSet(sd.getName() + " [" + CORRECTION_USER + "]");
            try {
                casStorageService.forceActionOnCas(sd, CORRECTION_USER, (doc, user) -> casStorageService.readCas(doc, user, UNMANAGED_NON_INITIALIZING_ACCESS), // 
                (cas) -> casDoctor.repair(project, cas, messageSet.messages), true);
            } catch (FileNotFoundException e) {
                // If there is no CAS for the correction user, then correction has not started
                // yet. This is not a problem, so we can ignore it. (REC: I wonder if this
                // assumption is correct in curation mode...)
                messageSet.messages.add(LogMessage.info(getClass(), "Correction seems to have not yet started."));
            } catch (Exception e) {
                messageSet.messages.add(new LogMessage(getClass(), LogLevel.ERROR, "Error checking annotations for [" + CORRECTION_USER + "] for [" + sd.getName() + "]: " + e.getMessage()));
                LOG.error("Error checking annotations for [{}] for [{}]", CORRECTION_USER, sd.getName(), e);
            }
            noticeIfThereAreNoMessages(messageSet);
            formModel.messageSets.add(messageSet);
        }
        // Repair CURATION_USER CAS
        {
            LogMessageSet messageSet = new LogMessageSet(sd.getName() + " [" + CURATION_USER + "]");
            try {
                casStorageService.forceActionOnCas(sd, CURATION_USER, (doc, user) -> casStorageService.readCas(doc, user, UNMANAGED_NON_INITIALIZING_ACCESS), // 
                (cas) -> casDoctor.repair(project, cas, messageSet.messages), true);
            } catch (FileNotFoundException e) {
                if (asList(CURATION_IN_PROGRESS, CURATION_FINISHED).contains(sd.getState())) {
                    messageSet.messages.add(LogMessage.error(getClass(), "Curation CAS missing."));
                } else {
                    // If there is no CAS for the curation user, then curation has not started
                    // yet. This is not a problem, so we can ignore it.
                    messageSet.messages.add(LogMessage.info(getClass(), "Curation has not started."));
                }
            } catch (Exception e) {
                messageSet.messages.add(new LogMessage(getClass(), LogLevel.ERROR, "Error checking annotations for [" + CURATION_USER + "] for [" + sd.getName() + "]: " + e.getMessage()));
                LOG.error("Error checking annotations for [{}] for [{}]", CURATION_USER, sd.getName(), e);
            }
            noticeIfThereAreNoMessages(messageSet);
            formModel.messageSets.add(messageSet);
        }
        // Repair regular annotator CASes
        for (AnnotationDocument ad : documentService.listAnnotationDocuments(sd)) {
            if (documentService.existsAnnotationCas(ad)) {
                LogMessageSet messageSet = new LogMessageSet(sd.getName() + " [" + ad.getUser() + "]");
                try {
                    casStorageService.forceActionOnCas(sd, ad.getUser(), (doc, user) -> casStorageService.readCas(doc, user, UNMANAGED_NON_INITIALIZING_ACCESS), // 
                    (cas) -> casDoctor.repair(project, cas, messageSet.messages), true);
                } catch (Exception e) {
                    messageSet.messages.add(new LogMessage(getClass(), LogLevel.ERROR, "Error repairing annotations of user [" + ad.getUser() + "] for [" + sd.getName() + "]: " + e.getMessage()));
                    LOG.error("Error repairing annotations of user [{}] for [{}]", ad.getUser(), sd.getName(), e);
                }
                noticeIfThereAreNoMessages(messageSet);
                formModel.messageSets.add(messageSet);
            }
        }
    }
    aTarget.add(this);
}
Also used : Form(org.apache.wicket.markup.html.form.Form) SpringBean(org.apache.wicket.spring.injection.annot.SpringBean) CORRECTION_USER(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.CORRECTION_USER) Safe(de.tudarmstadt.ukp.clarin.webanno.diag.repairs.Repair.Safe) LogMessage(de.tudarmstadt.ukp.clarin.webanno.support.logging.LogMessage) LoggerFactory(org.slf4j.LoggerFactory) CasStorageService(de.tudarmstadt.ukp.clarin.webanno.api.CasStorageService) CAS(org.apache.uima.cas.CAS) CURATION_USER(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.CURATION_USER) UNMANAGED_NON_INITIALIZING_ACCESS(de.tudarmstadt.ukp.clarin.webanno.api.casstorage.CasAccessMode.UNMANAGED_NON_INITIALIZING_ACCESS) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) CheckBoxMultipleChoice(org.apache.wicket.markup.html.form.CheckBoxMultipleChoice) Repair(de.tudarmstadt.ukp.clarin.webanno.diag.repairs.Repair) ChoiceRenderer(org.apache.wicket.markup.html.form.ChoiceRenderer) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) INITIAL_CAS_PSEUDO_USER(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.INITIAL_CAS_PSEUDO_USER) IModel(org.apache.wicket.model.IModel) ListView(org.apache.wicket.markup.html.list.ListView) UIMAException(org.apache.uima.UIMAException) Label(org.apache.wicket.markup.html.basic.Label) LabelPosition(org.apache.wicket.markup.html.form.AbstractChoice.LabelPosition) PROJECT_TYPE_CORRECTION(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.PROJECT_TYPE_CORRECTION) DocumentService(de.tudarmstadt.ukp.clarin.webanno.api.DocumentService) LogLevel(de.tudarmstadt.ukp.clarin.webanno.support.logging.LogLevel) Logger(org.slf4j.Logger) CURATION_IN_PROGRESS(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState.CURATION_IN_PROGRESS) ListItem(org.apache.wicket.markup.html.list.ListItem) WebAnnoConst(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst) CurationDocumentService(de.tudarmstadt.ukp.clarin.webanno.curation.storage.CurationDocumentService) ProjectSettingsPanelBase(de.tudarmstadt.ukp.clarin.webanno.ui.core.settings.ProjectSettingsPanelBase) CURATION_FINISHED(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState.CURATION_FINISHED) IOException(java.io.IOException) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) LambdaAjaxButton(de.tudarmstadt.ukp.clarin.webanno.support.lambda.LambdaAjaxButton) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Serializable(java.io.Serializable) List(java.util.List) PropertyModel(org.apache.wicket.model.PropertyModel) CasDoctor(de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor) ApplicationContextProvider(de.tudarmstadt.ukp.clarin.webanno.support.ApplicationContextProvider) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ImportExportService(de.tudarmstadt.ukp.clarin.webanno.api.ImportExportService) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) LogMessage(de.tudarmstadt.ukp.clarin.webanno.support.logging.LogMessage) CasDoctor(de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) FileNotFoundException(java.io.FileNotFoundException) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

DocumentService (de.tudarmstadt.ukp.clarin.webanno.api.DocumentService)6 ImportExportService (de.tudarmstadt.ukp.clarin.webanno.api.ImportExportService)6 CORRECTION_USER (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.CORRECTION_USER)6 INITIAL_CAS_PSEUDO_USER (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.INITIAL_CAS_PSEUDO_USER)6 PROJECT_TYPE_CORRECTION (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.PROJECT_TYPE_CORRECTION)6 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)6 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)6 ArrayList (java.util.ArrayList)6 Arrays.asList (java.util.Arrays.asList)6 CasStorageService (de.tudarmstadt.ukp.clarin.webanno.api.CasStorageService)5 List (java.util.List)5 RepositoryProperties (de.tudarmstadt.ukp.clarin.webanno.api.RepositoryProperties)4 ProjectImportRequest (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest)4 ExportedProject (de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject)4 File (java.io.File)4 AnnotationSchemaService (de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService)3 PROJECT_TYPE_ANNOTATION (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.PROJECT_TYPE_ANNOTATION)3 BackupProperties (de.tudarmstadt.ukp.clarin.webanno.api.dao.BackupProperties)3 CasStorageServiceImpl (de.tudarmstadt.ukp.clarin.webanno.api.dao.CasStorageServiceImpl)3 ImportExportServiceImpl (de.tudarmstadt.ukp.clarin.webanno.api.dao.ImportExportServiceImpl)3