use of org.hisp.dhis.feedback.TypeReport in project dhis2-core by dhis2.
the class DefaultObjectBundleService method commit.
@Override
public ObjectBundleCommitReport commit(ObjectBundle bundle) {
Map<Class<?>, TypeReport> typeReports = new HashMap<>();
ObjectBundleCommitReport commitReport = new ObjectBundleCommitReport(typeReports);
if (ObjectBundleMode.VALIDATE == bundle.getObjectBundleMode()) {
// skip if validate only
return commitReport;
}
List<Class<? extends IdentifiableObject>> klasses = getSortedClasses(bundle);
Session session = sessionFactory.getCurrentSession();
objectBundleHooks.forEach(hook -> hook.preCommit(bundle));
for (Class<? extends IdentifiableObject> klass : klasses) {
List<IdentifiableObject> nonPersistedObjects = bundle.getObjects(klass, false);
List<IdentifiableObject> persistedObjects = bundle.getObjects(klass, true);
objectBundleHooks.forEach(hook -> hook.preTypeImport(klass, nonPersistedObjects, bundle));
if (bundle.getImportMode().isCreateAndUpdate()) {
TypeReport typeReport = new TypeReport(klass);
typeReport.merge(handleCreates(session, klass, nonPersistedObjects, bundle));
typeReport.merge(handleUpdates(session, klass, persistedObjects, bundle));
typeReports.put(klass, typeReport);
} else if (bundle.getImportMode().isCreate()) {
typeReports.put(klass, handleCreates(session, klass, nonPersistedObjects, bundle));
} else if (bundle.getImportMode().isUpdate()) {
typeReports.put(klass, handleUpdates(session, klass, persistedObjects, bundle));
} else if (bundle.getImportMode().isDelete()) {
typeReports.put(klass, handleDeletes(session, klass, persistedObjects, bundle));
}
objectBundleHooks.forEach(hook -> hook.postTypeImport(klass, persistedObjects, bundle));
if (FlushMode.AUTO == bundle.getFlushMode())
session.flush();
}
if (!bundle.getImportMode().isDelete()) {
objectBundleHooks.forEach(hook -> hook.postCommit(bundle));
}
dbmsManager.clearSession();
cacheManager.clearCache();
bundle.setObjectBundleStatus(ObjectBundleStatus.COMMITTED);
return commitReport;
}
use of org.hisp.dhis.feedback.TypeReport in project dhis2-core by dhis2.
the class DefaultObjectBundleService method handleDeletes.
private TypeReport handleDeletes(Session session, Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle) {
TypeReport typeReport = new TypeReport(klass);
if (objects.isEmpty()) {
return typeReport;
}
String message = "(" + bundle.getUsername() + ") Deleting " + objects.size() + " object(s) of type " + objects.get(0).getClass().getSimpleName();
log.info(message);
if (bundle.hasTaskId()) {
notifier.notify(bundle.getTaskId(), message);
}
List<IdentifiableObject> persistedObjects = bundle.getPreheat().getAll(bundle.getPreheatIdentifier(), objects);
for (int idx = 0; idx < persistedObjects.size(); idx++) {
IdentifiableObject object = persistedObjects.get(idx);
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
typeReport.addObjectReport(objectReport);
objectBundleHooks.forEach(hook -> hook.preDelete(object, bundle));
manager.delete(object, bundle.getUser());
bundle.getPreheat().remove(bundle.getPreheatIdentifier(), object);
if (log.isDebugEnabled()) {
String msg = "(" + bundle.getUsername() + ") Deleted object '" + bundle.getPreheatIdentifier().getIdentifiersWithName(object) + "'";
log.debug(msg);
}
if (FlushMode.OBJECT == bundle.getFlushMode())
session.flush();
}
return typeReport;
}
use of org.hisp.dhis.feedback.TypeReport in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method validateForDelete.
private TypeReport validateForDelete(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle) {
TypeReport typeReport = new TypeReport(klass);
if (objects == null || objects.isEmpty()) {
return typeReport;
}
Iterator<IdentifiableObject> iterator = objects.iterator();
int idx = 0;
while (iterator.hasNext()) {
IdentifiableObject identifiableObject = iterator.next();
IdentifiableObject object = bundle.getPreheat().get(bundle.getPreheatIdentifier(), identifiableObject);
if (object == null || object.getId() == 0) {
if (Preheat.isDefaultClass(identifiableObject.getClass()))
continue;
ObjectReport objectReport = new ObjectReport(klass, idx, object != null ? object.getUid() : null);
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E5001, bundle.getPreheatIdentifier(), bundle.getPreheatIdentifier().getIdentifiersWithName(identifiableObject)).setMainId(object != null ? object.getUid() : null));
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
}
idx++;
}
return typeReport;
}
use of org.hisp.dhis.feedback.TypeReport 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.feedback.TypeReport in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method validateSecurity.
private TypeReport validateSecurity(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle, ImportStrategy importMode) {
TypeReport typeReport = new TypeReport(klass);
if (objects == null || objects.isEmpty()) {
return typeReport;
}
Iterator<IdentifiableObject> iterator = objects.iterator();
PreheatIdentifier identifier = bundle.getPreheatIdentifier();
int idx = 0;
while (iterator.hasNext()) {
IdentifiableObject object = iterator.next();
if (importMode.isCreate()) {
if (!aclService.canCreate(bundle.getUser(), klass)) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3000, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
} else {
IdentifiableObject persistedObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
if (importMode.isUpdate()) {
if (!aclService.canUpdate(bundle.getUser(), persistedObject)) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3001, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
} else if (importMode.isDelete()) {
if (!aclService.canDelete(bundle.getUser(), persistedObject)) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3002, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
}
}
if (User.class.isInstance(object)) {
User user = (User) object;
List<ErrorReport> errorReports = userService.validateUser(user, bundle.getUser());
if (!errorReports.isEmpty()) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReports(errorReports);
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
}
List<ErrorReport> sharingErrorReports = aclService.verifySharing(object, bundle.getUser());
if (!sharingErrorReports.isEmpty()) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReports(sharingErrorReports);
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
idx++;
}
return typeReport;
}
Aggregations