use of org.hisp.dhis.node.AbstractNode in project dhis2-core by dhis2.
the class DefaultFieldFilterService method buildNode.
private AbstractNode buildNode(FieldMap fieldMap, Class<?> klass, Object object, String nodeName) {
Schema schema = schemaService.getDynamicSchema(klass);
ComplexNode complexNode = new ComplexNode(nodeName);
complexNode.setNamespace(schema.getNamespace());
if (object == null) {
return new SimpleNode(schema.getName(), null);
}
updateFields(fieldMap, schema.getKlass());
for (String fieldKey : fieldMap.keySet()) {
AbstractNode child;
Property property = schema.getProperty(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());
Schema propertySchema = schemaService.getDynamicSchema(property.getKlass());
FieldMap fieldValue = fieldMap.get(fieldKey);
if (returnValue == null && property.isCollection()) {
continue;
}
if (property.isCollection()) {
updateFields(fieldValue, property.getItemKlass());
} else {
updateFields(fieldValue, property.getKlass());
}
if (fieldValue.isEmpty()) {
List<String> fields = Preset.defaultAssociationPreset().getFields();
if (property.isCollection()) {
Collection<?> collection = (Collection<?>) returnValue;
child = new CollectionNode(property.getCollectionName());
child.setNamespace(property.getNamespace());
if (property.isIdentifiableObject() && isProperIdObject(property.getItemKlass())) {
for (Object collectionObject : collection) {
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);
if (!node.getChildren().isEmpty()) {
child.addChild(node);
}
}
} else {
if (collection != null) {
for (Object collectionObject : collection) {
SimpleNode simpleNode = child.addChild(new SimpleNode(property.getName(), collectionObject));
simpleNode.setProperty(property);
}
}
}
} else if (property.isIdentifiableObject() && isProperIdObject(property.getKlass())) {
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), property.getKlass(), returnValue);
}
}
} else {
if (property.isCollection()) {
child = new CollectionNode(property.getCollectionName());
child.setNamespace(property.getNamespace());
for (Object collectionObject : (Collection<?>) returnValue) {
Node node = buildNode(fieldValue, property.getItemKlass(), collectionObject, property.getName());
if (!node.getChildren().isEmpty()) {
child.addChild(node);
}
}
} else {
child = buildNode(fieldValue, property.getKlass(), returnValue);
}
}
if (child != null) {
child.setName(fieldKey);
child.setProperty(property);
// TODO fix ugly hack, will be replaced by custom field serializer/deserializer
if (child.isSimple() && PeriodType.class.isInstance((((SimpleNode) child).getValue()))) {
child = new SimpleNode(child.getName(), ((PeriodType) ((SimpleNode) child).getValue()).getName());
}
complexNode.addChild(fieldValue.getPipeline().process(child));
}
}
return complexNode;
}
use of org.hisp.dhis.node.AbstractNode in project dhis2-core by dhis2.
the class DimensionController method getItems.
@SuppressWarnings("unchecked")
@RequestMapping(value = "/{uid}/items", method = RequestMethod.GET)
@ResponseBody
public RootNode getItems(@PathVariable String uid, @RequestParam Map<String, String> parameters, Model model, HttpServletRequest request, HttpServletResponse response) throws QueryParserException {
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
if (fields.isEmpty()) {
fields.addAll(Preset.defaultPreset().getFields());
}
List<DimensionalItemObject> items = dimensionService.getCanReadDimensionItems(uid);
Query query = queryService.getQueryFromUrl(getEntityClass(), filters, new ArrayList<>());
query.setObjects(items);
query.setDefaultOrder();
items = (List<DimensionalItemObject>) queryService.query(query);
RootNode rootNode = NodeUtils.createMetadata();
CollectionNode collectionNode = rootNode.addChild(fieldFilterService.filter(getEntityClass(), items, fields));
collectionNode.setName("items");
for (Node node : collectionNode.getChildren()) {
((AbstractNode) node).setName("item");
}
return rootNode;
}
use of org.hisp.dhis.node.AbstractNode in project dhis2-core by dhis2.
the class PagingNodeTransformer method transform.
@Override
public Node transform(Node node, List<String> args) {
checkNotNull(node);
checkArgument(!args.isEmpty(), "paging requires at least one parameter, .e.g: property|paging(page, pageSize)");
checkNotNull(node.getProperty());
int page;
int pageSize = Pager.DEFAULT_PAGE_SIZE;
try {
page = Integer.parseInt(args.get(0));
if (args.size() > 1) {
pageSize = Integer.parseInt(args.get(1));
}
} catch (NumberFormatException ex) {
return node;
}
Property property = node.getProperty();
if (property.isCollection()) {
Pager pager = new Pager(page, node.getChildren().size(), pageSize);
((AbstractNode) node).setChildren(PagerUtils.pageCollection(node.getChildren(), pager));
return node;
}
return node;
}
Aggregations