Search in sources :

Example 26 with Attribute

use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.

the class DhisConvenienceTest method createAttribute.

/**
 * @param uniqueCharacter A unique character to identify the object.
 */
public static Attribute createAttribute(char uniqueCharacter) {
    Attribute attribute = new Attribute("Attribute" + uniqueCharacter, ValueType.TEXT);
    attribute.setAutoFields();
    return attribute;
}
Also used : Attribute(org.hisp.dhis.attribute.Attribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute)

Example 27 with Attribute

use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.

the class GeoFeatureService method getCoordinates.

/**
 * Get the {@link GeoFeature} coordinate from {@link DimensionalItemObject}
 * <p>
 * The coordinate value is retrieved from {@link DimensionalItemObject}'s
 * geoJsonAttribute value.
 *
 * @param feature the {@link GeoFeature}
 * @param unit the {@link DimensionalItemObject} contains the coordinate
 *        values.
 * @param geoJsonAttribute The {@link Attribute} which has
 *        {@link ValueType#GEOJSON} and is assigned to
 *        {@link OrganisationUnit}.
 * @return the given {@link GeoFeature} with updated coordinate value and
 *         coordinate type.
 */
private void getCoordinates(GeoFeature feature, DimensionalItemObject unit, Attribute geoJsonAttribute) {
    if (geoJsonAttribute == null) {
        getCoordinates(feature, unit);
        return;
    }
    if (!unit.getClass().isAssignableFrom(OrganisationUnit.class)) {
        return;
    }
    OrganisationUnit organisationUnit = (OrganisationUnit) unit;
    Optional<AttributeValue> geoJsonAttributeValue = organisationUnit.getAttributeValues().stream().filter(attributeValue -> attributeValue.getAttribute().getUid().equals(geoJsonAttribute.getUid())).findFirst();
    if (!geoJsonAttributeValue.isPresent() || StringUtils.isBlank(geoJsonAttributeValue.get().getValue())) {
        getCoordinates(feature, unit);
        return;
    }
    try {
        GeoJsonObject geoJsonObject = new ObjectMapper().readValue(geoJsonAttributeValue.get().getValue(), GeoJsonObject.class);
        GeoFeature geoJsonFeature = geoJsonObject.accept(new GeoFeatureVisitor());
        if (geoJsonFeature == null) {
            return;
        }
        feature.setTy(geoJsonFeature.getTy());
        feature.setCo(geoJsonFeature.getCo());
    } catch (JsonProcessingException e) {
        log.error(String.format("Couldn't read GeoJson value from organisationUnit %s: ", organisationUnit), e);
        getCoordinates(feature, unit);
    }
}
Also used : Date(java.util.Date) ValueType(org.hisp.dhis.common.ValueType) ValidationUtils(org.hisp.dhis.system.util.ValidationUtils) GeoFeature(org.hisp.dhis.webapi.webdomain.GeoFeature) StringUtils(org.apache.commons.lang3.StringUtils) MultiPolygon(org.geojson.MultiPolygon) MultiLineString(org.geojson.MultiLineString) FeatureCollection(org.geojson.FeatureCollection) CoordinateObject(org.hisp.dhis.common.coordinate.CoordinateObject) Map(java.util.Map) OrganisationUnitGroupService(org.hisp.dhis.organisationunit.OrganisationUnitGroupService) ImmutableMap(com.google.common.collect.ImmutableMap) GeoJsonObject(org.geojson.GeoJsonObject) GeoJsonObjectVisitor(org.geojson.GeoJsonObjectVisitor) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Builder(lombok.Builder) AttributeService(org.hisp.dhis.attribute.AttributeService) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Optional(java.util.Optional) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) ORGUNIT_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID) ORGUNIT_GROUP_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_GROUP_DIM_ID) DataQueryService(org.hisp.dhis.analytics.DataQueryService) AttributeValue(org.hisp.dhis.attribute.AttributeValue) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Getter(lombok.Getter) Feature(org.geojson.Feature) Attribute(org.hisp.dhis.attribute.Attribute) ArrayList(java.util.ArrayList) GeometryCollection(org.geojson.GeometryCollection) HashSet(java.util.HashSet) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) LineString(org.geojson.LineString) Service(org.springframework.stereotype.Service) DisplayProperty(org.hisp.dhis.common.DisplayProperty) DataQueryRequest(org.hisp.dhis.common.DataQueryRequest) Polygon(org.geojson.Polygon) DimensionalObjectUtils(org.hisp.dhis.common.DimensionalObjectUtils) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) HttpServletResponse(javax.servlet.http.HttpServletResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AggregationType(org.hisp.dhis.analytics.AggregationType) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) OrganisationUnitGroupSet(org.hisp.dhis.organisationunit.OrganisationUnitGroupSet) MultiPoint(org.geojson.MultiPoint) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ObjectUtils(org.hisp.dhis.util.ObjectUtils) FeatureType(org.hisp.dhis.organisationunit.FeatureType) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Point(org.geojson.Point) Comparator(java.util.Comparator) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) AttributeValue(org.hisp.dhis.attribute.AttributeValue) GeoJsonObject(org.geojson.GeoJsonObject) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GeoFeature(org.hisp.dhis.webapi.webdomain.GeoFeature)

Example 28 with Attribute

use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.

the class ObjectBundleServiceAttributesTest method defaultSetupWithAttributes.

