Skip to content

Commit bb84543

Browse files
committed
basic insert and delete of config
1 parent 5bd4c74 commit bb84543

File tree

6 files changed

+267
-36
lines changed

6 files changed

+267
-36
lines changed

app/assets/javascripts/build/cohortdetails.js

+23-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/assets/javascripts/build/cohorts.js

+10-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/assets/javascripts/components/SubjectDetail.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ class SubjectDetail extends React.Component {
391391
};
392392

393393
setDomainCounts(newCounts) {
394-
if (newCounts != this.state.domainCounts) {
394+
if (newCounts !== this.state.domainCounts) {
395395
//console.log(newCounts);
396396
this.setState(prevState => ({
397397
domainCounts: newCounts

app/controllers/AnnotationController.scala

+193-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import scala.collection.mutable.ListBuffer
1818
*/
1919
class AnnotationController @Inject() (db: Database) extends Controller {
2020

21+
val formatter = new java.text.SimpleDateFormat("yyyy-MM-dd")
22+
2123
def putAnnotationQuestion(annotationId: Int) = Action { request =>
2224
val json = request.body.asJson.get
2325
val conn = db.getConnection()
@@ -438,13 +440,14 @@ class AnnotationController @Inject() (db: Database) extends Controller {
438440
)(AnnotationSetQuestion.apply, unlift(AnnotationSetQuestion.unapply))
439441
}
440442

441-
def getAnnotationSetQuestion(annotationSetDefinitionId:Long) = Action {
443+
def getAnnotationSetQuestion(annotationSetId:Long) = Action {
442444
val conn = db.getConnection()
443445
var annotationSetQuestion = List[AnnotationSetQuestion]()
444446
val queryString = s"""select aq.* from validation.annotation_set_question asq
445-
inner join validation.annotation_question aq on aq.annotation_question_id = asq.annotation_question_id
446-
where asq.annotation_set_definition_id = $annotationSetDefinitionId
447-
order by aq.annotation_question_id asc"""
447+
inner join validation.annotation_question aq on aq.annotation_question_id = asq.annotation_question_id
448+
inner join validation.annotation_set as1 on as1.annotation_set_definition_id = asq.annotation_set_definition_id
449+
where as1.annotation_set_id = $annotationSetId
450+
order by aq.annotation_question_id asc"""
448451
try {
449452
val rs = conn.createStatement().executeQuery(queryString)
450453
var i = 1
@@ -540,4 +543,190 @@ class AnnotationController @Inject() (db: Database) extends Controller {
540543
}
541544
Created("{success:True}")
542545
}
546+
547+
def deleteAnnotationSet() = Action { request =>
548+
val json = request.body.asJson.get
549+
val conn = db.getConnection()
550+
var success = false
551+
552+
try {
553+
val annotationSetId: Long = (json \ "annotation_set_id").asOpt[Long].getOrElse(-1L)
554+
if (annotationSetId > 0) {
555+
conn.createStatement().execute(
556+
s"""
557+
DELETE FROM validation.annotation_set_allocation where annotation_set_id = $annotationSetId
558+
""".stripMargin)
559+
conn.createStatement().execute(
560+
s"""
561+
DELETE FROM validation.annotation_set where annotation_set_id = $annotationSetId
562+
""".stripMargin)
563+
success = true
564+
}
565+
} finally {
566+
conn.close()
567+
}
568+
569+
570+
Created("{success: " + success + "}")
571+
}
572+
573+
case class AnswersJson(
574+
text: String,
575+
value: String
576+
)
577+
case class QuestionsJson(
578+
question_name: String,
579+
question_type: String,
580+
answers: List[AnswersJson]
581+
)
582+
case class AnnotationConfigJson(
583+
name: String,
584+
cohort_name: String,
585+
cohort_source: String,
586+
cohort_id: Int,
587+
owner: String,
588+
allocated_users: List[String],
589+
questions: List[QuestionsJson]
590+
)
591+
592+
object AnswersJson {
593+
implicit val format: Format[AnswersJson] = (
594+
(__ \ "text").format[String] and
595+
(__ \ "value").format[String]
596+
)(AnswersJson.apply, unlift(AnswersJson.unapply))
597+
}
598+
599+
object QuestionsJson {
600+
implicit val format: Format[QuestionsJson] = (
601+
(__ \ "question_name").format[String] and
602+
(__ \ "question_type").format[String] and
603+
(__ \ "answers").format[List[AnswersJson]]
604+
)(QuestionsJson.apply, unlift(QuestionsJson.unapply))
605+
}
606+
607+
def putAnnotationConfig() = Action { request =>
608+
val json = request.body.asJson.get
609+
val conn = db.getConnection()
610+
var name: String = "Unable to match"
611+
var success: Boolean = false
612+
var asId: Long = -1L
613+
var asdId: Long = -1L
614+
615+
try {
616+
617+
618+
name = (json \ "name").asOpt[String].getOrElse("")
619+
val cohortName: String = (json \ "cohort_name").asOpt[String].getOrElse("")
620+
val cohortSource: String = (json \ "cohort_source").asOpt[String].getOrElse("")
621+
val cohortId: Int = (json \ "cohort_id").asOpt[Int].getOrElse(-1)
622+
val owner: String = (json \ "owner").asOpt[String].getOrElse("")
623+
val allocatedUsers : List[String] = (json \ "allocated_users").asOpt[List[String]].getOrElse(List[String]())
624+
val questions = (json \ "questions").asOpt[List[QuestionsJson]].getOrElse(List[QuestionsJson]())
625+
626+
if (name.nonEmpty && cohortName.nonEmpty && cohortSource.nonEmpty &&
627+
owner.nonEmpty && allocatedUsers.nonEmpty && questions.nonEmpty && cohortId > 0) {
628+
629+
val dt = formatter.format(new java.util.Date())
630+
val asdIdRs = conn.createStatement().executeQuery("select nextval('validation.annotation_set_definition_seq')")
631+
asdId = if (asdIdRs.next()) asdIdRs.getLong(1) else -1L
632+
633+
val asIdRs = conn.createStatement().executeQuery("select nextval('validation.annotation_set_seq')")
634+
asId = if (asIdRs.next()) asIdRs.getLong(1) else -1L
635+
636+
if (asdId > 0 && asId > 0) {
637+
val asdInsertString =
638+
s"""
639+
INSERT INTO validation.annotation_set_definition
640+
(annotation_set_definition_id, name, config, owner, date_created, date_updated)
641+
VALUES
642+
('$asdId', '$name', '${request.body.asJson.toString.replace("'", "''")}', '$owner', '$dt', '$dt')
643+
""".stripMargin
644+
conn.createStatement().execute(asdInsertString)
645+
646+
val asInsertString =
647+
s"""
648+
INSERT INTO validation.annotation_set
649+
(annotation_set_id, annotation_set_definition_id, cohort_name, cohort_source, cohort_id, owner, date_created, date_updated)
650+
VALUES
651+
($asId, $asdId, '$cohortName', '$cohortSource', $cohortId, '$owner', '$dt', '$dt')
652+
""".stripMargin
653+
conn.createStatement().execute(asInsertString)
654+
655+
allocatedUsers.foreach(u => {
656+
val asaIdRs = conn.createStatement().executeQuery("select nextval('validation.annotation_set_allocation_seq')")
657+
val asaId: Long = if (asaIdRs.next()) asaIdRs.getLong(1) else -1L
658+
659+
val userIdRs = conn.createStatement().executeQuery(s"select user_id from validation.validation_user where username = '$u'")
660+
val userId: Long = if (userIdRs.next()) userIdRs.getLong(1) else -1L
661+
662+
if (userId > 0 && asaId > 0) {
663+
val asaInsertString =
664+
s"""
665+
INSERT INTO validation.annotation_set_allocation
666+
(annotation_set_allocation_id, annotation_set_id, user_id, allocated_items, date_created, date_updated)
667+
VALUES
668+
('$asaId', '$asId', '$userId', '', '$dt', '$dt')
669+
""".stripMargin
670+
conn.createStatement().execute(asaInsertString)
671+
}
672+
})
673+
674+
questions.foreach(q => {
675+
val aqIdRs = conn.createStatement().executeQuery("select nextval('validation.annotation_question_seq')")
676+
val aqId: Long = if (aqIdRs.next()) aqIdRs.getLong(1) else -1L
677+
678+
val asqIdRs = conn.createStatement().executeQuery("select nextval('validation.annotation_set_question_seq')")
679+
val asqId: Long = if (asqIdRs.next()) asqIdRs.getLong(1) else -1L
680+
681+
if (aqId > 0 && asqId > 0) {
682+
val aqInsertString =
683+
s"""
684+
INSERT INTO validation.annotation_question
685+
(annotation_question_id, question_name, question_type, help_text, constraints, date_created, date_updated)
686+
VALUES
687+
($aqId, '${q.question_name.replace("'", "''")}', '${q.question_type}', '', '', '$dt', '$dt')
688+
""".stripMargin
689+
conn.createStatement().execute(aqInsertString)
690+
691+
val asqInsertString =
692+
s"""
693+
INSERT INTO validation.annotation_set_question
694+
(annotation_set_question_id, annotation_set_definition_id, annotation_question_id)
695+
VALUES
696+
($asqId, $asdId, $aqId)
697+
""".stripMargin
698+
conn.createStatement().execute(asqInsertString)
699+
700+
q.answers.foreach(a => {
701+
val aqaIdRs = conn.createStatement().executeQuery("select nextval('validation.annotation_question_seq')")
702+
val aqaId: Long = if (aqaIdRs.next()) aqaIdRs.getLong(1) else -1L
703+
704+
if (aqaId > 0) {
705+
val aqaInsertInsertString =
706+
s"""
707+
INSERT INTO validation.annotation_question_answer
708+
(annotation_question_answer_id, annotation_question_id, text, value, help_text, date_created, date_updated)
709+
VALUES
710+
($aqaId, $aqId, '${a.text.replace("'", "''")}', '${a.value.replace("'", "''")}', '', '$dt', '$dt')
711+
""".stripMargin
712+
conn.createStatement().execute(aqaInsertInsertString)
713+
}
714+
})
715+
}
716+
717+
718+
})
719+
720+
success = true
721+
}
722+
}
723+
724+
} finally {
725+
conn.close()
726+
}
727+
Created("{success:" + success + ", name: '" + name +
728+
"', annotation_set_id: " + asId + ", annotation_set_definition_id: " + asdId + "}")
729+
}
730+
731+
543732
}

conf/routes

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ PUT /annotation_set/:annotationSetId controllers.AnnotationCo
2929
GET /annotation_set/name/:username controllers.AnnotationController.getAnnotationSetByUsername(username:String)
3030
GET /annotation_set controllers.AnnotationController.getAllAnnotationSets()
3131
GET /annotation_set/owner/:owner controllers.AnnotationController.getAllAnnotationSetsByOwner(owner:String)
32-
GET /annotation_set/question/:annotationSetDefinitionId controllers.AnnotationController.getAnnotationSetQuestion(annotationSetDefinitionId:Long)
32+
GET /annotation_set/question/:annotationSetId controllers.AnnotationController.getAnnotationSetQuestion(annotationSetId:Long)
3333
GET /annotation_set/name/:username/id/:validationSetId controllers.AnnotationController.getAnnotationSetByUsernameAndSetID(username:String,validationSetId:Int)
3434
GET /annotation_set/result/:username/id/:validationSetId controllers.AnnotationController.getAnnotationSetByUsernameAndSetID(username:String,validationSetId:Int)
3535
GET /annotation_set/result/:annotationSetId controllers.AnnotationController.getAnnotationSetBySetID(annotationSetId:Long)
3636
GET /download_result/:annotationSetId controllers.AnnotationController.downloadAnnotationSetById(annotationSetId:Long)
3737
POST /annotation_set/save controllers.AnnotationController.putAnnotationSetResult()
38+
POST /annotation_config/save controllers.AnnotationController.putAnnotationConfig()
39+
POST /annotation_set/delete controllers.AnnotationController.deleteAnnotationSet()
3840

3941
# Validation Table Endpoints
4042
GET /validation/users controllers.ValidationController.getUsers()

test/test.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "PTSD Validation",
3+
"cohort_name": "PTSD",
4+
"cohort_source": "WebAPI",
5+
"cohort_id": 6,
6+
"owner": "jduke",
7+
"allocated_users": ["jduke", "chilton9", "user"],
8+
"questions": [{
9+
"question_name": "What's your name?",
10+
"question_type": "TEXT",
11+
"answers" : []
12+
}, {
13+
"question_name": "What's age?",
14+
"question_type": "MULTIPLE_CHOICE",
15+
"answers": [{
16+
"text": "0-18",
17+
"value": "group1"
18+
},
19+
{
20+
"text": "19-35",
21+
"value": "group2"
22+
},
23+
{
24+
"text": "36-50",
25+
"value": "group3"
26+
},
27+
{
28+
"text": "51-70",
29+
"value": "group4"
30+
},
31+
{
32+
"text": "71 and older",
33+
"value": "group4"
34+
}
35+
]
36+
}]
37+
}

0 commit comments

Comments
 (0)