use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.
the class DefaultFieldFilterService method buildNode.
private AbstractNode buildNode(FieldMap fieldMap, Class<?> klass, Object object, User user, String nodeName, Defaults defaults) {
Schema schema = schemaService.getDynamicSchema(klass);
ComplexNode complexNode = new ComplexNode(nodeName);
complexNode.setNamespace(schema.getNamespace());
if (object == null) {
return new SimpleNode(schema.getName(), null);
}
if (shouldExclude(object, defaults)) {
return null;
}
updateFields(fieldMap, schema.getKlass());
if (fieldMap.containsKey("access") && schema.isIdentifiableObject()) {
Access access = aclService.getAccess((IdentifiableObject) object, user);
((BaseIdentifiableObject) object).setAccess(access);
}
if (fieldMap.containsKey("attribute") && AttributeValue.class.isAssignableFrom(object.getClass())) {
AttributeValue attributeValue = (AttributeValue) object;
attributeValue.setAttribute(attributeService.getAttribute(attributeValue.getAttribute().getUid()));
}
if (UserGroupAccess.class.isAssignableFrom(object.getClass())) {
UserGroupAccess userGroupAccess = (UserGroupAccess) object;
userGroupAccess.setDisplayName(userGroupService.getDisplayName(userGroupAccess.getUserGroupUid()));
}
if (UserAccess.class.isAssignableFrom(object.getClass())) {
UserAccess userAccess = (UserAccess) object;
userAccess.setDisplayName(userService.getDisplayName(userAccess.getUserUid()));
}
for (String fieldKey : fieldMap.keySet()) {
AbstractNode child = null;
Property property = schema.getProperty(fieldKey);
FieldMap fieldValue = fieldMap.get(fieldKey);
if (property == null || !property.isReadable()) {
// throw new FieldFilterException( fieldKey, schema );
log.debug("Unknown field property `" + fieldKey + "`, available fields are " + schema.getPropertyMap().keySet());
continue;
}
Object returnValue = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
Class<?> propertyClass = property.getKlass();
Schema propertySchema = schemaService.getDynamicSchema(propertyClass);
if (property.hasPropertyTransformer()) {
PropertyTransformer propertyTransformer = transformerCache.get(property.getPropertyTransformer().getName(), s -> {
try {
return property.getPropertyTransformer().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
});
if (returnValue != null) {
returnValue = propertyTransformer.transform(returnValue);
propertyClass = returnValue.getClass();
propertySchema = schemaService.getDynamicSchema(propertyClass);
updateFields(fieldValue, propertyTransformer.getKlass());
}
}
if (returnValue != null && propertySchema.getProperties().isEmpty() && !property.isCollection() && property.getKlass().isInterface() && !property.isIdentifiableObject()) {
// try to retrieve schema from concrete class
propertyClass = returnValue.getClass();
propertySchema = schemaService.getDynamicSchema(propertyClass);
}
if (returnValue == null && property.isCollection()) {
continue;
}
if (property.isCollection()) {
updateFields(fieldValue, property.getItemKlass());
} else {
updateFields(fieldValue, propertyClass);
}
if (fieldValue.isEmpty()) {
List<String> fields = Preset.defaultAssociationPreset().getFields();
if (property.isCollection()) {
Collection<?> collection = (Collection<?>) returnValue;
child = new CollectionNode(property.getCollectionName(), collection.size());
child.setNamespace(property.getNamespace());
if (property.isIdentifiableObject() && isProperIdObject(property.getItemKlass())) {
final boolean mayExclude = collection.isEmpty() || mayExclude(property.getItemKlass(), defaults);
for (Object collectionObject : collection) {
if (!mayExclude || !shouldExclude(collectionObject, defaults)) {
child.addChild(getProperties(property, collectionObject, fields));
}
}
} else if (!property.isSimple()) {
FieldMap map = getFullFieldMap(schemaService.getDynamicSchema(property.getItemKlass()));
for (Object collectionObject : collection) {
Node node = buildNode(map, property.getItemKlass(), collectionObject, user, defaults);
if (node != null && !node.getChildren().isEmpty()) {
child.addChild(node);
}
}
} else {
for (Object collectionObject : collection) {
SimpleNode simpleNode = child.addChild(new SimpleNode(property.getName(), collectionObject));
simpleNode.setProperty(property);
}
}
} else if (property.isIdentifiableObject() && isProperIdObject(propertyClass)) {
if (!shouldExclude(returnValue, defaults)) {
child = getProperties(property, returnValue, fields);
}
} else {
if (propertySchema.getProperties().isEmpty()) {
SimpleNode simpleNode = new SimpleNode(fieldKey, returnValue);
simpleNode.setAttribute(property.isAttribute());
simpleNode.setNamespace(property.getNamespace());
child = simpleNode;
} else {
child = buildNode(getFullFieldMap(propertySchema), propertyClass, returnValue, user, defaults);
}
}
} else {
if (property.isCollection()) {
child = new CollectionNode(property.getCollectionName());
child.setNamespace(property.getNamespace());
for (Object collectionObject : (Collection<?>) Objects.requireNonNull(returnValue)) {
Node node;
if (property.hasPropertyTransformer()) {
// if it has a transformer, re-get the schema (the
// item klass has probably changed)
Schema sch = schemaService.getDynamicSchema(HibernateProxyUtils.getRealClass(collectionObject));
node = buildNode(fieldValue, sch.getKlass(), collectionObject, user, property.getName(), defaults);
} else {
node = buildNode(fieldValue, property.getItemKlass(), collectionObject, user, property.getName(), defaults);
}
if (!Objects.requireNonNull(node).getChildren().isEmpty()) {
child.addChild(node);
}
}
} else {
returnValue = handleJsonbObjectProperties(klass, propertyClass, returnValue);
child = buildNode(fieldValue, propertyClass, returnValue, user, defaults);
}
}
if (child != null) {
child.setName(fieldKey);
child.setProperty(property);
// serializer/deserializer
if (child.isSimple() && (((SimpleNode) child).getValue()) instanceof PeriodType) {
child = new SimpleNode(child.getName(), ((PeriodType) ((SimpleNode) child).getValue()).getName());
}
complexNode.addChild(fieldValue.getPipeline().process(child));
}
}
return complexNode;
}
use of org.hisp.dhis.node.types.ComplexNode 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.ComplexNode 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.ComplexNode in project dhis2-core by dhis2.
the class NodeUtils method createPager.
public static Node createPager(Pager pager) {
ComplexNode pagerNode = new ComplexNode("pager");
pagerNode.setMetadata(true);
pagerNode.addChild(new SimpleNode("page", pager.getPage()));
pagerNode.addChild(new SimpleNode("pageCount", pager.getPageCount()));
pagerNode.addChild(new SimpleNode("total", pager.getTotal()));
pagerNode.addChild(new SimpleNode("pageSize", pager.getPageSize()));
pagerNode.addChild(new SimpleNode("nextPage", pager.getNextPage()));
pagerNode.addChild(new SimpleNode("prevPage", pager.getPrevPage()));
return pagerNode;
}
use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.
the class AbstractFullReadOnlyController method getObjectInternal.
@SuppressWarnings("unchecked")
private RootNode getObjectInternal(String uid, Map<String, String> parameters, List<String> filters, List<String> fields, User currentUser) throws Exception {
WebOptions options = new WebOptions(parameters);
List<T> entities = getEntity(uid, options);
if (entities.isEmpty()) {
throw new WebMessageException(notFound(getEntityClass(), uid));
}
Query query = queryService.getQueryFromUrl(getEntityClass(), filters, new ArrayList<>(), getPaginationData(options), options.getRootJunction());
query.setUser(currentUser);
query.setObjects(entities);
query.setDefaults(Defaults.valueOf(options.get("defaults", DEFAULTS)));
entities = (List<T>) queryService.query(query);
handleLinksAndAccess(entities, fields, true);
handleAttributeValues(entities, fields);
for (T entity : entities) {
postProcessResponseEntity(entity, options, parameters);
}
CollectionNode collectionNode = fieldFilterService.toCollectionNode(getEntityClass(), new FieldFilterParams(entities, fields, Defaults.valueOf(options.get("defaults", DEFAULTS))).setUser(currentUser));
if (options.isTrue("useWrapper") || entities.size() > 1) {
RootNode rootNode = NodeUtils.createMetadata(collectionNode);
rootNode.getConfig().setInclusionStrategy(getInclusionStrategy(parameters.get("inclusionStrategy")));
return rootNode;
} else {
List<Node> children = collectionNode.getChildren();
RootNode rootNode;
if (!children.isEmpty()) {
rootNode = NodeUtils.createRootNode(children.get(0));
} else {
rootNode = NodeUtils.createRootNode(new ComplexNode(getSchema().getSingular()));
}
rootNode.getConfig().setInclusionStrategy(getInclusionStrategy(parameters.get("inclusionStrategy")));
return rootNode;
}
}
Aggregations