use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class TestUtils method getFieldDescriptors.
public static Set<FieldDescriptor> getFieldDescriptors(Schema schema) {
Set<FieldDescriptor> fieldDescriptors = new HashSet<>();
Map<String, Property> persistedPropertyMap = schema.getPersistedProperties();
Iterator<?> persistedItr = persistedPropertyMap.keySet().iterator();
while (persistedItr.hasNext()) {
Property p = persistedPropertyMap.get(persistedItr.next());
String pName = p.isCollection() ? p.getCollectionName() : p.getName();
FieldDescriptor f = fieldWithPath(pName).description(TestUtils.getFieldDescription(p));
if (!p.isRequired()) {
f.optional().type(p.getPropertyType());
}
fieldDescriptors.add(f);
}
Map<String, Property> nonPersistedPropertyMap = schema.getNonPersistedProperties();
Iterator<?> nonPersistedItr = nonPersistedPropertyMap.keySet().iterator();
while (nonPersistedItr.hasNext()) {
Property p = nonPersistedPropertyMap.get(nonPersistedItr.next());
String pName = p.isCollection() ? p.getCollectionName() : p.getName();
FieldDescriptor f = fieldWithPath(pName).description(TestUtils.getFieldDescription(p));
if (!p.isRequired()) {
f.optional().type(p.getPropertyType());
}
fieldDescriptors.add(f);
}
return fieldDescriptors;
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class OrderParams method getOrders.
public List<Order> getOrders(Schema schema) {
Map<String, Order> orders = new LinkedHashMap<>();
for (String o : order) {
String[] split = o.split(":");
//Using ascending as default direction.
String direction = "asc";
if (split.length < 1) {
continue;
} else if (split.length == 2) {
direction = split[1].toLowerCase();
}
String propertyName = split[0];
Property property = schema.getProperty(propertyName);
if (orders.containsKey(propertyName) || !schema.haveProperty(propertyName) || !validProperty(property) || !validDirection(direction)) {
continue;
}
orders.put(propertyName, Order.from(direction, property));
}
return new ArrayList<>(orders.values());
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultCollectionService method clearCollectionItems.
@Override
@SuppressWarnings("unchecked")
public void clearCollectionItems(IdentifiableObject object, String pvProperty) throws WebMessageException, InvocationTargetException, IllegalAccessException {
Schema schema = schemaService.getDynamicSchema(object.getClass());
if (!schema.haveProperty(pvProperty)) {
throw new WebMessageException(WebMessageUtils.notFound("Property " + pvProperty + " does not exist on " + object.getClass().getName()));
}
Property property = schema.getProperty(pvProperty);
if (!property.isCollection() || !property.isIdentifiableObject()) {
throw new WebMessageException(WebMessageUtils.conflict("Only identifiable collections are allowed to be cleared."));
}
Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke(object);
manager.refresh(object);
if (property.isOwner()) {
collection.clear();
manager.update(object);
} else {
for (IdentifiableObject itemObject : collection) {
Schema itemSchema = schemaService.getDynamicSchema(property.getItemKlass());
Property itemProperty = itemSchema.propertyByRole(property.getOwningRole());
Collection<IdentifiableObject> itemCollection = (Collection<IdentifiableObject>) itemProperty.getGetterMethod().invoke(itemObject);
itemCollection.remove(object);
manager.update(itemObject);
manager.refresh(itemObject);
}
}
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultCollectionService method addCollectionItems.
@Override
@SuppressWarnings("unchecked")
public void addCollectionItems(IdentifiableObject object, String propertyName, List<IdentifiableObject> objects) throws Exception {
Schema schema = schemaService.getDynamicSchema(object.getClass());
if (!aclService.canUpdate(currentUserService.getCurrentUser(), object)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
if (!schema.haveProperty(propertyName)) {
throw new WebMessageException(WebMessageUtils.notFound("Property " + propertyName + " does not exist on " + object.getClass().getName()));
}
Property property = schema.getProperty(propertyName);
if (!property.isCollection() || !property.isIdentifiableObject()) {
throw new WebMessageException(WebMessageUtils.conflict("Only identifiable object collections can be added to."));
}
Collection<String> itemCodes = objects.stream().map(IdentifiableObject::getUid).collect(Collectors.toList());
if (itemCodes.isEmpty()) {
return;
}
List<? extends IdentifiableObject> items = manager.get(((Class<? extends IdentifiableObject>) property.getItemKlass()), itemCodes);
manager.refresh(object);
if (property.isOwner()) {
Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke(object);
for (IdentifiableObject item : items) {
if (!collection.contains(item))
collection.add(item);
}
manager.update(object);
} else {
Schema owningSchema = schemaService.getDynamicSchema(property.getItemKlass());
Property owningProperty = owningSchema.propertyByRole(property.getOwningRole());
for (IdentifiableObject item : items) {
try {
Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) owningProperty.getGetterMethod().invoke(item);
if (!collection.contains(object)) {
collection.add(object);
manager.update(item);
}
} catch (Exception ex) {
}
}
}
dbmsManager.clearSession();
cacheManager.clearCache();
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkUniqueness.
private List<ErrorReport> checkUniqueness(Class<? extends IdentifiableObject> klass, IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier) {
List<ErrorReport> errorReports = new ArrayList<>();
if (object == null || Preheat.isDefault(object))
return errorReports;
if (!preheat.getUniquenessMap().containsKey(object.getClass())) {
preheat.getUniquenessMap().put(object.getClass(), new HashMap<>());
}
Map<String, Map<Object, String>> uniquenessMap = preheat.getUniquenessMap().get(object.getClass());
Schema schema = schemaService.getDynamicSchema(object.getClass());
List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
uniqueProperties.forEach(property -> {
if (!uniquenessMap.containsKey(property.getName())) {
uniquenessMap.put(property.getName(), new HashMap<>());
}
Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (value != null) {
String persistedUid = uniquenessMap.get(property.getName()).get(value);
if (persistedUid != null) {
if (!object.getUid().equals(persistedUid)) {
errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E5003, property.getName(), value, identifier.getIdentifiersWithName(object), persistedUid).setMainId(persistedUid).setErrorProperty(property.getName()));
}
} else {
uniquenessMap.get(property.getName()).put(value, object.getUid());
}
}
});
return errorReports;
}
Aggregations