Search in sources :

Example 1 with AfterDocumentCreatedEvent

use of de.tudarmstadt.ukp.clarin.webanno.api.event.AfterDocumentCreatedEvent in project webanno by webanno.

the class DocumentServiceImpl method uploadSourceDocument.

@Override
@Transactional
public void uploadSourceDocument(InputStream aIs, SourceDocument aDocument) throws IOException {
    // Create the metadata record - this also assigns the ID to the document
    createSourceDocument(aDocument);
    // Import the actual content
    File targetFile = getSourceDocumentFile(aDocument);
    JCas jcas;
    try {
        FileUtils.forceMkdir(targetFile.getParentFile());
        try (OutputStream os = new FileOutputStream(targetFile)) {
            copyLarge(aIs, os);
        }
        // Check if the file has a valid format / can be converted without error
        // This requires that the document ID has already been assigned
        jcas = createInitialCas(aDocument);
    } catch (IOException e) {
        FileUtils.forceDelete(targetFile);
        removeSourceDocument(aDocument);
        throw e;
    } catch (Exception e) {
        FileUtils.forceDelete(targetFile);
        removeSourceDocument(aDocument);
        throw new IOException(e.getMessage(), e);
    }
    applicationEventPublisher.publishEvent(new AfterDocumentCreatedEvent(this, aDocument, jcas));
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID, String.valueOf(aDocument.getProject().getId()))) {
        Project project = aDocument.getProject();
        log.info("Imported source document [{}]({}) to project [{}]({})", aDocument.getName(), aDocument.getId(), project.getName(), project.getId());
    }
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) AfterDocumentCreatedEvent(de.tudarmstadt.ukp.clarin.webanno.api.event.AfterDocumentCreatedEvent) JCas(org.apache.uima.jcas.JCas) IOException(java.io.IOException) File(java.io.File) MDC(org.slf4j.MDC) NoResultException(javax.persistence.NoResultException) CASException(org.apache.uima.cas.CASException) UIMAException(org.apache.uima.UIMAException) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) IOException(java.io.IOException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AfterDocumentCreatedEvent (de.tudarmstadt.ukp.clarin.webanno.api.event.AfterDocumentCreatedEvent)1 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 NoResultException (javax.persistence.NoResultException)1 UIMAException (org.apache.uima.UIMAException)1 CASException (org.apache.uima.cas.CASException)1 JCas (org.apache.uima.jcas.JCas)1 ResourceInitializationException (org.apache.uima.resource.ResourceInitializationException)1 MDC (org.slf4j.MDC)1 Transactional (org.springframework.transaction.annotation.Transactional)1