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