use of de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject 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.export.model.ExportedProject in project webanno by webanno.
the class ProjectExportServiceImpl method exportProject.
@Override
@Transactional
public File exportProject(ProjectExportRequest aRequest, ProjectExportTaskMonitor aMonitor) throws ProjectExportException, IOException {
boolean success = false;
File exportTempDir = null;
try {
// Directory to store source documents and annotation documents
exportTempDir = File.createTempFile("webanno-project", "export");
exportTempDir.delete();
exportTempDir.mkdirs();
// Target file
File projectZipFile = new File(exportTempDir.getAbsolutePath() + ".zip");
ExportedProject exProjekt = exportProject(aRequest, aMonitor, exportTempDir);
// all metadata and project settings data from the database as JSON file
File projectSettings = File.createTempFile(EXPORTED_PROJECT, ".json");
JSONUtil.generatePrettyJson(exProjekt, projectSettings);
FileUtils.copyFileToDirectory(projectSettings, exportTempDir);
try {
ZipUtils.zipFolder(exportTempDir, projectZipFile);
} finally {
FileUtils.forceDelete(projectSettings);
System.gc();
FileUtils.forceDelete(exportTempDir);
}
success = true;
return projectZipFile;
} finally {
if (!success && exportTempDir != null) {
try {
FileUtils.forceDelete(exportTempDir);
} catch (IOException e) {
aMonitor.addMessage(LogMessage.error(this, "Unable to delete temporary export directory [%s]", exportTempDir));
log.error("Unable to delete temporary export directory [{}]", exportTempDir);
}
}
}
}
use of de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject in project webanno by webanno.
the class ProjectExportServiceImpl method loadExportedProject.
public static ExportedProject loadExportedProject(ZipFile aZip) throws IOException {
// Locate the project model in the ZIP file
ZipEntry projectSettingsEntry = null;
for (Enumeration<? extends ZipEntry> zipEnumerate = aZip.entries(); zipEnumerate.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) zipEnumerate.nextElement();
if (entry.toString().replace("/", "").startsWith(EXPORTED_PROJECT) && entry.toString().replace("/", "").endsWith(".json")) {
projectSettingsEntry = entry;
break;
}
}
// Load the project model from the JSON file
String text;
try (InputStream is = aZip.getInputStream(projectSettingsEntry)) {
text = IOUtils.toString(is, "UTF-8");
}
ExportedProject exProject = JSONUtil.getObjectMapper().readValue(text, ExportedProject.class);
return exProject;
}
use of de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject 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;
}
use of de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject in project webanno by webanno.
the class ProjectLogExporter method exportData.
@Override
public void exportData(ProjectExportRequest aRequest, ProjectExportTaskMonitor aMonitor, ExportedProject aExProject, File aStage) throws IOException {
Project project = aRequest.getProject();
File logDir = new File(aStage + LOG_FOLDER);
FileUtils.forceMkdir(logDir);
if (projectService.getProjectLogFile(project).exists()) {
FileUtils.copyFileToDirectory(projectService.getProjectLogFile(project), logDir);
}
}
Aggregations