Search in sources :

Example 6 with ExportedProject

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;
}
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 7 with ExportedProject

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);
            }
        }
    }
}
Also used : ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with ExportedProject

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;
}
Also used : InputStream(java.io.InputStream) ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) ZipEntry(java.util.zip.ZipEntry)

Example 9 with ExportedProject

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;
}
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)

Example 10 with ExportedProject

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);
    }
}
Also used : ExportedProject(de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Aggregations

ExportedProject (de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject)14 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)8 ZipFile (java.util.zip.ZipFile)8 File (java.io.File)7 ProjectExportException (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException)5 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)5 IOException (java.io.IOException)5 ProjectImportRequest (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectImportRequest)4 ArrayList (java.util.ArrayList)4 Pair (org.apache.commons.lang3.tuple.Pair)4 ProjectExporter (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExporter)3 ProjectExportRequest (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportRequest)2 ProjectExportTaskMonitor (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportTaskMonitor)2 FormatSupport (de.tudarmstadt.ukp.clarin.webanno.api.format.FormatSupport)2 ExportedSourceDocument (de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedSourceDocument)2 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)2 WebAnnoTsv3FormatSupport (de.tudarmstadt.ukp.clarin.webanno.tsv.WebAnnoTsv3FormatSupport)2 FileNotFoundException (java.io.FileNotFoundException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2