Search in sources :

Example 41 with Attribute

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

the class DataValueSetServiceTest method setUpTest.

@Override
public void setUpTest() {
    CategoryOptionCombo categoryOptionCombo = categoryService.getDefaultCategoryOptionCombo();
    userService = _userService;
    mockDataValueBatchHandler = new MockBatchHandler<>();
    mockDataValueAuditBatchHandler = new MockBatchHandler<>();
    mockBatchHandlerFactory = new MockBatchHandlerFactory();
    mockBatchHandlerFactory.registerBatchHandler(DataValueBatchHandler.class, mockDataValueBatchHandler);
    mockBatchHandlerFactory.registerBatchHandler(DataValueAuditBatchHandler.class, mockDataValueAuditBatchHandler);
    setDependency(BatchHandlerFactoryTarget.class, BatchHandlerFactoryTarget::setBatchHandlerFactory, mockBatchHandlerFactory, dataValueSetService);
    attribute = new Attribute("CUSTOM_ID", ValueType.TEXT);
    attribute.setUid(ATTRIBUTE_UID);
    attribute.setUnique(true);
    attribute.setOrganisationUnitAttribute(true);
    attribute.setDataElementAttribute(true);
    idObjectManager.save(attribute);
    categoryOptionA = createCategoryOption('A');
    categoryOptionB = createCategoryOption('B');
    categoryA = createCategory('A');
    categoryService.addCategory(categoryA);
    categoryOptionA.getCategories().add(categoryA);
    categoryOptionB.getCategories().add(categoryA);
    categoryService.addCategoryOption(categoryOptionB);
    categoryService.addCategoryOption(categoryOptionA);
    categoryComboA = createCategoryCombo('A', categoryA);
    categoryComboDef = categoryService.getDefaultCategoryCombo();
    ocDef = categoryService.getDefaultCategoryOptionCombo();
    ocDef.setCode("OC_DEF_CODE");
    categoryService.updateCategoryOptionCombo(ocDef);
    osA = new OptionSet("OptionSetA", ValueType.INTEGER);
    osA.getOptions().add(new Option("Blue", "1"));
    osA.getOptions().add(new Option("Green", "2"));
    osA.getOptions().add(new Option("Yellow", "3"));
    ocA = createCategoryOptionCombo(categoryComboA, categoryOptionA);
    ocB = createCategoryOptionCombo(categoryComboA, categoryOptionB);
    deA = createDataElement('A', categoryComboDef);
    deB = createDataElement('B', categoryComboDef);
    deC = createDataElement('C', categoryComboDef);
    deD = createDataElement('D', categoryComboDef);
    deE = createDataElement('E');
    deE.setOptionSet(osA);
    deF = createDataElement('F', categoryComboDef);
    deF.setValueType(ValueType.BOOLEAN);
    deG = createDataElement('G', categoryComboDef);
    deG.setValueType(ValueType.TRUE_ONLY);
    dsA = createDataSet('A', new MonthlyPeriodType());
    dsA.setCategoryCombo(categoryComboDef);
    dsB = createDataSet('B');
    dsB.setCategoryCombo(categoryComboDef);
    ouA = createOrganisationUnit('A');
    ouB = createOrganisationUnit('B');
    ouC = createOrganisationUnit('C');
    peA = createPeriod(PeriodType.getByNameIgnoreCase(MonthlyPeriodType.NAME), getDate(2012, 1, 1), getDate(2012, 1, 31));
    peB = createPeriod(PeriodType.getByNameIgnoreCase(MonthlyPeriodType.NAME), getDate(2012, 2, 1), getDate(2012, 2, 29));
    peC = createPeriod(PeriodType.getByNameIgnoreCase(MonthlyPeriodType.NAME), getDate(2012, 3, 1), getDate(2012, 3, 31));
    ocA.setUid("kjuiHgy67hg");
    ocB.setUid("Gad33qy67g5");
    deA.setUid("f7n9E0hX8qk");
    deB.setUid("Ix2HsbDMLea");
    deC.setUid("eY5ehpbEsB7");
    deE.setUid("jH26dja2f28");
    deF.setUid("jH26dja2f30");
    deG.setUid("jH26dja2f31");
    dsA.setUid("pBOMPrpg1QX");
    ouA.setUid("DiszpKrYNg8");
    ouB.setUid("BdfsJfj87js");
    ouC.setUid("j7Hg26FpoIa");
    ocA.setCode("OC_A");
    ocB.setCode("OC_B");
    deA.setCode("DE_A");
    deB.setCode("DE_B");
    deC.setCode("DE_C");
    deD.setCode("DE_D");
    dsA.setCode("DS_A");
    ouA.setCode("OU_A");
    ouB.setCode("OU_B");
    ouC.setCode("OU_C");
    categoryService.addCategoryCombo(categoryComboA);
    categoryService.addCategoryOptionCombo(ocA);
    categoryService.addCategoryOptionCombo(ocB);
    AttributeValue av1 = createAttributeValue(attribute, "DE1");
    dataElementService.addDataElement(deA);
    dataElementService.addDataElement(deB);
    dataElementService.addDataElement(deC);
    dataElementService.addDataElement(deD);
    dataElementService.addDataElement(deF);
    dataElementService.addDataElement(deG);
    attributeService.addAttributeValue(deA, av1);
    attributeService.addAttributeValue(deB, createAttributeValue(attribute, "DE2"));
    attributeService.addAttributeValue(deC, createAttributeValue(attribute, "DE3"));
    attributeService.addAttributeValue(deD, createAttributeValue(attribute, "DE4"));
    idObjectManager.save(osA);
    dsA.addDataSetElement(deA);
    dsA.addDataSetElement(deB);
    dsA.addDataSetElement(deC);
    dsA.addDataSetElement(deD);
    organisationUnitService.addOrganisationUnit(ouA);
    organisationUnitService.addOrganisationUnit(ouB);
    organisationUnitService.addOrganisationUnit(ouC);
    attributeService.addAttributeValue(ouA, createAttributeValue(attribute, "OU1"));
    attributeService.addAttributeValue(ouB, createAttributeValue(attribute, "OU2"));
    attributeService.addAttributeValue(ouC, createAttributeValue(attribute, "OU3"));
    dsA.addOrganisationUnit(ouA);
    dsA.addOrganisationUnit(ouC);
    periodService.addPeriod(peA);
    periodService.addPeriod(peB);
    periodService.addPeriod(peC);
    dataSetService.addDataSet(dsA);
    user = createUser('A', Lists.newArrayList(Authorities.F_SKIP_DATA_IMPORT_AUDIT.getAuthority()));
    user.setOrganisationUnits(Sets.newHashSet(ouA, ouB));
    userService.addUser(user);
    injectSecurityContext(user);
    CurrentUserService currentUserService = new MockCurrentUserService(user);
    setDependency(CurrentUserServiceTarget.class, CurrentUserServiceTarget::setCurrentUserService, currentUserService, dataValueSetService);
    enableDataSharing(user, dsA, AccessStringHelper.DATA_READ_WRITE);
    enableDataSharing(user, categoryOptionA, AccessStringHelper.DATA_READ_WRITE);
    enableDataSharing(user, categoryOptionB, AccessStringHelper.DATA_READ_WRITE);
    _userService.addUser(user);
    CompleteDataSetRegistration completeDataSetRegistration = new CompleteDataSetRegistration(dsA, peA, ouA, categoryOptionCombo, getDate(2012, 1, 9), "userA", new Date(), "userA", true);
    registrationService.saveCompleteDataSetRegistration(completeDataSetRegistration);
}
Also used : AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CurrentUserServiceTarget(org.hisp.dhis.user.CurrentUserServiceTarget) Date(java.util.Date) MockBatchHandlerFactory(org.hisp.dhis.mock.batchhandler.MockBatchHandlerFactory) BatchHandlerFactoryTarget(org.hisp.dhis.common.BatchHandlerFactoryTarget) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CategoryOption(org.hisp.dhis.category.CategoryOption) Option(org.hisp.dhis.option.Option) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) OptionSet(org.hisp.dhis.option.OptionSet) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 42 with Attribute

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

