Search in sources :

Example 6 with OptionSet

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

the class DefaultCsvImportService method dataElementsFromCsv.

private List<DataElement> dataElementsFromCsv(CsvReader reader) throws IOException {
    DataElementCategoryCombo categoryCombo = categoryService.getDefaultDataElementCategoryCombo();
    List<DataElement> list = new ArrayList<>();
    while (reader.readRecord()) {
        String[] values = reader.getValues();
        if (values != null && values.length > 0) {
            DataElement object = new DataElement();
            setIdentifiableObject(object, values);
            object.setShortName(getSafe(values, 3, object.getName(), 50));
            object.setDescription(getSafe(values, 4, null, null));
            object.setFormName(getSafe(values, 5, null, 230));
            String domainType = getSafe(values, 6, DataElementDomain.AGGREGATE.getValue(), 16);
            object.setDomainType(DataElementDomain.fromValue(domainType));
            object.setValueType(ValueType.valueOf(getSafe(values, 7, ValueType.INTEGER.toString(), 50)));
            object.setAggregationType(AggregationType.valueOf(getSafe(values, 8, AggregationType.SUM.toString(), 50)));
            String categoryComboUid = getSafe(values, 9, null, 11);
            object.setUrl(getSafe(values, 10, null, 255));
            object.setZeroIsSignificant(Boolean.valueOf(getSafe(values, 11, "false", null)));
            String optionSetUid = getSafe(values, 12, null, 11);
            String commentOptionSetUid = getSafe(values, 13, null, 11);
            object.setAutoFields();
            DataElementCategoryCombo cc = new DataElementCategoryCombo();
            cc.setUid(categoryComboUid);
            cc.setAutoFields();
            if (categoryComboUid == null) {
                cc.setUid(categoryCombo.getUid());
            }
            object.setDataElementCategoryCombo(cc);
            if (optionSetUid != null) {
                OptionSet optionSet = new OptionSet();
                optionSet.setUid(optionSetUid);
                optionSet.setAutoFields();
                object.setOptionSet(optionSet);
            }
            if (commentOptionSetUid != null) {
                OptionSet optionSet = new OptionSet();
                optionSet.setUid(commentOptionSetUid);
                optionSet.setAutoFields();
                object.setCommentOptionSet(optionSet);
            }
            list.add(object);
        }
    }
    return list;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) ArrayList(java.util.ArrayList) OptionSet(org.hisp.dhis.option.OptionSet)

Example 7 with OptionSet

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

the class VersionedObjectObjectBundleHook method postTypeImport.

@Override
public <T extends IdentifiableObject> void postTypeImport(Class<? extends IdentifiableObject> klass, List<T> objects, ObjectBundle bundle) {
    if (Section.class.isAssignableFrom(klass)) {
        Set<DataSet> dataSets = new HashSet<>();
        objects.forEach(o -> {
            DataSet dataSet = ((Section) o).getDataSet();
            if (dataSet.getId() > 0) {
                dataSets.add(dataSet);
            }
        });
        dataSets.forEach(ds -> {
            ds.increaseVersion();
            sessionFactory.getCurrentSession().save(ds);
        });
    } else if (Option.class.isAssignableFrom(klass)) {
        Set<OptionSet> optionSets = new HashSet<>();
        objects.forEach(o -> {
            Option option = (Option) o;
            if (option.getOptionSet() != null && option.getId() > 0) {
                optionSets.add(option.getOptionSet());
            }
        });
        optionSets.forEach(os -> {
            os.increaseVersion();
            sessionFactory.getCurrentSession().save(os);
        });
    }
}
Also used : HashSet(java.util.HashSet) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Option(org.hisp.dhis.option.Option) List(java.util.List) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) OptionSet(org.hisp.dhis.option.OptionSet) VersionedObject(org.hisp.dhis.common.VersionedObject) DataSet(org.hisp.dhis.dataset.DataSet) Set(java.util.Set) Section(org.hisp.dhis.dataset.Section) HashSet(java.util.HashSet) OptionSet(org.hisp.dhis.option.OptionSet) DataSet(org.hisp.dhis.dataset.DataSet) Set(java.util.Set) DataSet(org.hisp.dhis.dataset.DataSet) Option(org.hisp.dhis.option.Option) Section(org.hisp.dhis.dataset.Section) HashSet(java.util.HashSet)

Example 8 with OptionSet

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

the class ObjectBundleServiceAttributesTest method testCreateSimpleMetadataAttributeValuesUID.

