Search in sources :

Example 1 with LocationUpdater

use of eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater in project hale by halestudio.

the class TemplateProject method onSuccess.

@Override
protected void onSuccess(Void context, String projectId, File projectFile, Project project, ReportFile reportFile) {
    super.onSuccess(context, projectId, projectFile, project, reportFile);
    // update locations in project file
    LocationUpdater updater = new LocationUpdater(project, projectFile.toURI());
    updater.updateProject(false);
    resources.clear();
    List<Path> invalidSources = new ArrayList<>();
    Path projectFolder = getProjectFolder().toPath();
    // validate resources
    for (IOConfiguration config : project.getResources()) {
        Resource resource = new IOConfigurationResource(config, getProjectFile().toURI());
        // check if file URIs are valid and inside project folder
        URI source = resource.getSource();
        if (source != null) {
            Path path = null;
            if (source.getScheme() == null) {
                // is a relative URI
                path = projectFile.toPath().resolve(source.toString()).normalize();
            } else if ("file".equals(source.getScheme())) {
                // is a file URI
                path = Paths.get(source).normalize();
            }
            if (path != null) {
                if (!path.startsWith(projectFolder) || !Files.exists(path)) {
                    // invalid source
                    invalidSources.add(path);
                }
            }
        }
        resources.put(resource.getActionId(), resource);
    }
    valid = invalidSources.isEmpty();
    if (!valid) {
        StringBuilder builder = new StringBuilder("Files referenced by the project could not be found: ");
        for (int i = 0; i < invalidSources.size(); i++) {
            if (i > 0)
                builder.append(", ");
            Path path = invalidSources.get(i);
            builder.append(path.getFileName().toString());
        }
        notValidMessage = builder.toString();
    } else {
        notValidMessage = "";
    }
    // additionally, try to find out cell count
    definedRelations = 0;
    // check if default alignment file exists
    try {
        File defAlignmentFile = new File(URI.create(projectFile.toURI().toASCIIString() + "." + AlignmentIO.PROJECT_FILE_ALIGNMENT));
        if (defAlignmentFile.exists()) {
            try (InputStream in = new BufferedInputStream(new FileInputStream(defAlignmentFile))) {
                /*
					 * Try loading the file with JAXB - only supports 2.6+
					 * projects.
					 */
                AlignmentType alignment = JaxbToAlignment.load(in, null);
                // XXX ignoring base alignments
                int count = 0;
                for (Object element : alignment.getCellOrModifier()) {
                    if (element instanceof CellType) {
                        count++;
                    }
                }
                definedRelations = count;
            } catch (Exception e) {
            // ignore
            }
        }
    } catch (Exception e) {
    // ignore
    }
}
Also used : Path(java.nio.file.Path) LocationUpdater(eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOConfigurationResource(eu.esdihumboldt.hale.common.core.io.project.model.IOConfigurationResource) Resource(eu.esdihumboldt.hale.common.core.io.project.model.Resource) IOConfigurationResource(eu.esdihumboldt.hale.common.core.io.project.model.IOConfigurationResource) URI(java.net.URI) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) AlignmentType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType) BufferedInputStream(java.io.BufferedInputStream) CellType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType) ReportFile(eu.esdihumboldt.hale.common.headless.report.ReportFile) File(java.io.File)

Example 2 with LocationUpdater

use of eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater in project hale by halestudio.

