use of org.hisp.dhis.node.types.SimpleNode in project dhis2-core by dhis2.
the class IsNotEmptyNodeTransformer method transform.
@Override
public Node transform(Node node, List<String> args) {
checkNotNull(node);
checkNotNull(node.getProperty());
Property property = node.getProperty();
if (property.isCollection()) {
return new SimpleNode(property.getCollectionName(), !node.getChildren().isEmpty(), property.isAttribute());
} else if (property.isSimple()) {
return new SimpleNode(property.getName(), !ObjectUtils.isEmpty(((SimpleNode) node).getValue()), property.isAttribute());
}
return node;
}
use of org.hisp.dhis.node.types.SimpleNode in project dhis2-core by dhis2.
the class PluckNodeTransformer method transform.
@Override
public Node transform(Node node, List<String> args) {
checkNotNull(node);
checkNotNull(node.getProperty());
Property property = node.getProperty();
if (property.isCollection()) {
final String fieldName = (args == null || args.isEmpty()) ? null : StringUtils.defaultIfEmpty(args.get(0), null);
final CollectionNode collectionNode = new CollectionNode(node.getName(), node.getUnorderedChildren().size());
collectionNode.setNamespace(node.getNamespace());
for (final Node objectNode : node.getUnorderedChildren()) {
for (final Node fieldNode : objectNode.getUnorderedChildren()) {
if (fieldNode instanceof SimpleNode && (fieldName == null || fieldName.equals(fieldNode.getName()))) {
final SimpleNode childNode = new SimpleNode(fieldNode.getName(), ((SimpleNode) fieldNode).getValue());
childNode.setProperty(collectionNode.getProperty());
collectionNode.addChild(childNode);
break;
}
}
}
return collectionNode;
}
return node;
}
use of org.hisp.dhis.node.types.SimpleNode in project dhis2-core by dhis2.
the class DefaultFieldFilterServiceTest method baseIdentifiable.
@Test
void baseIdentifiable() {
final OrganisationUnit ou1 = new OrganisationUnit();
ou1.setUid("abc1");
ou1.setName("Test 1");
final OrganisationUnit ou2 = new OrganisationUnit();
ou2.setUid("abc2");
ou2.setName("Test 2");
final CategoryOption option = new CategoryOption();
option.setUid("def1");
option.getOrganisationUnits().add(ou1);
option.getOrganisationUnits().add(ou2);
final FieldFilterParams params = new FieldFilterParams(Collections.singletonList(option), Arrays.asList("id", "organisationUnits[id,name]"));
final ComplexNode node = service.toComplexNode(params);
Assertions.assertEquals("categoryOption", node.getName());
Assertions.assertTrue(getNamedNode(node.getUnorderedChildren(), "id") instanceof SimpleNode);
Assertions.assertEquals("def1", ((SimpleNode) getNamedNode(node.getUnorderedChildren(), "id")).getValue());
Assertions.assertTrue(getNamedNode(node.getUnorderedChildren(), "organisationUnits") instanceof CollectionNode);
final CollectionNode collectionNode = (CollectionNode) getNamedNode(node.getUnorderedChildren(), "organisationUnits");
Assertions.assertEquals(2, collectionNode.getUnorderedChildren().size());
final List<String> ouIds = new ArrayList<>();
final List<String> ouNames = new ArrayList<>();
Assertions.assertTrue(collectionNode.getUnorderedChildren().get(0) instanceof ComplexNode);
ComplexNode complexNode = (ComplexNode) collectionNode.getUnorderedChildren().get(0);
Assertions.assertEquals("organisationUnit", complexNode.getName());
Assertions.assertEquals(2, complexNode.getUnorderedChildren().size());
Assertions.assertTrue(getNamedNode(complexNode.getUnorderedChildren(), "id") instanceof SimpleNode);
SimpleNode simpleNode = (SimpleNode) getNamedNode(complexNode.getUnorderedChildren(), "id");
Assertions.assertEquals("id", simpleNode.getName());
ouIds.add(String.valueOf(simpleNode.getValue()));
Assertions.assertTrue(getNamedNode(complexNode.getUnorderedChildren(), "name") instanceof SimpleNode);
simpleNode = (SimpleNode) getNamedNode(complexNode.getUnorderedChildren(), "name");
Assertions.assertEquals("name", simpleNode.getName());
ouNames.add(String.valueOf(simpleNode.getValue()));
Assertions.assertTrue(collectionNode.getUnorderedChildren().get(1) instanceof ComplexNode);
complexNode = (ComplexNode) collectionNode.getUnorderedChildren().get(1);
Assertions.assertEquals("organisationUnit", complexNode.getName());
Assertions.assertEquals(2, complexNode.getUnorderedChildren().size());
Assertions.assertTrue(getNamedNode(complexNode.getUnorderedChildren(), "id") instanceof SimpleNode);
simpleNode = (SimpleNode) getNamedNode(complexNode.getUnorderedChildren(), "id");
Assertions.assertEquals("id", simpleNode.getName());
ouIds.add(String.valueOf(simpleNode.getValue()));
Assertions.assertTrue(getNamedNode(complexNode.getUnorderedChildren(), "name") instanceof SimpleNode);
simpleNode = (SimpleNode) getNamedNode(complexNode.getUnorderedChildren(), "name");
Assertions.assertEquals("name", simpleNode.getName());
ouNames.add(String.valueOf(simpleNode.getValue()));
assertThat(ouIds, Matchers.containsInAnyOrder("abc1", "abc2"));
assertThat(ouNames, Matchers.containsInAnyOrder("Test 1", "Test 2"));
}
use of org.hisp.dhis.node.types.SimpleNode in project dhis2-core by dhis2.
the class DefaultFieldFilterServiceTest method baseIdentifiableIdOnly.
@Test
void baseIdentifiableIdOnly() {
final OrganisationUnit ou1 = new OrganisationUnit();
ou1.setUid("abc1");
final OrganisationUnit ou2 = new OrganisationUnit();
ou2.setUid("abc2");
final CategoryOption option = new CategoryOption();
option.setUid("def1");
option.getOrganisationUnits().add(ou1);
option.getOrganisationUnits().add(ou2);
final FieldFilterParams params = new FieldFilterParams(Collections.singletonList(option), Arrays.asList("id", "organisationUnits"));
final ComplexNode node = service.toComplexNode(params);
Assertions.assertEquals("categoryOption", node.getName());
Assertions.assertTrue(getNamedNode(node.getUnorderedChildren(), "id") instanceof SimpleNode);
Assertions.assertEquals("def1", ((SimpleNode) getNamedNode(node.getUnorderedChildren(), "id")).getValue());
Assertions.assertTrue(getNamedNode(node.getUnorderedChildren(), "organisationUnits") instanceof CollectionNode);
final CollectionNode collectionNode = (CollectionNode) getNamedNode(node.getUnorderedChildren(), "organisationUnits");
Assertions.assertEquals(2, collectionNode.getUnorderedChildren().size());
final List<String> ouIds = new ArrayList<>();
Assertions.assertTrue(collectionNode.getUnorderedChildren().get(0) instanceof ComplexNode);
ComplexNode complexNode = (ComplexNode) collectionNode.getUnorderedChildren().get(0);
Assertions.assertEquals("organisationUnit", complexNode.getName());
Assertions.assertEquals(1, complexNode.getUnorderedChildren().size());
Assertions.assertTrue(complexNode.getUnorderedChildren().get(0) instanceof SimpleNode);
SimpleNode simpleNode = (SimpleNode) complexNode.getUnorderedChildren().get(0);
Assertions.assertEquals("id", simpleNode.getName());
ouIds.add(String.valueOf(simpleNode.getValue()));
Assertions.assertTrue(collectionNode.getUnorderedChildren().get(1) instanceof ComplexNode);
complexNode = (ComplexNode) collectionNode.getUnorderedChildren().get(1);
Assertions.assertEquals("organisationUnit", complexNode.getName());
Assertions.assertEquals(1, complexNode.getUnorderedChildren().size());
Assertions.assertTrue(complexNode.getUnorderedChildren().get(0) instanceof SimpleNode);
simpleNode = (SimpleNode) complexNode.getUnorderedChildren().get(0);
Assertions.assertEquals("id", simpleNode.getName());
ouIds.add(String.valueOf(simpleNode.getValue()));
assertThat(ouIds, Matchers.containsInAnyOrder("abc1", "abc2"));
}
use of org.hisp.dhis.node.types.SimpleNode in project dhis2-core by dhis2.
the class DataElementOperandControllerTest method buildResponse.
private CollectionNode buildResponse(List<DataElementOperand> dataElementOperands) {
CollectionNode collectionNode = new CollectionNode("dataElementOperands");
collectionNode.setWrapping(true);
collectionNode.setNamespace("http://dhis2.org/schema/dxf/2.0");
for (DataElementOperand dataElementOperand : dataElementOperands) {
ComplexNode complexNode = new ComplexNode("dataElementOperand");
complexNode.setNamespace(collectionNode.getNamespace());
complexNode.addChild(new SimpleNode("displayName", dataElementOperand.getDisplayName(), false));
complexNode.addChild(new SimpleNode("id", dataElementOperand.getDimensionItem(), true));
collectionNode.addChild(complexNode);
}
return collectionNode;
}
Aggregations