Search in sources :

Example 1 with EntitiesValidationReport

use of org.molgenis.data.importer.EntitiesValidationReport in project molgenis by molgenis.

the class EmxMetaDataParser method buildValidationReport.

private EntitiesValidationReport buildValidationReport(RepositoryCollection source, MyEntitiesValidationReport report, Map<String, EntityType> metaDataMap) {
    metaDataMap.values().forEach(entityTypeValidator::validate);
    metaDataMap.values().stream().map(EntityType::getAllAttributes).forEach(attributes -> attributes.forEach(attr -> {
        attributeValidator.validate(attr, ValidationMode.ADD_SKIP_ENTITY_VALIDATION);
    }));
    // validate package/entity/attribute tags
    metaDataMap.values().stream().map(EntityType::getPackage).filter(Objects::nonNull).forEach(package_ -> package_.getTags().forEach(tagValidator::validate));
    metaDataMap.values().forEach(entityType -> entityType.getTags().forEach(tagValidator::validate));
    metaDataMap.values().stream().map(EntityType::getAllAttributes).forEach(attributes -> attributes.forEach(attr -> {
        attr.getTags().forEach(tagValidator::validate);
    }));
    report = generateEntityValidationReport(source, report, metaDataMap);
    // Add entities without data
    for (String entityTypeId : metaDataMap.keySet()) {
        if (!report.getSheetsImportable().containsKey(entityTypeId))
            report.addEntity(entityTypeId, true);
    }
    return report;
}
Also used : EntitiesValidationReport(org.molgenis.data.importer.EntitiesValidationReport) DefaultPackage(org.molgenis.data.meta.DefaultPackage) Language(org.molgenis.data.i18n.model.Language) StringUtils(org.apache.commons.lang3.StringUtils) L10N_STRING(org.molgenis.data.i18n.model.L10nStringMetaData.L10N_STRING) SystemEntityType(org.molgenis.data.meta.SystemEntityType) PACKAGE_SEPARATOR(org.molgenis.data.meta.model.Package.PACKAGE_SEPARATOR) EntityTypeValidator(org.molgenis.data.validation.meta.EntityTypeValidator) L10nString(org.molgenis.data.i18n.model.L10nString) AttributeState(org.molgenis.data.importer.MyEntitiesValidationReport.AttributeState) LanguageService(org.molgenis.i18n.LanguageService) MetaDataParser(org.molgenis.data.importer.MetaDataParser) ValidationMode(org.molgenis.data.validation.meta.AttributeValidator.ValidationMode) org.molgenis.data(org.molgenis.data) ImmutableMap(com.google.common.collect.ImmutableMap) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Collections.emptyList(java.util.Collections.emptyList) EntityTypeUtils(org.molgenis.data.support.EntityTypeUtils) ParsedMetaData(org.molgenis.data.importer.ParsedMetaData) TagValidator(org.molgenis.data.validation.meta.TagValidator) String.format(java.lang.String.format) EntityTypeUtils.isStringType(org.molgenis.data.support.EntityTypeUtils.isStringType) FILE_META(org.molgenis.data.file.model.FileMetaMetaData.FILE_META) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) AttributeUtils.isIdAttributeTypeAllowed(org.molgenis.data.support.AttributeUtils.isIdAttributeTypeAllowed) EntityTypeUtils.isReferenceType(org.molgenis.data.support.EntityTypeUtils.isReferenceType) LANGUAGE(org.molgenis.data.i18n.model.LanguageMetadata.LANGUAGE) Maps.newLinkedHashMap(com.google.common.collect.Maps.newLinkedHashMap) TRUE(java.lang.Boolean.TRUE) EntityTypeDependencyResolver(org.molgenis.data.meta.EntityTypeDependencyResolver) I18nUtils.getLanguageCode(org.molgenis.data.i18n.I18nUtils.getLanguageCode) EntityUtils(org.molgenis.data.util.EntityUtils) Iterables(com.google.common.collect.Iterables) java.util(java.util) I18nUtils.isI18n(org.molgenis.data.i18n.I18nUtils.isI18n) MyEntitiesValidationReport(org.molgenis.data.importer.MyEntitiesValidationReport) DataConverter.toList(org.molgenis.data.DataConverter.toList) ENTITY_TYPE_META_DATA(org.molgenis.data.meta.model.EntityTypeMetadata.ENTITY_TYPE_META_DATA) Objects.requireNonNull(java.util.Objects.requireNonNull) ATTRIBUTE_META_DATA(org.molgenis.data.meta.model.AttributeMetadata.ATTRIBUTE_META_DATA) FALSE(java.lang.Boolean.FALSE) AttributeType(org.molgenis.data.meta.AttributeType) L10nStringFactory(org.molgenis.data.i18n.model.L10nStringFactory) LanguageFactory(org.molgenis.data.i18n.model.LanguageFactory) org.molgenis.data.meta.model(org.molgenis.data.meta.model) ImmutableMap.builder(com.google.common.collect.ImmutableMap.builder) Package(org.molgenis.data.meta.model.Package) AttributeValidator(org.molgenis.data.validation.meta.AttributeValidator) TAG(org.molgenis.data.meta.model.TagMetadata.TAG) SystemEntityType(org.molgenis.data.meta.SystemEntityType) L10nString(org.molgenis.data.i18n.model.L10nString)