the class ArchiveProjectReader method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    // copy resources to a temporary directory
    tempDir = Files.createTempDir();
    IOUtils.extract(tempDir, getSource().getInput());
    // create the project file via XMLProjectReader
    File baseFile = new File(tempDir, "project.halex");
    ProjectReader reader;
    if (!baseFile.exists()) {
        // look for project file
        log.debug("Default project file not found, looking for other candidates.");
        String candidate = ProjectIO.findProjectFile(tempDir);
        if (candidate != null) {
            log.info(MessageFormat.format("Loading {0} as project file from archive", candidate));
            baseFile = new File(tempDir, candidate);
        }
        reader = HaleIO.findIOProvider(ProjectReader.class, new FileIOSupplier(baseFile), candidate);
        if (reader == null) {
            reporter.error(new IOMessageImpl("Could not find reader for project file " + candidate, null));
            reporter.setSuccess(false);
            return reporter;
        }
    } else {
        // default project file
        reader = new XMLProjectReader();
    }
    LocatableInputSupplier<InputStream> tempSource = new FileIOSupplier(baseFile);
    // save old save configuration
    LocatableInputSupplier<? extends InputStream> oldSource = getSource();
    setSource(tempSource);
    reader.setSource(tempSource);
    reader.setProjectFiles(getProjectFiles());
    IOReport report = reader.execute(progress);
    Project readProject = reader.getProject();
    if (readProject != null) {
        /*
			 * Because the original source is only available here, update the
			 * project's resource paths here.
			 * 
			 * The only drawback is that the UILocationUpdater cannot be used.
			 */
        LocationUpdater updater = new LocationUpdater(readProject, tempSource.getLocation());
        // update resources
        // resources are made absolute (else they can't be found afterwards)
        updater.updateProject(false);
    }
    // set the real source
    setSource(oldSource);
    // set the read project
    setProjectChecked(readProject, reporter);
    return report;
}
Also used : Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) LocationUpdater(eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater) InputStream(java.io.InputStream) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File) ProjectReader(eu.esdihumboldt.hale.common.core.io.project.ProjectReader)

Example 3 with LocationUpdater

use of eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater in project hale by halestudio.

the class ArchiveProjectWriter method createProjectArchive.

/**
 * Creates the project archive.
 *
 * @param target {@link OutputStream} to write the archive to
 * @param reporter the reporter to use for the execution report
 * @param progress the progress indicator
 * @return the execution report
 * @throws IOException if an I/O operation fails
 * @throws IOProviderConfigurationException if the I/O provider was not
 *             configured properly
 */
public IOReport createProjectArchive(OutputStream target, IOReporter reporter, ProgressIndicator progress) throws IOException, IOProviderConfigurationException {
    ZipOutputStream zip = new ZipOutputStream(target);
    // all files related to the project are copied into a temporary
    // directory first and then packed into a zip file
    // create temporary directory and project file
    File tempDir = Files.createTempDir();
    File baseFile = new File(tempDir, "project.halex");
    // mark the temporary directory for clean-up if the project is closed
    CleanupService clean = HalePlatform.getService(CleanupService.class);
    if (clean != null) {
        clean.addTemporaryFiles(CleanupContext.PROJECT, tempDir);
    }
    LocatableOutputSupplier<OutputStream> out = new FileIOSupplier(baseFile);
    // false is correct if getParameter is null because false is default
    boolean includeWebresources = getParameter(INCLUDE_WEB_RESOURCES).as(Boolean.class, false);
    SubtaskProgressIndicator subtask = new SubtaskProgressIndicator(progress);
    // save old IO configurations
    List<IOConfiguration> oldResources = new ArrayList<IOConfiguration>();
    for (int i = 0; i < getProject().getResources().size(); i++) {
        // clone all IO configurations to work on different objects
        oldResources.add(getProject().getResources().get(i).clone());
    }
    IOConfiguration config = getProject().getSaveConfiguration();
    if (config == null) {
        config = new IOConfiguration();
    }
    IOConfiguration oldSaveConfig = config.clone();
    // copy resources to the temp directory and update xml schemas
    updateResources(tempDir, includeWebresources, subtask, reporter);
    // update target save configuration of the project
    config.getProviderConfiguration().put(PARAM_TARGET, Value.of(baseFile.toURI().toString()));
    // write project file via XMLProjectWriter
    XMLProjectWriter writer = new XMLProjectWriter();
    writer.setTarget(out);
    writer.setProject(getProject());
    writer.setProjectFiles(getProjectFiles());
    IOReport report = writer.execute(progress, reporter);
    // now after the project with its project files is written, look for the
    // alignment file and update it
    ProjectFileInfo newAlignmentInfo = getAlignmentFile(getProject());
    if (newAlignmentInfo != null) {
        URI newAlignment = tempDir.toURI().resolve(newAlignmentInfo.getLocation());
        XMLAlignmentUpdater.update(new File(newAlignment), newAlignment, includeWebresources, reporter);
    }
    // put the complete temp directory into a zip file
    IOUtils.zipDirectory(tempDir, zip);
    zip.close();
    // the files may not be deleted now as they will be needed if the
    // project is saved again w/o loading it first
    // update the relative resource locations
    LocationUpdater updater = new LocationUpdater(getProject(), out.getLocation());
    // resources are made absolute (else they can't be found afterwards),
    // e.g. when saving the project again before loading it
    updater.updateProject(false);
    // reset the save configurations that has been overridden by the XML
    // project writer
    getProject().setSaveConfiguration(oldSaveConfig);
    if (clean == null) {
        // if no clean service is available, assume the directory is not
        // needed anymore
        FileUtils.deleteDirectory(tempDir);
    }
    return report;
}
Also used : LocationUpdater(eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) SubtaskProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.SubtaskProgressIndicator) URI(java.net.URI) CleanupService(eu.esdihumboldt.hale.common.core.service.cleanup.CleanupService) ZipOutputStream(java.util.zip.ZipOutputStream) ProjectFileInfo(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File)

