use of org.hisp.dhis.schema.Schema in project dhis2-core by dhis2.
the class DefaultFieldFilterService method updateFields.
private void updateFields(FieldMap fieldMap, Class<?> klass, boolean expandOnly) {
Schema schema = schemaService.getDynamicSchema(klass);
List<String> cleanupFields = Lists.newArrayList();
for (String fieldKey : Sets.newHashSet(fieldMap.keySet())) {
Collection<Property> properties = schema.getReadableProperties().values();
if ("*".equals(fieldKey)) {
properties.stream().filter(property -> !fieldMap.containsKey(property.key())).forEach(property -> fieldMap.put(property.key(), new FieldMap()));
cleanupFields.add(fieldKey);
} else if (":persisted".equals(fieldKey)) {
properties.stream().filter(property -> !fieldMap.containsKey(property.key()) && property.isPersisted()).forEach(property -> fieldMap.put(property.key(), new FieldMap()));
cleanupFields.add(fieldKey);
} else if (":owner".equals(fieldKey)) {
properties.stream().filter(property -> !fieldMap.containsKey(property.key()) && property.isPersisted() && property.isOwner()).forEach(property -> fieldMap.put(property.key(), new FieldMap()));
cleanupFields.add(fieldKey);
} else if (fieldKey.startsWith(":")) {
Preset preset = presets.get(fieldKey.substring(1));
if (preset == null) {
continue;
}
List<String> fields = preset.getFields();
fields.stream().filter(field -> !fieldMap.containsKey(field)).forEach(field -> fieldMap.put(field, new FieldMap()));
cleanupFields.add(fieldKey);
} else if (fieldKey.startsWith("!") && !expandOnly) {
cleanupFields.add(fieldKey);
} else if (fieldKey.contains("::") || fieldKey.contains("|") || fieldKey.contains("~")) {
Matcher matcher = FIELD_PATTERN.matcher(fieldKey);
if (!matcher.find()) {
continue;
}
String fieldName = matcher.group("field");
FieldMap value = new FieldMap();
matcher = TRANSFORMER_PATTERN.matcher(fieldKey);
while (matcher.find()) {
String nameMatch = matcher.group("name");
String argsMatch = matcher.group("args");
if (transformers.containsKey(nameMatch)) {
NodeTransformer transformer = transformers.get(nameMatch);
List<String> args = argsMatch == null ? new ArrayList<>() : Lists.newArrayList(argsMatch.split(";"));
value.getPipeline().addTransformer(transformer, args);
}
}
fieldMap.put(fieldName, value);
cleanupFields.add(fieldKey);
}
}
for (String field : cleanupFields) {
fieldMap.remove(field);
if (!expandOnly) {
fieldMap.remove(field.substring(1));
}
}
}
use of org.hisp.dhis.schema.Schema 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;
}
ComplexNode complexNode = new ComplexNode(currentProperty.getName());
complexNode.setNamespace(currentProperty.getNamespace());
complexNode.setProperty(currentProperty);
Schema schema;
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;
}
use of org.hisp.dhis.schema.Schema in project dhis2-core by dhis2.
the class DefaultFieldFilterService method filter.
@Override
public CollectionNode filter(Class<?> klass, List<?> objects, List<String> fieldList) {
String fields = fieldList == null ? "" : Joiner.on(",").join(fieldList);
Schema rootSchema = schemaService.getDynamicSchema(klass);
CollectionNode collectionNode = new CollectionNode(rootSchema.getCollectionName());
collectionNode.setNamespace(rootSchema.getNamespace());
if (objects == null || objects.isEmpty()) {
return collectionNode;
}
FieldMap fieldMap = new FieldMap();
Schema schema = schemaService.getDynamicSchema(objects.get(0).getClass());
if (StringUtils.isEmpty(fields)) {
for (Property property : schema.getProperties()) {
fieldMap.put(property.getName(), new FieldMap());
}
} else {
fieldMap = fieldParser.parse(fields);
}
final FieldMap finalFieldMap = fieldMap;
objects.forEach(object -> collectionNode.addChild(buildNode(finalFieldMap, klass, object)));
return collectionNode;
}
use of org.hisp.dhis.schema.Schema 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.schema.Schema in project dhis2-core by dhis2.
the class HibernateMinMaxDataElementStore method parseFilter.
private Criteria parseFilter(Criteria criteria, List<String> filters) {
Conjunction conjunction = Restrictions.conjunction();
Schema schema = schemaService.getDynamicSchema(MinMaxDataElement.class);
if (!filters.isEmpty()) {
for (String filter : filters) {
String[] split = filter.split(":");
if (split.length != 3) {
throw new QueryParserException("Invalid filter: " + filter);
}
QueryPath queryPath = queryPlanner.getQueryPath(schema, split[0]);
Property property = queryParser.getProperty(schema, split[0]);
Criterion restriction = getRestriction(property, queryPath.getPath(), split[1], split[2]);
if (restriction != null) {
conjunction.add(restriction);
if (queryPath.haveAlias()) {
for (String alias : queryPath.getAlias()) {
criteria.createAlias(alias, alias);
}
}
}
}
}
criteria.add(conjunction);
return criteria;
}
Aggregations