Example 2 with EntitiesValidationReport

use of org.molgenis.data.importer.EntitiesValidationReport 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 3 with EntitiesValidationReport

use of org.molgenis.data.importer.EntitiesValidationReport in project molgenis by molgenis.

the class VcfImporterServiceTest method validateImportWithSamplesAlreadyExists.

@Test
public void validateImportWithSamplesAlreadyExists() {
    // Test with multiple input repositories not possible due to
    // https://github.com/molgenis/molgenis/issues/4544
    File file = mock(File.class);
    String entityTypeId0 = "entity0";
    List<String> entityTypeIds = Arrays.asList(entityTypeId0);
    String attrName0 = "attr0";
    Attribute attr0 = mock(Attribute.class);
    when(attr0.getName()).thenReturn(attrName0);
    String sampleAttrName0 = "sampleAttr0";
    Attribute sampleAttr0 = mock(Attribute.class);
    when(sampleAttr0.getName()).thenReturn(sampleAttrName0);
    String sampleEntityName0 = "entity0sample";
    EntityType sampleEntityType0 = mock(EntityType.class);
    when(sampleEntityType0.getId()).thenReturn(sampleEntityName0);
    when(sampleEntityType0.getOwnAttributes()).thenReturn(emptyList());
    when(sampleEntityType0.getAtomicAttributes()).thenReturn(singleton(sampleAttr0));
    when(sampleEntityType0.getOwnLookupAttributes()).thenReturn(emptyList());
    Attribute sampleAttr = mock(Attribute.class);
    when(sampleAttr.getName()).thenReturn(VcfAttributes.SAMPLES);
    when(sampleAttr.getRefEntity()).thenReturn(sampleEntityType0);
    when(sampleAttr.getDataType()).thenReturn(MREF);
    EntityType entityType0 = mock(EntityType.class);
    when(entityType0.getId()).thenReturn(entityTypeId0);
    when(entityType0.getAttribute(VcfAttributes.SAMPLES)).thenReturn(sampleAttr);
    when(entityType0.getOwnAttributes()).thenReturn(singletonList(sampleAttr));
    when(entityType0.getAtomicAttributes()).thenReturn(singletonList(sampleAttr));
    when(entityType0.getOwnLookupAttributes()).thenReturn(emptyList());
    @SuppressWarnings("unchecked") Repository<Entity> repo0 = mock(Repository.class);
    when(repo0.getName()).thenReturn(entityTypeId0);
    when(repo0.getEntityType()).thenReturn(entityType0);
    RepositoryCollection source = mock(RepositoryCollection.class);
    when(source.getEntityTypeIds()).thenReturn(entityTypeIds);
    when(source.getRepository(entityTypeId0)).thenReturn(repo0);
    when(dataService.hasRepository(entityTypeId0)).thenReturn(true);
    when(dataService.hasRepository(sampleEntityName0)).thenReturn(true);
    EntitiesValidationReport entitiesValidationReport = vcfImporterService.validateImport(file, source);
    assertFalse(entitiesValidationReport.valid());
    assertEquals(entitiesValidationReport.getFieldsAvailable(), emptyMap());
    Map<String, List<String>> importableFields = new HashMap<>();
    importableFields.put(entityTypeId0, singletonList(VcfAttributes.SAMPLES));
    importableFields.put(sampleEntityName0, singletonList(sampleAttrName0));
    assertEquals(entitiesValidationReport.getFieldsImportable(), importableFields);
    assertEquals(entitiesValidationReport.getFieldsRequired(), emptyMap());
    assertEquals(entitiesValidationReport.getFieldsUnknown(), emptyMap());
    assertEquals(entitiesValidationReport.getImportOrder(), emptyList());
    assertEquals(entitiesValidationReport.getPackages(), emptyList());
    Map<String, Boolean> sheetsImportable = new HashMap<>();
    sheetsImportable.put(entityTypeId0, Boolean.FALSE);
    sheetsImportable.put(sampleEntityName0, Boolean.FALSE);
    assertEquals(entitiesValidationReport.getSheetsImportable(), sheetsImportable);
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) EntityType(org.molgenis.data.meta.model.EntityType) EntitiesValidationReport(org.molgenis.data.importer.EntitiesValidationReport) File(java.io.File) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 4 with EntitiesValidationReport

use of org.molgenis.data.importer.EntitiesValidationReport in project molgenis by molgenis.

the class VcfImporterService method validateImport.

