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;
}
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);
});
}
}
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());
}
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());
}
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);
}
}
Aggregations