diff --git a/goonj/src/main/java/org/avni_integration_service/goonj/repository/ActivityRepository.java b/goonj/src/main/java/org/avni_integration_service/goonj/repository/ActivityRepository.java index 33e7ca97..6d28c8e8 100644 --- a/goonj/src/main/java/org/avni_integration_service/goonj/repository/ActivityRepository.java +++ b/goonj/src/main/java/org/avni_integration_service/goonj/repository/ActivityRepository.java @@ -27,6 +27,7 @@ import java.util.stream.Collectors; import static org.avni_integration_service.goonj.config.GoonjMappingDbConstants.*; +import static org.avni_integration_service.goonj.util.NumberUtil.getInteger; @Component("ActivityRepository") public class ActivityRepository extends GoonjBaseRepository implements ActivityConstants { @@ -129,7 +130,7 @@ private ActivityDTO createActivityRequest(Subject subject) { activityDTO.setOtherObjective((String) subject.getObservation(SPECIFY_OTHER_FOR_OBJECTIVE_OF_WORK)); /* Measurement fields */ activityDTO.setMeasurementType((String) subject.getObservation(MEASUREMENTS_TYPE)); - activityDTO.setNos((Integer) subject.getObservation(NOS)); + activityDTO.setNos(getInteger((subject.getObservation(NOS)))); activityDTO.setBreadth((subject.getObservation(BREADTH) == null) ? 0D : Double.valueOf(subject.getObservation(BREADTH).toString())); activityDTO.setDiameter((subject.getObservation(DIAMETER) == null) ? 0D : Double.valueOf(subject.getObservation(DIAMETER).toString())); activityDTO.setLength((subject.getObservation(LENGTH) == null) ? 0D : Double.valueOf(subject.getObservation(LENGTH).toString())); diff --git a/goonj/src/main/java/org/avni_integration_service/goonj/util/NumberUtil.java b/goonj/src/main/java/org/avni_integration_service/goonj/util/NumberUtil.java new file mode 100644 index 00000000..e78a65bc --- /dev/null +++ b/goonj/src/main/java/org/avni_integration_service/goonj/util/NumberUtil.java @@ -0,0 +1,10 @@ +package org.avni_integration_service.goonj.util; + +public class NumberUtil { + public static Integer getInteger(Object value) { + if (value == null) { + return null; + } + return Double.valueOf(value.toString()).intValue(); + } +} diff --git a/goonj/src/test/java/org/avni_integration_service/goonj/util/NumberUtilTest.java b/goonj/src/test/java/org/avni_integration_service/goonj/util/NumberUtilTest.java new file mode 100644 index 00000000..87922295 --- /dev/null +++ b/goonj/src/test/java/org/avni_integration_service/goonj/util/NumberUtilTest.java @@ -0,0 +1,27 @@ +package org.avni_integration_service.goonj.util; + +import org.avni_integration_service.util.ObjectJsonMapper; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.avni_integration_service.goonj.util.NumberUtil.getInteger; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class NumberUtilTest { + + @Test + public void shouldConvertToInteger() { + Map map = ObjectJsonMapper.readValue( + "{ \n" + + " \"a\": \"1\", \n" + + " \"b\": 1, \n" + + " \"c\": 1.5 \n" + + "}", java.util.Map.class); + assertEquals(getInteger(map.get("a")), 1); + assertEquals(getInteger(map.get("b")), 1); + assertEquals(getInteger(map.get("c")), 1); + assertEquals(getInteger(map.get("d")), null); + } + +}