use of org.molgenis.data.RepositoryCollection in project molgenis by molgenis.
the class ImportServiceFactory method getImportService.
/**
* Finds a suitable ImportService for a FileRepositoryCollection.
* <p>
* Import of mixed backend types in one FileRepositoryCollection isn't supported.
*
* @return ImportService
* @throws MolgenisDataException if no suitable ImportService is found for the FileRepositoryCollection
*/
public ImportService getImportService(File file, RepositoryCollection source) {
final Map<String, ImportService> importServicesMappedToExtensions = Maps.newHashMap();
importServices.stream().filter(importService -> importService.canImport(file, source)).forEach(importService -> {
for (String extension : importService.getSupportedFileExtensions()) {
importServicesMappedToExtensions.put(extension.toLowerCase(), importService);
}
});
String extension = FileExtensionUtils.findExtensionFromPossibilities(file.getName(), importServicesMappedToExtensions.keySet());
final ImportService importService = importServicesMappedToExtensions.get(extension);
if (importService == null)
throw new MolgenisDataException("Can not import file. No suitable importer found");
return importService;
}
use of org.molgenis.data.RepositoryCollection in project molgenis by molgenis.
the class PackageWizardPage method handleRequest.
@Override
public String handleRequest(HttpServletRequest request, BindingResult result, Wizard wizard) {
ImportWizardUtil.validateImportWizard(wizard);
ImportWizard importWizard = (ImportWizard) wizard;
String entityImportOption = importWizard.getEntityImportOption();
if (entityImportOption != null) {
try {
// convert input to database action
DatabaseAction entityDbAction = ImportWizardUtil.toDatabaseAction(entityImportOption);
if (entityDbAction == null) {
throw new IOException("unknown database action: " + entityImportOption);
}
RepositoryCollection repositoryCollection = fileRepositoryCollectionFactory.createFileRepositoryCollection(importWizard.getFile());
ImportService importService = importServiceFactory.getImportService(importWizard.getFile(), repositoryCollection);
// Do integration test only if there are no previous errors found
if (!importWizard.getEntitiesImportable().containsValue(false)) {
// The package name that is selected in the "package selection" page
String selectedPackage = request.getParameter("selectedPackage");
// The entities that can be imported
Map<String, Boolean> entitiesImportable = importService.determineImportableEntities(metaDataService, repositoryCollection, selectedPackage);
// The results of the attribute checks are stored in maps with the entityname as key, those need to be updated with the packagename
updateFieldReports(importWizard, selectedPackage, entitiesImportable);
// Set the entities that can be imported
importWizard.setEntitiesImportable(entitiesImportable);
// The entities that can not be imported. If even one entity can not be imported, everything fails
List<String> entitiesNotImportable = entitiesImportable.entrySet().stream().filter(entity -> !entity.getValue()).map(Map.Entry::getKey).collect(toList());
if (!entitiesNotImportable.isEmpty()) {
throw new MolgenisDataException("You are trying to upload entities that are not compatible with the already existing entities: " + entitiesNotImportable.toString());
}
}
} catch (RuntimeException | IOException e) {
ImportWizardUtil.handleException(e, importWizard, result, LOG, entityImportOption);
}
}
return null;
}
use of org.molgenis.data.RepositoryCollection in project molgenis by molgenis.
the class ValidationResultWizardPage method handleRequest.
@Override
public String handleRequest(HttpServletRequest request, BindingResult result, Wizard wizard) {
ImportWizardUtil.validateImportWizard(wizard);
ImportWizard importWizard = (ImportWizard) wizard;
String entityImportOption = importWizard.getEntityImportOption();
if (entityImportOption != null) {
try {
// convert input to database action
DatabaseAction entityDbAction = ImportWizardUtil.toDatabaseAction(entityImportOption);
if (entityDbAction == null)
throw new IOException("unknown database action: " + entityImportOption);
RepositoryCollection repositoryCollection = fileRepositoryCollectionFactory.createFileRepositoryCollection(importWizard.getFile());
ImportService importService = importServiceFactory.getImportService(importWizard.getFile(), repositoryCollection);
synchronized (this) {
ImportRun importRun = importRunService.addImportRun(SecurityUtils.getCurrentUsername(), false);
((ImportWizard) wizard).setImportRunId(importRun.getId());
asyncImportJobs.execute(new ImportJob(importService, SecurityContextHolder.getContext(), repositoryCollection, entityDbAction, importRun.getId(), importRunService, request.getSession(), importWizard.getSelectedPackage()));
}
} catch (RuntimeException | IOException e) {
ImportWizardUtil.handleException(e, importWizard, result, LOG, entityImportOption);
}
}
// Convert to list because it's less impossible use in FreeMarker
if (!userAccountService.getCurrentUser().isSuperuser()) {
String username = SecurityUtils.getCurrentUsername();
groups = RunAsSystemAspect.runAsSystem(() -> Lists.newArrayList(userService.getUserGroups(username)));
} else {
groups = dataService.findAll(GROUP, Group.class).collect(toList());
}
((ImportWizard) wizard).setGroups(groups);
return null;
}
use of org.molgenis.data.RepositoryCollection in project molgenis by molgenis.
the class OptionsWizardPage method validateInput.
private String validateInput(File file, ImportWizard wizard, BindingResult result) {
// decide what importer to use...
RepositoryCollection source = fileRepositoryCollectionFactory.createFileRepositoryCollection(file);
ImportService importService = importServiceFactory.getImportService(file, source);
EntitiesValidationReport validationReport = importService.validateImport(file, source);
wizard.setEntitiesImportable(validationReport.getSheetsImportable());
wizard.setFieldsDetected(validationReport.getFieldsImportable());
wizard.setFieldsRequired(validationReport.getFieldsRequired());
wizard.setFieldsAvailable(validationReport.getFieldsAvailable());
wizard.setFieldsUnknown(validationReport.getFieldsUnknown());
Set<String> allPackages = new HashSet<>(validationReport.getPackages());
for (Package p : dataService.getMeta().getPackages()) {
allPackages.add(p.getId());
}
List<String> entitiesInDefaultPackage = new ArrayList<>();
for (String entityTypeId : validationReport.getSheetsImportable().keySet()) {
if (validationReport.getSheetsImportable().get(entityTypeId)) {
if (isInDefaultPackage(entityTypeId, allPackages))
entitiesInDefaultPackage.add(entityTypeId);
}
}
wizard.setEntitiesInDefaultPackage(entitiesInDefaultPackage);
List<String> packages = new ArrayList<>(validationReport.getPackages());
packages.add(0, PACKAGE_DEFAULT);
wizard.setPackages(packages);
String msg = null;
if (validationReport.valid()) {
wizard.setFile(file);
msg = "File is validated and can be imported.";
} else {
wizard.setValidationMessage("File did not pass validation see results below. Please resolve the errors and try again.");
}
return msg;
}
use of org.molgenis.data.RepositoryCollection in project molgenis by molgenis.
the class UploadWizardPage method handleRequest.
@Override
public String handleRequest(HttpServletRequest request, BindingResult result, Wizard wizard) {
ImportWizardUtil.validateImportWizard(wizard);
ImportWizard importWizard = (ImportWizard) wizard;
String entityImportOption = request.getParameter("entity_option");
try {
File file = null;
Part part = request.getPart("upload");
if (part != null) {
file = FileUploadUtils.saveToTempFolder(part);
}
if (file == null) {
result.addError(new ObjectError("wizard", "No file selected"));
} else {
importWizard.setFile(file);
RepositoryCollection repositoryCollection = fileRepositoryCollectionFactory.createFileRepositoryCollection(file);
ImportService importService = importServiceFactory.getImportService(file, repositoryCollection);
importWizard.setSupportedDatabaseActions(importService.getSupportedDatabaseActions());
importWizard.setMustChangeEntityName(importService.getMustChangeEntityName());
}
} catch (Exception e) {
ImportWizardUtil.handleException(e, importWizard, result, LOG, entityImportOption);
}
return null;
}
Aggregations