Search in sources :

Example 16 with Option

use of org.hisp.dhis.option.Option in project dhis2-core by dhis2.

the class HideOptionProgramRuleActionValidator method validate.

@Override
public ProgramRuleActionValidationResult validate(ProgramRuleAction programRuleAction, ProgramRuleActionValidationContext validationContext) {
    // First checking the validity of DataElement and TEA
    ProgramRuleActionValidationResult result = super.validate(programRuleAction, validationContext);
    ProgramRule rule = validationContext.getProgramRule();
    if (!result.isValid()) {
        return result;
    }
    if (!programRuleAction.hasOption()) {
        log.debug(String.format("Option cannot be null for program rule: %s ", rule.getName()));
        return ProgramRuleActionValidationResult.builder().valid(false).errorReport(new ErrorReport(Option.class, ErrorCode.E4040, rule.getName())).build();
    }
    Option option = validationContext.getOption();
    if (option == null) {
        log.debug(String.format("Option %s associated with program rule %s does not exist", programRuleAction.getOption().getUid(), rule.getName()));
        return ProgramRuleActionValidationResult.builder().valid(false).errorReport(new ErrorReport(Option.class, ErrorCode.E4041, programRuleAction.getOption().getUid(), rule.getName())).build();
    }
    return ProgramRuleActionValidationResult.builder().valid(true).build();
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ProgramRuleActionValidationResult(org.hisp.dhis.programrule.ProgramRuleActionValidationResult) ProgramRule(org.hisp.dhis.programrule.ProgramRule) Option(org.hisp.dhis.option.Option)

Example 17 with Option

use of org.hisp.dhis.option.Option in project dhis2-core by dhis2.

the class OptionObjectBundleHookTest method validate.

@Test
void validate() {
    OptionSet optionSet = new OptionSet();
    optionSet.setUid("optionSet1");
    Option option = new Option();
    option.setName("optionName");
    option.setCode("optionCode");
    optionSet.addOption(option);
    OptionSet persistedOptionSet = new OptionSet();
    persistedOptionSet.setUid("optionSet1");
    Option persistedOption = new Option();
    persistedOption.setName("optionName");
    persistedOption.setCode("optionCode");
    persistedOptionSet.addOption(persistedOption);
    preheat.put(PreheatIdentifier.UID, persistedOptionSet);
    ObjectBundleParams objectBundleParams = new ObjectBundleParams();
    objectBundleParams.setPreheatIdentifier(PreheatIdentifier.UID);
    ObjectBundle bundle = new ObjectBundle(objectBundleParams, preheat, Collections.singletonMap(OptionSet.class, Collections.singletonList(persistedOptionSet)));
    List<ErrorReport> errors = hook.validate(option, bundle);
    Assertions.assertNotNull(errors);
    Assertions.assertEquals(1, errors.size());
    Assertions.assertEquals(ErrorCode.E4028, errors.get(0).getErrorCode());
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ObjectBundleParams(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleParams) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet) Test(org.junit.jupiter.api.Test)

Example 18 with Option

use of org.hisp.dhis.option.Option in project dhis2-core by dhis2.

the class CsvMetadataImportTest method testOptionSetMergeDuplicate.

@Test
void testOptionSetMergeDuplicate() throws IOException {
    // Import 1 OptionSet with 3 Options
    ImportReport importReport = runImport("metadata/optionSet_add.csv", CsvImportClass.OPTION_SET);
    assertEquals(4, importReport.getStats().getCreated());
    // Send payload with 5 Options, 2 new and 3 old from above
    importReport = runImport("metadata/optionSet_update_duplicate.csv", CsvImportClass.OPTION_SET, null, params -> {
        params.setIdentifier(PreheatIdentifier.CODE);
        params.setMergeMode(MergeMode.MERGE);
    });
    // Only 2 new Options are added
    assertEquals(2, importReport.getStats().getCreated());
    OptionSet optionSet = optionService.getOptionSetByCode("COLOR");
    // Total 5 Options added
    List<Option> options = optionSet.getOptions();
    assertEquals(5, options.size());
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) DataElementService(org.hisp.dhis.dataelement.DataElementService) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) CsvImportClass(org.hisp.dhis.dxf2.csv.CsvImportClass) ClassPathResource(org.springframework.core.io.ClassPathResource) Autowired(org.springframework.beans.factory.annotation.Autowired) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) ArrayList(java.util.ArrayList) OptionGroup(org.hisp.dhis.option.OptionGroup) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) User(org.hisp.dhis.user.User) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CsvImportService(org.hisp.dhis.dxf2.csv.CsvImportService) OptionService(org.hisp.dhis.option.OptionService) UserService(org.hisp.dhis.user.UserService) OptionGroupSet(org.hisp.dhis.option.OptionGroupSet) IOException(java.io.IOException) SchemaService(org.hisp.dhis.schema.SchemaService) MergeMode(org.hisp.dhis.common.MergeMode) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) Option(org.hisp.dhis.option.Option) CsvImportOptions(org.hisp.dhis.dxf2.csv.CsvImportOptions) OptionSet(org.hisp.dhis.option.OptionSet) DhisSpringTest(org.hisp.dhis.DhisSpringTest) InputStream(java.io.InputStream) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 19 with Option

