changed fetch type, edited API docs

This commit is contained in:
Markus Geiss
2015-12-14 10:22:59 +01:00
parent c50a45b2c8
commit 4444a8a0e9
10 changed files with 124 additions and 96 deletions
@@ -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<SurveyData> 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();
@@ -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;
}
@@ -9,6 +9,7 @@ import java.util.List;
public class QuestionData {
private Long id;
private List<ResponseData> responseDatas;
private String componentKey;
private String key;
@@ -20,9 +21,10 @@ public class QuestionData {
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) {
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<ResponseData> getResponseDatas() {
return responseDatas;
}
@@ -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;
}
@@ -18,7 +18,7 @@ public class Question extends AbstractPersistable<Long> {
@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<Response> responses;
@@ -15,11 +15,11 @@ import java.util.List;
@Table(name = "m_surveys")
public class Survey extends AbstractPersistable<Long> {
@OneToMany(mappedBy = "survey", fetch = FetchType.EAGER)
@OneToMany(mappedBy = "survey", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OrderBy("sequenceNo")
private List<Component> components;
@OneToMany(mappedBy = "survey", fetch = FetchType.EAGER)
@OneToMany(mappedBy = "survey", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OrderBy("sequenceNo")
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")
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);
}
@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();
@@ -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<Component> mapComponentDatas(final List<ComponentData> componentDatas) {
private static List<Component> mapComponentDatas(final List<ComponentData> componentDatas, final Survey survey) {
final List<Component> 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<QuestionData> 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()
));
}
}