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++;
}
}
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;
}
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++;
}
}
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;
}
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;
}
Aggregations