use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.
the class DataElementStoreTest method testDataElementFromAttribute.
// Fails with expected:<null> but was:<{"class":"class org.hisp.dhis.dataelement.DataElement"
@Ignore
@Test
public void testDataElementFromAttribute() throws NonUniqueAttributeValueException {
Attribute attribute = new Attribute("test", ValueType.TEXT);
attribute.setDataElementAttribute(true);
attributeService.addAttribute(attribute);
DataElement dataElementA = createDataElement('A');
DataElement dataElementB = createDataElement('B');
dataElementStore.save(dataElementA);
dataElementStore.save(dataElementB);
AttributeValue attributeValue = new AttributeValue("SOME VALUE", attribute);
attributeService.addAttributeValue(dataElementA, attributeValue);
dataElementA.getAttributeValues().add(attributeValue);
dataElementStore.update(dataElementA);
DataElement dataElement = dataElementStore.getByAttribute(attribute);
assertEquals(dataElement, dataElementA);
}
use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.
the class DataElementStoreTest method testAttributeValueFromAttributeAndValue.
@Test
public void testAttributeValueFromAttributeAndValue() throws NonUniqueAttributeValueException {
Attribute attribute = new Attribute("test", ValueType.TEXT);
attribute.setDataElementAttribute(true);
attributeService.addAttribute(attribute);
DataElement dataElementA = createDataElement('A');
DataElement dataElementB = createDataElement('B');
DataElement dataElementC = createDataElement('C');
dataElementStore.save(dataElementA);
dataElementStore.save(dataElementB);
dataElementStore.save(dataElementC);
AttributeValue attributeValueA = new AttributeValue("SOME VALUE", attribute);
AttributeValue attributeValueB = new AttributeValue("SOME VALUE", attribute);
AttributeValue attributeValueC = new AttributeValue("ANOTHER VALUE", attribute);
attributeService.addAttributeValue(dataElementA, attributeValueA);
attributeService.addAttributeValue(dataElementB, attributeValueB);
attributeService.addAttributeValue(dataElementC, attributeValueC);
dataElementStore.update(dataElementA);
dataElementStore.update(dataElementB);
dataElementStore.update(dataElementC);
List<AttributeValue> values = dataElementStore.getAttributeValueByAttributeAndValue(attribute, "SOME VALUE");
assertEquals(2, values.size());
values = dataElementStore.getAttributeValueByAttributeAndValue(attribute, "ANOTHER VALUE");
assertEquals(1, values.size());
}
use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.
the class OrganisationUnitLocationController method getEntitiesWithinRange.
/**
* Get Organisation Units within a distance from a location
*/
@RequestMapping(value = "/withinRange", method = RequestMethod.GET, produces = { "*/*", "application/json" })
public void getEntitiesWithinRange(@RequestParam Double longitude, @RequestParam Double latitude, @RequestParam Double distance, @RequestParam(required = false) String orgUnitGroupSetId, HttpServletResponse response) throws Exception {
List<OrganisationUnit> entityList = new ArrayList<>(organisationUnitService.getOrganisationUnitWithinDistance(longitude, latitude, distance));
for (OrganisationUnit organisationUnit : entityList) {
Set<AttributeValue> attributeValues = organisationUnit.getAttributeValues();
attributeValues.clear();
if (orgUnitGroupSetId != null) {
for (OrganisationUnitGroup organisationUnitGroup : organisationUnit.getGroups()) {
for (OrganisationUnitGroupSet orgunitGroupSet : organisationUnitGroup.getGroupSets()) {
if (orgunitGroupSet.getUid().compareTo(orgUnitGroupSetId) == 0) {
AttributeValue attributeValue = new AttributeValue();
// attributeValue.setAttribute( new Attribute( ORGUNIGROUP_SYMBOL, ORGUNIGROUP_SYMBOL ) );
attributeValue.setAttribute(new Attribute(ORGUNIGROUP_SYMBOL, ValueType.TEXT));
attributeValue.setValue(organisationUnitGroup.getSymbol());
attributeValues.add(attributeValue);
}
}
}
}
organisationUnit.setAttributeValues(attributeValues);
// Clear out all data not needed for this task
organisationUnit.removeAllDataSets();
organisationUnit.removeAllUsers();
organisationUnit.removeAllOrganisationUnitGroups();
}
renderService.toJson(response.getOutputStream(), entityList);
}
use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkUniqueAttributes.
private List<ErrorReport> checkUniqueAttributes(Class<? extends IdentifiableObject> klass, IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier) {
List<ErrorReport> errorReports = new ArrayList<>();
if (object == null || Preheat.isDefault(object) || !preheat.getUniqueAttributes().containsKey(klass)) {
return errorReports;
}
Set<AttributeValue> attributeValues = object.getAttributeValues();
// make copy for modification
List<String> uniqueAttributes = new ArrayList<>(preheat.getUniqueAttributes().get(klass));
if (!preheat.getUniqueAttributeValues().containsKey(klass)) {
preheat.getUniqueAttributeValues().put(klass, new HashMap<>());
}
Map<String, Map<String, String>> uniqueAttributeValues = preheat.getUniqueAttributeValues().get(klass);
if (uniqueAttributes.isEmpty()) {
return errorReports;
}
attributeValues.forEach(attributeValue -> {
Attribute attribute = preheat.get(identifier, attributeValue.getAttribute());
if (attribute == null || !attribute.isUnique() || StringUtils.isEmpty(attributeValue.getValue())) {
return;
}
if (uniqueAttributeValues.containsKey(attribute.getUid())) {
Map<String, String> values = uniqueAttributeValues.get(attribute.getUid());
if (values.containsKey(attributeValue.getValue()) && !values.get(attributeValue.getValue()).equals(object.getUid())) {
errorReports.add(new ErrorReport(Attribute.class, ErrorCode.E4009, IdentifiableObjectUtils.getDisplayName(attribute), attributeValue.getValue()).setMainId(attribute.getUid()).setErrorProperty("value"));
} else {
uniqueAttributeValues.get(attribute.getUid()).put(attributeValue.getValue(), object.getUid());
}
} else {
uniqueAttributeValues.put(attribute.getUid(), new HashMap<>());
uniqueAttributeValues.get(attribute.getUid()).put(attributeValue.getValue(), object.getUid());
}
});
return errorReports;
}
use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.
the class DimensionalObjectUtilsTest method testGetUidMapIsSchemeAttribute.
@Test
public void testGetUidMapIsSchemeAttribute() {
DataElement deA = new DataElement("DataElementA");
DataElement deB = new DataElement("DataElementB");
DataElement deC = new DataElement("DataElementC");
deA.setUid("A123456789A");
deB.setUid("A123456789B");
deC.setUid("A123456789C");
Attribute atA = new Attribute("AttributeA", ValueType.INTEGER);
atA.setUid("ATTR123456A");
AttributeValue avA = new AttributeValue("AttributeValueA", atA);
AttributeValue avB = new AttributeValue("AttributeValueB", atA);
deA.getAttributeValues().add(avA);
deB.getAttributeValues().add(avB);
List<DataElement> elements = Lists.newArrayList(deA, deB, deC);
String scheme = IdScheme.ATTR_ID_SCHEME_PREFIX + atA.getUid();
IdScheme idScheme = IdScheme.from(scheme);
Map<String, String> map = DimensionalObjectUtils.getDimensionItemIdSchemeMap(elements, idScheme);
assertEquals(3, map.size());
assertEquals("AttributeValueA", map.get("A123456789A"));
assertEquals("AttributeValueB", map.get("A123456789B"));
assertEquals(null, map.get("A123456789C"));
}
Aggregations