Search in sources :

Example 1 with ProjectExportException

use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException in project webanno by webanno.

the class CuratedDocumentsExporter method exportData.

/**
 * Copy, if exists, curation documents to a folder that will be exported as Zip file
 *
 * @param aStage
 *            The folder where curated documents are copied to be exported as Zip File
 */
@Override
public void exportData(ProjectExportRequest aRequest, ProjectExportTaskMonitor aMonitor, ExportedProject aExProject, File aStage) throws Exception {
    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<>();
    // Get all the source documents from the project
    List<SourceDocument> documents = documentService.listSourceDocuments(project);
    int initProgress = aMonitor.getProgress() - 1;
    int i = 1;
    for (SourceDocument sourceDocument : documents) {
        File curationCasDir = new File(aStage, CURATION_CAS_FOLDER + sourceDocument.getName());
        forceMkdir(curationCasDir);
        File curationDir = new File(aStage, CURATION_FOLDER + sourceDocument.getName());
        forceMkdir(curationDir);
        // finished or also the ones that are in progress
        if ((aRequest.isIncludeInProgress() && CURATION_IN_PROGRESS.equals(sourceDocument.getState())) || CURATION_FINISHED.equals(sourceDocument.getState())) {
            File curationCasFile = documentService.getCasFile(sourceDocument, CURATION_USER);
            if (curationCasFile.exists()) {
                // Copy CAS - this is used when importing the project again
                copyFileToDirectory(curationCasFile, curationCasDir);
                // Determine which format to use for export
                String formatId = FORMAT_AUTO.equals(aRequest.getFormat()) ? sourceDocument.getFormat() : aRequest.getFormat();
                FormatSupport format = importExportService.getWritableFormatById(formatId).orElseGet(() -> {
                    FormatSupport fallbackFormat = new WebAnnoTsv3FormatSupport();
                    aMonitor.addMessage(LogMessage.warn(this, "Curation: [%s] No writer" + " found for original format [%s] - exporting as [%s] " + "instead.", sourceDocument.getName(), formatId, fallbackFormat.getName()));
                    return fallbackFormat;
                });
                // Copy secondary export format for convenience - not used during import
                try {
                    File curationFile = importExportService.exportAnnotationDocument(sourceDocument, CURATION_USER, format, CURATION_USER, CURATION, true, bulkOperationContext);
                    copyFileToDirectory(curationFile, curationDir);
                    forceDelete(curationFile);
                } catch (Exception e) {
                    // ExceptionUtils.getRootCauseMessage(e) );
                    throw new ProjectExportException("Aborting due to unrecoverable error while exporting!");
                }
            }
        }
        aMonitor.setProgress(initProgress + (int) ceil(((double) i) / documents.size() * 10.0));
        i++;
    }
}
Also used : FormatSupport(de.tudarmstadt.ukp.clarin.webanno.api.format.FormatSupport) WebAnnoTsv3FormatSupport(de.tudarmstadt.ukp.clarin.webanno.tsv.WebAnnoTsv3FormatSupport) HashMap(java.util.HashMap) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) WebAnnoTsv3FormatSupport(de.tudarmstadt.ukp.clarin.webanno.tsv.WebAnnoTsv3FormatSupport) ProjectExportException(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException) IOException(java.io.IOException) ProjectExportException(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) ZipFile(java.util.zip.ZipFile) File(java.io.File) Pair(org.apache.commons.lang3.tuple.Pair)

Example 2 with ProjectExportException

use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException in project webanno by webanno.

the class ProjectExportCuratedDocumentsTask method export.

@Override
public File export(ProjectExportRequest aRequest, ProjectExportTaskMonitor aMonitor) throws ProjectExportException {
    Project project = aRequest.getProject();
    File exportFile = null;
    File exportTempDir = null;
    try {
        exportTempDir = File.createTempFile("webanno", "export");
        exportTempDir.delete();
        exportTempDir.mkdirs();
        boolean curationDocumentExist = documentService.existsCurationDocument(project);
        if (!curationDocumentExist) {
            throw new ProjectExportException("No curation document created yet for this document");
        }
        ProjectExportRequest request = aRequest;
        request.setProject(project);
        exportCuratedDocuments(request, exportTempDir, false, aMonitor);
        exportFile = new File(exportTempDir.getAbsolutePath() + "_curated_documents.zip");
        ZipUtils.zipFolder(exportTempDir, exportFile);
    } catch (Exception e) {
        if (exportFile != null) {
            try {
                FileUtils.forceDelete(exportTempDir);
            } catch (IOException ex) {
                aMonitor.addMessage(LogMessage.error(this, "Unable to export file after export failed: %s", ex.getMessage()));
            }
        }
        throw new ProjectExportException(e);
    } finally {
        if (exportTempDir != null) {
            try {
                FileUtils.forceDelete(exportTempDir);
            } catch (IOException e) {
                aMonitor.addMessage(LogMessage.error(this, "Unable to delete temp file: %s", e.getMessage()));
            }
        }
    }
    return exportFile;
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) ProjectExportRequest(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportRequest) IOException(java.io.IOException) File(java.io.File) ProjectExportException(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException) IOException(java.io.IOException) ProjectExportException(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException)

