Search in sources :

Example 1 with IOProviderDescriptor

use of eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor in project hale by halestudio.

the class ProjectValidator method execute.

/**
 * @see eu.esdihumboldt.hale.common.core.io.impl.AbstractIOProvider#execute(eu.esdihumboldt.hale.common.core.io.ProgressIndicator,
 *      eu.esdihumboldt.hale.common.core.io.report.IOReporter)
 */
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    ValidatorConfigurationService service = getServiceProvider().getService(ValidatorConfigurationService.class);
    if (service == null) {
        reporter.setSuccess(false);
        throw new RuntimeException("Unable to find validator configurations");
    }
    Collection<IOProviderDescriptor> validators = new ArrayList<>();
    validators.addAll(HaleIO.getProviderFactories(ConfigurableInstanceValidator.class));
    List<ValidatorConfiguration> configurations = service.getConfigurations();
    progress.begin("Performing project validation", configurations.size());
    reporter.setSuccess(true);
    SubtaskProgressIndicator subProgress = new SubtaskProgressIndicator(progress);
    int i = 0;
    for (ValidatorConfiguration configuration : configurations) {
        for (IOProviderDescriptor validatorFactory : HaleIO.filterFactoriesByConfigurationType(validators, configuration.getContentType())) {
            try {
                // Assert that the validator can validate the exported
                // content type, skip otherwise
                boolean compatible = validatorFactory.getSupportedTypes().stream().anyMatch(type -> getContentType().isKindOf(type));
                if (!compatible) {
                    reporter.info(new IOMessageImpl(MessageFormat.format("Validator \"{0}\" skipped: cannot validate exported content type \"{1}\"", validatorFactory.getIdentifier(), getContentType().getId()), null));
                    continue;
                }
                ConfigurableInstanceValidator validator = (ConfigurableInstanceValidator) validatorFactory.createExtensionObject();
                subProgress.begin(MessageFormat.format("Executing project validator ({0}/{1})", ++i, configurations.size()), ProgressIndicator.UNKNOWN);
                validator.setSchemas(getSchemas());
                validator.setSource(getSource());
                validator.setContentType(getContentType());
                validator.setServiceProvider(getServiceProvider());
                validator.configure(configuration);
                validator.validate();
                IOReport result = validator.execute(null);
                if (result != null) {
                    reporter.importMessages(result);
                    if (!result.isSuccess()) {
                        reporter.setSuccess(false);
                    }
                }
            } catch (Exception e) {
                reporter.error(new IOMessageImpl("Error running project validator", e));
                reporter.setSuccess(false);
            }
            subProgress.end();
            progress.advance(1);
        }
    }
    progress.end();
    return reporter;
}
Also used : IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) ArrayList(java.util.ArrayList) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) SubtaskProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.SubtaskProgressIndicator) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) ValidatorConfigurationService(eu.esdihumboldt.hale.io.validation.service.ValidatorConfigurationService)

Example 2 with IOProviderDescriptor

use of eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor in project hale by halestudio.

the class AbstractWFSSource method createControls.

/**
 * @see ImportSource#createControls(Composite)
 */
@Override
public void createControls(Composite parent) {
    parent.setLayout(new GridLayout(4, false));
    // caption
    // placeholder
    new Label(parent, SWT.NONE);
    Label caption = new Label(parent, SWT.NONE);
    caption.setText(getCaption());
    caption.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, false, 3, 1));
    // source file
    // target URL field
    sourceURL = new URLSourceURIFieldEditor("sourceURL", "URL:", parent) {

        // the following methods are overridden so the capabilities button
        // may appear on the same line
        @Override
        public int getNumberOfControls() {
            return super.getNumberOfControls() + 1;
        }

        @Override
        protected void doFillIntoGrid(Composite parent, int numColumns) {
            super.doFillIntoGrid(parent, numColumns - 1);
        }
    };
    sourceURL.setPage(getPage());
    // set custom URI filter
    sourceURL.setURIFilter(createHistoryURIFilter());
    // set content types for URI field
    Collection<IOProviderDescriptor> factories = getConfiguration().getFactories();
    HashSet<IContentType> supportedTypes = new HashSet<IContentType>();
    for (IOProviderDescriptor factory : factories) {
        supportedTypes.addAll(factory.getSupportedTypes());
    }
    sourceURL.setContentTypes(supportedTypes);
    sourceURL.setPropertyChangeListener(new IPropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent event) {
            if (event.getProperty().equals(FieldEditor.IS_VALID)) {
                getPage().setMessage(null);
                updateState(true);
            } else if (event.getProperty().equals(FieldEditor.VALUE)) {
                getPage().setMessage(null);
                updateState(true);
            }
        }
    });
    // button to determine from capabilities
    Button capButton = new Button(parent, SWT.PUSH);
    capButton.setText("...");
    capButton.setToolTipText("Determine based on WFS Capabilities");
    capButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            determineSource(sourceURL);
        }
    });
    // provider selection
    // label
    Label providerLabel = new Label(parent, SWT.NONE);
    providerLabel.setText("Import as");
    // create provider combo
    ComboViewer providers = createProviders(parent);
    providers.getControl().setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
    // initial state update
    updateState(true);
}
Also used : URLSourceURIFieldEditor(eu.esdihumboldt.hale.ui.io.source.URLSourceURIFieldEditor) IPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) Composite(org.eclipse.swt.widgets.Composite) IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) IContentType(org.eclipse.core.runtime.content.IContentType) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) ComboViewer(org.eclipse.jface.viewers.ComboViewer) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) HashSet(java.util.HashSet)

