Search in sources :

Example 1 with FilesIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FilesIOSupplier in project hale by halestudio.

the class IOWizard method performFinish.

/**
 * @see Wizard#performFinish()
 *
 * @return <code>true</code> if executing the I/O provider was successful
 */
@Override
public boolean performFinish() {
    if (getProvider() == null) {
        return false;
    }
    if (!applyConfiguration()) {
        return false;
    }
    if (initializeUsedLocations() == null && provider instanceof ImportProvider) {
        // could not find any URIs in the provider.
        return false;
    }
    URI uriLoc = null;
    // To avoid NPE in the case of export provider.
    if (uriLocations != null) {
        uriLoc = uriLocations.poll();
    }
    // either have to process all files or export provider
    while (uriLoc != null || !(provider instanceof ImportProvider)) {
        // FileIOSupplier
        if (provider instanceof ImportProvider) {
            ImportProvider importProvider = (ImportProvider) provider;
            LocatableInputSupplier<? extends InputStream> source = importProvider.getSource();
            // supplier, the import will likely fail
            if (source instanceof GZipInputSupplier) {
                source = ((GZipInputSupplier) source).getSource();
            }
            if (source instanceof FilesIOSupplier || source instanceof FileIOSupplier) {
                // always set FileIOSupplier to avoid huge impacts.
                importProvider.setSource(new FileIOSupplier(new File(uriLoc)));
            }
        }
        // else it is a non-file source or export provider and proceed with
        // the code without updating the provider.
        IOReporter defReport = provider.createReporter();
        // validate and execute provider
        try {
            // validate configuration
            provider.validate();
            ProjectService ps = PlatformUI.getWorkbench().getService(ProjectService.class);
            URI projectLoc = ps.getLoadLocation() == null ? null : ps.getLoadLocation();
            boolean isProjectResource = false;
            if (actionId != null) {
                // XXX instead move project resource to action?
                ActionUI factory = ActionUIExtension.getInstance().findActionUI(actionId);
                isProjectResource = factory.isProjectResource();
            }
            // prevent loading of duplicate resources
            if (isProjectResource && provider instanceof ImportProvider && !getProviderFactory().allowDuplicateResource()) {
                String currentResource = ((ImportProvider) provider).getSource().getLocation().toString();
                URI currentAbsolute = URI.create(currentResource);
                if (projectLoc != null && !currentAbsolute.isAbsolute()) {
                    currentAbsolute = projectLoc.resolve(currentAbsolute);
                }
                for (IOConfiguration conf : ((Project) ps.getProjectInfo()).getResources()) {
                    Value otherResourceValue = conf.getProviderConfiguration().get(ImportProvider.PARAM_SOURCE);
                    if (otherResourceValue == null) {
                        continue;
                    }
                    String otherResource = otherResourceValue.as(String.class);
                    URI otherAbsolute = URI.create(otherResource);
                    if (projectLoc != null && !otherAbsolute.isAbsolute()) {
                        otherAbsolute = projectLoc.resolve(otherAbsolute);
                    }
                    String action = conf.getActionId();
                    // resource is already loaded into the project
                    if (currentAbsolute.equals(otherAbsolute) && Objects.equal(actionId, action)) {
                        // check if the resource is loaded with a provider
                        // that allows duplicates
                        boolean allowDuplicate = false;
                        IOProviderDescriptor providerFactory = IOProviderExtension.getInstance().getFactory(conf.getProviderId());
                        if (providerFactory != null) {
                            allowDuplicate = providerFactory.allowDuplicateResource();
                        }
                        if (!allowDuplicate) {
                            log.userError("Resource is already loaded. Loading duplicate resources is aborted!");
                            // duplicate resource will be uploaded.
                            return false;
                        }
                    }
                }
            }
            // enable provider internal caching
            if (isProjectResource && provider instanceof CachingImportProvider) {
                ((CachingImportProvider) provider).setProvideCache();
            }
            IOReport report = execute(provider, defReport);
            if (report != null) {
                // add report to report server
                ReportService repService = PlatformUI.getWorkbench().getService(ReportService.class);
                repService.addReport(report);
                // show message to user
                if (report.isSuccess()) {
                    // let advisor handle results
                    try {
                        getContainer().run(true, false, new IRunnableWithProgress() {

                            @Override
                            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                                monitor.beginTask("Completing operation...", IProgressMonitor.UNKNOWN);
                                try {
                                    advisor.handleResults(provider);
                                } finally {
                                    monitor.done();
                                }
                            }
                        });
                    } catch (InvocationTargetException e) {
                        log.userError("Error processing results:\n" + e.getCause().getLocalizedMessage(), e.getCause());
                        return false;
                    } catch (Exception e) {
                        log.userError("Error processing results:\n" + e.getLocalizedMessage(), e);
                        return false;
                    }
                    // add to project service if necessary
                    if (isProjectResource)
                        ps.rememberIO(actionId, getProviderFactory().getIdentifier(), provider);
                } else {
                    // error message
                    log.userError(report.getSummary() + "\nPlease see the report for details.");
                    return false;
                }
            }
        } catch (IOProviderConfigurationException e) {
            // user feedback
            log.userError("Validation of the provider configuration failed:\n" + e.getLocalizedMessage(), e);
            return false;
        }
        if (uriLocations != null) {
            uriLoc = uriLocations.poll();
        } else {
            break;
        }
    }
    return true;
}
Also used : GZipInputSupplier(eu.esdihumboldt.hale.common.core.io.impl.GZipEnabledImport.GZipInputSupplier) CachingImportProvider(eu.esdihumboldt.hale.common.core.io.CachingImportProvider) ImportProvider(eu.esdihumboldt.hale.common.core.io.ImportProvider) ActionUI(eu.esdihumboldt.hale.ui.io.action.ActionUI) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) URI(java.net.URI) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) ProjectService(eu.esdihumboldt.hale.ui.service.project.ProjectService) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) ReportService(eu.esdihumboldt.hale.ui.service.report.ReportService) CachingImportProvider(eu.esdihumboldt.hale.common.core.io.CachingImportProvider) Value(eu.esdihumboldt.hale.common.core.io.Value) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) FilesIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FilesIOSupplier) File(java.io.File)