Example 3 with ProjectExportException

use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException in project webanno by webanno.

the class ProjectExportCuratedDocumentsTask method exportCuratedDocuments.

/**
 * Copy, if exists, curation documents to a folder that will be exported as Zip file
 *
 * @param aCopyDir
 *            The folder where curated documents are copied to be exported as Zip File
 */
private void exportCuratedDocuments(ProjectExportRequest aModel, File aCopyDir, boolean aIncludeInProgress, ProjectExportTaskMonitor aMonitor) throws ProjectExportException, IOException {
    Project project = aModel.getProject();
    // Get all the source documents from the project
    List<de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument> documents = documentService.listSourceDocuments(project);
    // Determine which format to use for export.
    FormatSupport format;
    if (FORMAT_AUTO.equals(aModel.getFormat())) {
        format = new WebAnnoTsv3FormatSupport();
    } else {
        format = importExportService.getWritableFormatById(aModel.getFormat()).orElseGet(() -> {
            // aModel.getFormat());
            return new WebAnnoTsv3FormatSupport();
        });
    }
    int initProgress = aMonitor.getProgress() - 1;
    int i = 1;
    for (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument sourceDocument : documents) {
        File curationCasDir = new File(aCopyDir + CURATION_AS_SERIALISED_CAS + sourceDocument.getName());
        FileUtils.forceMkdir(curationCasDir);
        File curationDir = new File(aCopyDir + CURATION_FOLDER + sourceDocument.getName());
        FileUtils.forceMkdir(curationDir);
        // finished or also the ones that are in progress
        if ((aIncludeInProgress && SourceDocumentState.CURATION_IN_PROGRESS.equals(sourceDocument.getState())) || SourceDocumentState.CURATION_FINISHED.equals(sourceDocument.getState())) {
            File curationCasFile = documentService.getCasFile(sourceDocument, WebAnnoConst.CURATION_USER);
            if (curationCasFile.exists()) {
                // Copy CAS - this is used when importing the project again
                FileUtils.copyFileToDirectory(curationCasFile, curationCasDir);
                // Copy secondary export format for convenience - not used during import
                try {
                    File curationFile = importExportService.exportAnnotationDocument(sourceDocument, WebAnnoConst.CURATION_USER, format, WebAnnoConst.CURATION_USER, Mode.CURATION);
                    FileUtils.copyFileToDirectory(curationFile, curationDir);
                    FileUtils.forceDelete(curationFile);
                } catch (Exception e) {
                    // ExceptionUtils.getRootCauseMessage(e) );
                    throw new ProjectExportException("Aborting due to unrecoverable error while exporting!");
                }
            }
        }
        aMonitor.setProgress(initProgress + (int) Math.ceil(((double) i) / documents.size() * 10.0));
        i++;
    }
}
Also used : FormatSupport(de.tudarmstadt.ukp.clarin.webanno.api.format.FormatSupport) WebAnnoTsv3FormatSupport(de.tudarmstadt.ukp.clarin.webanno.tsv.WebAnnoTsv3FormatSupport) Mode(de.tudarmstadt.ukp.clarin.webanno.model.Mode) WebAnnoTsv3FormatSupport(de.tudarmstadt.ukp.clarin.webanno.tsv.WebAnnoTsv3FormatSupport) IOException(java.io.IOException) ProjectExportException(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException) ProjectExportException(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) File(java.io.File)

Example 4 with ProjectExportException

use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException in project webanno by webanno.

the class ProjectExportServiceImpl method exportProject.

