use of org.hisp.dhis.attribute.AttributeValue 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.AttributeValue in project dhis2-core by dhis2.
the class OrganisationUnitLocationController method getParentByLocation.
/**
* Get lowest level Org Units that includes the location in their polygon shape.
*/
@RequestMapping(value = "/orgUnitByLocation", method = RequestMethod.GET, produces = { "*/*", "application/json" })
public void getParentByLocation(@RequestParam Double longitude, @RequestParam Double latitude, @RequestParam(required = false) String topOrgUnit, @RequestParam(required = false) Integer targetLevel, HttpServletResponse response) throws Exception {
List<OrganisationUnit> entityList = new ArrayList<>(organisationUnitService.getOrganisationUnitByCoordinate(longitude, latitude, topOrgUnit, targetLevel));
for (OrganisationUnit organisationUnit : entityList) {
Set<AttributeValue> attributeValues = organisationUnit.getAttributeValues();
attributeValues.clear();
organisationUnit.removeAllDataSets();
organisationUnit.removeAllUsers();
organisationUnit.removeAllOrganisationUnitGroups();
}
renderService.toJson(response.getOutputStream(), entityList);
}
use of org.hisp.dhis.attribute.AttributeValue in project dhis2-core by dhis2.
the class CsdController method createCsd.
private Csd createCsd(Iterable<OrganisationUnit> organisationUnits) {
Csd csd = new Csd();
csd.getFacilityDirectory().setFacilities(new ArrayList<>());
for (OrganisationUnit organisationUnit : organisationUnits) {
boolean isFacility = false;
for (OrganisationUnitGroup group : organisationUnit.getGroups()) {
if (group.getName().equals(FACILITY_DISCRIMINATOR_GROUP)) {
isFacility = true;
break;
}
}
// skip if orgunit is not a health facility
if (!isFacility) {
continue;
}
Facility facility = new Facility();
facility.setOid("urn:x-dhis:facility." + organisationUnit.getUid());
facility.getOtherID().add(new OtherID(organisationUnit.getUid(), "dhis2-uid"));
if (organisationUnit.getCode() != null) {
facility.getOtherID().add(new OtherID(organisationUnit.getCode(), "dhis2-code"));
}
facility.setPrimaryName(organisationUnit.getDisplayName());
if (organisationUnit.getContactPerson() != null) {
Contact contact = new Contact();
Person person = new Person();
Name name = new Name();
contact.setPerson(person);
person.setName(name);
name.getCommonNames().add(new CommonName(organisationUnit.getContactPerson()));
facility.getContacts().add(contact);
}
String facilityStatus = "Open";
for (OrganisationUnitGroup organisationUnitGroup : organisationUnit.getGroups()) {
if (organisationUnitGroup == null) {
continue;
}
Set<String> groupSetNames = organisationUnitGroup.getGroupSets().stream().map(OrganisationUnitGroupSet::getName).collect(Collectors.toSet());
if (groupSetNames.contains(FACILITY_STATUS_GROUPSET)) {
facilityStatus = organisationUnitGroup.getCode();
continue;
}
if (groupSetNames.contains(FACILITY_TYPE_GROUPSET)) {
if (organisationUnitGroup.getCode() == null) {
continue;
}
CodedType codedType = new CodedType();
codedType.setCode(organisationUnitGroup.getCode());
codedType.setCodingSchema("Unknown");
for (AttributeValue attributeValue : organisationUnitGroup.getAttributeValues()) {
if (attributeValue.getAttribute().getName().equals("code_system")) {
codedType.setCodingSchema(attributeValue.getValue());
break;
}
}
codedType.setValue(organisationUnitGroup.getDisplayName());
facility.getCodedTypes().add(codedType);
}
if (groupSetNames.contains(FACILITY_OWNERSHIP_GROUPSET)) {
Organization organization = new Organization("urn:x-dhis:ownership." + organisationUnitGroup.getUid());
facility.getOrganizations().add(organization);
for (DataSet dataSet : organisationUnit.getDataSets()) {
for (AttributeValue attributeValue : dataSet.getAttributeValues()) {
if (attributeValue.getAttribute().getName().equals(DATASET_SERVICE_ATTRIBUTE)) {
Service service = new Service();
service.setOid("urn:x-dhis:dataSet." + dataSet.getUid());
service.getNames().add(new Name(new CommonName(attributeValue.getValue())));
organization.getServices().add(service);
break;
}
}
}
}
}
if (organisationUnit.getFeatureType() == FeatureType.POINT) {
Geocode geocode = new Geocode();
try {
GeoUtils.Coordinates coordinates = GeoUtils.parseCoordinates(organisationUnit.getCoordinates());
geocode.setLongitude(coordinates.lng);
geocode.setLatitude(coordinates.lat);
facility.setGeocode(geocode);
} catch (NumberFormatException ignored) {
}
}
Record record = new Record();
record.setCreated(organisationUnit.getCreated());
record.setUpdated(organisationUnit.getLastUpdated());
record.setStatus(facilityStatus);
facility.setRecord(record);
Map<String, List<AddressLine>> addressLines = Maps.newHashMap();
List<AttributeValue> attributeValues = new ArrayList<>(organisationUnit.getAttributeValues());
Collections.sort(attributeValues, AttributeValueSortOrderComparator.INSTANCE);
for (AttributeValue attributeValue : attributeValues) {
if (attributeValue.getAttribute().getName().startsWith("Address_")) {
String[] attributeSplit = attributeValue.getAttribute().getName().split("_");
if (attributeSplit.length > 3) {
continue;
}
if (addressLines.get(attributeSplit[1]) == null) {
addressLines.put(attributeSplit[1], Lists.<AddressLine>newArrayList());
}
AddressLine addressLine = new AddressLine();
addressLine.setComponent(attributeSplit[2]);
addressLine.setValue(attributeValue.getValue());
addressLines.get(attributeSplit[1]).add(addressLine);
}
}
for (String key : addressLines.keySet()) {
Address address = new Address(key);
address.setAddressLines(addressLines.get(key));
facility.getAddresses().add(address);
}
csd.getFacilityDirectory().getFacilities().add(facility);
}
return csd;
}
use of org.hisp.dhis.attribute.AttributeValue 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.AttributeValue in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkReferences.
private List<PreheatErrorReport> checkReferences(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, boolean skipSharing) {
List<PreheatErrorReport> preheatErrorReports = new ArrayList<>();
if (object == null) {
return preheatErrorReports;
}
Schema schema = schemaService.getDynamicSchema(object.getClass());
schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).forEach(p -> {
if (skipCheck(p.getKlass()) || skipCheck(p.getItemKlass())) {
return;
}
if (!p.isCollection()) {
IdentifiableObject refObject = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
IdentifiableObject ref = preheat.get(identifier, refObject);
if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
if (!("user".equals(p.getName()) && User.class.isAssignableFrom(p.getKlass()) && skipSharing)) {
preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getName()));
}
}
} else {
Collection<IdentifiableObject> objects = ReflectionUtils.newCollectionInstance(p.getKlass());
Collection<IdentifiableObject> refObjects = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
for (IdentifiableObject refObject : refObjects) {
if (Preheat.isDefault(refObject))
continue;
IdentifiableObject ref = preheat.get(identifier, refObject);
if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getCollectionName()));
} else {
objects.add(refObject);
}
}
ReflectionUtils.invokeMethod(object, p.getSetterMethod(), objects);
}
});
if (schema.havePersistedProperty("attributeValues")) {
object.getAttributeValues().stream().filter(attributeValue -> attributeValue.getAttribute() != null && preheat.get(identifier, attributeValue.getAttribute()) == null).forEach(attributeValue -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(attributeValue.getAttribute()), identifier.getIdentifiersWithName(object), "attributeValues")));
}
if (schema.havePersistedProperty("userGroupAccesses")) {
object.getUserGroupAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUserGroup() != null && preheat.get(identifier, userGroupAccess.getUserGroup()) == null).forEach(userGroupAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userGroupAccesses.getUserGroup()), identifier.getIdentifiersWithName(object), "userGroupAccesses")));
}
if (schema.havePersistedProperty("userAccesses")) {
object.getUserAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUser() != null && preheat.get(identifier, userGroupAccess.getUser()) == null).forEach(userAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userAccesses.getUser()), identifier.getIdentifiersWithName(object), "userAccesses")));
}
return preheatErrorReports;
}
Aggregations