the class EnrollmentSecurityTest method testAddEnrollmentWithOrgUnitIdSchemeToOrgUnitWithoutProgramAccess.

@Test
void testAddEnrollmentWithOrgUnitIdSchemeToOrgUnitWithoutProgramAccess() {
    programA.getSharing().setPublicAccess(AccessStringHelper.DEFAULT);
    manager.updateNoAcl(programA);
    Enrollment en = createEnrollment(programA.getUid(), maleA.getUid());
    Attribute attribute = new Attribute();
    attribute.setUnique(true);
    attribute.setUid("D1DDOl5hTsL");
    attribute.setValueType(ValueType.NUMBER);
    attribute.setOrganisationUnitAttribute(true);
    attribute.setName("OrgUnitAttribute");
    attributeService.addAttribute(attribute);
    AttributeValue av = new AttributeValue();
    av.setAttribute(attribute);
    av.setValue("1025");
    organisationUnitB.setAttributeValues(Collections.singleton(av));
    manager.updateNoAcl(organisationUnitB);
    en.setOrgUnit(av.getValue());
    ImportOptions importOptions = new ImportOptions();
    importOptions.getIdSchemes().setOrgUnitIdScheme("ATTRIBUTE");
    importOptions.getIdSchemes().getOrgUnitIdScheme().setAttribute("D1DDOl5hTsL");
    ImportSummary importSummary = enrollmentService.addEnrollment(en, importOptions);
    assertEquals(ImportStatus.ERROR, importSummary.getStatus());
    assertEquals("Program is not assigned to this Organisation Unit: " + av.getValue(), importSummary.getDescription());
    programA.getSharing().setPublicAccess(AccessStringHelper.DEFAULT);
    programA.getOrganisationUnits().add(organisationUnitB);
    manager.updateNoAcl(programA);
    importSummary = enrollmentService.addEnrollment(en, importOptions);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
}
Also used : AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 43 with Attribute

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