Example 2 with FilesIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FilesIOSupplier in project hale by halestudio.

the class IOWizard method initializeUsedLocations.

/**
 * Method to initialize used locations. It initializes usedLocations as a
 * list in case multiple files were selected otherwise as a single resource
 * in case of non-file source like HTTP/HTTPS or JDBC URIs.
 *
 * @return the usedLocations
 */
private Queue<URI> initializeUsedLocations() {
    List<URI> uris = new ArrayList<>();
    if (provider instanceof ImportProvider) {
        LocatableInputSupplier<? extends InputStream> source = ((ImportProvider) getProvider()).getSource();
        // the first selected file
        if (source instanceof GZipInputSupplier) {
            source = ((GZipInputSupplier) source).getSource();
        }
        if (source instanceof FilesIOSupplier) {
            uris = ((FilesIOSupplier) source).getLocations();
        } else {
            // non-file locations like HTTP/HTTPS or JDBC URIs or when a
            // single file is selected.
            URI location = ((ImportProvider) getProvider()).getSource().getLocation();
            uris = Arrays.asList(location);
        }
        // in case of any error dialog is shown during the import and the
        // user again clicks on the finish then this queue won't be
        // initialized and the dialog will close. To prevent this, the
        // queue is initialized every time this function is called.
        uriLocations = new LinkedList<>(uris);
    }
    return uriLocations;
}
Also used : ArrayList(java.util.ArrayList) GZipInputSupplier(eu.esdihumboldt.hale.common.core.io.impl.GZipEnabledImport.GZipInputSupplier) CachingImportProvider(eu.esdihumboldt.hale.common.core.io.CachingImportProvider) ImportProvider(eu.esdihumboldt.hale.common.core.io.ImportProvider) FilesIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FilesIOSupplier) URI(java.net.URI)

Example 3 with FilesIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FilesIOSupplier in project hale by halestudio.

the class AbstractMultipleFilesSource method getSource.

/**
 * @see eu.esdihumboldt.hale.ui.io.source.AbstractProviderSource#getSource()
 */
@Override
protected LocatableInputSupplier<? extends InputStream> getSource() {
    List<String> stringValues = sourceFile.getStringValues();
    List<File> files = new ArrayList<File>();
    List<URI> uris = new ArrayList<>();
    File file = new File(stringValues.get(0));
    if (file.isAbsolute()) {
        for (String s : stringValues) {
            files.add(new File(s));
            uris.add(new File(s).toURI());
        }
    } else {
        for (String path : stringValues) {
            URI relativeURI = IOUtils.relativeFileToURI(new File(path));
            File absoluteFile = new File(projectLocation.resolve(relativeURI));
            files.add(absoluteFile);
            uris.add(relativeURI);
        }
    }
    FilesIOSupplier filesIOSupplier = new FilesIOSupplier(files, uris);
    return filesIOSupplier;
}
Also used : ArrayList(java.util.ArrayList) FilesIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FilesIOSupplier) File(java.io.File) URI(java.net.URI)

Aggregations

FilesIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FilesIOSupplier)3 URI (java.net.URI)3 CachingImportProvider (eu.esdihumboldt.hale.common.core.io.CachingImportProvider)2 ImportProvider (eu.esdihumboldt.hale.common.core.io.ImportProvider)2 GZipInputSupplier (eu.esdihumboldt.hale.common.core.io.impl.GZipEnabledImport.GZipInputSupplier)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 Value (eu.esdihumboldt.hale.common.core.io.Value)1 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)1 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)1 Project (eu.esdihumboldt.hale.common.core.io.project.model.Project)1 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)1 IOReporter (eu.esdihumboldt.hale.common.core.io.report.IOReporter)1 FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)1 ActionUI (eu.esdihumboldt.hale.ui.io.action.ActionUI)1 ProjectService (eu.esdihumboldt.hale.ui.service.project.ProjectService)1 ReportService (eu.esdihumboldt.hale.ui.service.report.ReportService)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1