use of org.hisp.dhis.option.Option in project dhis2-core by dhis2.

the class DefaultMetadataExportServiceTest method getParamsFromMapNoSecondary.

@Test
void getParamsFromMapNoSecondary() {
    Mockito.when(schemaService.getSchemaByPluralName(Mockito.eq("options"))).thenReturn(new Schema(Option.class, "option", "options"));
    final Map<String, List<String>> params = new HashMap<>();
    params.put("options", Collections.singletonList("true"));
    MetadataExportParams exportParams = service.getParamsFromMap(params);
    Assertions.assertFalse(exportParams.getClasses().contains(JobConfiguration.class));
    Assertions.assertTrue(exportParams.getClasses().contains(Option.class));
}
Also used : HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) Option(org.hisp.dhis.option.Option) List(java.util.List) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) Test(org.junit.jupiter.api.Test)

Example 20 with Option

use of org.hisp.dhis.option.Option in project dhis2-core by dhis2.

the class VersionedObjectObjectBundleHook method postCreate.

@Override
public <T extends IdentifiableObject> void postCreate(T persistedObject, ObjectBundle bundle) {
    VersionedObject versionedObject = null;
    if (Section.class.isInstance(persistedObject)) {
        versionedObject = ((Section) persistedObject).getDataSet();
    } else if (Option.class.isInstance(persistedObject)) {
        versionedObject = ((Option) persistedObject).getOptionSet();
    }
    if (versionedObject != null) {
        versionedObject.increaseVersion();
        sessionFactory.getCurrentSession().save(versionedObject);
    }
}
Also used : VersionedObject(org.hisp.dhis.common.VersionedObject) Option(org.hisp.dhis.option.Option)

Aggregations

Option (org.hisp.dhis.option.Option)40 OptionSet (org.hisp.dhis.option.OptionSet)23 Test (org.junit.jupiter.api.Test)16 List (java.util.List)11 DataElement (org.hisp.dhis.dataelement.DataElement)10 HashMap (java.util.HashMap)8 Legend (org.hisp.dhis.legend.Legend)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)6 CategoryOption (org.hisp.dhis.category.CategoryOption)6 HashSet (java.util.HashSet)5 ObjectBundle (org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 VersionedObject (org.hisp.dhis.common.VersionedObject)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 DhisSpringTest (org.hisp.dhis.DhisSpringTest)3 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)3 OptionGroup (org.hisp.dhis.option.OptionGroup)3 OptionGroupSet (org.hisp.dhis.option.OptionGroupSet)3