@Test
public 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);
    objectBundleValidationService.validate(bundle);
    objectBundleService.commit(bundle);
    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.getDataElementCategoryCombo());
        assertEquals(defaults.get(DataElementCategoryCombo.class), dataElement.getDataElementCategoryCombo());
    }
    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.getUserCredentials());
    assertEquals("admin", user.getUserCredentials().getUsername());
    assertFalse(user.getUserCredentials().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) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) DataSet(org.hisp.dhis.dataset.DataSet) Attribute(org.hisp.dhis.attribute.Attribute) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) DataElement(org.hisp.dhis.dataelement.DataElement) List(java.util.List) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) 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.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 9 with OptionSet

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

the class CsvMetadataImportTest method testOptionSetImport.

@Test
public void testOptionSetImport() throws Exception {
    input = new ClassPathResource("metadata/optionSets.csv").getInputStream();
    Metadata metadata = csvImportService.fromCsv(input, OptionSet.class);
    assertEquals(4, metadata.getOptionSets().size());
    assertEquals(3, metadata.getOptionSets().get(0).getOptions().size());
    assertEquals(3, metadata.getOptionSets().get(1).getOptions().size());
    assertEquals(3, metadata.getOptionSets().get(2).getOptions().size());
    assertEquals(3, metadata.getOptionSets().get(3).getOptions().size());
    MetadataImportParams params = new MetadataImportParams();
    params.addMetadata(schemaService.getMetadataSchemas(), metadata);
    ImportReport importReport = importService.importMetadata(params);
    assertEquals(16, importReport.getStats().getCreated());
    List<OptionSet> optionSets = new ArrayList<>(optionService.getAllOptionSets());
    assertEquals(4, optionSets.size());
    assertEquals(3, optionSets.get(0).getOptions().size());
    assertEquals(3, optionSets.get(1).getOptions().size());
    assertEquals(3, optionSets.get(2).getOptions().size());
    assertEquals(3, optionSets.get(3).getOptions().size());
}
Also used : ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) ArrayList(java.util.ArrayList) OptionSet(org.hisp.dhis.option.OptionSet) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 10 with OptionSet

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

the class DefaultCsvImportService method setOptionSetsFromCsv.

/**
     * Option set format:
     * <p>
     * <ul>
     * <li>option set name</li>
     * <li>option set uid</li>
     * <li>option set code</li>
     * <li>option name</li>
     * <li>option uid</li>
     * <li>option code</li>
     * </ul>
     */
private void setOptionSetsFromCsv(CsvReader reader, Metadata metadata) throws IOException {
    ListMap<String, Option> nameOptionMap = new ListMap<>();
    Map<String, OptionSet> nameOptionSetMap = new HashMap<>();
    while (reader.readRecord()) {
        String[] values = reader.getValues();
        if (values != null && values.length > 0) {
            OptionSet optionSet = new OptionSet();
            setIdentifiableObject(optionSet, values);
            optionSet.setAutoFields();
            optionSet.setValueType(ValueType.TEXT);
            Option option = new Option();
            option.setName(getSafe(values, 3, null, 230));
            option.setUid(getSafe(values, 4, CodeGenerator.generateUid(), 11));
            option.setCode(getSafe(values, 5, null, 50));
            option.setAutoFields();
            if (optionSet.getName() == null || option.getCode() == null) {
                continue;
            }
            nameOptionSetMap.put(optionSet.getName(), optionSet);
            nameOptionMap.putValue(optionSet.getName(), option);
            metadata.getOptions().add(option);
        }
    }
    for (String optionSetName : nameOptionSetMap.keySet()) {
        OptionSet optionSet = nameOptionSetMap.get(optionSetName);
        List<Option> options = new ArrayList<>(nameOptionMap.get(optionSetName));
        optionSet.setOptions(options);
        metadata.getOptionSets().add(optionSet);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet) ListMap(org.hisp.dhis.common.ListMap)

Aggregations

OptionSet (org.hisp.dhis.option.OptionSet)12 Option (org.hisp.dhis.option.Option)6 DataElement (org.hisp.dhis.dataelement.DataElement)5 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 DhisSpringTest (org.hisp.dhis.DhisSpringTest)3 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)3 Attribute (org.hisp.dhis.attribute.Attribute)2 DataElementCategoryOption (org.hisp.dhis.dataelement.DataElementCategoryOption)2 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)2 DataSet (org.hisp.dhis.dataset.DataSet)2 ProgramDataElementDimensionItem (org.hisp.dhis.program.ProgramDataElementDimensionItem)2 Test (org.junit.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Rectangle (com.lowagie.text.Rectangle)1 PdfPCell (com.lowagie.text.pdf.PdfPCell)1 PdfPTable (com.lowagie.text.pdf.PdfPTable)1 Date (java.util.Date)1