Search in sources :

Example 1 with Property

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;
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) AbstractNode(org.hisp.dhis.node.AbstractNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) Schema(org.hisp.dhis.schema.Schema) CollectionNode(org.hisp.dhis.node.types.CollectionNode) AbstractNode(org.hisp.dhis.node.AbstractNode) Node(org.hisp.dhis.node.Node) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) CollectionNode(org.hisp.dhis.node.types.CollectionNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) Collection(java.util.Collection) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) Property(org.hisp.dhis.schema.Property)

Example 2 with Property

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;
}
Also used : Schema(org.hisp.dhis.schema.Schema) CollectionNode(org.hisp.dhis.node.types.CollectionNode) Property(org.hisp.dhis.schema.Property)

Example 3 with Property

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;
}
Also used : QueryPath(org.hisp.dhis.query.planner.QueryPath) QueryParserException(org.hisp.dhis.query.QueryParserException) Criterion(org.hibernate.criterion.Criterion) Schema(org.hisp.dhis.schema.Schema) Conjunction(org.hibernate.criterion.Conjunction) Property(org.hisp.dhis.schema.Property)

Example 4 with Property

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();
}
Also used : UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Schema(org.hisp.dhis.schema.Schema) Collection(java.util.Collection) Property(org.hisp.dhis.schema.Property) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 5 with Property

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;
}
Also used : Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property)

Aggregations

Property (org.hisp.dhis.schema.Property)126 Schema (org.hisp.dhis.schema.Schema)69 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)36 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)26 Collection (java.util.Collection)21 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)21 List (java.util.List)20 Map (java.util.Map)16 Test (org.junit.jupiter.api.Test)16 Attribute (org.hisp.dhis.attribute.Attribute)14 ReflectionUtils (org.hisp.dhis.system.util.ReflectionUtils)14 Collectors (java.util.stream.Collectors)13 User (org.hisp.dhis.user.User)13 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)12 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)12 SimpleNode (org.hisp.dhis.node.types.SimpleNode)12 Query (org.hisp.dhis.query.Query)12 SchemaService (org.hisp.dhis.schema.SchemaService)12 Transactional (org.springframework.transaction.annotation.Transactional)12