@Override @Transactional public JCas readAnnotationCas(SourceDocument aDocument, String aUserName) throws IOException { // If there is no CAS yet for the source document, create one. JCas jcas = casStorageService.readOrCreateCas(aDocument, aUserName, () -> { // Convert the source file into an annotation CAS return createOrReadInitialCas(aDocument); }); // We intentionally do not upgrade the CAS here because in general the IDs // must remain stable. If an upgrade is required the caller should do it return jcas; }
@Override @Transactional public JCas readAnnotationCas(SourceDocument aDocument, String aUserName) throws IOException { // If there is no CAS yet for the source document, create one. JCas jcas = casStorageService.readOrCreateCas(aDocument, aUserName, () -> { // Convert the source file into an annotation CAS return createOrReadInitialCas(aDocument); }); // We intentionally do not upgrade the CAS here because in general the IDs // must remain stable. If an upgrade is required the caller should do it return jcas; }
@Override public JCas createOrReadInitialCas(SourceDocument aDocument) throws IOException { Validate.notNull(aDocument, "Source document must be specified"); log.info("Loading initial CAS for source document " + "[{}]({}) in project [{}]({})", aDocument.getName(), aDocument.getId(), aDocument.getProject().getName(), aDocument.getProject().getId()); return casStorageService.readOrCreateCas(aDocument, INITIAL_CAS_PSEUDO_USER, () -> { // Normally, the initial CAS should be created on document import, but after // adding this feature, the existing projects do not yet have initial CASes, so // we create them here lazily try { return importExportService.importCasFromFile( getSourceDocumentFile(aDocument), aDocument.getProject(), aDocument.getFormat()); } catch (UIMAException e) { throw new IOException("Unable to create CAS: " + e.getMessage(), e); } }); }
@Override public JCas createOrReadInitialCas(SourceDocument aDocument) throws IOException { Validate.notNull(aDocument, "Source document must be specified"); log.info("Loading initial CAS for source document " + "[{}]({}) in project [{}]({})", aDocument.getName(), aDocument.getId(), aDocument.getProject().getName(), aDocument.getProject().getId()); return casStorageService.readOrCreateCas(aDocument, INITIAL_CAS_PSEUDO_USER, () -> { // Normally, the initial CAS should be created on document import, but after // adding this feature, the existing projects do not yet have initial CASes, so // we create them here lazily try { return importExportService.importCasFromFile( getSourceDocumentFile(aDocument), aDocument.getProject(), aDocument.getFormat()); } catch (UIMAException e) { throw new IOException("Unable to create CAS: " + e.getMessage(), e); } }); }
@Test public void testReadOrCreateCas() throws Exception { SourceDocument doc = makeSourceDocument(2l, 2l); String user = "test"; JCas cas = sut.readOrCreateCas(doc, user, () -> { try { return JCasFactory.createText("This is a test"); } catch (UIMAException e) { throw new IOException(e); } }); assertThat(sut.getCasFile(doc, user)).exists(); assertThat(sut.existsCas(doc, user)).isTrue(); JCas cas2 = sut.readCas(doc, user); assertThat(cas2.getDocumentText()).isEqualTo(cas.getDocumentText()); sut.deleteCas(doc, user); assertThat(sut.getCasFile(doc, user)).doesNotExist(); assertThat(sut.existsCas(doc, user)).isFalse(); }