Search in sources :

Example 1 with RepositoryCollection

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;
}
Also used : Component(org.springframework.stereotype.Component) List(java.util.List) Lists(com.google.common.collect.Lists) RepositoryCollection(org.molgenis.data.RepositoryCollection) Map(java.util.Map) FileExtensionUtils(org.molgenis.data.file.util.FileExtensionUtils) Set(java.util.Set) Maps(com.google.common.collect.Maps) MolgenisDataException(org.molgenis.data.MolgenisDataException) OrderComparator(org.springframework.core.OrderComparator) File(java.io.File) Collectors.toSet(java.util.stream.Collectors.toSet) MolgenisDataException(org.molgenis.data.MolgenisDataException)

Example 2 with RepositoryCollection

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;
}
Also used : DatabaseAction(org.molgenis.data.DatabaseAction) IOException(java.io.IOException) RepositoryCollection(org.molgenis.data.RepositoryCollection) MolgenisDataException(org.molgenis.data.MolgenisDataException) ImportService(org.molgenis.data.importer.ImportService) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with RepositoryCollection

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;
}
Also used : DatabaseAction(org.molgenis.data.DatabaseAction) RepositoryCollection(org.molgenis.data.RepositoryCollection) IOException(java.io.IOException)

Example 4 with RepositoryCollection

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;
}
Also used : RepositoryCollection(org.molgenis.data.RepositoryCollection) EntitiesValidationReport(org.molgenis.data.importer.EntitiesValidationReport) ImportService(org.molgenis.data.importer.ImportService) ArrayList(java.util.ArrayList) Package(org.molgenis.data.meta.model.Package) HashSet(java.util.HashSet)

Example 5 with RepositoryCollection

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;
}
Also used : RepositoryCollection(org.molgenis.data.RepositoryCollection) ObjectError(org.springframework.validation.ObjectError) ImportService(org.molgenis.data.importer.ImportService) Part(javax.servlet.http.Part) File(java.io.File)

Aggregations

RepositoryCollection (org.molgenis.data.RepositoryCollection)13 MolgenisDataException (org.molgenis.data.MolgenisDataException)4 ImportService (org.molgenis.data.importer.ImportService)4 File (java.io.File)3 DatabaseAction (org.molgenis.data.DatabaseAction)3 EntityType (org.molgenis.data.meta.model.EntityType)3 IOException (java.io.IOException)2 Map (java.util.Map)2 Attribute (org.molgenis.data.meta.model.Attribute)2 Package (org.molgenis.data.meta.model.Package)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Collectors.toSet (java.util.stream.Collectors.toSet)1