diff --git a/api-docs/apiLive.htm b/api-docs/apiLive.htm index b5e40ca60..551de5b13 100644 --- a/api-docs/apiLive.htm +++ b/api-docs/apiLive.htm @@ -37724,24 +37724,24 @@ Request Body: "countryCode":"KE", "validFrom":null, "validTo":null, - "components": + "componentDatas": [ { "key":"Household", - "text":"Information about the household." + "text":"Information about the household.", "description":null, "sequenceNo":1 } - ] - "questions": + ], + "questionDatas": [ { "componentKey":"Household", "key":"Familiy members", - "text":"How many persons live in the household?" + "text":"How many persons live in the household?", "description":null, "sequenceNo":1, - "responses": + "responseDatas": [ { "text":"1 to 3", @@ -37761,7 +37761,8 @@ Request Body: ] } ] -} +} + 200 OK @@ -37784,50 +37785,52 @@ No Request Body [ { - "id":1 - "key":"ppi-kenya-2010", - "name":"PPI Survey for Kenya, version 2010", - "description":null, - "countryCode":"KE", - "validFrom":"2015-11-25T00:00:00.000Z", - "validTo":"2115-12-31T23:59:59.999Z", - "components": - [ + "id": 1, + "componentDatas": [ { - "key":"Household", - "text":"Information about the household." - "description":null, - "sequenceNo":1 + "id": 1, + "key": "Household", + "text": "Information about the household.", + "description": null, + "sequenceNo": 1 } - ] - "questions": - [ + ], + "questionDatas": [ { - "componentKey":"Household", - "key":"Familiy members", - "text":"How many persons live in the household?" - "description":null, - "sequenceNo":1, - "responses": - [ + "id": 1, + "responseDatas": [ { - "text":"1 to 3", - "value":1, - "sequenceNo":1 + "id": 1, + "text": "1 to 3", + "value": 1, + "sequenceNo": 1 }, { - "text":"3 to 7", - "value":5, - "sequenceNo":2 + "id": 2, + "text": "3 to 7", + "value": 5, + "sequenceNo": 2 }, { - "text":"more than 7", - "value":13, - "sequenceNo":3 + "id": 3, + "text": "more than 7", + "value": 13, + "sequenceNo": 3 } - ] + ], + "componentKey": "Household", + "key": "Familiy members", + "text": "How many persons live in the household?", + "description": null, + "sequenceNo": 1 } - ] + ], + "key": "ppi-kenya-2010", + "name": "PPI Survey for Kenya, version 2010", + "description": null, + "countryCode": "KE", + "validFrom": 1450047600000, + "validTo": 4607276399000 } ] @@ -37849,52 +37852,53 @@ No Request Body { - "id":1 - "key":"ppi-kenya-2010", - "name":"PPI Survey for Kenya, version 2010", - "description":null, - "countryCode":"KE", - "validFrom":"2015-11-25T00:00:00.000Z", - "validTo":"2115-12-31T23:59:59.999Z", - "components": - [ + "id": 1, + "componentDatas": [ { - "key":"Household", - "text":"Information about the household." - "description":null, - "sequenceNo":1 + "id": 1, + "key": "Household", + "text": "Information about the household.", + "description": null, + "sequenceNo": 1 } - ] - "questions": - [ + ], + "questionDatas": [ { - "componentKey":"Household", - "key":"Familiy members", - "text":"How many persons live in the household?" - "description":null, - "sequenceNo":1, - "responses": - [ + "id": 1, + "responseDatas": [ { - "text":"1 to 3", - "value":1, - "sequenceNo":1 + "id": 1, + "text": "1 to 3", + "value": 1, + "sequenceNo": 1 }, { - "text":"3 to 7", - "value":5, - "sequenceNo":2 + "id": 2, + "text": "3 to 7", + "value": 5, + "sequenceNo": 2 }, { - "text":"more than 7", - "value":13, - "sequenceNo":3 + "id": 3, + "text": "more than 7", + "value": 13, + "sequenceNo": 3 } - ] + ], + "componentKey": "Household", + "key": "Familiy members", + "text": "How many persons live in the household?", + "description": null, + "sequenceNo": 1 } - ] + ], + "key": "ppi-kenya-2010", + "name": "PPI Survey for Kenya, version 2010", + "description": null, + "countryCode": "KE", + "validFrom": 1450047600000, + "validTo": 4607276399000 } - diff --git a/mifosng-provider/src/main/java/org/mifosplatform/spm/api/SpmApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/spm/api/SpmApiResource.java index c994f6c3a..aa32dcabb 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/spm/api/SpmApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/spm/api/SpmApiResource.java @@ -14,6 +14,7 @@ import org.mifosplatform.spm.util.SurveyMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @@ -38,6 +39,7 @@ public class SpmApiResource { @GET @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) + @Transactional public List fetchActiveSurveys() { this.securityContext.authenticatedUser(); @@ -58,6 +60,7 @@ public class SpmApiResource { @Path("/{id}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) + @Transactional public SurveyData findSurvey(@PathParam("id") final Long id) { this.securityContext.authenticatedUser(); @@ -73,6 +76,7 @@ public class SpmApiResource { @POST @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) + @Transactional public void createSurvey(final SurveyData surveyData) { this.securityContext.authenticatedUser(); @@ -85,6 +89,7 @@ public class SpmApiResource { @Path("/{id}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) + @Transactional public void deactivateSurvey(@PathParam("id") final Long id) { this.securityContext.authenticatedUser(); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/spm/data/ComponentData.java b/mifosng-provider/src/main/java/org/mifosplatform/spm/data/ComponentData.java index ee221e9e2..fbebc6ad9 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/spm/data/ComponentData.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/spm/data/ComponentData.java @@ -7,6 +7,7 @@ package org.mifosplatform.spm.data; public class ComponentData { + private Long id; private String key; private String text; private String description; @@ -16,15 +17,20 @@ public class ComponentData { super(); } - public ComponentData(final String key, final String text, + public ComponentData(final Long id, final String key, final String text, final String description, final Integer sequenceNo) { super(); + this.id = id; this.key = key; this.text = text; this.description = description; this.sequenceNo = sequenceNo; } + public Long getId() { + return id; + } + public String getKey() { return key; } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/spm/data/QuestionData.java b/mifosng-provider/src/main/java/org/mifosplatform/spm/data/QuestionData.java index 31f6d990f..b03c1e4e0 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/spm/data/QuestionData.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/spm/data/QuestionData.java @@ -9,6 +9,7 @@ import java.util.List; public class QuestionData { + private Long id; private List responseDatas; private String componentKey; private String key; @@ -20,9 +21,10 @@ public class QuestionData { super(); } - public QuestionData(final List responseDatas, final String componentKey, final String key, + public QuestionData(final Long id, final List responseDatas, final String componentKey, final String key, final String text, final String description, final Integer sequenceNo) { super(); + this.id = id; this.responseDatas = responseDatas; this.componentKey = componentKey; this.key = key; @@ -31,6 +33,10 @@ public class QuestionData { this.sequenceNo = sequenceNo; } + public Long getId() { + return id; + } + public List getResponseDatas() { return responseDatas; } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/spm/data/ResponseData.java b/mifosng-provider/src/main/java/org/mifosplatform/spm/data/ResponseData.java index c96bafac8..218fb2692 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/spm/data/ResponseData.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/spm/data/ResponseData.java @@ -7,6 +7,7 @@ package org.mifosplatform.spm.data; public class ResponseData { + private Long id; private String text; private Integer value; private Integer sequenceNo; @@ -15,14 +16,19 @@ public class ResponseData { super(); } - public ResponseData(final String text, final Integer value, + public ResponseData(final Long id, final String text, final Integer value, final Integer sequenceNo) { super(); + this.id = id; this.text = text; this.value = value; this.sequenceNo = sequenceNo; } + public Long getId() { + return id; + } + public String getText() { return text; } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/spm/domain/Question.java b/mifosng-provider/src/main/java/org/mifosplatform/spm/domain/Question.java index 0d4c3c36f..234864fc9 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/spm/domain/Question.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/spm/domain/Question.java @@ -18,7 +18,7 @@ public class Question extends AbstractPersistable { @JoinColumn(name = "survey_id") private Survey survey; - @OneToMany(mappedBy = "question", fetch = FetchType.EAGER) + @OneToMany(mappedBy = "question", fetch = FetchType.LAZY, cascade = CascadeType.ALL) @OrderBy("sequenceNo") private List responses; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/spm/domain/Survey.java b/mifosng-provider/src/main/java/org/mifosplatform/spm/domain/Survey.java index 4d61786f0..bb2a6a92c 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/spm/domain/Survey.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/spm/domain/Survey.java @@ -15,11 +15,11 @@ import java.util.List; @Table(name = "m_surveys") public class Survey extends AbstractPersistable { - @OneToMany(mappedBy = "survey", fetch = FetchType.EAGER) + @OneToMany(mappedBy = "survey", fetch = FetchType.LAZY, cascade = CascadeType.ALL) @OrderBy("sequenceNo") private List components; - @OneToMany(mappedBy = "survey", fetch = FetchType.EAGER) + @OneToMany(mappedBy = "survey", fetch = FetchType.LAZY, cascade = CascadeType.ALL) @OrderBy("sequenceNo") private List questions; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/spm/repository/SurveyRepository.java b/mifosng-provider/src/main/java/org/mifosplatform/spm/repository/SurveyRepository.java index 8e644a90b..dce877ccf 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/spm/repository/SurveyRepository.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/spm/repository/SurveyRepository.java @@ -18,5 +18,6 @@ public interface SurveyRepository extends JpaRepository { @Query("select s from Survey s where :pointInTime between s.validFrom and s.validTo") List fetchActiveSurveys(@Param("pointInTime") final Date pointInTime); - Survey findByKey(final String key); + @Query("select s from Survey s where s.key = :key and :pointInTime between s.validFrom and s.validTo") + Survey findByKey(@Param("key") final String key, @Param("pointInTime") final Date pointInTime); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/spm/service/SpmService.java b/mifosng-provider/src/main/java/org/mifosplatform/spm/service/SpmService.java index 7d884f91f..54b26b14c 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/spm/service/SpmService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/spm/service/SpmService.java @@ -42,28 +42,27 @@ public class SpmService { return this.surveyRepository.findOne(id); } - @Transactional public Survey createSurvey(final Survey survey) { this.securityContext.authenticatedUser(); - final Survey previousSurvey = this.surveyRepository.findByKey(survey.getKey()); + final Survey previousSurvey = this.surveyRepository.findByKey(survey.getKey(), new Date()); if (previousSurvey != null) { this.deactivateSurvey(previousSurvey.getId()); } // set valid from to start of today - final DateTime dateTime = DateTime + final DateTime validFrom = DateTime .now() .withHourOfDay(0) .withMinuteOfHour(0) .withSecondOfMinute(0) .withMillisOfSecond(0); - survey.setValidFrom(dateTime.toDate()); + survey.setValidFrom(validFrom.toDate()); // set valid from to end in 100 years - dateTime + final DateTime validTo = validFrom .withDayOfMonth(31) .withMonthOfYear(12) .withHourOfDay(23) @@ -72,12 +71,11 @@ public class SpmService { .withMillisOfSecond(999) .plusYears(100); - survey.setValidTo(dateTime.toDate()); + survey.setValidTo(validTo.toDate()); return this.surveyRepository.save(survey); } - @Transactional public void deactivateSurvey(final Long id) { this.securityContext.authenticatedUser(); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/spm/util/SurveyMapper.java b/mifosng-provider/src/main/java/org/mifosplatform/spm/util/SurveyMapper.java index a2dcf5c00..458f4189e 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/spm/util/SurveyMapper.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/spm/util/SurveyMapper.java @@ -34,7 +34,7 @@ public class SurveyMapper { public static Survey map(final SurveyData surveyData) { final Survey survey = new Survey(); - survey.setComponents(SurveyMapper.mapComponentDatas(surveyData.getComponentDatas())); + survey.setComponents(SurveyMapper.mapComponentDatas(surveyData.getComponentDatas(), survey)); survey.setQuestions(SurveyMapper.mapQuestionDatas(surveyData.getQuestionDatas(), survey)); survey.setKey(surveyData.getKey()); survey.setName(surveyData.getName()); @@ -48,18 +48,20 @@ public class SurveyMapper { if (components != null) { for (final Component component : components) { componentDatas.add(new ComponentData( - component.getKey(), component.getText(), component.getDescription(), component.getSequenceNo() + component.getId(), component.getKey(), component.getText(), component.getDescription(), + component.getSequenceNo() )); } } return componentDatas; } - private static List mapComponentDatas(final List componentDatas) { + private static List mapComponentDatas(final List componentDatas, final Survey survey) { final List components = new ArrayList<>(); if (componentDatas != null) { for (final ComponentData componentData : componentDatas) { final Component component = new Component(); + component.setSurvey(survey); component.setKey(componentData.getKey()); component.setText(componentData.getText()); component.setDescription(componentData.getDescription()); @@ -74,8 +76,8 @@ public class SurveyMapper { final List questionDatas = new ArrayList<>(); if (questions != null) { for (final Question question : questions) { - questionDatas.add(new QuestionData( - SurveyMapper.mapResponses(question.getResponses()), question.getComponentKey(), question.getKey(), + questionDatas.add(new QuestionData(question.getId(), + SurveyMapper.mapResponses(question.getResponses()), question.getComponentKey(), question.getKey(), question.getText(), question.getDescription(), question.getSequenceNo() )); } @@ -106,7 +108,7 @@ public class SurveyMapper { if (responses != null) { for (final Response response : responses) { responseDatas.add(new ResponseData( - response.getText(), response.getValue(), response.getSequenceNo() + response.getId(), response.getText(), response.getValue(), response.getSequenceNo() )); } }