private void defaultSetupWithAttributes() {
    Attribute attribute = new Attribute("AttributeA", ValueType.TEXT);
    attribute.setUid("d9vw7V9Mw8W");
    attribute.setUnique(true);
    attribute.setMandatory(true);
    attribute.setDataElementAttribute(true);
    manager.save(attribute);
    AttributeValue attributeValue1 = new AttributeValue("Value1", attribute);
    AttributeValue attributeValue2 = new AttributeValue("Value2", attribute);
    AttributeValue attributeValue3 = new AttributeValue("Value3", attribute);
    DataElement de1 = createDataElement('A');
    DataElement de2 = createDataElement('B');
    DataElement de3 = createDataElement('C');
    transactionTemplate.execute(status -> {
        manager.save(de1);
        manager.save(de2);
        manager.save(de3);
        attributeService.addAttributeValue(de1, attributeValue1);
        attributeService.addAttributeValue(de2, attributeValue2);
        attributeService.addAttributeValue(de3, attributeValue3);
        manager.clear();
        return null;
    });
    User user = createUser('A');
    manager.save(user);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) AttributeValue(org.hisp.dhis.attribute.AttributeValue) User(org.hisp.dhis.user.User) Attribute(org.hisp.dhis.attribute.Attribute)

Example 29 with Attribute

use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.

the class HibernateGenericStore method getAllByAttributes.

@Override
public List<T> getAllByAttributes(List<Attribute> attributes) {
    CriteriaBuilder builder = getCriteriaBuilder();
    CriteriaQuery<T> query = builder.createQuery(getClazz());
    Root<T> root = query.from(getClazz());
    query.select(root).distinct(true);
    List<Predicate> predicates = attributes.stream().map(attribute -> builder.isNotNull(builder.function(FUNCTION_JSONB_EXTRACT_PATH, String.class, root.get("attributeValues"), builder.literal(attribute.getUid())))).collect(Collectors.toList());
    query.where(builder.or(predicates.toArray(new Predicate[predicates.size()])));
    return getSession().createQuery(query).list();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) AttributeValue(org.hisp.dhis.attribute.AttributeValue) Criteria(org.hibernate.Criteria) JsonAttributeValueBinaryType(org.hisp.dhis.hibernate.jsonb.type.JsonAttributeValueBinaryType) Session(org.hibernate.Session) Function(java.util.function.Function) TypedQuery(javax.persistence.TypedQuery) ObjectDeletionRequestedEvent(org.hisp.dhis.common.ObjectDeletionRequestedEvent) Attribute(org.hisp.dhis.attribute.Attribute) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) GenericStore(org.hisp.dhis.common.GenericStore) NonUniqueResultException(javax.persistence.NonUniqueResultException) Lists(com.google.common.collect.Lists) Predicate(javax.persistence.criteria.Predicate) NativeQuery(org.hibernate.query.NativeQuery) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Query(org.hibernate.query.Query) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Expression(javax.persistence.criteria.Expression) Root(javax.persistence.criteria.Root) AuditLogUtil(org.hisp.dhis.common.AuditLogUtil) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SessionFactory(org.hibernate.SessionFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) QueryHints(org.hibernate.annotations.QueryHints) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Order(javax.persistence.criteria.Order) Predicate(javax.persistence.criteria.Predicate)

Example 30 with Attribute

use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.

the class HibernateGenericStore method countAllValuesByAttributes.

@Override
public long countAllValuesByAttributes(List<Attribute> attributes) {
    CriteriaBuilder builder = getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<T> root = query.from(getClazz());
    query.select(builder.countDistinct(root));
    List<Predicate> predicates = attributes.stream().map(attribute -> builder.isNotNull(builder.function(FUNCTION_JSONB_EXTRACT_PATH, String.class, root.get("attributeValues"), builder.literal(attribute.getUid())))).collect(Collectors.toList());
    query.where(builder.or(predicates.toArray(new Predicate[predicates.size()])));
    return getSession().createQuery(query).getSingleResult();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) AttributeValue(org.hisp.dhis.attribute.AttributeValue) Criteria(org.hibernate.Criteria) JsonAttributeValueBinaryType(org.hisp.dhis.hibernate.jsonb.type.JsonAttributeValueBinaryType) Session(org.hibernate.Session) Function(java.util.function.Function) TypedQuery(javax.persistence.TypedQuery) ObjectDeletionRequestedEvent(org.hisp.dhis.common.ObjectDeletionRequestedEvent) Attribute(org.hisp.dhis.attribute.Attribute) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) GenericStore(org.hisp.dhis.common.GenericStore) NonUniqueResultException(javax.persistence.NonUniqueResultException) Lists(com.google.common.collect.Lists) Predicate(javax.persistence.criteria.Predicate) NativeQuery(org.hibernate.query.NativeQuery) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Query(org.hibernate.query.Query) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Expression(javax.persistence.criteria.Expression) Root(javax.persistence.criteria.Root) AuditLogUtil(org.hisp.dhis.common.AuditLogUtil) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SessionFactory(org.hibernate.SessionFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) QueryHints(org.hibernate.annotations.QueryHints) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Order(javax.persistence.criteria.Order) Predicate(javax.persistence.criteria.Predicate)

Aggregations

Attribute (org.hisp.dhis.attribute.Attribute)56 Test (org.junit.jupiter.api.Test)28 AttributeValue (org.hisp.dhis.attribute.AttributeValue)27 HashMap (java.util.HashMap)13 DhisSpringTest (org.hisp.dhis.DhisSpringTest)10 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)10 List (java.util.List)8 DataElement (org.hisp.dhis.dataelement.DataElement)8 ArrayList (java.util.ArrayList)7 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)7 Property (org.hisp.dhis.schema.Property)7 Schema (org.hisp.dhis.schema.Schema)7 OrganisationUnitGroup (org.hisp.dhis.organisationunit.OrganisationUnitGroup)6 OrganisationUnitGroupSet (org.hisp.dhis.organisationunit.OrganisationUnitGroupSet)6 Test (org.junit.Test)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Lists (com.google.common.collect.Lists)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4