the class ObjectBundleServiceAttributesTest method testCreateSimpleMetadataAttributeValuesUID.

@Test
void testCreateSimpleMetadataAttributeValuesUID() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/simple_metadata_with_av.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validationReport = objectBundleValidationService.validate(bundle);
    assertFalse(validationReport.hasErrorReports());
    transactionTemplate.execute(status -> {
        objectBundleService.commit(bundle);
        return null;
    });
    List<OrganisationUnit> organisationUnits = manager.getAll(OrganisationUnit.class);
    List<DataElement> dataElements = manager.getAll(DataElement.class);
    List<DataSet> dataSets = manager.getAll(DataSet.class);
    List<UserAuthorityGroup> userRoles = manager.getAll(UserAuthorityGroup.class);
    List<User> users = manager.getAll(User.class);
    List<Option> options = manager.getAll(Option.class);
    List<OptionSet> optionSets = manager.getAll(OptionSet.class);
    List<Attribute> attributes = manager.getAll(Attribute.class);
    assertFalse(organisationUnits.isEmpty());
    assertFalse(dataElements.isEmpty());
    assertFalse(dataSets.isEmpty());
    assertFalse(users.isEmpty());
    assertFalse(userRoles.isEmpty());
    assertEquals(2, attributes.size());
    assertEquals(2, options.size());
    assertEquals(1, optionSets.size());
    Map<Class<? extends IdentifiableObject>, IdentifiableObject> defaults = manager.getDefaults();
    DataSet dataSet = dataSets.get(0);
    User user = users.get(0);
    OptionSet optionSet = optionSets.get(0);
    for (DataElement dataElement : dataElements) {
        assertNotNull(dataElement.getCategoryCombo());
        assertEquals(defaults.get(CategoryCombo.class), dataElement.getCategoryCombo());
    }
    assertFalse(dataSet.getSources().isEmpty());
    assertFalse(dataSet.getDataSetElements().isEmpty());
    assertEquals(1, dataSet.getSources().size());
    assertEquals(2, dataSet.getDataSetElements().size());
    assertEquals(PeriodType.getPeriodTypeByName("Monthly"), dataSet.getPeriodType());
    assertNotNull(user);
    assertEquals("admin", user.getUsername());
    assertFalse(user.getUserAuthorityGroups().isEmpty());
    assertFalse(user.getOrganisationUnits().isEmpty());
    assertEquals("PdWlltZnVZe", user.getOrganisationUnit().getUid());
    assertEquals(2, optionSet.getOptions().size());
    // attribute value check
    DataElement dataElementA = manager.get(DataElement.class, "SG4HuKlNEFH");
    DataElement dataElementB = manager.get(DataElement.class, "CCwk5Yx440o");
    DataElement dataElementC = manager.get(DataElement.class, "j5PneRdU7WT");
    DataElement dataElementD = manager.get(DataElement.class, "k90AVpBahO4");
    assertNotNull(dataElementA);
    assertNotNull(dataElementB);
    assertNotNull(dataElementC);
    assertNotNull(dataElementD);
    assertTrue(dataElementA.getAttributeValues().isEmpty());
    assertTrue(dataElementB.getAttributeValues().isEmpty());
    assertFalse(dataElementC.getAttributeValues().isEmpty());
    assertFalse(dataElementD.getAttributeValues().isEmpty());
}
Also used : User(org.hisp.dhis.user.User) DataSet(org.hisp.dhis.dataset.DataSet) Attribute(org.hisp.dhis.attribute.Attribute) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) DataElement(org.hisp.dhis.dataelement.DataElement) CategoryCombo(org.hisp.dhis.category.CategoryCombo) List(java.util.List) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) ClassPathResource(org.springframework.core.io.ClassPathResource) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet) Test(org.junit.jupiter.api.Test)