Example 3 with IOProviderDescriptor

use of eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor in project hale by halestudio.

the class HaleConnectSource method createControls.

/**
 * @see ImportSource#createControls(Composite)
 */
@Override
public void createControls(Composite parent) {
    parent.setLayout(new GridLayout(3, false));
    HaleConnectService hcs = HaleUI.getServiceProvider().getService(HaleConnectService.class);
    /*
		 * Login status label
		 */
    loginStatusLabel = new Label(parent, SWT.NONE);
    loginStatusLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
    loginButton = new Button(parent, SWT.PUSH);
    loginButton.setText("Login");
    loginButton.addSelectionListener(new SelectionAdapter() {

        /**
         * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
         */
        @Override
        public void widgetSelected(SelectionEvent e) {
            HaleConnectLoginDialog loginDialog = HaleConnectLoginHandler.createLoginDialog(Display.getCurrent().getActiveShell());
            if (loginDialog.open() == Dialog.OK) {
                HaleConnectLoginHandler.performLogin(loginDialog);
                updateLoginStatus();
            }
        }
    });
    /*
		 * Project name text field
		 */
    projectName = new StringFieldEditor("project", "Project", parent) {

        // the following methods are overridden so the button
        // may appear on the same line
        @Override
        public int getNumberOfControls() {
            return super.getNumberOfControls() + 1;
        }

        @Override
        protected void doFillIntoGrid(Composite parent, int numColumns) {
            super.doFillIntoGrid(parent, numColumns - 1);
        }
    };
    projectName.setEmptyStringAllowed(false);
    projectName.setErrorMessage("Please select a project before continuing.");
    projectName.setPage(getPage());
    projectName.getTextControl(parent).setEditable(false);
    projectName.getTextControl(parent).addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDown(MouseEvent e) {
            selectProject();
        }
    });
    projectName.setPropertyChangeListener(new IPropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent event) {
            if (event.getProperty().equals(FieldEditor.IS_VALID)) {
                getPage().setMessage(null);
                updateState(false);
            } else if (event.getProperty().equals(FieldEditor.VALUE)) {
                getPage().setMessage(null);
                updateState(false);
            }
        }
    });
    /*
		 * Select project button
		 */
    selectProjectButton = new Button(parent, SWT.PUSH);
    selectProjectButton.setText("Select");
    selectProjectButton.setToolTipText("Select project");
    selectProjectButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    selectProjectButton.setEnabled(hcs.isLoggedIn());
    selectProjectButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            selectProject();
        }
    });
    /*
		 * Supported types
		 */
    supportedTypes = new HashSet<IContentType>();
    if (getConfiguration().getContentType() != null) {
        supportedTypes.add(getConfiguration().getContentType());
    } else {
        // set content types for file field
        Collection<IOProviderDescriptor> factories = getConfiguration().getFactories();
        for (IOProviderDescriptor factory : factories) {
            supportedTypes.addAll(factory.getSupportedTypes());
        }
    }
    // types combo
    Composite group = new Composite(parent, SWT.NONE);
    group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1));
    group.setLayout(GridLayoutFactory.fillDefaults().numColumns(3).create());
    // label
    Label providerLabel = new Label(parent, SWT.NONE);
    providerLabel.setText("Import as");
    // create provider combo
    ComboViewer providers = createProviders(parent);
    providers.getControl().setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));
    updateLoginStatus();
    // initial state update
    updateState(true);
}
Also used : IPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) MouseEvent(org.eclipse.swt.events.MouseEvent) Composite(org.eclipse.swt.widgets.Composite) IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) MouseAdapter(org.eclipse.swt.events.MouseAdapter) HaleConnectLoginDialog(eu.esdihumboldt.hale.io.haleconnect.ui.HaleConnectLoginDialog) IContentType(org.eclipse.core.runtime.content.IContentType) StringFieldEditor(org.eclipse.jface.preference.StringFieldEditor) GridLayout(org.eclipse.swt.layout.GridLayout) HaleConnectService(eu.esdihumboldt.hale.io.haleconnect.HaleConnectService) Button(org.eclipse.swt.widgets.Button) ComboViewer(org.eclipse.jface.viewers.ComboViewer) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent)

Example 4 with IOProviderDescriptor

