use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultPreheatService method handleUniqueProperties.
private Map<String, Map<Object, String>> handleUniqueProperties(Schema schema, List<IdentifiableObject> objects) {
List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
Map<String, Map<Object, String>> map = new HashMap<>();
for (IdentifiableObject object : objects) {
uniqueProperties.forEach(property -> {
if (!map.containsKey(property.getName()))
map.put(property.getName(), new HashMap<>());
Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (value != null)
map.get(property.getName()).put(value, object.getUid());
});
}
return map;
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class AbstractCrudController method partialUpdateObject.
@RequestMapping(value = "/{uid}", method = RequestMethod.PATCH)
public void partialUpdateObject(@PathVariable("uid") String pvUid, @RequestParam Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws Exception {
WebOptions options = new WebOptions(rpParameters);
List<T> entities = getEntity(pvUid, options);
if (entities.isEmpty()) {
throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), pvUid));
}
T persistedObject = entities.get(0);
User user = currentUserService.getCurrentUser();
if (!aclService.canUpdate(user, persistedObject)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
String payload = StreamUtils.copyToString(request.getInputStream(), Charset.forName("UTF-8"));
List<String> properties = new ArrayList<>();
T object = null;
if (isJson(request)) {
properties = getJsonProperties(payload);
object = renderService.fromJson(payload, getEntityClass());
} else if (isXml(request)) {
properties = getXmlProperties(payload);
object = renderService.fromXml(payload, getEntityClass());
}
prePatchEntity(persistedObject, object);
properties = getPersistedProperties(properties);
if (properties.isEmpty() || object == null) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
return;
}
Schema schema = getSchema();
for (String keyProperty : properties) {
Property property = schema.getProperty(keyProperty);
Object value = property.getGetterMethod().invoke(object);
property.getSetterMethod().invoke(persistedObject, value);
}
manager.update(persistedObject);
postPatchEntity(persistedObject);
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DataElementCategoryControllerDocumentation method testAddDeleteCollectionItem.
@Test
public void testAddDeleteCollectionItem() throws Exception {
MockHttpSession session = getSession("ALL");
DataElementCategory category = createDataElementCategory('A');
manager.save(category);
Schema schema = schemaService.getSchema(DataElementCategory.class);
List<Property> properties = schema.getProperties();
for (Property property : properties) {
if (property.isCollection()) {
String collectionName = property.getCollectionName();
IdentifiableObject item = createTestObject(property.getItemKlass(), 'A', category);
if (item == null) {
continue;
} else {
manager.save(item);
}
mvc.perform(post("/" + ENDPOINT + "/" + category.getUid() + "/" + collectionName + "/" + item.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andDo(documentPrettyPrint(ENDPOINT + "/add" + collectionName)).andExpect(status().isNoContent());
mvc.perform(delete("/" + ENDPOINT + "/" + category.getUid() + "/" + collectionName + "/" + item.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andDo(documentPrettyPrint(ENDPOINT + "/delete" + collectionName)).andExpect(status().isNoContent());
}
}
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DataElementControllerDocumentation method testAddDeleteCollectionItem.
@Test
public void testAddDeleteCollectionItem() throws Exception {
MockHttpSession session = getSession("ALL");
DataElement de = createDataElement('A');
manager.save(de);
Schema schema = schemaService.getSchema(DataElement.class);
List<Property> properties = schema.getProperties();
for (Property property : properties) {
if (property.isCollection()) {
String collectionName = property.getCollectionName();
IdentifiableObject item = createTestObject(property.getItemKlass(), 'A');
if (item == null) {
continue;
} else {
manager.save(item);
}
mvc.perform(post("/dataElements/" + de.getUid() + "/" + collectionName + "/" + item.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andDo(documentPrettyPrint("data-elements/add" + collectionName)).andExpect(status().isNoContent());
mvc.perform(delete("/dataElements/" + de.getUid() + "/" + collectionName + "/" + item.getUid()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andDo(documentPrettyPrint("data-elements/delete" + collectionName)).andExpect(status().isNoContent());
}
}
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class EmbeddedObjectObjectBundleHook method handleEmbeddedObjects.
private <T extends IdentifiableObject> void handleEmbeddedObjects(T object, ObjectBundle bundle, Collection<Property> properties) {
for (Property property : properties) {
if (property.isCollection()) {
Collection<?> objects = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
objects.forEach(o -> {
if (property.isIdentifiableObject()) {
((BaseIdentifiableObject) o).setAutoFields();
}
preheatService.connectReferences(o, bundle.getPreheat(), bundle.getPreheatIdentifier());
});
} else {
Object o = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (property.isIdentifiableObject()) {
((BaseIdentifiableObject) o).setAutoFields();
}
preheatService.connectReferences(o, bundle.getPreheat(), bundle.getPreheatIdentifier());
}
}
}
Aggregations