Example 44 with Attribute

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

the class ValidatingEventCheckerTest method createObjectBundle.

private ObjectBundle createObjectBundle() {
    Attribute attribute1 = new Attribute();
    attribute1.setUid("u1");
    ObjectBundleParams objectBundleParams = new ObjectBundleParams();
    Preheat preheat = new Preheat();
    final Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objectMap = new HashMap<>();
    objectMap.put(Attribute.class, new ArrayList<>());
    objectMap.get(Attribute.class).add(attribute1);
    preheat.put(PreheatIdentifier.UID, attribute1);
    return new ObjectBundle(objectBundleParams, preheat, objectMap);
}
Also used : ObjectBundleParams(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleParams) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) Attribute(org.hisp.dhis.attribute.Attribute) HashMap(java.util.HashMap) Preheat(org.hisp.dhis.preheat.Preheat) ArrayList(java.util.ArrayList) List(java.util.List) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 45 with Attribute

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

the class DefaultOrgUnitProfileService method getAttributes.

/**
 * Retrieves a list of attribute data items for the given org unit profile
 * and org unit.
 *
 * @param profile the {@link OrganisationUnitProfile}.
 * @param orgUnit the {@link OrganisationUnit}.
 * @return a list of {@link ProfileItem}.
 */
private List<ProfileItem> getAttributes(OrgUnitProfile profile, OrganisationUnit orgUnit) {
    if (CollectionUtils.isEmpty(profile.getAttributes())) {
        return ImmutableList.of();
    }
    List<Attribute> attributes = idObjectManager.getByUid(Attribute.class, profile.getAttributes());
    if (CollectionUtils.isEmpty(attributes)) {
        return ImmutableList.of();
    }
    List<ProfileItem> items = new ArrayList<>();
    for (Attribute attribute : attributes) {
        AttributeValue attributeValue = orgUnit.getAttributeValue(attribute);
        if (attributeValue != null) {
            items.add(new ProfileItem(attribute.getUid(), attribute.getDisplayName(), attributeValue.getValue()));
        }
    }
    return items;
}
Also used : ProfileItem(org.hisp.dhis.orgunitprofile.ProfileItem) AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute) ArrayList(java.util.ArrayList)

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