use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportRequest 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++;
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportRequest in project webanno by webanno.
the class LayerExporterTest method runExportImportAndFetchLayers.
private ArgumentCaptor<AnnotationLayer> runExportImportAndFetchLayers() throws Exception {
// Export the project
ProjectExportRequest exportRequest = new ProjectExportRequest();
ProjectExportTaskMonitor monitor = new ProjectExportTaskMonitor();
exportRequest.setProject(project);
ExportedProject exportedProject = new ExportedProject();
sut.exportData(exportRequest, monitor, exportedProject, workFolder);
// Import the project again
ArgumentCaptor<AnnotationLayer> captor = ArgumentCaptor.forClass(AnnotationLayer.class);
doNothing().when(annotationService).createOrUpdateLayer(captor.capture());
ProjectImportRequest importRequest = new ProjectImportRequest(true);
ZipFile zipFile = mock(ZipFile.class);
sut.importData(importRequest, project, exportedProject, zipFile);
return captor;
}
use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportRequest in project webanno by webanno.
the class AeroRemoteApiController method projectExport.
@ApiOperation(value = "Export a project to a ZIP file")
@RequestMapping(value = ("/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + EXPORT), method = RequestMethod.GET, produces = { "application/zip", APPLICATION_JSON_UTF8_VALUE })
public ResponseEntity<InputStreamResource> projectExport(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @RequestParam(value = PARAM_FORMAT) Optional<String> aFormat) throws Exception {
// Get project (this also ensures that it exists and that the current user can access it
Project project = getProject(aProjectId);
// Check if the format is supported
if (aFormat.isPresent()) {
importExportService.getWritableFormatById(aFormat.get()).orElseThrow(() -> new UnsupportedFormatException("Format [%s] cannot be exported. Exportable formats are %s.", aFormat.get(), importExportService.getWritableFormats().stream().map(FormatSupport::getId).sorted().collect(Collectors.toList()).toString()));
}
ProjectExportRequest request = new ProjectExportRequest(project, aFormat.orElse(WebAnnoTsv3FormatSupport.ID), true);
ProjectExportTaskMonitor monitor = new ProjectExportTaskMonitor();
File exportedFile = exportService.exportProject(request, monitor);
// Turn the file into a resource and auto-delete the file when the resource closes the
// stream.
InputStreamResource result = new InputStreamResource(new FileInputStream(exportedFile) {
@Override
public void close() throws IOException {
super.close();
FileUtils.forceDelete(exportedFile);
}
});
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.valueOf("application/zip"));
httpHeaders.setContentLength(exportedFile.length());
httpHeaders.set("Content-Disposition", "attachment; filename=\"" + exportedFile.getName() + "\"");
return new ResponseEntity<>(result, httpHeaders, HttpStatus.OK);
}
use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportRequest 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;
}
Aggregations