diff --git a/src/main/java/org/ohdsi/webapi/annotation/annotation/Annotation.java b/src/main/java/org/ohdsi/webapi/annotation/annotation/Annotation.java new file mode 100644 index 0000000000..9d6bcc8f7a --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/annotation/Annotation.java @@ -0,0 +1,102 @@ +package org.ohdsi.webapi.annotation.annotation; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.annotations.Parameter; +import org.ohdsi.webapi.annotation.set.QuestionSet; + +@Entity(name = "Annotation") +@Table( + name = "annotation" +) +public class Annotation { + + @Id + @GenericGenerator( + name="annotation_generator", + strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", + parameters = { + @Parameter(name="sequence_name",value="annotation_seq"), + @Parameter(name="increment_size",value="1") + } + ) + @GeneratedValue(generator = "annotation_generator") + @Column(name = "annotation_id") + private int id; + + @Column(name = "subject_id") + private int subjectId; + + @Column(name = "cohort_sample_id") + private int cohortSampleId; + + @ManyToOne + @JoinColumn(name = "question_set_id") + private QuestionSet questionSet; + + //***** GETTERS/SETTERS ****** + + /** + * @return the id + */ + public int getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(int id) { + this.id = id; + } + + /** + * @return the subjectId + */ + public int getSubjectId() { + return subjectId; + } + + /** + * @param subjectId the subjectId to set + */ + public void setSubjectId(int subjectId) { + this.subjectId = subjectId; + } + + /** + * @return the cohortId + */ + public int getCohortSampleId() { + return cohortSampleId; + } + + /** + * @param cohortSampleId the cohortId to set + */ + public void setCohortSampleId(int cohortSampleId) { + this.cohortSampleId = cohortSampleId; + } + + /** + * @return the set + */ + public QuestionSet getQuestionSet() { + return questionSet; + } + + /** + * @param set the set to set + */ + public void setQuestionSet(QuestionSet questionSet) { + this.questionSet = questionSet; + } + +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationController.java b/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationController.java new file mode 100644 index 0000000000..b73601f34b --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationController.java @@ -0,0 +1,167 @@ +package org.ohdsi.webapi.annotation.annotation; + +import java.util.ArrayList; +import java.util.List; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Consumes; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.QueryParam; +import javax.ws.rs.PathParam; +import org.json.JSONObject; +import org.json.JSONArray; +import org.ohdsi.webapi.annotation.result.ResultService; +import org.ohdsi.webapi.annotation.study.Study; +import org.ohdsi.webapi.annotation.study.StudyService; +import org.ohdsi.webapi.cohortdefinition.CohortDefinition; +import org.ohdsi.webapi.cohortdefinition.CohortDefinitionRepository; +import org.ohdsi.webapi.cohortsample.CohortSample; +import org.ohdsi.webapi.cohortsample.CohortSampleRepository; +import org.ohdsi.webapi.cohortsample.CohortSamplingService; +import org.ohdsi.webapi.cohortsample.dto.SampleElementDTO; +import org.ohdsi.webapi.source.Source; +import org.ohdsi.webapi.source.SourceService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.ohdsi.webapi.annotation.set.QuestionSetRepository; +import org.ohdsi.webapi.annotation.set.QuestionSet; +import org.springframework.web.bind.annotation.RequestBody; + + +@Path("annotation") +@Component +public class AnnotationController { + + @Autowired + private StudyService studyService; + + @Autowired + private ResultService resultService; + + @Autowired + private CohortDefinitionRepository cohortDefinitionRepository; + + @Autowired + private CohortSamplingService cohortSamplingService; + + @Autowired + private CohortSampleRepository sampleRepository; + + @Autowired + private AnnotationService annotationService; + + @Autowired + private QuestionSetRepository questionSetRepository; + + @Autowired + private SourceService sourceService; + + @GET + @Path("/") + @Produces(MediaType.APPLICATION_JSON) + public List getAnnotations( + @QueryParam("cohortSampleId") final int cohortSampleId, + @QueryParam("subjectId") final int subjectId, + @QueryParam("setId") final int setId + ) { + List returnAnnotations; + returnAnnotations = getFullAnnotations(cohortSampleId,subjectId,setId); + List summaries = new ArrayList(); + for(Annotation singleAnno : returnAnnotations){ +// can we see about doing this in a more performant manner? + AnnotationSummary tempAnnoSummary=new AnnotationSummary(singleAnno); + summaries.add(tempAnnoSummary); + } + return summaries; + } + + @GET + @Path("/fullquestion") + @Produces(MediaType.APPLICATION_JSON) + public List getFullAnnotations( + @QueryParam("cohortSampleId") final int cohortSampleId, + @QueryParam("subjectId") final int subjectId, + @QueryParam("setId") final int setId + ) { + List returnAnnotations=null; + if (cohortSampleId != 0 && subjectId != 0 && setId != 0) { + System.out.println("made it into the search function"); + returnAnnotations= annotationService.getAnnotationByCohortSampleIdAndBySubjectIdAndByQuestionSetId(cohortSampleId, subjectId, setId); + } + else if (cohortSampleId !=0 && setId!=0){ + returnAnnotations= annotationService.getAnnotationByCohortSampleIdAndByQuestionSetId(cohortSampleId,setId); + } + else if(cohortSampleId!=0){ + returnAnnotations= annotationService.getAnnotationsByCohortSampleId(cohortSampleId); + } + else if(setId !=0){ + returnAnnotations= annotationService.getAnnotationsByQuestionSetId(setId); + } + else{ + returnAnnotations=annotationService.getAnnotations(); + } + return returnAnnotations; + } + + @POST + @Path("/") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public void addResult(@RequestBody String payload) { + System.out.println(payload); + JSONObject jsonpayload = new JSONObject(payload); + System.out.println(jsonpayload); + System.out.println(jsonpayload.get("results")); + System.out.printf("cohortId: %s\n",jsonpayload.get("cohortId").toString()); + System.out.printf("cohortSampleId: %s\n",jsonpayload.get("cohortSampleId").toString()); + System.out.printf("subjectId: %s\n",jsonpayload.get("subjectId").toString()); + System.out.printf("setId: %s\n",jsonpayload.get("setId").toString()); + Annotation tempAnnotation = annotationService.getAnnotationsByAnnotationId(jsonpayload.getInt("id")); + System.out.printf("annotationID:%d\n",tempAnnotation.getId()); + JSONArray array = jsonpayload.getJSONArray("results"); + Study study = studyService.getStudyByQuestionSetIdAndSampleId(jsonpayload.getInt("setId"),jsonpayload.getInt("cohortSampleId") ); + resultService.insertResults(tempAnnotation,array,study); + } + + @POST + @Path("/sample") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public void addAnnotation(@RequestBody String payload) { + JSONObject jsonpayload = new JSONObject(payload); + int cohortSampleId = Integer.parseInt(jsonpayload.get("sampleId").toString()); + CohortSample cohortSample =sampleRepository.findById(cohortSampleId); + List temp = cohortSamplingService.getSample(cohortSampleId, false).getElements(); + Study study = new Study(); + int questionSetId = Integer.parseInt(jsonpayload.get("annotationSetId").toString()); + QuestionSet questionSet = questionSetRepository.findById(questionSetId); + study.setQuestionSet(questionSetRepository.findById(questionSetId)); + study.setCohortSample(cohortSample); + CohortDefinition cohortDefinition= cohortDefinitionRepository.findOneWithDetail(cohortSample.getCohortDefinitionId()); + study.setCohortDefinition(cohortDefinition); + Source source = sourceService.findBySourceKey(jsonpayload.get("sourceKey").toString()); + study.setSource(source); + studyService.addStudy(study); + for (SampleElementDTO element : temp){ + System.out.println("element"+element); + System.out.println("element GetPersonID"+element.getPersonId()); + Annotation annotation = new Annotation(); + annotation.setSubjectId(Integer.parseInt(element.getPersonId())); + annotation.setCohortSampleId(cohortSampleId); + annotation.setQuestionSet(questionSet); + annotationService.addAnnotation(annotation); + } + } + + @GET + @Path("/{annotationID}") + @Produces(MediaType.APPLICATION_JSON) + public Annotation getResults(@PathParam("annotationID") String annotationID) { + int annotationIdInt=Integer.parseInt(annotationID); + Annotation ourAnno =annotationService.getAnnotationsByAnnotationId(annotationIdInt); + return ourAnno; + } +} + diff --git a/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationDto.java b/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationDto.java new file mode 100644 index 0000000000..915533a595 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationDto.java @@ -0,0 +1,102 @@ +package org.ohdsi.webapi.annotation.annotation; + +import java.util.HashSet; +import java.util.Set; +import org.ohdsi.webapi.annotation.result.ResultDto; + +public class AnnotationDto { + + private int id; + private int subjectId; + private int sampleId; + private int annotationSetId; + private String name; + private Set results = new HashSet(); + + //***** GETTERS/SETTERS ****** + + /** + * @return the id + */ + public int getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(int id) { + this.id = id; + } + + /** + * @return the subjectId + */ + public int getSubjectId() { + return subjectId; + } + + /** + * @param subjectId the subjectId to set + */ + public void setSubjectId(int subjectId) { + this.subjectId = subjectId; + } + + /** + * @return the cohortId + */ + public int getsampleId() { + return sampleId; + } + + /** + * @param sampleId the cohortId to set + */ + public void setsampleId(int sampleId) { + this.sampleId = sampleId; + } + + /** + * @return the setId + */ + public int getannotationSetId() { + return annotationSetId; + } + + /** + * @param setId the setId to set + */ + public void setannotationSetId(int setId) { + this.annotationSetId = setId; + } + + /** + * @return the results + */ + public Set getResults() { + return new HashSet(results); + } + + /** + * @param results the results to set + */ + protected void setResults(Set results) { + this.results = results; + } + + /** + * @return the sample name + */ + public String getSampleName() { + return name; + } + + /** + * @param name to the slected sample name + */ + public void setSampleName(String name) { + this.name = name; + } + +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationRepository.java b/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationRepository.java new file mode 100644 index 0000000000..1e060dbeb8 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationRepository.java @@ -0,0 +1,17 @@ +package org.ohdsi.webapi.annotation.annotation; + +import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; +import java.util.Set; +import org.ohdsi.webapi.annotation.annotation.Annotation; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +public interface AnnotationRepository extends JpaRepository { + + public Set findOneByCohortSampleIdAndSubjectIdAndQuestionSetId(int cohortSampleId, int subjectId, int questionSetId); + public List findByCohortSampleIdAndQuestionSetId(int cohortSampleId, int questionSetId); + public List findByCohortSampleId(int cohortSampleId); + public List findByQuestionSetId(int questionSetId); + public Annotation findById(int annotation_id); +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationService.java b/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationService.java new file mode 100644 index 0000000000..6325f65b2f --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationService.java @@ -0,0 +1,60 @@ +package org.ohdsi.webapi.annotation.annotation; + +import org.springframework.stereotype.Service; +import org.springframework.beans.factory.annotation.Autowired; +import java.util.ArrayList; +import java.util.List; + +@Service +public class AnnotationService { + + @Autowired + private AnnotationRepository annotationRepository; + + public List getAnnotations() { + List annotations = new ArrayList(); + annotationRepository.findAll().forEach(annotations::add); + return annotations; + } + + public Annotation getAnnotationsByAnnotationId(int annotationId) { + Annotation result = annotationRepository.findById(annotationId); + return result; + } + + public Annotation getAnnotationsByAnnotationId(Annotation annotation) { + Annotation result = annotationRepository.findById(annotation.getId()); + return result; + } + + public List getAnnotationByCohortSampleIdAndBySubjectIdAndByQuestionSetId(int cohortSampleId, int subjectId, int questionSetId) { + System.out.printf("cohortSampleId %d\n",cohortSampleId); + System.out.printf("subjectId %d\n",subjectId); + System.out.printf("questionSetId %d\n",questionSetId); + + List annotations = new ArrayList(); + annotationRepository.findOneByCohortSampleIdAndSubjectIdAndQuestionSetId(cohortSampleId, subjectId, questionSetId) + .forEach(annotations::add); + return annotations; + } + + public List getAnnotationByCohortSampleIdAndByQuestionSetId(int cohortSampleId, int setId) { +// this becomes getByStudyId +// checking if there is a study with the sample and question set that has already been created + List annotations = new ArrayList(); + annotationRepository.findByCohortSampleIdAndQuestionSetId(cohortSampleId, setId) + .forEach(annotations::add); + return annotations; + } + + public Annotation addAnnotation(Annotation annotation) { + return annotationRepository.save(annotation); + } + + public List getAnnotationsByCohortSampleId(int cohortSampleId) { + return annotationRepository.findByCohortSampleId(cohortSampleId); + } + public List getAnnotationsByQuestionSetId(int questionSetId) { + return annotationRepository.findByQuestionSetId(questionSetId); + } +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationSummary.java b/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationSummary.java new file mode 100644 index 0000000000..2d0cc8bb14 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/annotation/AnnotationSummary.java @@ -0,0 +1,18 @@ +package org.ohdsi.webapi.annotation.annotation; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; + +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) +public class AnnotationSummary { + private int id; + private int subjectId; + private int cohortSampleId; + private int questionSetId; + + public AnnotationSummary(Annotation annotation) { + this.id = annotation.getId(); + this.subjectId= annotation.getSubjectId(); + this.cohortSampleId= annotation.getCohortSampleId(); + this.questionSetId=annotation.getQuestionSet().getId(); + } +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/answer/Answer.java b/src/main/java/org/ohdsi/webapi/annotation/answer/Answer.java new file mode 100644 index 0000000000..a57243fb5c --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/answer/Answer.java @@ -0,0 +1,120 @@ +package org.ohdsi.webapi.annotation.answer; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.ManyToOne; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.JoinColumn; + +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.annotations.Parameter; +import org.ohdsi.webapi.annotation.question.Question; +import com.fasterxml.jackson.annotation.JsonIgnore; + + +@Entity(name = "Answer") +@Table(name = "annotation_answer") +public class Answer { + + @Id + @GenericGenerator( + name="answer_generator", + strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", + parameters = { + @Parameter(name="sequence_name",value="annotation_answer_seq"), + @Parameter(name="increment_size",value="1") + } + ) + @GeneratedValue(generator = "answer_generator") + @Column(name = "answer_id") + private int id; + + @JsonIgnore + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "question_id") + private Question question; + + @Column(name = "text") + private String text; + + @Column(name = "value") + private String value; + + @Column(name = "help_text") + private String helpText; + + //***** GETTERS/SETTERS ****** + + /** + * @return the id + */ + public int getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(int id) { + this.id = id; + } + + /** + * @return the question + */ + public Question getQuestion() { + return question; + } + + /** + * @param question the question to set + */ + public void setQuestion(Question question) { + this.question = question; + } + + /** + * @return the text + */ + public String getText() { + return text; + } + + /** + * @param text the text to set + */ + public void setText(String text) { + this.text = text; + } + + /** + * @return the value + */ + public String getValue() { + return value; + } + + /** + * @param value the value to set + */ + public void setValue(String value) { + this.value = value; + } + + /** + * @return the helpText + */ + public String getHelpText() { + return helpText; + } + + /** + * @param helpText the helpText to set + */ + public void setHelpText(String helpText) { + this.helpText = helpText; + } +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/answer/AnswerController.java b/src/main/java/org/ohdsi/webapi/annotation/answer/AnswerController.java new file mode 100644 index 0000000000..247b8a2d95 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/answer/AnswerController.java @@ -0,0 +1,25 @@ +package org.ohdsi.webapi.annotation.answer; + +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Consumes; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Path("annotation/") +@Component +public class AnswerController { + + @Autowired + private AnswerService answerService; + + @POST + @Path("answers") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public void addQuestion(Answer answer) { + answerService.addAnswer(answer); + } +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/answer/AnswerRepository.java b/src/main/java/org/ohdsi/webapi/annotation/answer/AnswerRepository.java new file mode 100644 index 0000000000..80945adc55 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/answer/AnswerRepository.java @@ -0,0 +1,7 @@ +package org.ohdsi.webapi.annotation.answer; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface AnswerRepository extends JpaRepository { + public Answer findById(int answerId); +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/answer/AnswerService.java b/src/main/java/org/ohdsi/webapi/annotation/answer/AnswerService.java new file mode 100644 index 0000000000..c241ead8d5 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/answer/AnswerService.java @@ -0,0 +1,17 @@ +package org.ohdsi.webapi.annotation.answer; + +import org.springframework.stereotype.Service; +import org.springframework.beans.factory.annotation.Autowired; + +@Service +public class AnswerService { + + @Autowired + private AnswerRepository answerRepository; + + public void addAnswer(Answer answer) { + answerRepository.save(answer); + } + + public Answer getAnswerById(int answerId){return answerRepository.findById(answerId);} +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/navigation/Navigation.java b/src/main/java/org/ohdsi/webapi/annotation/navigation/Navigation.java new file mode 100644 index 0000000000..62a220dc14 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/navigation/Navigation.java @@ -0,0 +1,83 @@ +package org.ohdsi.webapi.annotation.navigation; +import java.util.List; +public class Navigation { + + private int prevSubjectId; + private int nextSubjectId; + private int nextUnannotatedSubjectId; + private int numProfileSamples; + private int numAnnotations; + + //***** GETTERS/SETTERS ****** + + /** + * @return the prevSubjectId + */ + public int getPrevSubjectId() { + return prevSubjectId; + } + + /** + * @param prevSubjectId the prevSubjectId to set + */ + public void setPrevSubjectId(int prevSubjectId) { + this.prevSubjectId = prevSubjectId; + } + + /** + * @return the nextSubjectId + */ + public int getNextSubjectId() { + return nextSubjectId; + } + + /** + * @param nextSubjectId the nextSubjectId to set + */ + public void setNextSubjectId(int nextSubjectId) { + this.nextSubjectId = nextSubjectId; + } + + /** + * @return the nextUnannotatedSubjectId + */ + public int getNextUnannotatedSubjectId() { + return nextUnannotatedSubjectId; + } + + /** + * @param nextUnannotatedSubjectId the nextUnannotatedSubjectId to set + */ + public void setNextUnannotatedSubjectId(int nextUnannotatedSubjectId) { + this.nextUnannotatedSubjectId = nextUnannotatedSubjectId; + } + + /** + * @return the numProfileSamples + */ + public int getNumProfileSamples() { + return numProfileSamples; + } + + /** + * @param numProfileSamples the numProfileSamples to set + */ + public void setNumProfileSamples(int numProfileSamples) { + this.numProfileSamples = numProfileSamples; + } + + /** + * @return the numAnnotations + */ + public int getNumAnnotations() { + return numAnnotations; + } + + /** + * @param numAnnotations the numAnnotations to set + */ + public void setNumAnnotations(int numAnnotations) { + this.numAnnotations = numAnnotations; + } + +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/navigation/NavigationController.java b/src/main/java/org/ohdsi/webapi/annotation/navigation/NavigationController.java new file mode 100644 index 0000000000..9b6861da43 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/navigation/NavigationController.java @@ -0,0 +1,52 @@ +package org.ohdsi.webapi.annotation.navigation; + +import java.util.List; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Consumes; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.ohdsi.webapi.cohortresults.ProfileSampleRecord; +import javax.ws.rs.QueryParam; +import org.ohdsi.webapi.source.Source; +import org.ohdsi.webapi.service.AbstractDaoService; +import org.ohdsi.webapi.cohortresults.*; +import javax.annotation.PostConstruct; +import org.ohdsi.webapi.annotation.annotation.AnnotationService; +import com.fasterxml.jackson.databind.ObjectMapper; + +@Path("annotation/") +@Component +public class NavigationController extends AbstractDaoService { + + @Autowired + private VisualizationDataRepository visualizationDataRepository; + + @Autowired + private AnnotationService annotationService; + + + @Autowired + private NavigationService ns; + + private CohortResultsAnalysisRunner queryRunner = null; + + + @GET + @Path("navigation") + @Produces(MediaType.APPLICATION_JSON) + public Navigation getNavigation( + @QueryParam("cohortId") final int cohortId, + @QueryParam("subjectId") final int subjectId, + @QueryParam("sampleName") final String sampleName, + @QueryParam("sourceKey") final String sourceKey + ) { + Source source = getSourceRepository().findBySourceKey(sourceKey); + Navigation nav = ns.navData(new Navigation(), subjectId, sampleName, cohortId, source); + + return nav; + } + +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/navigation/NavigationService.java b/src/main/java/org/ohdsi/webapi/annotation/navigation/NavigationService.java new file mode 100644 index 0000000000..7485e48ad5 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/navigation/NavigationService.java @@ -0,0 +1,60 @@ +package org.ohdsi.webapi.annotation.navigation; + +import java.util.Collection; +import org.ohdsi.webapi.util.PreparedStatementRenderer; +import static org.ohdsi.webapi.util.SecurityUtils.whitelist; +import org.springframework.jdbc.core.RowMapper; +import org.ohdsi.webapi.source.SourceDaimon; +import org.ohdsi.webapi.util.SessionUtils; +import org.ohdsi.webapi.source.Source; +import java.util.List; +import java.util.ArrayList; +import org.springframework.stereotype.Service; +import org.ohdsi.webapi.cohortsample.CohortSampleRepository; +import org.springframework.beans.factory.annotation.Autowired; +import java.sql.ResultSet; +import java.sql.SQLException; +import org.ohdsi.webapi.service.AbstractDaoService; +import org.ohdsi.webapi.annotation.navigation.Navigation; +import org.ohdsi.webapi.cohortsample.CohortSample; + + + + +@Service +public class NavigationService extends AbstractDaoService { + private final String BASE_SQL_PATH = "/resources/navigation/"; + + @Autowired + private CohortSampleRepository cohortSampleRepository; + + + public Navigation navData(Navigation nav, int subjectId, String sampleName, int cohortId, Source source) + { + if (sampleName.indexOf("_") != -1) { + sampleName = sampleName.replaceAll("_", " "); + } + List sample = new ArrayList(); + int annCount = 0; + boolean foundNext = false; + for(int i = 0; i < sample.size(); i++) { + if((int) sample.get(i)[0] == subjectId) { + nav.setPrevSubjectId(i == 0 ? (int) sample.get(sample.size() - 1)[0] : (int) sample.get(i-1)[0]); + nav.setNextSubjectId(sample.size() == i + 1 ? (int) sample.get(0)[0] : (int) sample.get(i+1)[0]); + } + if ((boolean) sample.get(i)[1] == true) { + annCount++; + } + if (!foundNext && (boolean) sample.get(i)[1] == true) { + nav.setNextUnannotatedSubjectId( (int)sample.get(i)[0]); + foundNext = true; + } + + } + nav.setNumProfileSamples(sample.size()); + nav.setNumAnnotations(annCount); + System.out.println(nav.getNumAnnotations()); + return nav; + } + +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/question/Question.java b/src/main/java/org/ohdsi/webapi/annotation/question/Question.java new file mode 100644 index 0000000000..f632ed0c5e --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/question/Question.java @@ -0,0 +1,184 @@ +package org.ohdsi.webapi.annotation.question; + +import java.util.List; +import java.util.ArrayList; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.CascadeType; +import javax.persistence.FetchType; +import javax.persistence.OneToMany; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.annotations.GenericGenerator; +import org.ohdsi.webapi.annotation.answer.Answer; +import org.ohdsi.webapi.annotation.set.QuestionSet; +import com.fasterxml.jackson.annotation.JsonIgnore; + +@Entity(name = "Question") +@Table(name = "annotation_question") +public class Question { + + @Id + @GenericGenerator( + name="question_generator", + strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", + parameters = { + @org.hibernate.annotations.Parameter(name="sequence_name",value="annotation_question_seq"), + @org.hibernate.annotations.Parameter(name="increment_size",value="1") + } + ) + @GeneratedValue(generator = "question_generator") + @Column(name = "question_id") + private int id; + + @Column(name = "question_name") + private String text; + + @Column(name = "question_type") + private String type; + + @Column(name = "help_text") + private String helpText; + + @Column(name = "case_question") + private Boolean caseQuestion = false; + + @Column(name = "required") + private Boolean required = true; + + @JsonIgnore + @ManyToOne + @JoinColumn(name = "set_id") + private QuestionSet set; + + @OneToMany( + fetch = FetchType.EAGER, + mappedBy = "question", + cascade = CascadeType.ALL, + orphanRemoval = true + ) + private List answers = new ArrayList<>(); + + //***** GETTERS/SETTERS ****** + + /** + * @return the id + */ + public int getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(int id) { + this.id = id; + } + + /** + * @return the text + */ + public String getText() { + return text; + } + + /** + * @param text the text to set + */ + public void setText(String text) { + this.text = text; + } + + /** + * @return the type + */ + public String getType() { + return type; + } + + /** + * @param type the type to set + */ + public void setType(String type) { + this.type = type; + } + + /** + * @return the helpText + */ + public String getHelpText() { + return helpText; + } + + /** + * @param helpText the helpText to set + */ + public void setHelpText(String helpText) { + this.helpText = helpText; + } + + /** + * @return the set + */ + public QuestionSet getSet() { + return set; + } + + /** + * @param set the set to set + */ + public void setSet(QuestionSet set) { + this.set = set; + } + + /** + * @return the answers + */ + public List getAnswers() { + return new ArrayList(answers); + } + + /** + * @param answers the answers to set + */ + protected void setAnswers(List answers) { + this.answers = answers; + } + + public void addToAnswers(Answer answer) { + answer.setQuestion(this); + this.answers.add(answer); + } + + /** + * @return caseQuestion + */ + public Boolean getCaseQuestion() { + return caseQuestion; + } + + /** + * @param caseQuestion caseQuestion to set + */ + public void setCaseQuestion(Boolean caseQuestion) { + this.caseQuestion = caseQuestion; + } + + /** + * @return required + */ + public Boolean getRequired() { + return required; + } + + /** + * @param required required to set + */ + public void setRequired(Boolean required) { + this.required = required; + } +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/question/QuestionController.java b/src/main/java/org/ohdsi/webapi/annotation/question/QuestionController.java new file mode 100644 index 0000000000..4e2c196900 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/question/QuestionController.java @@ -0,0 +1,39 @@ +package org.ohdsi.webapi.annotation.question; + +import java.util.List; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Consumes; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Path("annotation/") +@Component +public class QuestionController { + + @Autowired + private QuestionService questionService; + + @GET + @Path("questions") + @Produces(MediaType.APPLICATION_JSON) + public List getAllQuestions() { + return questionService.getAllQuestions(); + } + + @POST + @Path("questions") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public void addQuestion(Question question) { + + question.getAnswers().forEach((answer) -> { + question.addToAnswers(answer); + }); + + questionService.addQuestion(question); + } +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/question/QuestionRepository.java b/src/main/java/org/ohdsi/webapi/annotation/question/QuestionRepository.java new file mode 100644 index 0000000000..ee455bf7a4 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/question/QuestionRepository.java @@ -0,0 +1,12 @@ +package org.ohdsi.webapi.annotation.question; + +import org.ohdsi.webapi.annotation.annotation.Annotation; +import org.springframework.data.jpa.repository.JpaRepository; +import org.ohdsi.webapi.annotation.question.Question; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; + +public interface QuestionRepository extends JpaRepository { + public Question findById(int questionId); +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/question/QuestionService.java b/src/main/java/org/ohdsi/webapi/annotation/question/QuestionService.java new file mode 100644 index 0000000000..b34c9c2ed4 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/question/QuestionService.java @@ -0,0 +1,31 @@ +package org.ohdsi.webapi.annotation.question; + +import org.springframework.stereotype.Service; +import org.springframework.beans.factory.annotation.Autowired; +import java.util.ArrayList; +import java.util.List; +import org.ohdsi.webapi.annotation.question.QuestionRepository; +import org.ohdsi.webapi.annotation.question.Question; + +@Service +public class QuestionService { + + @Autowired + private QuestionRepository questionRepository; + + public List getAllQuestions() { + List questions = new ArrayList(); + questionRepository.findAll() + .forEach(questions::add); + return questions; + } + + public Question getQuestionByQuestionId(int questionId){ + return questionRepository.findById(questionId); + } + + public void addQuestion(Question question) { + questionRepository.save(question); + } + +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/result/Result.java b/src/main/java/org/ohdsi/webapi/annotation/result/Result.java new file mode 100644 index 0000000000..04fb3801ed --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/result/Result.java @@ -0,0 +1,84 @@ +package org.ohdsi.webapi.annotation.result; + +import org.ohdsi.webapi.annotation.annotation.Annotation; + +public class Result { + + private Annotation annotation; + private int questionId; + private int answerId; + private String value; + private String type; + + //***** GETTERS/SETTERS ****** + + /** + * @return the annotation + */ + public Annotation getAnnotation() { + return annotation; + } + + /** + * @param annotation the annotation to set + */ + public void setAnnotation(Annotation annotation) { + this.annotation = annotation; + } + + /** + * @return the answerId + */ + public int getAnswerId() { + return answerId; + } + + /** + * @param answerId the answerId to set + */ + public void setAnswerId(int answerId) { + this.answerId = answerId; + } + + /** + * @return the questionId + */ + public int getQuestionId() { + return questionId; + } + + /** + * @param questionId the questionId to set + */ + public void setQuestionId(int questionId) { + this.questionId = questionId; + } + + /** + * @return the type + */ + public String getType() { + return type; + } + + /** + * @param type the type to set + */ + public void setType(String type) { + this.type = type; + } + + /** + * @return the value + */ + public String getValue() { + return value; + } + + /** + * @param value the value to set + */ + public void setValue(String value) { + this.value = value; + } +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/result/ResultController.java b/src/main/java/org/ohdsi/webapi/annotation/result/ResultController.java new file mode 100644 index 0000000000..1307cbf855 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/result/ResultController.java @@ -0,0 +1,99 @@ +package org.ohdsi.webapi.annotation.result; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; + +import org.ohdsi.webapi.annotation.annotation.Annotation; +import org.ohdsi.webapi.annotation.annotation.AnnotationService; +import org.ohdsi.webapi.annotation.answer.Answer; +import org.ohdsi.webapi.annotation.answer.AnswerService; +import org.ohdsi.webapi.annotation.question.Question; +import org.ohdsi.webapi.annotation.question.QuestionService; +import org.ohdsi.webapi.annotation.study.Study; +import org.ohdsi.webapi.annotation.study.StudyService; +import org.ohdsi.webapi.source.Source; +import org.ohdsi.webapi.source.SourceService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.*; + +@Path("/annotations/results") +@Component +public class ResultController { + + @Autowired + private ResultService resultService; + + @Autowired + private QuestionService questionService; + + @Autowired + private AnnotationService annotationService; + + @Autowired + private StudyService studyService; + + @Autowired + private SourceService sourceService; + + @Autowired + private AnswerService answerService; + + @GET + @Path("/{annotationID}") + @Produces(MediaType.APPLICATION_JSON) + public List getResultsByAnnotationId(@PathParam("annotationID") int annotationID) { + return resultService.getResultsByAnnotationId(annotationID); + } + + @GET + @Path("/") + @Produces(MediaType.APPLICATION_JSON) + public List getResults( + @QueryParam("questionSetId") final int questionId + ) { + return resultService.getResultsByQuestionSetId(questionId); + } + + @GET + @Path("/completeResults") + @Produces(MediaType.APPLICATION_JSON) + public List getFullResults( + @QueryParam("questionSetId") final int questionSetId, + @QueryParam("cohortSampleId") final int cohortSampleId, + @QueryParam("studyId") final int studyId + ) { + Study study = null; + if(studyId!=0){ + study=studyService.getStudyById(studyId); + } + else if (questionSetId!=0 && cohortSampleId!=0){ + study=studyService.getStudyByQuestionSetIdAndSampleId(questionSetId,cohortSampleId); + } + else{ + return null; + } + List resultlist=resultService.getResultsByStudy(study); + List superList = new ArrayList(); + Source source = sourceService.findBySourceId(study.getCohortSample().getSourceId()); + for (Result result : resultlist){ + Question myQuestion = questionService.getQuestionByQuestionId(result.getQuestionId()); + SuperResultDto tempdto = new SuperResultDto(result); + Annotation tempanno = annotationService.getAnnotationsByAnnotationId(result.getAnnotation()); + Answer tempAnswer = answerService.getAnswerById(result.getAnswerId()); + tempdto.setAnswerText(tempAnswer.getText()); + tempdto.setAnswerValue(result.getValue()); + tempdto.setPatientId(tempanno.getSubjectId()); + tempdto.setCohortName(study.getCohortDefinition().getName()); + tempdto.setCohortId( study.getCohortDefinition().getId()); + tempdto.setDataSourceKey(source.getSourceKey()); + tempdto.setCohortSampleName(study.getCohortSample().getName()); + tempdto.setQuestionSetName(study.getQuestionSet().getName()); + tempdto.setCaseStatus(myQuestion.getCaseQuestion()); + tempdto.setQuestionText(myQuestion.getText()); + superList.add(tempdto); + } + return superList; + } +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/result/ResultDto.java b/src/main/java/org/ohdsi/webapi/annotation/result/ResultDto.java new file mode 100644 index 0000000000..fcd1663e94 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/result/ResultDto.java @@ -0,0 +1,68 @@ +package org.ohdsi.webapi.annotation.result; + +public class ResultDto { + + private int answerId; + private int questionId; + private String value; + private String type; + + //***** GETTERS/SETTERS ****** + + /** + * @return the answerId + */ + public int getAnswerId() { + return answerId; + } + + /** + * @param answerId the answerId to set + */ + public void setAnswerId(int answerId) { + this.answerId = answerId; + } + + /** + * @return the questionId + */ + public int getQuestionId() { + return questionId; + } + + /** + * @param questionId the questionId to set + */ + public void setQuestionId(int questionId) { + this.questionId = questionId; + } + + /** + * @return the value + */ + public String getValue() { + return value; + } + + /** + * @param value the value to set + */ + public void setValue(String value) { + this.value = value; + } + + /** + * @return the type + */ + public String getType() { + return type; + } + + /** + * @param type the type to set + */ + public void setType(String type) { + this.type = type; + } + +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/result/ResultService.java b/src/main/java/org/ohdsi/webapi/annotation/result/ResultService.java new file mode 100644 index 0000000000..70bf6400c3 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/result/ResultService.java @@ -0,0 +1,212 @@ +package org.ohdsi.webapi.annotation.result; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.ohdsi.webapi.annotation.annotation.Annotation; +import org.ohdsi.webapi.annotation.annotation.AnnotationService; +import org.ohdsi.webapi.annotation.study.Study; +import org.ohdsi.webapi.annotation.study.StudyService; +import org.ohdsi.webapi.cohortsample.CohortSample; +import org.ohdsi.webapi.cohortsample.CohortSampleRepository; +import org.ohdsi.webapi.service.AbstractDaoService; +import org.ohdsi.webapi.source.Source; +import org.ohdsi.webapi.source.SourceDaimon; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.stereotype.Service; +import org.ohdsi.webapi.util.PreparedStatementRenderer; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Service +public class ResultService extends AbstractDaoService { + + @Autowired + private CohortSampleRepository sampleRepository; + + @Autowired + private AnnotationService annotationService; + + @Autowired + private StudyService studyService; + + public List getResultsByAnnotationId(int AnnotationID){ + Annotation ourAnno =annotationService.getAnnotationsByAnnotationId(AnnotationID); + return getResultsByAnnotation(ourAnno); + } + + public List getResultsByAnnotation(Annotation annotation){ + CohortSample sample = sampleRepository.findById(annotation.getCohortSampleId()); + Source source = getSourceRepository().findBySourceId(sample.getSourceId()); + JdbcTemplate jdbcTemplate = getSourceJdbcTemplate(source); + PreparedStatementRenderer renderer = new PreparedStatementRenderer(source, "/resources/annotationresult/sql/findResultsByAnnotationId.sql", + new String[]{"results_schema", "CDM_schema"}, + new String[]{source.getTableQualifier(SourceDaimon.DaimonType.Results), source.getTableQualifier(SourceDaimon.DaimonType.CDM)}, + "annotation_id", annotation.getId()); + return jdbcTemplate.query(renderer.getSql(),renderer.getOrderedParams(),new ResultRowMapper()); + } + + public List getResultsByAnnotations(List annotations){ + String AnnotationIds=""+annotations.get(0).getId(); + int[] annotationIds = annotations.stream() + .mapToInt(Annotation::getId) + .toArray(); + System.out.println("AnnotationIDs: "+AnnotationIds); + CohortSample sample = sampleRepository.findById(annotations.get(0).getCohortSampleId()); + Source source = getSourceRepository().findBySourceId(sample.getSourceId()); + JdbcTemplate jdbcTemplate = getSourceJdbcTemplate(source); + PreparedStatementRenderer renderer = new PreparedStatementRenderer(source, "/resources/annotationresult/sql/findResultsByAnnotationIds.sql", + new String[]{"results_schema", "CDM_schema"}, + new String[]{source.getTableQualifier(SourceDaimon.DaimonType.Results), source.getTableQualifier(SourceDaimon.DaimonType.CDM)}, + "idList", annotationIds); + System.out.println(renderer); + return jdbcTemplate.query(renderer.getSql(),renderer.getOrderedParams(),new ResultRowMapper()); + } + + public Result getResultByAnnotationIDAndQuestionID(int AnnotationID,int QuestionID){ + System.out.printf("checking for result with questionID:%s and annotationID:%s \n",QuestionID,AnnotationID); + Annotation ourAnno =annotationService.getAnnotationsByAnnotationId(AnnotationID); + CohortSample sample = sampleRepository.findById(ourAnno.getCohortSampleId()); + Source source = getSourceRepository().findBySourceId(sample.getSourceId()); + JdbcTemplate jdbcTemplate = getSourceJdbcTemplate(source); + String[] sqlParameters = new String[] { "annotation_id", "question_id"}; + Object[] sqlValues = new Object[] {AnnotationID,QuestionID}; + PreparedStatementRenderer renderer = new PreparedStatementRenderer(source, "/resources/annotationresult/sql/findResultsByAnnotationIdAndQuestionId.sql", + new String[]{"results_schema", "CDM_schema"}, + new String[]{source.getTableQualifier(SourceDaimon.DaimonType.Results), source.getTableQualifier(SourceDaimon.DaimonType.CDM)}, + sqlParameters, sqlValues); + System.out.printf("Running query: %s with params: %s\n",renderer.getSql(),renderer.getOrderedParams().toString()); + List results= jdbcTemplate.query(renderer.getSql(),renderer.getOrderedParams(), new ResultRowMapper()); + if (results.isEmpty()){ + return null; + } + return results.get(0); + } + + public List getResultsByQuestionSetIdAndSampleId(int questionSetId,int sampleId){ + Study study = studyService.getStudyByQuestionSetIdAndSampleId(questionSetId,sampleId); + return getResultsByStudyId(study.getId()); + } + + public List getResultsByStudyId(int studyId){ + Study study = studyService.getStudyById(studyId); + return getResultsByStudy(study); + } + + public List getResultsByStudy(Study study){ + CohortSample sample = study.getCohortSample(); + Source source = getSourceRepository().findBySourceId(sample.getSourceId()); + JdbcTemplate jdbcTemplate = getSourceJdbcTemplate(source); + PreparedStatementRenderer renderer = new PreparedStatementRenderer(source, "/resources/annotationresult/sql/findResultsByStudyId.sql", + new String[]{"results_schema", "CDM_schema"}, + new String[]{source.getTableQualifier(SourceDaimon.DaimonType.Results), source.getTableQualifier(SourceDaimon.DaimonType.CDM)}, + "studyId", study.getId()); + return jdbcTemplate.query(renderer.getSql(),renderer.getOrderedParams(),new ResultRowMapper()); + } + + public List getResultsByQuestionSetId(int questionSetId) { + List results = new ArrayList(); + List annos=annotationService.getAnnotationsByQuestionSetId(questionSetId); + List> collections = annos.stream() + .collect(Collectors.groupingBy(x -> x.getCohortSampleId())) + .entrySet().stream() + .map(e -> { ArrayList c = new ArrayList(); c.addAll(e.getValue()); return c; }) + .collect(Collectors.toList()); + for(ArrayList annoList : collections){ + results.addAll(getResultsByAnnotations(annoList)); + } + return results; + } + + public void deleteResultsByAnnotationId(Annotation annotation){ + List deleteVariables = new ArrayList<>(); + CohortSample sample = sampleRepository.findById(annotation.getCohortSampleId()); + Source source = getSourceRepository().findBySourceId(sample.getSourceId()); + JdbcTemplate jdbcTemplate = getSourceJdbcTemplate(source); + String[] parameters = new String[] { "results_schema" }; + String[] parameterValues = new String[] { source.getTableQualifier(SourceDaimon.DaimonType.Results) }; + String[] deletesqlParameters = new String[] { "annotation_id"}; + Object[] deletesqlValues = new Object[] {annotation.getId()}; + PreparedStatementRenderer deleterenderer = new PreparedStatementRenderer(source, "/resources/annotationresult/sql/deleteResultsByAnnotationId.sql", parameters, parameterValues, deletesqlParameters, deletesqlValues); + String deleteStatement = deleterenderer.getSql(); + deleteVariables.add(deleterenderer.getOrderedParams()); + jdbcTemplate.batchUpdate(deleteStatement, deleteVariables); + } + + public void insertResults(Annotation annotation, JSONArray results, Study study) { +// might want to do a check- if the annotation ID+question ID already exists, update the existing row instead. Other option is just only query the latest instead + CohortSample sample = sampleRepository.findById(annotation.getCohortSampleId()); + Source source = getSourceRepository().findBySourceId(sample.getSourceId()); + JdbcTemplate jdbcTemplate = getSourceJdbcTemplate(source); + if (results.isNull(0)) { + return; + } + + String[] parameters = new String[] { "results_schema" }; + String[] parameterValues = new String[] { source.getTableQualifier(SourceDaimon.DaimonType.Results) }; + String[] sqlParameters = new String[] { "annotation_id", "question_id", "answer_id", "value", "type", "study_id" }; + + String statement = null; + List variables = new ArrayList<>(); + Boolean hasCleared=false; + for(int i=0; i < results.length(); i++){ + JSONObject object = results.getJSONObject(i); + if(!hasCleared && !getResultsByAnnotationId(annotation.getId()).isEmpty()){ +// this entry already exists, need to update here instead of adding to the pile + deleteResultsByAnnotationId(annotation); + hasCleared=true; + } + Object[] sqlValues = new Object[] { + annotation.getId(), + Integer.parseInt(object.get("questionId").toString()), + Integer.parseInt(object.get("answerId").toString()), + object.get("value").toString(), + object.get("type").toString(), + study.getId()}; + + PreparedStatementRenderer renderer = new PreparedStatementRenderer(source, "/resources/annotationresult/sql/insertResults.sql", parameters, parameterValues, sqlParameters, sqlValues); + + if (statement == null) { + statement = renderer.getSql(); + System.out.printf("statement: %s\n",statement); + } + + variables.add(renderer.getOrderedParams()); + } + System.out.printf("variables: %s\n",variables.toArray().toString()); + jdbcTemplate.batchUpdate(statement, variables); + } + + /** Maps a SQL result to a sample element. */ + private class ResultRowMapper implements RowMapper { + + ResultRowMapper() { + } + + @Override + public Result mapRow(ResultSet rs, int rowNum) throws SQLException { + int AnnotationIdInt=rs.getInt("annotation_id"); + if(AnnotationIdInt == 0){ + System.out.println("Annotation was null, none found"); + return null; + } + Result result = new Result(); + Annotation tempAnno = annotationService.getAnnotationsByAnnotationId(AnnotationIdInt); + if(tempAnno == null){ + System.out.println("Annotation was null, none found"); + return null; + } + result.setAnnotation(tempAnno); + result.setQuestionId(rs.getInt("question_id")); + result.setAnswerId(rs.getInt("answer_id")); + result.setValue(rs.getString("value")); + result.setType(rs.getString("type")); + return result; + } + } +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/result/SuperResultDto.java b/src/main/java/org/ohdsi/webapi/annotation/result/SuperResultDto.java new file mode 100644 index 0000000000..9732d55a88 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/result/SuperResultDto.java @@ -0,0 +1,143 @@ +package org.ohdsi.webapi.annotation.result; + +import org.ohdsi.webapi.annotation.annotation.Annotation; +import org.ohdsi.webapi.annotation.annotation.AnnotationService; +import org.ohdsi.webapi.annotation.answer.Answer; +import org.ohdsi.webapi.annotation.answer.AnswerService; +import org.ohdsi.webapi.annotation.question.Question; +import org.ohdsi.webapi.annotation.question.QuestionService; +import org.ohdsi.webapi.annotation.study.Study; +import org.ohdsi.webapi.source.Source; +import org.springframework.beans.factory.annotation.Autowired; + +//this table is for human viewing- don't expect to ever review this again +public class SuperResultDto { + + @Autowired + private AnnotationService annotationService; + + @Autowired + private QuestionService questionService; + + @Autowired + private AnswerService answerService; + + private int cohortId; + private String cohortName; + private String dataSourceKey; + private String cohortSampleName; + private String questionSetName; + private int patientId; + private String questionText; + private String answerText; + private String answerValue; + private Boolean caseStatus; + + public SuperResultDto(Result result){ + this.answerValue = result.getValue(); + } + + public SuperResultDto(Result result, Study study, Source source){ + Question myQuestion = questionService.getQuestionByQuestionId(result.getQuestionId()); + this.caseStatus = myQuestion.getCaseQuestion(); + this.questionText = myQuestion.getText(); + Answer tempAnswer = answerService.getAnswerById(result.getAnswerId()); + this.answerText = tempAnswer.getText(); + Annotation tempanno = annotationService.getAnnotationsByAnnotationId(result.getAnnotation()); + this.answerValue = result.getValue(); + this.patientId = tempanno.getSubjectId(); + this.cohortName= study.getCohortDefinition().getName(); + this.cohortId = study.getCohortDefinition().getId(); + this.dataSourceKey = source.getSourceKey(); + this.cohortSampleName = study.getCohortSample().getName(); + this.questionSetName = study.getQuestionSet().getName(); + } + + public SuperResultDto(Result result, Study study, Source source,Question myQuestion,Annotation tempanno){ + this.caseStatus = myQuestion.getCaseQuestion(); + this.questionText = myQuestion.getText(); + this.answerValue = result.getValue(); + this.patientId = tempanno.getSubjectId(); + this.cohortName= study.getCohortDefinition().getName(); + this.cohortId = study.getCohortDefinition().getId(); + this.dataSourceKey = source.getSourceKey(); + this.cohortSampleName = study.getCohortSample().getName(); + this.questionSetName = study.getQuestionSet().getName(); + Answer tempAnswer = answerService.getAnswerById(result.getAnswerId()); + this.answerText = tempAnswer.getText(); + } + + //***** GETTERS/SETTERS ***** + + public int getCohortId() { + return cohortId; + } + + public void setCohortId(int cohortId) { + this.cohortId = cohortId; + } + + public String getCohortName() { + return cohortName; + } + + public void setCohortName(String cohortName) { + this.cohortName = cohortName; + } + + public String getDataSourceKey() { + return dataSourceKey; + } + + public void setDataSourceKey(String dataSourceKey) { + this.dataSourceKey = dataSourceKey; + } + + public String getCohortSampleName() { + return cohortSampleName; + } + + public void setCohortSampleName(String cohortSampleName) { + this.cohortSampleName = cohortSampleName; + } + + public String getQuestionSetName() {return questionSetName;} + + public void setQuestionSetName(String questionSetName) { this.questionSetName = questionSetName; } + + public int getPatientId() { + return patientId; + } + + public void setPatientId(int patientId) { + this.patientId = patientId; + } + + public String getQuestionText() { + return questionText; + } + + public void setQuestionText(String questionText) { + this.questionText = questionText; + } + + public String getAnswerText() {return answerText;} + + public void setAnswerText(String answerText) {this.answerText = answerText;} + + public String getAnswerValue() { + return answerValue; + } + + public void setAnswerValue(String answerValue) { + this.answerValue = answerValue; + } + + public Boolean getCaseStatus() { + return caseStatus; + } + + public void setCaseStatus(Boolean caseStatus) { + this.caseStatus = caseStatus; + } +} \ No newline at end of file diff --git a/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSampleDto.java b/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSampleDto.java new file mode 100644 index 0000000000..03f4ca2672 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSampleDto.java @@ -0,0 +1,50 @@ +package org.ohdsi.webapi.annotation.set; + +public class QuestionSampleDto { + private int QuestionSetId; + private int CohortSampleId; + private String QuestionSetName; + private String CohortSampleName; + + public QuestionSampleDto(int questionSetId, int cohortSampleId, String questionSetName, String cohortSampleName) { + this.QuestionSetId = questionSetId; + this.CohortSampleId = cohortSampleId; + this.QuestionSetName = questionSetName; + this.CohortSampleName = cohortSampleName; + } + + //***** GETTERS/SETTERS ****** + + public int getQuestionSetId() { + return QuestionSetId; + } + + public void setQuestionSetId(int questionSetId) { + this.QuestionSetId = questionSetId; + } + + public int getCohortSampleId() { + return CohortSampleId; + } + + public void setCohortSampleId(int cohortSampleId) { + this.CohortSampleId = cohortSampleId; + } + + public String getQuestionSetName() { + return QuestionSetName; + } + + public void setQuestionSetName(String questionSetName) { + this.QuestionSetName = questionSetName; + } + + public String getCohortSampleName() { + return CohortSampleName; + } + + public void setCohortSampleName(String cohortSampleName) { + this.CohortSampleName = cohortSampleName; + } + +} \ No newline at end of file diff --git a/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSet.java b/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSet.java new file mode 100644 index 0000000000..ea3189364e --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSet.java @@ -0,0 +1,105 @@ +package org.ohdsi.webapi.annotation.set; + +import java.util.LinkedHashSet; +import java.util.Set; +import javax.persistence.*; + +import org.hibernate.annotations.GenericGenerator; +import org.ohdsi.webapi.annotation.question.Question; + +@Entity(name = "QuestionSet") +@Table(name = "annotation_set") +public class QuestionSet { + + @Id + @GenericGenerator( + name="set_generator", + strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", + parameters = { + @org.hibernate.annotations.Parameter(name="sequence_name",value="annotation_set_seq"), + @org.hibernate.annotations.Parameter(name="increment_size",value="1") + } + ) + @GeneratedValue(generator = "set_generator") + @Column(name = "set_id") + private int id; + + @Column(name = "cohort_definition_id") + private int cohortId; + + private String name; + + @OneToMany( + fetch = FetchType.EAGER, + mappedBy = "set", + cascade = CascadeType.ALL, + orphanRemoval = true + ) + @OrderBy("id") + private Set questions = new LinkedHashSet(); + + //***** GETTERS/SETTERS ****** + + /** + * @return the id + */ + public int getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(int id) { + this.id = id; + } + + /** + * @return the cohortId + */ + public int getCohortId() { + return cohortId; + } + + /** + * @param cohortId the cohortId to set + */ + public void setCohortId(int cohortId) { + this.cohortId = cohortId; + } + + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the questions + */ + public Set getQuestions() { + return new LinkedHashSet(questions); + } + + /** + * @param questions the questions to set + */ + protected void setQuestions(LinkedHashSet questions) { + this.questions = questions; + } + + public void addToQuestions(Question question) { + question.setSet(this); + this.questions.add(question); + } + +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSetController.java b/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSetController.java new file mode 100644 index 0000000000..2dd7cda734 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSetController.java @@ -0,0 +1,68 @@ +package org.ohdsi.webapi.annotation.set; + +import java.util.List; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; + +@Path("annotation/") +@Component +public class QuestionSetController { + + @Autowired + private QuestionSetService questionSetService; + + @GET + @Path("sets") + @Produces(MediaType.APPLICATION_JSON) + public List getSets(@QueryParam("cohortId") final Integer cohortId) { + + if (cohortId != null) { + return questionSetService.getSetsByCohortId(cohortId); + } + + return questionSetService.getSets(); + } + + @GET + @Path("getsets") + @Produces(MediaType.APPLICATION_JSON) + public List getSets( + @QueryParam("cohortId") final int cohortId + ) { + return questionSetService.getSamplesAndSetsByCohortId(cohortId); + } + + @GET + @Path("deleteSet/{questionSetId}") + @Produces(MediaType.APPLICATION_JSON) + public ResponseEntity deleteSet( + @PathParam("questionSetId") int questionSetId + ) { + if(questionSetService.deleteQuestionSet(questionSetId)){ + return ResponseEntity.status(200).body("The Question Set has been deleted"); + } + else{ + return ResponseEntity.status(400).body("Could not delete the Question Set"); + } + } + + @POST + @Path("sets") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public void addSet(QuestionSet set) { + + set.getQuestions().forEach((question) -> { + set.addToQuestions(question); + question.getAnswers().forEach((answer) -> { + question.addToAnswers(answer); + }); + }); + + questionSetService.addSet(set); + } +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSetRepository.java b/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSetRepository.java new file mode 100644 index 0000000000..81c5b3bfd5 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSetRepository.java @@ -0,0 +1,16 @@ +package org.ohdsi.webapi.annotation.set; + +import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; +import java.util.Set; +import org.springframework.data.jpa.repository.Query; + + +public interface QuestionSetRepository extends JpaRepository { + + public Set findByCohortId(Integer cohortId); + public QuestionSet findById(int id); + + @Query("Select distinct new org.ohdsi.webapi.annotation.set.QuestionSampleDto(q.id,c.id,q.name,c.name) FROM QuestionSet q INNER JOIN Annotation a ON a.questionSet.id = q.id INNER JOIN CohortSample c ON c.id = a.cohortSampleId WHERE q.cohortId = ?1") + public List findSamplesAndSetsByCohortId(int cohortId); +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSetService.java b/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSetService.java new file mode 100644 index 0000000000..c471de54ac --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/set/QuestionSetService.java @@ -0,0 +1,53 @@ +package org.ohdsi.webapi.annotation.set; + +import org.ohdsi.webapi.annotation.annotation.AnnotationService; +import org.springframework.stereotype.Service; +import org.springframework.beans.factory.annotation.Autowired; +import java.util.ArrayList; +import java.util.List; + +@Service +public class QuestionSetService { + + @Autowired + private QuestionSetRepository questionSetRepository; + + @Autowired + private AnnotationService annotationService; + + public List getSets() { + List sets = new ArrayList(); + questionSetRepository.findAll() + .forEach(sets::add); + return sets; + } + + public List getSetsByCohortId(Integer cohortId) { + List sets = new ArrayList(); + questionSetRepository.findByCohortId(cohortId) + .forEach(sets::add); + return sets; + } + + public void addSet(QuestionSet set) { + questionSetRepository.save(set); + } + + public QuestionSet findQuestionSetByQuestionSetId(int questionSetId){ + return questionSetRepository.findById(questionSetId); + } + + public List getSamplesAndSetsByCohortId(int cohortId) { + return questionSetRepository.findSamplesAndSetsByCohortId(cohortId); + } + + public boolean deleteQuestionSet(int questionSetId) { + if(annotationService.getAnnotationsByQuestionSetId(questionSetId).isEmpty()){ + questionSetRepository.delete(questionSetId); + return true; + } + else{ + return false; + } + } +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/study/Study.java b/src/main/java/org/ohdsi/webapi/annotation/study/Study.java new file mode 100644 index 0000000000..99fde1211d --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/study/Study.java @@ -0,0 +1,82 @@ +package org.ohdsi.webapi.annotation.study; + +import org.hibernate.annotations.GenericGenerator; +import org.ohdsi.webapi.annotation.set.QuestionSet; +import org.ohdsi.webapi.cohortdefinition.CohortDefinition; +import org.ohdsi.webapi.cohortsample.CohortSample; +import org.ohdsi.webapi.source.Source; + +import javax.persistence.*; + +@Entity(name = "Study") +@Table(name = "annotation_study") +public class Study { + @Id + @GenericGenerator( + name="annotation_study_generator", + strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", + parameters = { + @org.hibernate.annotations.Parameter(name="sequence_name",value="annotation_study_seq"), + @org.hibernate.annotations.Parameter(name="increment_size",value="1") + } + ) + @GeneratedValue(generator = "annotation_study_generator") + @Column(name = "study_id") + private int id; + + @ManyToOne + @JoinColumn(name = "question_set_id") + private QuestionSet questionSet; + + @ManyToOne + @JoinColumn(name = "cohort_definition_id") + private CohortDefinition cohortDefinition; + + @ManyToOne + @JoinColumn(name = "cohort_sample_id") + private CohortSample cohortSample; + + @ManyToOne + @JoinColumn(name = "source_id") + private Source source; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public QuestionSet getQuestionSet() { + return questionSet; + } + + public void setQuestionSet(QuestionSet questionSet) { + this.questionSet = questionSet; + } + + public CohortDefinition getCohortDefinition() { + return cohortDefinition; + } + + public void setCohortDefinition(CohortDefinition cohortDefinition) { + this.cohortDefinition = cohortDefinition; + } + + public CohortSample getCohortSample() { + return cohortSample; + } + + public void setCohortSample(CohortSample cohortSample) { + this.cohortSample = cohortSample; + } + + public Source getSource() { + return source; + } + + public void setSource(Source source) { + this.source = source; + } +} diff --git a/src/main/java/org/ohdsi/webapi/annotation/study/StudyRepository.java b/src/main/java/org/ohdsi/webapi/annotation/study/StudyRepository.java new file mode 100644 index 0000000000..9ab5bd41b3 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/study/StudyRepository.java @@ -0,0 +1,14 @@ +package org.ohdsi.webapi.annotation.study; + +import org.ohdsi.webapi.annotation.annotation.Annotation; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +public interface StudyRepository extends JpaRepository { + + @Query("Select s FROM Study s WHERE s.id = ?1") + public Study findByStudyId(int study_id); + + @Query("Select s FROM Study s WHERE s.questionSet.id = ?1 AND s.cohortSample.id = ?2") + public Study findByQuestionSetIdAndSampleId(int questionSetId,int sampleId); +} \ No newline at end of file diff --git a/src/main/java/org/ohdsi/webapi/annotation/study/StudyService.java b/src/main/java/org/ohdsi/webapi/annotation/study/StudyService.java new file mode 100644 index 0000000000..64024a3571 --- /dev/null +++ b/src/main/java/org/ohdsi/webapi/annotation/study/StudyService.java @@ -0,0 +1,21 @@ +package org.ohdsi.webapi.annotation.study; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + + +@Service +public class StudyService { + @Autowired + private StudyRepository studyRepository; + + public void addStudy(Study study) { + studyRepository.save(study); + } + public Study getStudyById(int studyId){ + return studyRepository.findByStudyId(studyId); + } + public Study getStudyByQuestionSetIdAndSampleId(int questionSetId, int sampleId){ + return studyRepository.findByQuestionSetIdAndSampleId(questionSetId,sampleId); + } +} diff --git a/src/main/resources/db/migration/postgresql/V2.10.1.20210823142322__annotations.sql b/src/main/resources/db/migration/postgresql/V2.10.1.20210823142322__annotations.sql new file mode 100644 index 0000000000..bbe7181e9e --- /dev/null +++ b/src/main/resources/db/migration/postgresql/V2.10.1.20210823142322__annotations.sql @@ -0,0 +1,86 @@ +CREATE SEQUENCE ${ohdsiSchema}.annotation_set_seq; + +CREATE TABLE ${ohdsiSchema}.annotation_set ( + set_id integer NOT NULL DEFAULT NEXTVAL('annotation_set_seq'), + name VARCHAR(255) NOT NULL, + cohort_definition_id integer NOT NULL, + date_created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now()), + date_updated TIMESTAMP WITH TIME ZONE, + CONSTRAINT fk_annotation_set_cohor_definition_id FOREIGN KEY (cohort_definition_id) + REFERENCES ${ohdsiSchema}.cohort_definition (id) ON DELETE CASCADE, + CONSTRAINT annotation_set_pkey PRIMARY KEY (set_id) +); + +CREATE INDEX idx_cohort_annotation_set ON ${ohdsiSchema}.annotation_set (cohort_definition_id); + +CREATE SEQUENCE ${ohdsiSchema}.annotation_question_seq; + +CREATE TABLE ${ohdsiSchema}.annotation_question ( + question_id integer NOT NULL DEFAULT NEXTVAL('annotation_question_seq'), + set_id integer NOT NULL, + question_name VARCHAR(255) NOT NULL, + question_type VARCHAR(50) NOT NULL, + required Boolean NOT NULL, + case_question Boolean NOT NULL, + help_text text, + date_created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now()), + date_updated TIMESTAMP WITH TIME ZONE, + CONSTRAINT annotation_questions_pkey PRIMARY KEY (question_id), + CONSTRAINT annotation_question_set_fk FOREIGN KEY (set_id) + REFERENCES ${ohdsiSchema}.annotation_set(set_id) ON DELETE CASCADE +); + +CREATE INDEX idx_annotation_set_question ON ${ohdsiSchema}.annotation_question (set_id); + +CREATE SEQUENCE ${ohdsiSchema}.annotation_answer_seq; + +CREATE TABLE ${ohdsiSchema}.annotation_answer ( + answer_id integer NOT NULL DEFAULT NEXTVAL('annotation_answer_seq'), + question_id integer NOT NULL, + text character varying(250) NOT NULL, + value character varying(250) NOT NULL, + help_text text, + CONSTRAINT annotation_answer_pkey PRIMARY KEY (answer_id), + CONSTRAINT annotation_answer_fk FOREIGN KEY (question_id) + REFERENCES ${ohdsiSchema}.annotation_question(question_id) ON DELETE CASCADE +); + +CREATE INDEX idx_annotation_answer_question ON ${ohdsiSchema}.annotation_question (question_id); + +CREATE SEQUENCE ${ohdsiSchema}.annotation_seq; + +CREATE TABLE ${ohdsiSchema}.annotation ( + annotation_id integer NOT NULL DEFAULT NEXTVAL('annotation_seq'), + subject_id integer NOT NULL, + cohort_sample_id integer NOT NULL, + question_set_id integer NOT NULL, + date_created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now()), + CONSTRAINT annotation_pkey PRIMARY KEY (annotation_id), + CONSTRAINT annotation_set_fk FOREIGN KEY (question_set_id) + REFERENCES ${ohdsiSchema}.annotation_set(set_id), + CONSTRAINT annotation_sample_fk FOREIGN KEY (cohort_sample_id) + REFERENCES ${ohdsiSchema}.cohort_sample(id), + CONSTRAINT unq_subject_sample_set UNIQUE(subject_id, cohort_sample_id, question_set_id) +); + +CREATE INDEX idx_annotation_sample_subject ON ${ohdsiSchema}.annotation (cohort_sample_id,subject_id); +CREATE INDEX idx_annotation_set ON ${ohdsiSchema}.annotation (question_set_id); + +CREATE SEQUENCE ${ohdsiSchema}.annotation_study_seq; + +CREATE TABLE ${ohdsiSchema}.annotation_study ( + study_id integer NOT NULL DEFAULT NEXTVAL('annotation_study_seq'), + cohort_sample_id integer NOT NULL, + question_set_id integer NOT NULL, + cohort_definition_id integer NOT NULL, + source_id integer NOT NULL, + CONSTRAINT annotation_study_pkey PRIMARY KEY (study_id), + CONSTRAINT annotation_study_set_fk FOREIGN KEY (question_set_id) + REFERENCES ${ohdsiSchema}.annotation_set(set_id), + CONSTRAINT annotation_study_sample_fk FOREIGN KEY (cohort_sample_id) + REFERENCES ${ohdsiSchema}.cohort_sample(id), + CONSTRAINT annotation_study_definition_fk FOREIGN KEY (cohort_definition_id) + REFERENCES ${ohdsiSchema}.cohort_definition(id), + CONSTRAINT annotation_study_source_fk FOREIGN KEY (source_id) + REFERENCES ${ohdsiSchema}.source(source_id) +); diff --git a/src/main/resources/db/migration/postgresql/V2.10.1.20211108000000__annotations_security.sql b/src/main/resources/db/migration/postgresql/V2.10.1.20211108000000__annotations_security.sql new file mode 100644 index 0000000000..a8bb2e4ce2 --- /dev/null +++ b/src/main/resources/db/migration/postgresql/V2.10.1.20211108000000__annotations_security.sql @@ -0,0 +1,41 @@ +INSERT INTO ${ohdsiSchema}.sec_permission(id, value, description) +VALUES (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:get', 'Gets annotation summary'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:*:get', 'Get annotation result by ID.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:fullquestion:get', 'Get annotation result by ID.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:post', 'Adds an annotation result.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:sample:post', 'Adds an annotation to sample.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:answers:post', 'Adds an annotation to sample.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:navigation:get', 'Gets annotation navigation.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:questions:get', 'Gets annotation questions.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:questions:post', 'Adds question to annotation.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:results:get', 'Gets annotation results.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:results:*:get', 'Gets annotation results by ID.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:results:completeResults:get', 'Gets complete annotation results.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:sets:get', 'Gets annotation sets.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:sets:post', 'Create annotation sets.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:getsets:get', 'Gets annotation sets as getsets.'), + (NEXTVAL('${ohdsiSchema}.sec_permission_id_seq'), 'annotation:deleteset:*:get', 'Deletes an annotation set.') +; + +INSERT INTO ${ohdsiSchema}.sec_role_permission(role_id, permission_id) +SELECT sr.id, sp.id +FROM ${ohdsiSchema}.sec_permission SP, + ${ohdsiSchema}.sec_role sr +WHERE sp.value IN ( + 'annotation:get', + 'annotation:*:get', + 'annotation:fullquestion:get', + 'annotation:post', + 'annotation:sample:post', + 'annotation:answers:post', + 'annotation:navigation:get', + 'annotation:questions:get', + 'annotation:questions:post', + 'annotation:results:get', + 'annotation:results:*:get', + 'annotation:sets:get', + 'annotation:sets:post', + 'annotation:getsets:get', + 'annotation:deleteset:*:get') + AND sr.name IN ('Atlas users'); + \ No newline at end of file diff --git a/src/main/resources/ddl/results/annotation_result.sql b/src/main/resources/ddl/results/annotation_result.sql new file mode 100644 index 0000000000..c11222075d --- /dev/null +++ b/src/main/resources/ddl/results/annotation_result.sql @@ -0,0 +1,10 @@ +IF OBJECT_ID('@results_schema.annotation_result', 'U') IS NULL +CREATE TABLE @results_schema.annotation_result( + annotation_id integer NOT NULL, + question_id integer NOT NULL, + answer_id bigint NOT NULL, + study_id int NOT NULL, + value VARCHAR(255) NOT NULL, + type VARCHAR(255) NOT NULL, + last_update_time datetime DEFAULT GETDATE() +); diff --git a/src/main/resources/resources/annotationresult/sql/deleteResultsByAnnotationId.sql b/src/main/resources/resources/annotationresult/sql/deleteResultsByAnnotationId.sql new file mode 100644 index 0000000000..365f4b6c15 --- /dev/null +++ b/src/main/resources/resources/annotationresult/sql/deleteResultsByAnnotationId.sql @@ -0,0 +1,3 @@ +DELETE FROM @results_schema.annotation_result +WHERE annotation_id = @annotation_id +; diff --git a/src/main/resources/resources/annotationresult/sql/findResultsByAnnotationId.sql b/src/main/resources/resources/annotationresult/sql/findResultsByAnnotationId.sql new file mode 100644 index 0000000000..aa8cb669c5 --- /dev/null +++ b/src/main/resources/resources/annotationresult/sql/findResultsByAnnotationId.sql @@ -0,0 +1,4 @@ +SELECT * +FROM @results_schema.annotation_result s +WHERE s.annotation_id = @annotation_id +; \ No newline at end of file diff --git a/src/main/resources/resources/annotationresult/sql/findResultsByAnnotationIdAndQuestionId.sql b/src/main/resources/resources/annotationresult/sql/findResultsByAnnotationIdAndQuestionId.sql new file mode 100644 index 0000000000..1e42dcda96 --- /dev/null +++ b/src/main/resources/resources/annotationresult/sql/findResultsByAnnotationIdAndQuestionId.sql @@ -0,0 +1,4 @@ +SELECT * +FROM @results_schema.annotation_result s +WHERE s.annotation_id = @annotation_id AND s.question_id = @question_id +; \ No newline at end of file diff --git a/src/main/resources/resources/annotationresult/sql/findResultsByAnnotationIds.sql b/src/main/resources/resources/annotationresult/sql/findResultsByAnnotationIds.sql new file mode 100644 index 0000000000..f9351990e5 --- /dev/null +++ b/src/main/resources/resources/annotationresult/sql/findResultsByAnnotationIds.sql @@ -0,0 +1,4 @@ +SELECT * +FROM @results_schema.annotation_result s +WHERE s.annotation_id IN ( @idList ) +; \ No newline at end of file diff --git a/src/main/resources/resources/annotationresult/sql/findResultsByQuestionSetId.sql b/src/main/resources/resources/annotationresult/sql/findResultsByQuestionSetId.sql new file mode 100644 index 0000000000..1200f5c516 --- /dev/null +++ b/src/main/resources/resources/annotationresult/sql/findResultsByQuestionSetId.sql @@ -0,0 +1,4 @@ +SELECT * +FROM @results_schema.annotation_result s +WHERE s.question_id in (@annotationId) +; \ No newline at end of file diff --git a/src/main/resources/resources/annotationresult/sql/findResultsByStudyId.sql b/src/main/resources/resources/annotationresult/sql/findResultsByStudyId.sql new file mode 100644 index 0000000000..599c8d8127 --- /dev/null +++ b/src/main/resources/resources/annotationresult/sql/findResultsByStudyId.sql @@ -0,0 +1,4 @@ +SELECT * +FROM @results_schema.annotation_result s +WHERE s.study_id in (@studyId) +; \ No newline at end of file diff --git a/src/main/resources/resources/annotationresult/sql/insertResults.sql b/src/main/resources/resources/annotationresult/sql/insertResults.sql new file mode 100644 index 0000000000..b357452940 --- /dev/null +++ b/src/main/resources/resources/annotationresult/sql/insertResults.sql @@ -0,0 +1,3 @@ +INSERT INTO @results_schema.annotation_result (annotation_id, question_id, answer_id, value, type,study_id) +VALUES (@annotation_id, @question_id, @answer_id, @value, @type, @study_id) +; diff --git a/src/main/resources/resources/annotationresult/sql/updateResults.sql b/src/main/resources/resources/annotationresult/sql/updateResults.sql new file mode 100644 index 0000000000..ab4178888a --- /dev/null +++ b/src/main/resources/resources/annotationresult/sql/updateResults.sql @@ -0,0 +1,4 @@ +UPDATE @results_schema.annotation_result +SET answer_id = @answer_id, value = @value, last_update_time=GETDATE() +WHERE annotation_id = @annotation_id AND question_id = @question_id +;