use of org.hisp.dhis.schema.Property 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.schema.Property 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.Property 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;
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultCollectionService method delCollectionItems.
@Override
@SuppressWarnings("unchecked")
public void delCollectionItems(IdentifiableObject object, String propertyName, List<IdentifiableObject> objects) throws Exception {
Schema schema = schemaService.getDynamicSchema(object.getClass());
if (!aclService.canUpdate(currentUserService.getCurrentUser(), object)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
if (!schema.haveProperty(propertyName)) {
throw new WebMessageException(WebMessageUtils.notFound("Property " + propertyName + " does not exist on " + object.getClass().getName()));
}
Property property = schema.getProperty(propertyName);
if (!property.isCollection() || !property.isIdentifiableObject()) {
throw new WebMessageException(WebMessageUtils.conflict("Only identifiable object collections can be removed from."));
}
Collection<String> itemCodes = objects.stream().map(IdentifiableObject::getUid).collect(Collectors.toList());
if (itemCodes.isEmpty()) {
return;
}
List<? extends IdentifiableObject> items = manager.get(((Class<? extends IdentifiableObject>) property.getItemKlass()), itemCodes);
manager.refresh(object);
if (property.isOwner()) {
Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke(object);
for (IdentifiableObject item : items) {
if (collection.contains(item))
collection.remove(item);
}
} else {
Schema owningSchema = schemaService.getDynamicSchema(property.getItemKlass());
Property owningProperty = owningSchema.propertyByRole(property.getOwningRole());
for (IdentifiableObject item : items) {
try {
Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) owningProperty.getGetterMethod().invoke(item);
if (collection.contains(object)) {
collection.remove(object);
manager.update(item);
}
} catch (Exception ex) {
}
}
}
manager.update(object);
dbmsManager.clearSession();
cacheManager.clearCache();
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultQueryParser method getProperty.
@Override
public Property getProperty(Schema schema, String path) throws QueryParserException {
String[] paths = path.split("\\.");
Schema currentSchema = schema;
Property currentProperty = null;
for (int i = 0; i < paths.length; i++) {
if (!currentSchema.haveProperty(paths[i])) {
return null;
}
currentProperty = currentSchema.getProperty(paths[i]);
if (currentProperty == null) {
throw new QueryParserException("Unknown path property: " + paths[i] + " (" + path + ")");
}
if ((currentProperty.isSimple() && !currentProperty.isCollection()) && i != (paths.length - 1)) {
throw new QueryParserException("Simple type was found before finished parsing path expression, please check your path string.");
}
if (currentProperty.isCollection()) {
currentSchema = schemaService.getDynamicSchema(currentProperty.getItemKlass());
} else {
currentSchema = schemaService.getDynamicSchema(currentProperty.getKlass());
}
}
return currentProperty;
}
Aggregations