Merge pull request #1562 from mgeiss/MIFOSX-SPM-2

changed fetch type, edited API docs
This commit is contained in:
Adi Narayana Raju
2015-12-16 09:20:47 +05:30
10 changed files with 124 additions and 96 deletions
+80 -76
View File
@@ -37724,24 +37724,24 @@ Request Body:
"countryCode":"KE", "countryCode":"KE",
"validFrom":null, "validFrom":null,
"validTo":null, "validTo":null,
"components": "componentDatas":
[ [
{ {
"key":"Household", "key":"Household",
"text":"Information about the household." "text":"Information about the household.",
"description":null, "description":null,
"sequenceNo":1 "sequenceNo":1
} }
] ],
"questions": "questionDatas":
[ [
{ {
"componentKey":"Household", "componentKey":"Household",
"key":"Familiy members", "key":"Familiy members",
"text":"How many persons live in the household?" "text":"How many persons live in the household?",
"description":null, "description":null,
"sequenceNo":1, "sequenceNo":1,
"responses": "responseDatas":
[ [
{ {
"text":"1 to 3", "text":"1 to 3",
@@ -37761,7 +37761,8 @@ Request Body:
] ]
} }
] ]
} </code> }
</code>
<code class="method-response"> <code class="method-response">
200 OK 200 OK
</code> </code>
@@ -37784,50 +37785,52 @@ No Request Body
<code class="method-response"> <code class="method-response">
[ [
{ {
"id":1 "id": 1,
"key":"ppi-kenya-2010", "componentDatas": [
"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":
[
{ {
"key":"Household", "id": 1,
"text":"Information about the household." "key": "Household",
"description":null, "text": "Information about the household.",
"sequenceNo":1 "description": null,
"sequenceNo": 1
} }
] ],
"questions": "questionDatas": [
[
{ {
"componentKey":"Household", "id": 1,
"key":"Familiy members", "responseDatas": [
"text":"How many persons live in the household?"
"description":null,
"sequenceNo":1,
"responses":
[
{ {
"text":"1 to 3", "id": 1,
"value":1, "text": "1 to 3",
"sequenceNo":1 "value": 1,
"sequenceNo": 1
}, },
{ {
"text":"3 to 7", "id": 2,
"value":5, "text": "3 to 7",
"sequenceNo":2 "value": 5,
"sequenceNo": 2
}, },
{ {
"text":"more than 7", "id": 3,
"value":13, "text": "more than 7",
"sequenceNo":3 "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
} }
] ]
</code> </code>
@@ -37849,52 +37852,53 @@ No Request Body
</code> </code>
<code class="method-response"> <code class="method-response">
{ {
"id":1 "id": 1,
"key":"ppi-kenya-2010", "componentDatas": [
"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":
[
{ {
"key":"Household", "id": 1,
"text":"Information about the household." "key": "Household",
"description":null, "text": "Information about the household.",
"sequenceNo":1 "description": null,
"sequenceNo": 1
} }
] ],
"questions": "questionDatas": [
[
{ {
"componentKey":"Household", "id": 1,
"key":"Familiy members", "responseDatas": [
"text":"How many persons live in the household?"
"description":null,
"sequenceNo":1,
"responses":
[
{ {
"text":"1 to 3", "id": 1,
"value":1, "text": "1 to 3",
"sequenceNo":1 "value": 1,
"sequenceNo": 1
}, },
{ {
"text":"3 to 7", "id": 2,
"value":5, "text": "3 to 7",
"sequenceNo":2 "value": 5,
"sequenceNo": 2
}, },
{ {
"text":"more than 7", "id": 3,
"value":13, "text": "more than 7",
"sequenceNo":3 "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
} }
</code> </code>
</div> </div>
</div> </div>
@@ -14,6 +14,7 @@ import org.mifosplatform.spm.util.SurveyMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
@@ -38,6 +39,7 @@ public class SpmApiResource {
@GET @GET
@Consumes({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
@Transactional
public List<SurveyData> fetchActiveSurveys() { public List<SurveyData> fetchActiveSurveys() {
this.securityContext.authenticatedUser(); this.securityContext.authenticatedUser();
@@ -58,6 +60,7 @@ public class SpmApiResource {
@Path("/{id}") @Path("/{id}")
@Consumes({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
@Transactional
public SurveyData findSurvey(@PathParam("id") final Long id) { public SurveyData findSurvey(@PathParam("id") final Long id) {
this.securityContext.authenticatedUser(); this.securityContext.authenticatedUser();
@@ -73,6 +76,7 @@ public class SpmApiResource {
@POST @POST
@Consumes({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
@Transactional
public void createSurvey(final SurveyData surveyData) { public void createSurvey(final SurveyData surveyData) {
this.securityContext.authenticatedUser(); this.securityContext.authenticatedUser();
@@ -85,6 +89,7 @@ public class SpmApiResource {
@Path("/{id}") @Path("/{id}")
@Consumes({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
@Transactional
public void deactivateSurvey(@PathParam("id") final Long id) { public void deactivateSurvey(@PathParam("id") final Long id) {
this.securityContext.authenticatedUser(); this.securityContext.authenticatedUser();
@@ -7,6 +7,7 @@ package org.mifosplatform.spm.data;
public class ComponentData { public class ComponentData {
private Long id;
private String key; private String key;
private String text; private String text;
private String description; private String description;
@@ -16,15 +17,20 @@ public class ComponentData {
super(); 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) { final String description, final Integer sequenceNo) {
super(); super();
this.id = id;
this.key = key; this.key = key;
this.text = text; this.text = text;
this.description = description; this.description = description;
this.sequenceNo = sequenceNo; this.sequenceNo = sequenceNo;
} }
public Long getId() {
return id;
}
public String getKey() { public String getKey() {
return key; return key;
} }
@@ -9,6 +9,7 @@ import java.util.List;
public class QuestionData { public class QuestionData {
private Long id;
private List<ResponseData> responseDatas; private List<ResponseData> responseDatas;
private String componentKey; private String componentKey;
private String key; private String key;
@@ -20,9 +21,10 @@ public class QuestionData {
super(); super();
} }
public QuestionData(final List<ResponseData> responseDatas, final String componentKey, final String key, public QuestionData(final Long id, final List<ResponseData> responseDatas, final String componentKey, final String key,
final String text, final String description, final Integer sequenceNo) { final String text, final String description, final Integer sequenceNo) {
super(); super();
this.id = id;
this.responseDatas = responseDatas; this.responseDatas = responseDatas;
this.componentKey = componentKey; this.componentKey = componentKey;
this.key = key; this.key = key;
@@ -31,6 +33,10 @@ public class QuestionData {
this.sequenceNo = sequenceNo; this.sequenceNo = sequenceNo;
} }
public Long getId() {
return id;
}
public List<ResponseData> getResponseDatas() { public List<ResponseData> getResponseDatas() {
return responseDatas; return responseDatas;
} }
@@ -7,6 +7,7 @@ package org.mifosplatform.spm.data;
public class ResponseData { public class ResponseData {
private Long id;
private String text; private String text;
private Integer value; private Integer value;
private Integer sequenceNo; private Integer sequenceNo;
@@ -15,14 +16,19 @@ public class ResponseData {
super(); super();
} }
public ResponseData(final String text, final Integer value, public ResponseData(final Long id, final String text, final Integer value,
final Integer sequenceNo) { final Integer sequenceNo) {
super(); super();
this.id = id;
this.text = text; this.text = text;
this.value = value; this.value = value;
this.sequenceNo = sequenceNo; this.sequenceNo = sequenceNo;
} }
public Long getId() {
return id;
}
public String getText() { public String getText() {
return text; return text;
} }
@@ -18,7 +18,7 @@ public class Question extends AbstractPersistable<Long> {
@JoinColumn(name = "survey_id") @JoinColumn(name = "survey_id")
private Survey survey; private Survey survey;
@OneToMany(mappedBy = "question", fetch = FetchType.EAGER) @OneToMany(mappedBy = "question", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OrderBy("sequenceNo") @OrderBy("sequenceNo")
private List<Response> responses; private List<Response> responses;
@@ -15,11 +15,11 @@ import java.util.List;
@Table(name = "m_surveys") @Table(name = "m_surveys")
public class Survey extends AbstractPersistable<Long> { public class Survey extends AbstractPersistable<Long> {
@OneToMany(mappedBy = "survey", fetch = FetchType.EAGER) @OneToMany(mappedBy = "survey", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OrderBy("sequenceNo") @OrderBy("sequenceNo")
private List<Component> components; private List<Component> components;
@OneToMany(mappedBy = "survey", fetch = FetchType.EAGER) @OneToMany(mappedBy = "survey", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OrderBy("sequenceNo") @OrderBy("sequenceNo")
private List<Question> questions; private List<Question> questions;
@@ -18,5 +18,6 @@ public interface SurveyRepository extends JpaRepository<Survey, Long> {
@Query("select s from Survey s where :pointInTime between s.validFrom and s.validTo") @Query("select s from Survey s where :pointInTime between s.validFrom and s.validTo")
List<Survey> fetchActiveSurveys(@Param("pointInTime") final Date pointInTime); List<Survey> 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);
} }
@@ -42,28 +42,27 @@ public class SpmService {
return this.surveyRepository.findOne(id); return this.surveyRepository.findOne(id);
} }
@Transactional
public Survey createSurvey(final Survey survey) { public Survey createSurvey(final Survey survey) {
this.securityContext.authenticatedUser(); this.securityContext.authenticatedUser();
final Survey previousSurvey = this.surveyRepository.findByKey(survey.getKey()); final Survey previousSurvey = this.surveyRepository.findByKey(survey.getKey(), new Date());
if (previousSurvey != null) { if (previousSurvey != null) {
this.deactivateSurvey(previousSurvey.getId()); this.deactivateSurvey(previousSurvey.getId());
} }
// set valid from to start of today // set valid from to start of today
final DateTime dateTime = DateTime final DateTime validFrom = DateTime
.now() .now()
.withHourOfDay(0) .withHourOfDay(0)
.withMinuteOfHour(0) .withMinuteOfHour(0)
.withSecondOfMinute(0) .withSecondOfMinute(0)
.withMillisOfSecond(0); .withMillisOfSecond(0);
survey.setValidFrom(dateTime.toDate()); survey.setValidFrom(validFrom.toDate());
// set valid from to end in 100 years // set valid from to end in 100 years
dateTime final DateTime validTo = validFrom
.withDayOfMonth(31) .withDayOfMonth(31)
.withMonthOfYear(12) .withMonthOfYear(12)
.withHourOfDay(23) .withHourOfDay(23)
@@ -72,12 +71,11 @@ public class SpmService {
.withMillisOfSecond(999) .withMillisOfSecond(999)
.plusYears(100); .plusYears(100);
survey.setValidTo(dateTime.toDate()); survey.setValidTo(validTo.toDate());
return this.surveyRepository.save(survey); return this.surveyRepository.save(survey);
} }
@Transactional
public void deactivateSurvey(final Long id) { public void deactivateSurvey(final Long id) {
this.securityContext.authenticatedUser(); this.securityContext.authenticatedUser();
@@ -34,7 +34,7 @@ public class SurveyMapper {
public static Survey map(final SurveyData surveyData) { public static Survey map(final SurveyData surveyData) {
final Survey survey = new Survey(); 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.setQuestions(SurveyMapper.mapQuestionDatas(surveyData.getQuestionDatas(), survey));
survey.setKey(surveyData.getKey()); survey.setKey(surveyData.getKey());
survey.setName(surveyData.getName()); survey.setName(surveyData.getName());
@@ -48,18 +48,20 @@ public class SurveyMapper {
if (components != null) { if (components != null) {
for (final Component component : components) { for (final Component component : components) {
componentDatas.add(new ComponentData( componentDatas.add(new ComponentData(
component.getKey(), component.getText(), component.getDescription(), component.getSequenceNo() component.getId(), component.getKey(), component.getText(), component.getDescription(),
component.getSequenceNo()
)); ));
} }
} }
return componentDatas; return componentDatas;
} }
private static List<Component> mapComponentDatas(final List<ComponentData> componentDatas) { private static List<Component> mapComponentDatas(final List<ComponentData> componentDatas, final Survey survey) {
final List<Component> components = new ArrayList<>(); final List<Component> components = new ArrayList<>();
if (componentDatas != null) { if (componentDatas != null) {
for (final ComponentData componentData : componentDatas) { for (final ComponentData componentData : componentDatas) {
final Component component = new Component(); final Component component = new Component();
component.setSurvey(survey);
component.setKey(componentData.getKey()); component.setKey(componentData.getKey());
component.setText(componentData.getText()); component.setText(componentData.getText());
component.setDescription(componentData.getDescription()); component.setDescription(componentData.getDescription());
@@ -74,8 +76,8 @@ public class SurveyMapper {
final List<QuestionData> questionDatas = new ArrayList<>(); final List<QuestionData> questionDatas = new ArrayList<>();
if (questions != null) { if (questions != null) {
for (final Question question : questions) { for (final Question question : questions) {
questionDatas.add(new QuestionData( questionDatas.add(new QuestionData(question.getId(),
SurveyMapper.mapResponses(question.getResponses()), question.getComponentKey(), question.getKey(), SurveyMapper.mapResponses(question.getResponses()), question.getComponentKey(), question.getKey(),
question.getText(), question.getDescription(), question.getSequenceNo() question.getText(), question.getDescription(), question.getSequenceNo()
)); ));
} }
@@ -106,7 +108,7 @@ public class SurveyMapper {
if (responses != null) { if (responses != null) {
for (final Response response : responses) { for (final Response response : responses) {
responseDatas.add(new ResponseData( responseDatas.add(new ResponseData(
response.getText(), response.getValue(), response.getSequenceNo() response.getId(), response.getText(), response.getValue(), response.getSequenceNo()
)); ));
} }
} }