Updated to live database

parent fe5a2d86
......@@ -3,11 +3,6 @@ apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
//jar {
// baseName = 'wst-dev'
// version = '0.0.1'
//}
war {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
......@@ -25,7 +20,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.5.RELEASE")
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.1.RELEASE")
}
}
......@@ -40,7 +35,7 @@ dependencies {
compile(
"org.springframework.boot:spring-boot-starter-web",
"org.springframework.boot:spring-boot-starter-data-jpa",
"com.h2database:h2"
"net.sourceforge.jtds:jtds:1.3.1"
)
testCompile(
......
#Wed Aug 27 12:04:51 CEST 2014
#Tue Jan 27 19:37:53 CET 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.0-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-bin.zip
package no.nsd.websurvey.tromso;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.domain.Survey;
import no.nsd.websurvey.tromso.repository.ParticipantRepository;
import no.nsd.websurvey.tromso.repository.SurveyRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.*;
/**
* Persists test data for demonstration and tests.
* Populate the embedded H2 database with two {@link no.nsd.websurvey.tromso.domain.Survey} entities
* each containing 100 {@link no.nsd.websurvey.tromso.domain.Participant} entities.
*/
public class InitializeEmbeddedDataBaseData {
private static final Logger logger = LoggerFactory.getLogger(InitializeEmbeddedDataBaseData.class);
public InitializeEmbeddedDataBaseData(SurveyRepository surveyRepository, ParticipantRepository participantRepository) {
logger.info("Creating entities and populating H2 embedded datasource");
Survey surveyOne = surveyRepository.save(new Survey());
Survey surveyTwo = surveyRepository.save(new Survey());
for (int i : generateIdsFromFile()) {
final Participant p = new Participant(i, statusCode(), surveyOne);
participantRepository.save(p);
}
for (int i : generateIdsFromFile()) {
Participant p = new Participant(i, statusCode(), surveyTwo);
participantRepository.save(p);
}
}
private List<Integer> generateIdsFromFile() {
List<Integer> list = new ArrayList<>();
try(
InputStreamReader fileReader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("ids"));
BufferedReader bufferedReader = new BufferedReader(fileReader)
) {
String s;
while((s = bufferedReader.readLine()) != null) {
list.add(Integer.parseInt(s));
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
* Provide random status codes to provide the correct data
* necessary for the API.
* @return a random {@link java.lang.Integer} between 0 and 3.
*/
private static int statusCode() {
Random rand = new Random();
return rand.nextInt((3)+1);
}
}
package no.nsd.websurvey.tromso;
import no.nsd.websurvey.tromso.repository.ParticipantRepository;
import no.nsd.websurvey.tromso.repository.SurveyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
......@@ -14,29 +11,11 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import javax.annotation.PostConstruct;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class WebSurveyTromso extends SpringBootServletInitializer {
@Autowired
private SurveyRepository surveyRepository;
@Autowired
private ParticipantRepository participantRepository;
/**
* Make sure that dummy data is populated after the context is
* running to provide DI of the correct dependencies.
*/
@PostConstruct
public void init() {
InitializeEmbeddedDataBaseData initializeEmbeddedDataBaseData =
new InitializeEmbeddedDataBaseData(surveyRepository, participantRepository);
}
/**
* Not using the default {@link org.springframework.http.converter.HttpMessageConverter} implementation
* because setPrettyPrint(true) is not enable by default by Spring Boot.
......
package no.nsd.websurvey.tromso.controller;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.service.ParticipantService;
import no.nsd.websurvey.tromso.service.SurveyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "/participant")
@Description("Controller for presenting Participants.")
public class ParticipantController {
@Autowired
private ParticipantService participantService;
@Autowired
private SurveyService surveyService;
@RequestMapping(value = "{id}", method = RequestMethod.GET)
public Participant getParticipant(@PathVariable("id") Long id) {
return participantService.findById(id);
}
@RequestMapping(value = "all", method = RequestMethod.GET)
public List<Participant> getAllParticipants() {
return participantService.findAll();
}
@RequestMapping(value = "fromsurvey/{id}", method = RequestMethod.GET)
public List<Participant> getParticipantsFromSurveyId(@PathVariable("id") Long id) {
return participantService.findBySurveyId(id);
}
}
\ No newline at end of file
package no.nsd.websurvey.tromso.controller;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.service.ParticipantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* This is the primary controller that will be used
* to fetch the API data requested.
*/
@RestController
public class QueryController {
@Autowired
private ParticipantService participantService;
@RequestMapping(value = "/{surveyId}/participant/{participantId}")
public Participant getParticipantFromSurvey(
@PathVariable("surveyId") Long surveyId,
@PathVariable("participantId") int remoteId) {
return participantService.findByRemoteIdAndSurveyId(remoteId, surveyId);
}
@RequestMapping(value = "/{surveyId}/{participantId}")
public Participant getParticipantFromSurveyTwo(
@PathVariable("surveyId") Long surveyId,
@PathVariable("participantId") int remoteId) {
return participantService.findByRemoteIdAndSurveyId(remoteId, surveyId);
}
}
\ No newline at end of file
package no.nsd.websurvey.tromso.controller;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.domain.Survey;
import no.nsd.websurvey.tromso.service.SurveyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("survey")
@Description("Controller for presenting Surveys.")
public class SurveyController {
@Autowired
private SurveyService surveyService;
@RequestMapping(value = "all", method = RequestMethod.GET)
public List<Survey> getAllSurveys() {
return surveyService.findAll();
}
@RequestMapping(value = "{id}", method = RequestMethod.GET)
public Survey getSurvey(@PathVariable("id") Long id) {
return surveyService.findById(id);
}
}
\ No newline at end of file
package no.nsd.websurvey.tromso.controller;
import no.nsd.websurvey.tromso.domain.ResponseObject;
import no.nsd.websurvey.tromso.domain.SurveyUsers;
import no.nsd.websurvey.tromso.service.SurveyUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author Dag Østgulen Heradstveit
*/
@RestController
@RequestMapping("/")
public class SurveyUsersController {
private final int Q1 = 187;
private final int Q2 = 222;
private SurveyUsersService surveyUsersService;
@Autowired
public SurveyUsersController(SurveyUsersService surveyUsersService) {
this.surveyUsersService = surveyUsersService;
}
@RequestMapping(value = "/Q1", method = RequestMethod.GET)
public List<ResponseObject> findAllByQ1() {
return surveyUsersService.findBySid(Q1);
}
@RequestMapping(value = "/Q2", method = RequestMethod.GET)
public List<ResponseObject> findAllByQ2() {
return surveyUsersService.findBySid(Q2);
}
@RequestMapping(value = "/Q1/participant/{id}", method = RequestMethod.GET)
public ResponseObject findByQ1(@PathVariable("id") int id) {
return surveyUsersService.findById(id, Q1);
}
@RequestMapping(value = "/Q2/participant/{id}", method = RequestMethod.GET)
public ResponseObject findByQ2(@PathVariable("id") int id) {
return surveyUsersService.findById(id, Q2);
}
}
\ No newline at end of file
package no.nsd.websurvey.tromso.domain;
import com.fasterxml.jackson.annotation.JsonBackReference;
import javax.persistence.*;
/**
* Dummy Participant implementation class.
* Not currently connected to any repositories or
* services to provide actual data.
*/
@Entity
@Table(name = "participant")
public class Participant {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "remoteid")
private Integer remoteId;
@Column(name = "statuscode")
private int statusCode;
@JsonBackReference
@ManyToOne
@JoinColumn(name = "survey_id")
private Survey survey;
public Participant() {}
public Participant(Integer remoteId, int statusCode, Survey survey) {
this.remoteId = remoteId;
this.statusCode = statusCode;
this.survey = survey;
}
public Integer getRemoteId() {
return remoteId;
}
public void setRemoteId(Integer remoteId) {
this.remoteId = remoteId;
}
public Participant(int statusCode) {
this.statusCode = statusCode;
}
public Participant(Survey survey, int statusCode) {
this.survey = survey;
this.statusCode = statusCode;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getStatusCode() {
return statusCode;
}
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
public Survey getSurvey() {
return survey;
}
public void setSurvey(Survey survey) {
this.survey = survey;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Participant that = (Participant) o;
if (statusCode != that.statusCode) return false;
if (!id.equals(that.id)) return false;
if (!remoteId.equals(that.remoteId)) return false;
if (survey != null ? !survey.equals(that.survey) : that.survey != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + statusCode;
result = 31 * result + (survey != null ? survey.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Participant{" +
"id=" + id +
", remoteId=" + remoteId +
", statusCode=" + statusCode +
'}';
}
}
package no.nsd.websurvey.tromso.domain;
/**
* @author Dag Østgulen Heradstveit
*/
public class ResponseObject {
private int id;
private int status;
public ResponseObject(int id, int status) {
this.id = id;
this.status = status;
}
public ResponseObject() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
@Override
public String toString() {
return "ResponseObject{" +
"id=" + id +
", status=" + status +
'}';
}
}
package no.nsd.websurvey.tromso.domain;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "survey")
public class Survey {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@JsonManagedReference
@OneToMany(fetch = FetchType.EAGER, mappedBy = "survey")
private Set<Participant> participants = new HashSet<>();
public Survey() {
//ORM
}
public Survey(Set<Participant> participants) {
this.participants = participants;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set<Participant> getParticipants() {
return participants;
}
public void setParticipants(Set<Participant> participants) {
this.participants = participants;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Survey survey = (Survey) o;
return id.equals(survey.id);
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public String toString() {
return "Survey{" +
"id=" + id +
", participants=" + participants +
'}';
}
}
package no.nsd.websurvey.tromso.domain;
import javax.persistence.*;
/**
* @author Dag Østgulen Heradstveit
*/
@Entity
@Table(name = "surveyusers")
public class SurveyUsers {
@Id
private int id;
private int sid;
private int completed;
@Column(name = "useragent")
private String userAgent;
public SurveyUsers() {
}
public SurveyUsers(int id, int sid, int completed, String userAgent) {
this.id = id;
this.sid = sid;
this.completed = completed;
this.userAgent = userAgent;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public int getCompleted() {
return completed;
}
public void setCompleted(int completed) {
this.completed = completed;
}
public String getUserAgent() {
return userAgent;
}
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
@Override
public String toString() {
return "SurveyUsers{" +
"id=" + id +
", sid=" + sid +
", completed=" + completed +
", userAgent='" + userAgent + '\'' +
'}';
}
}
package no.nsd.websurvey.tromso.repository;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.domain.Survey;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
public interface ParticipantRepository extends CrudRepository<Participant, Long> {
public List<Participant> findBySurvey(Survey survey);
public List<Participant> findBySurveyId(Long id);
public Participant findByIdAndSurveyId(Long id, Long surveyId);
public List<Participant> findByStatusCodeNot(int statusCode);
public Participant findByRemoteIdAndSurveyId(int remoteId, Long surveyId);
}
package no.nsd.websurvey.tromso.repository;
import no.nsd.websurvey.tromso.domain.Survey;
import org.springframework.data.repository.CrudRepository;
public interface SurveyRepository extends CrudRepository<Survey, Long> {
}
package no.nsd.websurvey.tromso.repository;
import no.nsd.websurvey.tromso.domain.SurveyUsers;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface SurveyUsersRepository extends CrudRepository<SurveyUsers, Integer> {
public Optional<SurveyUsers> findByIdAndSid(int id, int sid);
public List<SurveyUsers> findBySid(int sid);
}
package no.nsd.websurvey.tromso.service;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.domain.Survey;
import java.util.List;
/**
* Provides access to all {@link no.nsd.websurvey.tromso.domain.Participant} entities
* made available through the persistence layer.
*/
public interface ParticipantService {
/**
* Return one {@link no.nsd.websurvey.tromso.domain.Participant} domain object.
* @param id for the patient
* @return a poulated {@link no.nsd.websurvey.tromso.domain.Participant} object.
*/
public Participant findById(Long id);
/**
* Find and return all {@link no.nsd.websurvey.tromso.domain.Participant} from the database.
* This method fetch and maps every entity in the database and will
* become slower the more data available. {@link org.springframework.data.repository.PagingAndSortingRepository}
* should be considered in such cases.
* @return {@link java.util.List} of {@link no.nsd.websurvey.tromso.domain.Participant}
*/
public List<Participant> findAll();