Example 4 with LocationUpdater

use of eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater in project hale by halestudio.

the class HeadlessProjectAdvisor method handleResults.

@Override
public void handleResults(ProjectReader provider) {
    project = provider.getProject();
    projectLocation = provider.getSource().getLocation();
    updater = new LocationUpdater(project, projectLocation);
    // no need to keep relative paths in the headless environment
    updater.updateProject(false);
    // inject project into advisors (mappable types)
    sourceSchemaAdvisor.setProject(project);
    targetSchemaAdvisor.setProject(project);
    // execute loaded I/O configurations
    List<IOConfiguration> confs = new ArrayList<IOConfiguration>(project.getResources());
    // but remove source data actions first
    Iterator<IOConfiguration> it = confs.iterator();
    while (it.hasNext()) {
        if (InstanceIO.ACTION_LOAD_SOURCE_DATA.equals(it.next().getActionId())) {
            it.remove();
        }
    }
    try {
        HeadlessIO.executeConfigurations(confs, advisors, reportHandler, this);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    Map<String, ProjectFile> projectFiles = provider.getProjectFiles();
    // apply remaining project files
    for (ProjectFile file : projectFiles.values()) {
        file.apply();
    }
}
Also used : LocationUpdater(eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ProjectFile(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile) ActionProjectFile(eu.esdihumboldt.hale.common.core.io.project.extension.internal.ActionProjectFile)

Aggregations

LocationUpdater (eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater)4 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)2 FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URI (java.net.URI)2 AlignmentType (eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType)1 CellType (eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType)1 SubtaskProgressIndicator (eu.esdihumboldt.hale.common.core.io.impl.SubtaskProgressIndicator)1 ProjectReader (eu.esdihumboldt.hale.common.core.io.project.ProjectReader)1 ActionProjectFile (eu.esdihumboldt.hale.common.core.io.project.extension.internal.ActionProjectFile)1 IOConfigurationResource (eu.esdihumboldt.hale.common.core.io.project.model.IOConfigurationResource)1 Project (eu.esdihumboldt.hale.common.core.io.project.model.Project)1 ProjectFile (eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile)1 ProjectFileInfo (eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo)1 Resource (eu.esdihumboldt.hale.common.core.io.project.model.Resource)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1