Search in sources :

Example 11 with CasStorageSession

use of de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession in project webanno by webanno.

the class CasStorageServiceImplTest method testReadOrCreateCas.

@Test
public void testReadOrCreateCas() throws Exception {
    try (CasStorageSession casStorageSession = openNested(true)) {
        // Setup fixture
        SourceDocument doc = makeSourceDocument(3l, 3l, "test");
        String user = "test";
        String text = "This is a test";
        createCasFile(doc, user, text);
        // Actual test
        JCas cas = sut.readCas(doc, user).getJCas();
        assertThat(cas.getDocumentText()).isEqualTo(text);
        sut.deleteCas(doc, user);
        assertThat(sut.getCasFile(doc, user)).doesNotExist();
        assertThat(sut.existsCas(doc, user)).isFalse();
    }
}
Also used : SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) CasStorageSession(de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession) Test(org.junit.Test)

Example 12 with CasStorageSession

use of de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession in project webanno by webanno.

the class CasStorageServiceImplTest method testWriteReadExistsDeleteCas.

@Test
public void testWriteReadExistsDeleteCas() throws Exception {
    try (CasStorageSession casStorageSession = openNested(true)) {
        // Setup fixture
        SourceDocument doc = makeSourceDocument(1l, 1l, "test");
        JCas templateCas = JCasFactory.createText("This is a test");
        casStorageSession.add("cas", EXCLUSIVE_WRITE_ACCESS, templateCas.getCas());
        String user = "test";
        sut.writeCas(doc, templateCas.getCas(), user);
        assertThat(sut.getCasFile(doc, user)).exists();
        assertThat(sut.existsCas(doc, user)).isTrue();
        // Actual test
        CAS cas = sut.readCas(doc, user);
        assertThat(cas.getDocumentText()).isEqualTo(templateCas.getDocumentText());
        sut.deleteCas(doc, user);
        assertThat(sut.getCasFile(doc, user)).doesNotExist();
        assertThat(sut.existsCas(doc, user)).isFalse();
        assertThat(casStorageSession.contains(cas)).isFalse();
    }
}
Also used : CAS(org.apache.uima.cas.CAS) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) CasStorageSession(de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession) Test(org.junit.Test)

Example 13 with CasStorageSession

use of de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession in project webanno by webanno.

the class DocumentServiceImplTest method thatReadingNonExistentAnnotationCasCreatesNewCas.

@Test
public void thatReadingNonExistentAnnotationCasCreatesNewCas() throws Exception {
    try (CasStorageSession session = CasStorageSession.open()) {
        SourceDocument sourceDocument = makeSourceDocument(1l, 1l, "test");
        User user = makeUser();
        JCas cas = sut.readAnnotationCas(sourceDocument, user.getUsername()).getJCas();
        assertThat(cas).isNotNull();
        assertThat(cas.getDocumentText()).isEqualTo("Test");
        assertThat(storageService.getCasFile(sourceDocument, user.getUsername())).exists();
    }
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) CasStorageSession(de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession) Test(org.junit.Test)

Example 14 with CasStorageSession

use of de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession in project webanno by webanno.

the class DocumentServiceImplTest method thatCreatingOrReadingInitialCasForNewDocumentCreatesNewCas.

@Test
public void thatCreatingOrReadingInitialCasForNewDocumentCreatesNewCas() throws Exception {
    try (CasStorageSession session = CasStorageSession.open()) {
        SourceDocument doc = makeSourceDocument(1l, 1l, "test");
        JCas cas = sut.createOrReadInitialCas(doc).getJCas();
        assertThat(cas).isNotNull();
        assertThat(cas.getDocumentText()).isEqualTo("Test");
        assertThat(storageService.getCasFile(doc, INITIAL_CAS_PSEUDO_USER)).exists();
    }
}
Also used : SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) CasStorageSession(de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession) Test(org.junit.Test)

Example 15 with CasStorageSession

use of de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession in project webanno by webanno.

the class ImportExportServiceImpl method exportAnnotationDocument.

@Override
@Transactional
public File exportAnnotationDocument(SourceDocument aDocument, String aUser, FormatSupport aFormat, String aFileName, Mode aMode, boolean aStripExtension, Map<Pair<Project, String>, Object> aBulkOperationContext) throws UIMAException, IOException, ClassNotFoundException {
    Map<Pair<Project, String>, Object> bulkOperationContext = aBulkOperationContext;
    if (bulkOperationContext == null) {
        bulkOperationContext = new HashMap<>();
    }
    String username;
    // (Annotated) or the automated document
    if (aMode.equals(ANNOTATION) || aMode.equals(AUTOMATION) || aMode.equals(CORRECTION)) {
        username = aUser;
    } else // The merge result will be exported
    {
        username = CURATION_USER;
    }
    // Read file
    File exportFile;
    try (CasStorageSession session = CasStorageSession.openNested()) {
        CAS cas = casStorageService.readCas(aDocument, username);
        exportFile = exportCasToFile(cas, aDocument, aFileName, aFormat, aStripExtension, aBulkOperationContext);
    }
    Project project = aDocument.getProject();
    try (MDC.MDCCloseable closable = MDC.putCloseable(KEY_PROJECT_ID, String.valueOf(project.getId()))) {
        log.info("Exported annotations [{}]({}) for user [{}] from project [{}]({}) " + "using format [{}]", aDocument.getName(), aDocument.getId(), aUser, project.getName(), project.getId(), aFormat.getId());
    }
    return exportFile;
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) CAS(org.apache.uima.cas.CAS) CasStorageSession(de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession) File.createTempFile(java.io.File.createTempFile) File(java.io.File) FileUtils.copyFile(org.apache.commons.io.FileUtils.copyFile) MDC(org.slf4j.MDC) Pair(org.apache.commons.lang3.tuple.Pair) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

CasStorageSession (de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession)15 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)10 Test (org.junit.Test)9 CAS (org.apache.uima.cas.CAS)8 IOException (java.io.IOException)7 JCas (org.apache.uima.jcas.JCas)6 File (java.io.File)5 FileNotFoundException (java.io.FileNotFoundException)5 ArrayList (java.util.ArrayList)5 UIMAException (org.apache.uima.UIMAException)5 CasSessionException (de.tudarmstadt.ukp.clarin.webanno.api.casstorage.CasSessionException)4 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)4 CasDoctorException (de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctorException)3 List (java.util.List)3 CasProvider (de.tudarmstadt.ukp.clarin.webanno.api.CasProvider)2 RepositoryProperties (de.tudarmstadt.ukp.clarin.webanno.api.RepositoryProperties)2 INITIAL_CAS_PSEUDO_USER (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.INITIAL_CAS_PSEUDO_USER)2 CasKey (de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasKey)2 SessionManagedCas (de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.SessionManagedCas)2 LayerConfigurationChangedEvent (de.tudarmstadt.ukp.clarin.webanno.api.event.LayerConfigurationChangedEvent)2