use of org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle in project dhis2-core by dhis2.
the class ObjectBundleServiceTest method testDeleteRequiresValidReferencesAUTO.
@Test
public void testDeleteRequiresValidReferencesAUTO() throws IOException {
Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/de_validate6.json").getInputStream(), RenderFormat.JSON);
ObjectBundleParams params = new ObjectBundleParams();
params.setObjectBundleMode(ObjectBundleMode.VALIDATE);
params.setPreheatIdentifier(PreheatIdentifier.AUTO);
params.setImportStrategy(ImportStrategy.DELETE);
params.setObjects(metadata);
ObjectBundle bundle = objectBundleService.create(params);
ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
assertFalse(validate.getTypeReportMap(DataElement.class).getObjectReports().isEmpty());
assertEquals(3, validate.getErrorReportsByCode(DataElement.class, ErrorCode.E5001).size());
}
use of org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle in project dhis2-core by dhis2.
the class ObjectBundleServiceTest method testCreateMetadataWithSuperuserRoleInjected.
@Test
public void testCreateMetadataWithSuperuserRoleInjected() throws IOException {
createUserAndInjectSecurityContext(true);
Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/metadata_superuser_bug.json").getInputStream(), RenderFormat.JSON);
ObjectBundleParams params = new ObjectBundleParams();
params.setObjectBundleMode(ObjectBundleMode.COMMIT);
params.setImportStrategy(ImportStrategy.CREATE_AND_UPDATE);
params.setObjects(metadata);
ObjectBundle bundle = objectBundleService.create(params);
ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
assertFalse(validate.isEmpty());
assertEquals(1, validate.getErrorReportsByCode(UserAuthorityGroup.class, ErrorCode.E5003).size());
}
use of org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle in project dhis2-core by dhis2.
the class ObjectBundleServiceTest method testUpdateMetadataWithValidationRules.
@Test
public void testUpdateMetadataWithValidationRules() throws IOException {
Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/metadata_with_vr.json").getInputStream(), RenderFormat.JSON);
ObjectBundleParams params = new ObjectBundleParams();
params.setObjectBundleMode(ObjectBundleMode.COMMIT);
params.setImportStrategy(ImportStrategy.CREATE);
params.setObjects(metadata);
ObjectBundle bundle = objectBundleService.create(params);
ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
assertTrue(validate.getErrorReports().isEmpty());
objectBundleService.commit(bundle);
metadata = renderService.fromMetadata(new ClassPathResource("dxf2/metadata_with_vr_update.json").getInputStream(), RenderFormat.JSON);
params = new ObjectBundleParams();
params.setObjectBundleMode(ObjectBundleMode.COMMIT);
params.setImportStrategy(ImportStrategy.UPDATE);
params.setObjects(metadata);
bundle = objectBundleService.create(params);
validate = objectBundleValidationService.validate(bundle);
assertTrue(validate.getErrorReports().isEmpty());
objectBundleService.commit(bundle);
List<DataSet> dataSets = manager.getAll(DataSet.class);
List<OrganisationUnit> organisationUnits = manager.getAll(OrganisationUnit.class);
List<DataElement> dataElements = manager.getAll(DataElement.class);
List<UserAuthorityGroup> userRoles = manager.getAll(UserAuthorityGroup.class);
List<User> users = manager.getAll(User.class);
List<ValidationRule> validationRules = manager.getAll(ValidationRule.class);
assertFalse(dataSets.isEmpty());
assertFalse(organisationUnits.isEmpty());
assertFalse(dataElements.isEmpty());
assertFalse(users.isEmpty());
assertFalse(userRoles.isEmpty());
assertEquals(2, validationRules.size());
ValidationRule validationRule1 = manager.get(ValidationRule.class, "ztzsVjSIWg7");
assertNotNull(validationRule1.getLeftSide());
assertNotNull(validationRule1.getRightSide());
ValidationRule validationRule2 = manager.get(ValidationRule.class, "TGvH4Hiyduc");
assertNotNull(validationRule2.getLeftSide());
assertNotNull(validationRule2.getRightSide());
}
use of org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method validate.
@Override
public ObjectBundleValidationReport validate(ObjectBundle bundle) {
Timer timer = new SystemTimer().start();
ObjectBundleValidationReport validation = new ObjectBundleValidationReport();
if ((bundle.getUser() == null || bundle.getUser().isSuper()) && bundle.isSkipValidation()) {
log.warn("Skipping validation for metadata import by user '" + bundle.getUsername() + "'. Not recommended.");
return validation;
}
List<Class<? extends IdentifiableObject>> klasses = getSortedClasses(bundle);
for (Class<? extends IdentifiableObject> klass : klasses) {
TypeReport typeReport = new TypeReport(klass);
List<IdentifiableObject> nonPersistedObjects = bundle.getObjects(klass, false);
List<IdentifiableObject> persistedObjects = bundle.getObjects(klass, true);
List<IdentifiableObject> allObjects = bundle.getObjectMap().get(klass);
handleDefaults(nonPersistedObjects);
handleDefaults(persistedObjects);
typeReport.merge(checkDuplicateIds(klass, persistedObjects, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
if (bundle.getImportMode().isCreateAndUpdate()) {
typeReport.merge(runValidationHooks(klass, nonPersistedObjects, bundle));
typeReport.merge(runValidationHooks(klass, persistedObjects, bundle));
typeReport.merge(validateSecurity(klass, nonPersistedObjects, bundle, ImportStrategy.CREATE));
typeReport.merge(validateSecurity(klass, persistedObjects, bundle, ImportStrategy.UPDATE));
typeReport.merge(validateBySchemas(klass, nonPersistedObjects, bundle));
typeReport.merge(validateBySchemas(klass, persistedObjects, bundle));
typeReport.merge(checkUniqueness(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
typeReport.merge(checkUniqueness(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
typeReport.merge(checkMandatoryAttributes(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
typeReport.merge(checkMandatoryAttributes(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
typeReport.merge(checkUniqueAttributes(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
typeReport.merge(checkUniqueAttributes(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
TypeReport checkReferences = checkReferences(klass, allObjects, bundle.getPreheat(), bundle.getPreheatIdentifier(), bundle.isSkipSharing());
if (!checkReferences.getErrorReports().isEmpty() && AtomicMode.ALL == bundle.getAtomicMode()) {
typeReport.getStats().incIgnored();
}
typeReport.getStats().incCreated(nonPersistedObjects.size());
typeReport.getStats().incUpdated(persistedObjects.size());
typeReport.merge(checkReferences);
} else if (bundle.getImportMode().isCreate()) {
typeReport.merge(runValidationHooks(klass, nonPersistedObjects, bundle));
typeReport.merge(validateSecurity(klass, nonPersistedObjects, bundle, ImportStrategy.CREATE));
typeReport.merge(validateForCreate(klass, persistedObjects, bundle));
typeReport.merge(validateBySchemas(klass, nonPersistedObjects, bundle));
typeReport.merge(checkUniqueness(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
typeReport.merge(checkMandatoryAttributes(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
typeReport.merge(checkUniqueAttributes(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
TypeReport checkReferences = checkReferences(klass, allObjects, bundle.getPreheat(), bundle.getPreheatIdentifier(), bundle.isSkipSharing());
if (!checkReferences.getErrorReports().isEmpty() && AtomicMode.ALL == bundle.getAtomicMode()) {
typeReport.getStats().incIgnored();
}
typeReport.getStats().incCreated(nonPersistedObjects.size());
typeReport.merge(checkReferences);
} else if (bundle.getImportMode().isUpdate()) {
typeReport.merge(runValidationHooks(klass, persistedObjects, bundle));
typeReport.merge(validateSecurity(klass, persistedObjects, bundle, ImportStrategy.UPDATE));
typeReport.merge(validateForUpdate(klass, nonPersistedObjects, bundle));
typeReport.merge(validateBySchemas(klass, persistedObjects, bundle));
typeReport.merge(checkUniqueness(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
typeReport.merge(checkMandatoryAttributes(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
typeReport.merge(checkUniqueAttributes(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
TypeReport checkReferences = checkReferences(klass, allObjects, bundle.getPreheat(), bundle.getPreheatIdentifier(), bundle.isSkipSharing());
if (!checkReferences.getErrorReports().isEmpty() && AtomicMode.ALL == bundle.getAtomicMode()) {
typeReport.getStats().incIgnored();
}
typeReport.getStats().incUpdated(persistedObjects.size());
typeReport.merge(checkReferences);
} else if (bundle.getImportMode().isDelete()) {
typeReport.merge(validateSecurity(klass, persistedObjects, bundle, ImportStrategy.DELETE));
typeReport.merge(validateForDelete(klass, nonPersistedObjects, bundle));
typeReport.getStats().incDeleted(persistedObjects.size());
}
validation.addTypeReport(typeReport);
}
validateAtomicity(bundle, validation);
bundle.setObjectBundleStatus(ObjectBundleStatus.VALIDATED);
log.info("(" + bundle.getUsername() + ") Import:Validation took " + timer.toString());
return validation;
}
use of org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle in project dhis2-core by dhis2.
the class VersionedObjectObjectBundleHook method postTypeImport.
@Override
public <T extends IdentifiableObject> void postTypeImport(Class<? extends IdentifiableObject> klass, List<T> objects, ObjectBundle bundle) {
if (Section.class.isAssignableFrom(klass)) {
Set<DataSet> dataSets = new HashSet<>();
objects.forEach(o -> {
DataSet dataSet = ((Section) o).getDataSet();
if (dataSet.getId() > 0) {
dataSets.add(dataSet);
}
});
dataSets.forEach(ds -> {
ds.increaseVersion();
sessionFactory.getCurrentSession().save(ds);
});
} else if (Option.class.isAssignableFrom(klass)) {
Set<OptionSet> optionSets = new HashSet<>();
objects.forEach(o -> {
Option option = (Option) o;
if (option.getOptionSet() != null && option.getId() > 0) {
optionSets.add(option.getOptionSet());
}
});
optionSets.forEach(os -> {
os.increaseVersion();
sessionFactory.getCurrentSession().save(os);
});
}
}
Aggregations