use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class OrganisationUnitController method getEntityList.
@Override
@SuppressWarnings("unchecked")
protected List<OrganisationUnit> getEntityList(WebMetadata metadata, WebOptions options, List<String> filters, List<Order> orders) throws QueryParserException {
List<OrganisationUnit> objects = Lists.newArrayList();
User currentUser = currentUserService.getCurrentUser();
boolean anySpecialPropertySet = ObjectUtils.anyIsTrue(options.isTrue("userOnly"), options.isTrue("userDataViewOnly"), options.isTrue("userDataViewFallback"), options.isTrue("levelSorted"));
boolean anyQueryPropertySet = ObjectUtils.firstNonNull(options.get("query"), options.getInt("level"), options.getInt("maxLevel")) != null || options.isTrue("withinUserHierarchy");
String memberObject = options.get("memberObject");
String memberCollection = options.get("memberCollection");
if (options.isTrue("userOnly")) {
objects = new ArrayList<>(currentUser.getOrganisationUnits());
} else if (options.isTrue("userDataViewOnly")) {
objects = new ArrayList<>(currentUser.getDataViewOrganisationUnits());
} else if (options.isTrue("userDataViewFallback")) {
if (currentUser.hasDataViewOrganisationUnit()) {
objects = new ArrayList<>(currentUser.getDataViewOrganisationUnits());
} else {
objects = organisationUnitService.getOrganisationUnitsAtLevel(1);
}
} else if (options.isTrue("levelSorted")) {
objects = new ArrayList<>(manager.getAll(getEntityClass()));
Collections.sort(objects, OrganisationUnitByLevelComparator.INSTANCE);
} else if (anyQueryPropertySet) {
OrganisationUnitQueryParams params = new OrganisationUnitQueryParams();
params.setQuery(options.get("query"));
params.setLevel(options.getInt("level"));
params.setMaxLevels(options.getInt("maxLevel"));
params.setParents(options.isTrue("withinUserHierarchy") ? currentUser.getOrganisationUnits() : Sets.newHashSet());
objects = organisationUnitService.getOrganisationUnitsByQuery(params);
}
// ---------------------------------------------------------------------
// Standard Query handling
// ---------------------------------------------------------------------
Query query = queryService.getQueryFromUrl(getEntityClass(), filters, orders, options.getRootJunction());
query.setUser(currentUser);
query.setDefaultOrder();
if (anySpecialPropertySet || anyQueryPropertySet) {
query.setObjects(objects);
}
List<OrganisationUnit> list = (List<OrganisationUnit>) queryService.query(query);
// ---------------------------------------------------------------------
// Collection member count in hierarchy handling
// ---------------------------------------------------------------------
IdentifiableObject member = null;
if (memberObject != null && memberCollection != null && (member = manager.get(memberObject)) != null) {
for (OrganisationUnit unit : list) {
Long count = organisationUnitService.getOrganisationUnitHierarchyMemberCount(unit, member, memberCollection);
unit.setMemberCount((count != null ? count.intValue() : 0));
}
}
return list;
}
use of org.hisp.dhis.common.IdentifiableObject 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.common.IdentifiableObject 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.common.IdentifiableObject 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;
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkReferences.
private List<PreheatErrorReport> checkReferences(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, boolean skipSharing) {
List<PreheatErrorReport> preheatErrorReports = new ArrayList<>();
if (object == null) {
return preheatErrorReports;
}
Schema schema = schemaService.getDynamicSchema(object.getClass());
schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).forEach(p -> {
if (skipCheck(p.getKlass()) || skipCheck(p.getItemKlass())) {
return;
}
if (!p.isCollection()) {
IdentifiableObject refObject = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
IdentifiableObject ref = preheat.get(identifier, refObject);
if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
if (!("user".equals(p.getName()) && User.class.isAssignableFrom(p.getKlass()) && skipSharing)) {
preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getName()));
}
}
} else {
Collection<IdentifiableObject> objects = ReflectionUtils.newCollectionInstance(p.getKlass());
Collection<IdentifiableObject> refObjects = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
for (IdentifiableObject refObject : refObjects) {
if (Preheat.isDefault(refObject))
continue;
IdentifiableObject ref = preheat.get(identifier, refObject);
if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getCollectionName()));
} else {
objects.add(refObject);
}
}
ReflectionUtils.invokeMethod(object, p.getSetterMethod(), objects);
}
});
if (schema.havePersistedProperty("attributeValues")) {
object.getAttributeValues().stream().filter(attributeValue -> attributeValue.getAttribute() != null && preheat.get(identifier, attributeValue.getAttribute()) == null).forEach(attributeValue -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(attributeValue.getAttribute()), identifier.getIdentifiersWithName(object), "attributeValues")));
}
if (schema.havePersistedProperty("userGroupAccesses")) {
object.getUserGroupAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUserGroup() != null && preheat.get(identifier, userGroupAccess.getUserGroup()) == null).forEach(userGroupAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userGroupAccesses.getUserGroup()), identifier.getIdentifiersWithName(object), "userGroupAccesses")));
}
if (schema.havePersistedProperty("userAccesses")) {
object.getUserAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUser() != null && preheat.get(identifier, userGroupAccess.getUser()) == null).forEach(userAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userAccesses.getUser()), identifier.getIdentifiersWithName(object), "userAccesses")));
}
return preheatErrorReports;
}
Aggregations