Skip to content

Commit

Permalink
merge backend/enhancement/annotation-service impl to develop
Browse files Browse the repository at this point in the history
  • Loading branch information
emresin12 committed Dec 25, 2023
2 parents e008d12 + a61f24a commit 3d72b27
Show file tree
Hide file tree
Showing 17 changed files with 306 additions and 141 deletions.
Original file line number Diff line number Diff line change
@@ -1,95 +1,86 @@
package com.group6.annotationservice.Utils
import com.group6.annotationservice.dtos.AnnotationDto
import com.group6.annotationservice.dtos.BodyDto
import com.group6.annotationservice.dtos.SelectorDto
import com.group6.annotationservice.dtos.TargetDto

import com.group6.annotationservice.dtos.*
import com.group6.annotationservice.entity.Annotation
import com.group6.annotationservice.entity.Body
import com.group6.annotationservice.entity.Selector
import com.group6.annotationservice.entity.Target
import com.group6.annotationservice.entity.FragmentSelector
import com.group6.annotationservice.entity.TextPositionSelector

object DtoConverter {
fun convertAnnotationDtoToAnnotation(annotationDto: AnnotationDto): Annotation {
val annotation = Annotation(
motivation = annotationDto.motivation,
creator = annotationDto.creator,
created = annotationDto.created,
id = annotationDto.id,
type = annotationDto.type,
context = annotationDto.context
)

val bodyList = ArrayList<Body>()
if (annotationDto.body != null){
for (bodyDto in annotationDto.body) {
val body = Body(
type = bodyDto.type,
format = bodyDto.format,
value = bodyDto.value,
language = bodyDto.language,
purpose = bodyDto.purpose,
id = bodyDto.id,
annotation = annotation
)
bodyList.add(body)
}
}
val targetList = ArrayList<Target>()
for (targetDto in annotationDto.target) {
val selectorList = ArrayList<Selector>()
if (targetDto.selectors != null){
for (selectorDto in targetDto.selectors) {
val selector = Selector(
type = selectorDto.type,
startIndex = selectorDto.startIndex,
endIndex = selectorDto.endIndex
)
selectorList.add(selector)
}
}
val target = Target(
id = targetDto.id,
format = targetDto.format,
language = targetDto.language,
annotation = annotation,
selector = selectorList
)

targetList.add(target)
}
annotation.body = bodyList
annotation.target = targetList
// fun convertAnnotationDtoToAnnotation(annotationDto: AnnotationDto): Annotation {
// val annotation = Annotation(
// motivation = annotationDto.motivation,
// creator = annotationDto.creator,
// created = annotationDto.created,
// id = annotationDto.id,
// type = annotationDto.type,
// context = annotationDto.context
// )
//
// val bodyList = ArrayList<Body>()
// if (annotationDto.body != null) {
// for (bodyDto in annotationDto.body) {
// val body = Body(
// type = bodyDto.type,
// format = bodyDto.format,
// value = bodyDto.value,
// language = bodyDto.language,
// purpose = bodyDto.purpose,
// id = bodyDto.id,
// annotation = annotation
// )
// bodyList.add(body)
// }
// }
//
// val targetList = ArrayList<Target>()
//
// for (targetDto in annotationDto.target) {
//
// val selectorList = ArrayList<Selector>()
//
// if (targetDto.selectors != null) {
// for (selectorDto in targetDto.selectors) {
//
// if (selectorDto.type == "FragmentSelector") {
// val selector = FragmentSelector(
// type = selectorDto.type,
// value = (selectorDto as FragmentSelectorDto).value
// )
// selectorList.add(selector)
// }
// else if (selectorDto.type == "TextPositionSelector"){
// val selector = TextPositionSelector(
// type = selectorDto.type,
// startIndex = (selectorDto as TextPositionSelectorDto).start,
// endIndex = selectorDto.end
// )
// selectorList.add(selector)
// }
//
// }
// }
//
// val target = Target(
// id = targetDto.id,
// format = targetDto.format,
// language = targetDto.language,
// annotations = annotation,
// selector = selectorList
// )
//
// targetList.add(target)
// }
// annotation.body = bodyList
// annotation.target = targetList
//
//
// return annotation
// }


return annotation
}
fun convertAnnotationToAnnotationDto(annotation: Annotation): AnnotationDto {
val targetDtoArrayList = ArrayList<TargetDto>()
for (annotationTarget in annotation.target) {
val selectorDtoList = ArrayList<SelectorDto>()
if (annotationTarget.selector != null){
for (selector in annotationTarget.selector) {
val selectorDto = SelectorDto(
type = selector.type,
startIndex = selector.startIndex,
endIndex = selector.endIndex
)
selectorDtoList.add(selectorDto)
}
}

val targetDto = TargetDto(
id = annotationTarget.id,
format = annotationTarget.format,
language = annotationTarget.language,
selectors = selectorDtoList
)
targetDtoArrayList.add(targetDto)
}

val bodyDtoList = ArrayList<BodyDto>()
if(annotation.body != null){

if (annotation.body != null) {
for (body in annotation.body!!) {
val bodyDto = BodyDto(
type = body.type,
Expand All @@ -103,15 +94,53 @@ object DtoConverter {
}
}

return AnnotationDto(
motivation = annotation.motivation,
var targetDto = TargetDto()
val target = annotation.target
if (target != null) { // it wont be null

targetDto = TargetDto(
id = target.id,
format = target.format,
language = target.language
)
if (annotation.selector != null) {
val selector = annotation.selector
if (selector is FragmentSelector) {
val selectorDto = FragmentSelectorDto(
type = selector.type,
value = selector.value
)
targetDto.selector = selectorDto
} else if (selector is TextPositionSelector) {
val selectorDto = TextPositionSelectorDto(
type = selector.type,
start = selector.startIndex,
end = selector.endIndex
)
targetDto.selector = selectorDto
}
}
}
val motivationList = ArrayList<String>()
val motivations = annotation.motivation
if (motivations != null) {

for (motivation in motivations) {
motivationList.add(motivation.value)
}
}
val annotationDto = AnnotationDto(
motivation = motivationList,
creator = annotation.creator,
created = annotation.created,
id = annotation.id,
type = annotation.type,
context = annotation.context,
body = bodyDtoList,
target = targetDtoArrayList
target = targetDto
)

return annotationDto

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@ package com.group6.annotationservice.controller

import com.group6.annotationservice.Utils.DtoConverter
import com.group6.annotationservice.dtos.AnnotationDto
import com.group6.annotationservice.dtos.GetAnnotationsByTargetIdRequest
import com.group6.annotationservice.dtos.SelectorDto
import com.group6.annotationservice.service.AnnotationService
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import com.group6.annotationservice.entity.Annotation

@RestController
@RequestMapping("/annotation")
class AnnotationController(
private val annotationService: AnnotationService
) {
@PostMapping("parse-selector")
fun parseSelector(@RequestBody selector: SelectorDto): ResponseEntity<String> {

return ResponseEntity.ok("test")
}
@PostMapping("/create")
fun createAnnotation(@RequestBody annotationDto: AnnotationDto): ResponseEntity<AnnotationDto> {
val annotation = DtoConverter.convertAnnotationDtoToAnnotation(annotationDto)

val responseAnnotation = DtoConverter
.convertAnnotationToAnnotationDto(annotationService.createAnnotation(annotation))
.convertAnnotationToAnnotationDto(annotationService.createAnnotation(annotationDto))
return ResponseEntity(responseAnnotation, HttpStatus.CREATED)
}

Expand All @@ -30,9 +36,9 @@ class AnnotationController(
return ResponseEntity.ok(responseAnnotation)
}

@GetMapping("/get-annotations-by-target/{targetId}")
fun getAnnotationsByTarget(@PathVariable targetId: String): ResponseEntity<List<AnnotationDto>> {
val annotations = annotationService.getAnnotationsByTarget(targetId)
@PostMapping("/get-annotations-by-target")
fun getAnnotationsByTarget(@RequestBody request: GetAnnotationsByTargetIdRequest): ResponseEntity<List<AnnotationDto>> {
val annotations = annotationService.getAnnotationsByTarget(request.targetId)
val responseAnnotationList = annotations.map { DtoConverter.convertAnnotationToAnnotationDto(it) }
return ResponseEntity.ok(responseAnnotationList)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.group6.annotationservice.dtos

import com.group6.annotationservice.entity.Motivation
import java.time.LocalDateTime
import java.util.UUID
import java.util.*

data class AnnotationDto (
val context: String = "http://www.w3.org/ns/anno.jsonld",
val id: String = "" + UUID.randomUUID().toString(), //todo put our domain name here and generate a uuid
val type: String = "Annotation",
val motivation: List<Motivation>? = null,
val motivation: List<String>? = null,
val creator: String? = null,
val created: LocalDateTime? = LocalDateTime.now(), // ISO-8601 datetime format
val body: List<BodyDto>? = null,
val target: List<TargetDto>
val target: TargetDto = TargetDto()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.group6.annotationservice.dtos

import com.fasterxml.jackson.annotation.JsonTypeName

@JsonTypeName("FragmentSelector")
data class FragmentSelectorDto(
override val type: String = "FragmentSelector",
val value: String = "xywh=,,,", // e.g., "annotea is awesome"
) : SelectorDto()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.group6.annotationservice.dtos

data class GetAnnotationsByTargetIdRequest(
val targetId: String = ""
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.group6.annotationservice.dtos

import lombok.AllArgsConstructor
import lombok.Data
import lombok.NoArgsConstructor
import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo

@NoArgsConstructor
@AllArgsConstructor
@Data
data class SelectorDto (
val type: String = "TextPositionSelector",
val startIndex: Int, // Starting character position
val endIndex: Int // Ending character position
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "type",
visible = true
)
@JsonSubTypes(
JsonSubTypes.Type(value = TextPositionSelectorDto::class, name = "TextPositionSelector"),
JsonSubTypes.Type(value = FragmentSelectorDto::class, name = "FragmentSelector")
)
abstract class SelectorDto(
open val type: String = ""
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.group6.annotationservice.dtos

import com.group6.annotationservice.entity.Selector
import java.util.UUID
import java.util.*

class TargetDto (
val id: String = "" + UUID.randomUUID().toString(), //todo put our domain name here and generate a uuid
val format: String? = null,
val language: String? = null,
val selectors: List<SelectorDto>? = null
var selector: SelectorDto? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.group6.annotationservice.dtos

import com.fasterxml.jackson.annotation.JsonTypeName

@JsonTypeName("TextPositionSelector")
data class TextPositionSelectorDto(
override val type: String = "TextPositionSelector",
val start: Int = 0, // e.g., 0
val end: Int = 0, // e.g., 10

) : SelectorDto()
Loading

0 comments on commit 3d72b27

Please sign in to comment.