use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.
the class UserPropertyTransformerTest method testFieldNodeServiceSerializer.
@Test
void testFieldNodeServiceSerializer() throws JsonProcessingException {
Simple simple = new Simple(1, "Simple1");
simple.setUser(createUser('a'));
simple.getUser().setUuid(uuid);
simple.getUsers().add(createUser('A'));
simple.getUsers().add(createUser('B'));
simple.getUsers().add(createUser('C'));
simple.getUsers().add(createUser('D'));
ComplexNode complexNode = nodeService.toNode(simple);
RootNode rootNode = NodeUtils.createRootNode(complexNode);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
nodeService.serialize(rootNode, "application/json", outputStream);
String jsonSource = outputStream.toString();
verifyJsonSource(jsonSource);
Simple simpleFromJson = jsonMapper.readValue(jsonSource, Simple.class);
assertEquals(1, simpleFromJson.getId());
assertEquals("Simple1", simpleFromJson.getName());
assertNotNull(simple.getUser());
assertEquals("usernamea", simple.getUser().getUsername());
assertEquals(uuid, simple.getUser().getUuid());
assertNotNull(simple.getUsers());
assertEquals(4, simple.getUsers().size());
FieldFilterParams params = new FieldFilterParams(Collections.singletonList(simple), Collections.singletonList("id,name,user[id,code],users[id,code]"));
fieldFilterService.toComplexNode(params);
}
use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.
the class UserPropertyTransformerTest method testNodeServiceSerializer.
@Test
void testNodeServiceSerializer() throws JsonProcessingException {
Simple simple = new Simple(1, "Simple1");
simple.setUser(createUser('a'));
simple.getUser().setUuid(uuid);
ComplexNode complexNode = nodeService.toNode(simple);
RootNode rootNode = NodeUtils.createRootNode(complexNode);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
nodeService.serialize(rootNode, "application/json", outputStream);
String jsonSource = outputStream.toString();
verifyJsonSource(jsonSource);
Simple simpleFromJson = jsonMapper.readValue(jsonSource, Simple.class);
assertEquals(1, simpleFromJson.getId());
assertEquals("Simple1", simpleFromJson.getName());
assertNotNull(simple.getUser());
assertEquals("usernamea", simple.getUser().getUsername());
assertEquals(uuid, simple.getUser().getUuid());
}
use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.
the class DefaultFieldFilterService method buildNode.
private AbstractNode buildNode(final FieldMap fieldMap, final Object klassInstance, final String namespace) {
final ComplexNode complexNode = new ComplexNode(decapitalize(klassInstance.getClass().getSimpleName()));
complexNode.setNamespace(namespace);
for (final String fieldKey : fieldMap.keySet()) {
try {
final String originalName = org.apache.commons.lang3.StringUtils.substringBefore(fieldKey, "~");
final String rename = org.apache.commons.lang3.StringUtils.substringBetween(fieldKey, "(", ")");
final Field field = klassInstance.getClass().getDeclaredField(originalName);
// NOSONAR
field.setAccessible(true);
final Object value = ReflectionUtils.invokeGetterMethod(originalName, klassInstance);
if (org.apache.commons.lang3.StringUtils.isNotBlank(rename)) {
complexNode.addChild(new SimpleNode(rename, value));
} else {
complexNode.addChild(new SimpleNode(originalName, value));
}
} catch (NoSuchFieldException e) {
log.warn("Error reading attribute", e);
}
}
return complexNode;
}
use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.
the class DefaultFieldFilterService method toComplexNode.
@Override
public ComplexNode toComplexNode(FieldFilterParams params) {
if (params.getObjects().isEmpty()) {
return null;
}
Object object = params.getObjects().get(0);
CollectionNode collectionNode = toCollectionNode(object.getClass(), params);
if (!collectionNode.getChildren().isEmpty()) {
return (ComplexNode) collectionNode.getChildren().get(0);
}
return null;
}
use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.
the class DefaultFieldFilterService method getProperties.
private ComplexNode getProperties(Property currentProperty, Object object, List<String> fields) {
if (object == null) {
return null;
}
// objects
if (isBaseIdentifiableObjectIdOnly(object, fields)) {
return createBaseIdentifiableObjectIdNode(currentProperty, object);
}
ComplexNode complexNode = new ComplexNode(currentProperty.getName());
complexNode.setNamespace(currentProperty.getNamespace());
complexNode.setProperty(currentProperty);
Schema schema;
if (currentProperty.hasPropertyTransformer()) {
schema = schemaService.getDynamicSchema(HibernateProxyUtils.getRealClass(object));
} else if (currentProperty.isCollection()) {
schema = schemaService.getDynamicSchema(currentProperty.getItemKlass());
} else {
schema = schemaService.getDynamicSchema(currentProperty.getKlass());
}
for (String field : fields) {
Property property = schema.getProperty(field);
if (property == null) {
continue;
}
Object returnValue = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
SimpleNode simpleNode = new SimpleNode(field, returnValue);
simpleNode.setAttribute(property.isAttribute());
simpleNode.setNamespace(property.getNamespace());
simpleNode.setProperty(property);
complexNode.addChild(simpleNode);
}
return complexNode;
}
Aggregations