use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.
the class DefaultFieldFilterService method filter.
@Override
public ComplexNode filter(Object object, List<String> fieldList) {
Assert.notNull(object, "Object cannot be null");
CollectionNode collectionNode = filter(object.getClass(), Lists.newArrayList(object), fieldList);
if (collectionNode.getChildren().size() > 0) {
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 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.types.ComplexNode in project dhis2-core by dhis2.
the class IndexController method createRootNode.
private RootNode createRootNode() {
RootNode rootNode = NodeUtils.createMetadata();
CollectionNode collectionNode = rootNode.addChild(new CollectionNode("resources"));
for (Schema schema : schemaService.getSchemas()) {
if (schema.haveApiEndpoint()) {
ComplexNode complexNode = collectionNode.addChild(new ComplexNode("resource"));
// TODO add i18n to this
complexNode.addChild(new SimpleNode("displayName", beautify(schema.getPlural())));
complexNode.addChild(new SimpleNode("singular", schema.getSingular()));
complexNode.addChild(new SimpleNode("plural", schema.getPlural()));
complexNode.addChild(new SimpleNode("href", contextService.getApiPath() + schema.getRelativeApiEndpoint()));
}
}
return rootNode;
}
use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.
the class ExcelNodeSerializer method startWriteRootNode.
@Override
protected void startWriteRootNode(RootNode rootNode) throws Exception {
XSSFCreationHelper creationHelper = workbook.getCreationHelper();
int rowIdx = 1;
for (Node collectionNode : rootNode.getChildren()) {
if (collectionNode.isCollection()) {
for (Node complexNode : collectionNode.getChildren()) {
XSSFRow row = sheet.createRow(rowIdx++);
int cellIdx = 0;
for (Node node : complexNode.getChildren()) {
if (node.isSimple()) {
XSSFCell cell = row.createCell(cellIdx++);
cell.setCellValue(getValue((SimpleNode) node));
if (node.haveProperty() && PropertyType.URL.equals(node.getProperty().getPropertyType())) {
XSSFHyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
hyperlink.setAddress(getValue((SimpleNode) node));
hyperlink.setLabel(getValue((SimpleNode) node));
cell.setHyperlink(hyperlink);
} else if (node.haveProperty() && PropertyType.EMAIL.equals(node.getProperty().getPropertyType())) {
XSSFHyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.EMAIL);
hyperlink.setAddress(getValue((SimpleNode) node));
hyperlink.setLabel(getValue((SimpleNode) node));
cell.setHyperlink(hyperlink);
}
}
}
}
}
}
}
use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.
the class DefaultMetadataVersionService method getMetadataVersionsAsNode.
@Override
public RootNode getMetadataVersionsAsNode(List<MetadataVersion> versions) {
RootNode rootNode = NodeUtils.createRootNode("metadataversions");
CollectionNode collectionNode = new CollectionNode("metadataversions", true);
rootNode.addChild(collectionNode);
for (MetadataVersion version : versions) {
ComplexNode complexNode = new ComplexNode("");
complexNode.addChild(new SimpleNode("name", version.getName()));
complexNode.addChild(new SimpleNode("type", version.getType()));
complexNode.addChild(new SimpleNode("created", version.getCreated()));
complexNode.addChild(new SimpleNode("id", version.getUid()));
complexNode.addChild(new SimpleNode("importdate", version.getImportDate()));
complexNode.addChild(new SimpleNode("hashCode", version.getHashCode()));
collectionNode.addChild(complexNode);
}
return rootNode;
}
Aggregations