@Override
public EntitiesValidationReport validateImport(File file, RepositoryCollection source) {
    EntitiesValidationReport report = new EntitiesValidationReportImpl();
    Iterator<String> it = source.getEntityTypeIds().iterator();
    if (it.hasNext()) {
        String entityTypeId = it.next();
        EntityType emd = source.getRepository(entityTypeId).getEntityType();
        // Vcf entity
        boolean entityExists = runAsSystem(() -> dataService.hasRepository(entityTypeId));
        report.getSheetsImportable().put(entityTypeId, !entityExists);
        // Available Attributes
        List<String> availableAttributeNames = Lists.newArrayList();
        for (Attribute attr : emd.getAtomicAttributes()) {
            availableAttributeNames.add(attr.getName());
        }
        report.getFieldsImportable().put(entityTypeId, availableAttributeNames);
        // Sample entity
        Attribute sampleAttribute = emd.getAttribute(VcfAttributes.SAMPLES);
        if (sampleAttribute != null) {
            String sampleEntityName = sampleAttribute.getRefEntity().getId();
            boolean sampleEntityExists = runAsSystem(() -> dataService.hasRepository(sampleEntityName));
            report.getSheetsImportable().put(sampleEntityName, !sampleEntityExists);
            List<String> availableSampleAttributeNames = Lists.newArrayList();
            for (Attribute attr : sampleAttribute.getRefEntity().getAtomicAttributes()) {
                availableSampleAttributeNames.add(attr.getName());
            }
            report.getFieldsImportable().put(sampleEntityName, availableSampleAttributeNames);
        }
    }
    return report;
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntitiesValidationReport(org.molgenis.data.importer.EntitiesValidationReport) Attribute(org.molgenis.data.meta.model.Attribute) EntitiesValidationReportImpl(org.molgenis.data.importer.EntitiesValidationReportImpl)

Example 5 with EntitiesValidationReport

use of org.molgenis.data.importer.EntitiesValidationReport in project molgenis by molgenis.

the class VcfImporterServiceTest method validateImportWithoutSamplesAlreadyExists.

@Test
public void validateImportWithoutSamplesAlreadyExists() {
    // Test with multiple input repositories not possible due to
    // https://github.com/molgenis/molgenis/issues/4544
    File file = mock(File.class);
    String entityTypeId0 = "entity0";
    List<String> entityTypeIds = Arrays.asList(entityTypeId0);
    String attrName0 = "attr0";
    Attribute attr0 = mock(Attribute.class);
    when(attr0.getName()).thenReturn(attrName0);
    EntityType entityType0 = mock(EntityType.class);
    when(entityType0.getId()).thenReturn(entityTypeId0);
    when(entityType0.getOwnAttributes()).thenReturn(singletonList(attr0));
    when(entityType0.getAtomicAttributes()).thenReturn(singletonList(attr0));
    when(entityType0.getOwnLookupAttributes()).thenReturn(emptyList());
    @SuppressWarnings("unchecked") Repository<Entity> repo0 = mock(Repository.class);
    when(repo0.getName()).thenReturn(entityTypeId0);
    when(repo0.getEntityType()).thenReturn(entityType0);
    RepositoryCollection source = mock(RepositoryCollection.class);
    when(source.getEntityTypeIds()).thenReturn(entityTypeIds);
    when(source.getRepository(entityTypeId0)).thenReturn(repo0);
    when(dataService.hasRepository(entityTypeId0)).thenReturn(true);
    EntitiesValidationReport entitiesValidationReport = vcfImporterService.validateImport(file, source);
    assertFalse(entitiesValidationReport.valid());
    assertEquals(entitiesValidationReport.getFieldsAvailable(), emptyMap());
    assertEquals(entitiesValidationReport.getFieldsImportable(), singletonMap(entityTypeId0, singletonList(attrName0)));
    assertEquals(entitiesValidationReport.getFieldsRequired(), emptyMap());
    assertEquals(entitiesValidationReport.getFieldsUnknown(), emptyMap());
    assertEquals(entitiesValidationReport.getImportOrder(), emptyList());
    assertEquals(entitiesValidationReport.getPackages(), emptyList());
    assertEquals(entitiesValidationReport.getSheetsImportable(), singletonMap(entityTypeId0, Boolean.FALSE));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntitiesValidationReport(org.molgenis.data.importer.EntitiesValidationReport) Attribute(org.molgenis.data.meta.model.Attribute) File(java.io.File) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Aggregations

EntitiesValidationReport (org.molgenis.data.importer.EntitiesValidationReport)8 File (java.io.File)4 Attribute (org.molgenis.data.meta.model.Attribute)4 EntityType (org.molgenis.data.meta.model.EntityType)4 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)3 Test (org.testng.annotations.Test)3 EntitiesValidationReportImpl (org.molgenis.data.importer.EntitiesValidationReportImpl)2 Package (org.molgenis.data.meta.model.Package)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMap.builder (com.google.common.collect.ImmutableMap.builder)1 Iterables (com.google.common.collect.Iterables)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)1 Maps.newLinkedHashMap (com.google.common.collect.Maps.newLinkedHashMap)1 FALSE (java.lang.Boolean.FALSE)1 TRUE (java.lang.Boolean.TRUE)1 String.format (java.lang.String.format)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1