@@ -18,6 +18,8 @@ import scala.collection.mutable.ListBuffer
18
18
*/
19
19
class AnnotationController @ Inject () (db : Database ) extends Controller {
20
20
21
+ val formatter = new java.text.SimpleDateFormat (" yyyy-MM-dd" )
22
+
21
23
def putAnnotationQuestion (annotationId : Int ) = Action { request =>
22
24
val json = request.body.asJson.get
23
25
val conn = db.getConnection()
@@ -438,13 +440,14 @@ class AnnotationController @Inject() (db: Database) extends Controller {
438
440
)(AnnotationSetQuestion .apply, unlift(AnnotationSetQuestion .unapply))
439
441
}
440
442
441
- def getAnnotationSetQuestion (annotationSetDefinitionId : Long ) = Action {
443
+ def getAnnotationSetQuestion (annotationSetId : Long ) = Action {
442
444
val conn = db.getConnection()
443
445
var annotationSetQuestion = List [AnnotationSetQuestion ]()
444
446
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 """
448
451
try {
449
452
val rs = conn.createStatement().executeQuery(queryString)
450
453
var i = 1
@@ -540,4 +543,190 @@ class AnnotationController @Inject() (db: Database) extends Controller {
540
543
}
541
544
Created (" {success:True}" )
542
545
}
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
+
543
732
}
0 commit comments