use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class EmbeddedObjectObjectBundleHook method preUpdate.
@Override
public <T extends IdentifiableObject> void preUpdate(T object, T persistedObject, ObjectBundle bundle) {
Schema schema = schemaService.getDynamicSchema(object.getClass());
if (schema == null || schema.getEmbeddedObjectProperties().isEmpty()) {
return;
}
Collection<Property> properties = schema.getEmbeddedObjectProperties().values();
clearEmbeddedObjects(persistedObject, properties);
handleEmbeddedObjects(object, bundle, properties);
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class PeriodTypeObjectBundleHook method preUpdate.
@Override
public <T extends IdentifiableObject> void preUpdate(T object, T persistedObject, ObjectBundle bundle) {
Schema schema = schemaService.getDynamicSchema(object.getClass());
for (Property property : schema.getPropertyMap().values()) {
if (PeriodType.class.isAssignableFrom(property.getKlass())) {
PeriodType periodType = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (periodType != null) {
periodType = bundle.getPreheat().getPeriodTypeMap().get(periodType.getName());
ReflectionUtils.invokeMethod(object, property.getSetterMethod(), periodType);
}
}
}
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class PeriodTypeObjectBundleHook method preCreate.
@Override
public <T extends IdentifiableObject> void preCreate(T object, ObjectBundle bundle) {
Schema schema = schemaService.getDynamicSchema(object.getClass());
for (Property property : schema.getPropertyMap().values()) {
if (PeriodType.class.isAssignableFrom(property.getKlass())) {
PeriodType periodType = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (periodType != null) {
periodType = bundle.getPreheat().getPeriodTypeMap().get(periodType.getName());
ReflectionUtils.invokeMethod(object, property.getSetterMethod(), periodType);
}
}
}
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultSchemaValidator method validate.
@Override
public List<ErrorReport> validate(Object object, boolean persisted) {
List<ErrorReport> errorReports = new ArrayList<>();
if (object == null) {
return errorReports;
}
Schema schema = schemaService.getDynamicSchema(object.getClass());
if (schema == null) {
return errorReports;
}
Class<?> klass = object.getClass();
for (Property property : schema.getProperties()) {
if (persisted && !property.isPersisted()) {
continue;
}
Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (value == null) {
if (property.isRequired() && !Preheat.isDefaultClass(property.getKlass())) {
errorReports.add(new ErrorReport(klass, ErrorCode.E4000, property.getName()).setErrorKlass(property.getKlass()).setErrorProperty(property.getName()));
}
continue;
}
errorReports.addAll(validateString(klass, value, property));
errorReports.addAll(validateCollection(klass, value, property));
errorReports.addAll(validateInteger(klass, value, property));
errorReports.addAll(validateFloat(klass, value, property));
errorReports.addAll(validateDouble(klass, value, property));
}
if (User.class.isInstance(object)) {
User user = (User) object;
if (user.getUserCredentials() != null) {
errorReports.addAll(validate(user.getUserCredentials(), persisted));
}
}
return errorReports;
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class EmbeddedObjectObjectBundleHook method handleEmbeddedObjects.
private void handleEmbeddedObjects(IdentifiableObject object, ObjectBundle bundle, Collection<Property> properties) {
for (Property property : properties) {
Object propertyObject = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (property.isCollection()) {
Collection<?> objects = (Collection<?>) propertyObject;
objects.forEach(itemPropertyObject -> {
handleProperty(itemPropertyObject, bundle, property);
handleEmbeddedAnalyticalProperty(itemPropertyObject, bundle, property);
});
} else {
handleProperty(propertyObject, bundle, property);
handleEmbeddedAnalyticalProperty(propertyObject, bundle, property);
}
}
}
Aggregations