use of eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor in project hale by halestudio.

the class ProjectServiceImpl method save.

/**
 * @see ProjectService#save()
 */
@Override
public void save() {
    File projectFile;
    IOConfiguration saveConfig;
    synchronized (this) {
        projectFile = this.projectFile;
        saveConfig = main.getSaveConfiguration();
    }
    if (projectFile != null || canSaveTo(projectLocation)) {
        Collection<IOProviderDescriptor> providers = HaleIO.getProviderFactories(ProjectWriter.class);
        // use configuration from previous save if possible
        if (saveConfig != null) {
            // get provider ...
            ProjectWriter writer = null;
            for (IOProviderDescriptor factory : providers) {
                if (factory.getIdentifier().equals(saveConfig.getProviderId())) {
                    /*
						 * Check if the content type the project was loaded with
						 * is supported for saving.
						 * 
						 * Example for a changed content type: A saved project
						 * archive may have been extracted and the internal XML
						 * project file loaded.
						 */
                    if (projectLoadContentType != null) {
                        if (factory.getSupportedTypes() == null || !factory.getSupportedTypes().contains(projectLoadContentType)) {
                            log.warn("Project cannot be saved with the same settings it was originally saved with, as the content type has changed.");
                            break;
                        }
                    }
                    try {
                        writer = (ProjectWriter) factory.createExtensionObject();
                    } catch (Exception e) {
                        log.error("Could not create project writer", e);
                    }
                }
            }
            if (writer != null) {
                // configure provider
                writer.loadConfiguration(saveConfig.getProviderConfiguration());
                // moved externally)
                if (projectFile != null) {
                    writer.setTarget(new FileIOSupplier(projectFile));
                } else {
                    writer.setTarget(new NoStreamOutputSupplier(projectLocation));
                }
                ListenableFuture<IOReport> result = ProjectResourcesUtil.executeProvider(writer, saveProjectAdvisor, true, null);
                PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            IOReport report = result.get();
                            if (!report.isSuccess()) {
                                log.userError("The project could not be saved. Please check the report for more details.");
                            }
                        } catch (InterruptedException | ExecutionException e) {
                            log.userError("The project could not be saved.", e);
                        }
                    }
                });
            } else {
                log.info("The project cannot be saved because the format the project was saved with is not available or has changed.");
                // use save as instead
                saveAs();
            }
        } else if (projectFile != null) {
            // use I/O provider and content type mechanisms to try saving
            // the project file
            ProjectWriter writer = HaleIO.findIOProvider(ProjectWriter.class, new FileIOSupplier(projectFile), projectFile.getAbsolutePath());
            if (writer != null) {
                ProjectResourcesUtil.executeProvider(writer, saveProjectAdvisor, null);
            } else {
                log.error("The project cannot be saved because the format is not available.");
                // use save as instead
                saveAs();
            }
        } else {
            saveAs();
        }
    } else {
        saveAs();
    }
}
Also used : IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) ProjectWriter(eu.esdihumboldt.hale.common.core.io.project.ProjectWriter) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) NoStreamOutputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.NoStreamOutputSupplier) ProjectFile(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with IOProviderDescriptor

use of eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor 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;
    }
    // create default report
    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!");
                        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(getProvider());
                            } 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);
                return true;
            } else {
                // error message
                log.userError(report.getSummary() + "\nPlease see the report for details.");
                return false;
            }
        } else
            return true;
    } catch (IOProviderConfigurationException e) {
        // user feedback
        log.userError("Validation of the provider configuration failed:\n" + e.getLocalizedMessage(), e);
        return false;
    }
}
Also used : 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) ActionUI(eu.esdihumboldt.hale.ui.io.action.ActionUI) CachingImportProvider(eu.esdihumboldt.hale.common.core.io.CachingImportProvider) ImportProvider(eu.esdihumboldt.hale.common.core.io.ImportProvider) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) URI(java.net.URI) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) 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)

Aggregations

IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)40 IContentType (org.eclipse.core.runtime.content.IContentType)17 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)8 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)8 Label (org.eclipse.swt.widgets.Label)7 IOException (java.io.IOException)6 HashSet (java.util.HashSet)6 ComboViewer (org.eclipse.jface.viewers.ComboViewer)6 SelectionEvent (org.eclipse.swt.events.SelectionEvent)6 GridData (org.eclipse.swt.layout.GridData)6 Composite (org.eclipse.swt.widgets.Composite)6 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)5 ArrayList (java.util.ArrayList)5 IPropertyChangeListener (org.eclipse.jface.util.IPropertyChangeListener)5 PropertyChangeEvent (org.eclipse.jface.util.PropertyChangeEvent)5 GridLayout (org.eclipse.swt.layout.GridLayout)5 IOProvider (eu.esdihumboldt.hale.common.core.io.IOProvider)4 FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)4 InstanceWriter (eu.esdihumboldt.hale.common.instance.io.InstanceWriter)4 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)4