private ExportedProject exportProject(ProjectExportRequest aRequest, ProjectExportTaskMonitor aMonitor, File aStage) throws ProjectExportException, IOException {
    Deque<ProjectExporter> deque = new LinkedList<>(exporters);
    Set<Class<? extends ProjectExporter>> initsSeen = new HashSet<>();
    Set<ProjectExporter> initsDeferred = SetUtils.newIdentityHashSet();
    ExportedProject exProject = new ExportedProject();
    exProject.setName(aRequest.getProject().getName());
    try {
        while (!deque.isEmpty()) {
            ProjectExporter initializer = deque.pop();
            if (initsDeferred.contains(initializer)) {
                throw new IllegalStateException("Circular initializer dependencies in " + initsDeferred + " via " + initializer);
            }
            if (initsSeen.containsAll(initializer.getExportDependencies())) {
                log.debug("Applying project exporter: {}", initializer);
                initializer.exportData(aRequest, aMonitor, exProject, aStage);
                initsSeen.add(initializer.getClass());
                initsDeferred.clear();
            } else {
                log.debug("Deferring project exporter as dependencies are not yet fulfilled: [{}]", initializer);
                deque.add(initializer);
                initsDeferred.add(initializer);
            }
        }
    } catch (IOException e) {
        // as-is. This allows us to handle export cancellation in the project export UI panel
        throw e;
    } catch (Exception e) {
        throw new ProjectExportException("Project export failed", e);
    }
    return exProject;
}
Also used : ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) IOException(java.io.IOException) ProjectExporter(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExporter) LinkedList(java.util.LinkedList) ProjectExportException(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException) IOException(java.io.IOException) ProjectExportException(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException) HashSet(java.util.HashSet)

Example 5 with ProjectExportException

use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException in project webanno by webanno.

the class ProjectExportServiceImpl method importProject.

@Override
@Transactional
public Project importProject(ProjectImportRequest aRequest, ZipFile aZip) throws ProjectExportException {
    long start = currentTimeMillis();
    Deque<ProjectExporter> deque = new LinkedList<>(exporters);
    Set<Class<? extends ProjectExporter>> initsSeen = new HashSet<>();
    Set<ProjectExporter> initsDeferred = SetUtils.newIdentityHashSet();
    Project project = new Project();
    try {
        ExportedProject exProject = loadExportedProject(aZip);
        // If the name of the project is already taken, generate a new name
        String projectName = exProject.getName();
        if (projectService.existsProject(projectName)) {
            projectName = copyProjectName(projectName);
        }
        project.setName(projectName);
        // We need to set the mode here already because the mode is a non-null column.
        // In older versions of WebAnno, the mode was an enum which was serialized as upper-case
        // during export but as lower-case in the database. This is compensating for this case.
        project.setMode(StringUtils.lowerCase(exProject.getMode(), Locale.US));
        // Initial saving of the project
        projectService.createProject(project);
        // Apply the importers
        while (!deque.isEmpty()) {
            ProjectExporter importer = deque.pop();
            if (initsDeferred.contains(importer)) {
                throw new IllegalStateException("Circular initializer dependencies in " + initsDeferred + " via " + importer);
            }
            if (initsSeen.containsAll(importer.getImportDependencies())) {
                log.debug("Applying project importer: {}", importer);
                importer.importData(aRequest, project, exProject, aZip);
                initsSeen.add(importer.getClass());
                initsDeferred.clear();
            } else {
                log.debug("Deferring project exporter as dependencies are not yet fulfilled: [{}]", importer);
                deque.add(importer);
                initsDeferred.add(importer);
            }
        }
    } catch (Exception e) {
        throw new ProjectExportException("Project import failed", e);
    }
    log.info("Imported project [{}]({}) ({})", project.getName(), project.getId(), formatDurationWords(currentTimeMillis() - start, true, true));
    return project;
}
Also used : LinkedList(java.util.LinkedList) ProjectExportException(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException) IOException(java.io.IOException) ProjectExportException(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException) ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) ProjectExporter(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExporter) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ProjectExportException (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException)7 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)6 ExportedProject (de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject)5 File (java.io.File)5 IOException (java.io.IOException)5 ZipFile (java.util.zip.ZipFile)3 ProjectExporter (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExporter)2 FormatSupport (de.tudarmstadt.ukp.clarin.webanno.api.format.FormatSupport)2 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)2 WebAnnoTsv3FormatSupport (de.tudarmstadt.ukp.clarin.webanno.tsv.WebAnnoTsv3FormatSupport)2 FileNotFoundException (java.io.FileNotFoundException)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 ProjectExportRequest (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportRequest)1 ExportedTrainingDocument (de.tudarmstadt.ukp.clarin.webanno.automation.service.export.model.ExportedTrainingDocument)1 ExportedSourceDocument (de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedSourceDocument)1 Mode (de.tudarmstadt.ukp.clarin.webanno.model.Mode)1 TrainingDocument (de.tudarmstadt.ukp.clarin.webanno.model.TrainingDocument)1 HashMap (java.util.HashMap)1 Pair (org.apache.commons.lang3.tuple.Pair)1