Search in sources :

Example 1 with ProjectExporter

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

the class ProjectExportServiceImpl method init.

/* package private */
void init() {
    List<ProjectExporter> exps = new ArrayList<>();
    if (exportersProxy != null) {
        exps.addAll(exportersProxy);
        AnnotationAwareOrderComparator.sort(exps);
        Set<Class<? extends ProjectExporter>> exporterClasses = new HashSet<>();
        for (ProjectExporter init : exps) {
            if (exporterClasses.add(init.getClass())) {
                log.info("Found project exporter: {}", ClassUtils.getAbbreviatedName(init.getClass(), 20));
            } else {
                throw new IllegalStateException("There cannot be more than once instance " + "of each project exporter class! Duplicate instance of class: " + init.getClass());
            }
        }
    }
    exporters = Collections.unmodifiableList(exps);
}
Also used : ArrayList(java.util.ArrayList) ProjectExporter(de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExporter) HashSet(java.util.HashSet)

Example 2 with ProjectExporter

use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExporter 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 3 with ProjectExporter

use of de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExporter 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

ProjectExporter (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExporter)3 HashSet (java.util.HashSet)3 ProjectExportException (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException)2 ExportedProject (de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject)2 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)1 ArrayList (java.util.ArrayList)1 Transactional (org.springframework.transaction.annotation.Transactional)1