Search in sources :

Example 6 with AttributeValue

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);
}
Also used : OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute) ArrayList(java.util.ArrayList) OrganisationUnitGroupSet(org.hisp.dhis.organisationunit.OrganisationUnitGroupSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with AttributeValue

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);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) AttributeValue(org.hisp.dhis.attribute.AttributeValue) ArrayList(java.util.ArrayList) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with AttributeValue

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;
}
Also used : AttributeValue(org.hisp.dhis.attribute.AttributeValue) Organization(org.hisp.dhis.web.ohie.csd.domain.Organization) Address(org.hisp.dhis.web.ohie.csd.domain.Address) DataSet(org.hisp.dhis.dataset.DataSet) ArrayList(java.util.ArrayList) CommonName(org.hisp.dhis.web.ohie.csd.domain.CommonName) Name(org.hisp.dhis.web.ohie.csd.domain.Name) Csd(org.hisp.dhis.web.ohie.csd.domain.Csd) GeoUtils(org.hisp.dhis.web.ohie.utils.GeoUtils) Record(org.hisp.dhis.web.ohie.csd.domain.Record) List(java.util.List) ArrayList(java.util.ArrayList) CommonName(org.hisp.dhis.web.ohie.csd.domain.CommonName) Geocode(org.hisp.dhis.web.ohie.csd.domain.Geocode) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) AddressLine(org.hisp.dhis.web.ohie.csd.domain.AddressLine) Service(org.hisp.dhis.web.ohie.csd.domain.Service) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) OtherID(org.hisp.dhis.web.ohie.csd.domain.OtherID) CodedType(org.hisp.dhis.web.ohie.csd.domain.CodedType) Contact(org.hisp.dhis.web.ohie.csd.domain.Contact) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) Facility(org.hisp.dhis.web.ohie.csd.domain.Facility) Person(org.hisp.dhis.web.ohie.csd.domain.Person)

Example 9 with AttributeValue

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;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with AttributeValue

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;
}
Also used : AtomicMode(org.hisp.dhis.dxf2.metadata.AtomicMode) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PropertyType(org.hisp.dhis.schema.PropertyType) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) AttributeValue(org.hisp.dhis.attribute.AttributeValue) IdentifiableObjectUtils(org.hisp.dhis.common.IdentifiableObjectUtils) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Preheat(org.hisp.dhis.preheat.Preheat) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.hisp.dhis.attribute.Attribute) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) TypeReport(org.hisp.dhis.feedback.TypeReport) UserCredentials(org.hisp.dhis.user.UserCredentials) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Service(org.springframework.stereotype.Service) Map(java.util.Map) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) User(org.hisp.dhis.user.User) ErrorCode(org.hisp.dhis.feedback.ErrorCode) ObjectReport(org.hisp.dhis.feedback.ObjectReport) Period(org.hisp.dhis.period.Period) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) UserService(org.hisp.dhis.user.UserService) Iterator(java.util.Iterator) Collection(java.util.Collection) Set(java.util.Set) Timer(org.hisp.dhis.commons.timer.Timer) SchemaService(org.hisp.dhis.schema.SchemaService) Property(org.hisp.dhis.schema.Property) Collectors(java.util.stream.Collectors) SchemaValidator(org.hisp.dhis.schema.validation.SchemaValidator) List(java.util.List) AclService(org.hisp.dhis.security.acl.AclService) PeriodType(org.hisp.dhis.period.PeriodType) Log(org.apache.commons.logging.Log) Schema(org.hisp.dhis.schema.Schema) LogFactory(org.apache.commons.logging.LogFactory) Transactional(org.springframework.transaction.annotation.Transactional) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Aggregations

AttributeValue (org.hisp.dhis.attribute.AttributeValue)17 Attribute (org.hisp.dhis.attribute.Attribute)13 Test (org.junit.Test)7 DhisSpringTest (org.hisp.dhis.DhisSpringTest)6 ArrayList (java.util.ArrayList)5 DataElement (org.hisp.dhis.dataelement.DataElement)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)3 User (org.hisp.dhis.user.User)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ErrorReport (org.hisp.dhis.feedback.ErrorReport)2 OrganisationUnitGroup (org.hisp.dhis.organisationunit.OrganisationUnitGroup)2 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1