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;
}
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);
}
}
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);
}
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();
}
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();
}
Aggregations