Skip to content

Commit

Permalink
https://github.com/DASISH/qddt-client/issues/770
Browse files Browse the repository at this point in the history
fixed  e
  • Loading branch information
StigNorland committed Feb 8, 2022
1 parent 349a262 commit ee97d96
Show file tree
Hide file tree
Showing 37 changed files with 732 additions and 340 deletions.
66 changes: 43 additions & 23 deletions src/main/kotlin/no/nsd/qddt/controller/AbstractRestController.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package no.nsd.qddt.controller

import no.nsd.qddt.config.RevisionEntityImpl
import no.nsd.qddt.model.builder.xml.XmlDDIFragmentAssembler
import no.nsd.qddt.model.classes.AbstractEntityAudit
import no.nsd.qddt.model.classes.UriId
import no.nsd.qddt.model.interfaces.IBasedOn
import no.nsd.qddt.model.interfaces.RepLoaderService
import no.nsd.qddt.repository.BaseMixedRepository
import org.hibernate.Hibernate
Expand Down Expand Up @@ -54,17 +56,39 @@ abstract class AbstractRestController<T : AbstractEntityAudit>(val repository: B
}
}

@ResponseBody
open fun getRevisions(@PathVariable uri: UUID, pageable: Pageable): RepresentationModel<*> {


open fun getRevisions(@PathVariable uuid: UUID, pageable: Pageable, ofClass: Class<T>): PagedModel<RepresentationModel<EntityModel<T>>> {
val qPage: Pageable = if (pageable.sort.isUnsorted) {
PageRequest.of(pageable.pageNumber, pageable.pageSize, Sort.Direction.DESC, "modified")
} else {
pageable
}

logger.debug("getRevisions PagedModel: {}", qPage)
val revisions = repository.findRevisions(uri, qPage).map { rev -> entityRevisionModelBuilder(rev) }
return PagedModel.of(revisions.content, pageMetadataBuilder(revisions))
val auditReader = AuditReaderFactory.get(entityManager)

val revQuery: AuditQuery = auditReader.createQuery().forRevisionsOfEntity(ofClass, false)
.add(AuditEntity.property("changeKind").ne(IBasedOn.ChangeKind.IN_DEVELOPMENT))
.add(AuditEntity.property("id").eq(uuid))



val result = revQuery.resultList
.filterIndexed { index, _ -> index > qPage.offset && index < qPage.offset + qPage.pageSize }
.map {
val rev = auditReader.createQuery().forEntitiesAtRevision(ofClass, (it as RevisionEntityImpl).id ).singleResult.apply {
(this as T).version.rev = (it as RevisionEntityImpl).id
}
entityModelBuilder(rev as T)
}

return PagedModel.of(result, PagedModel.PageMetadata(result.size.toLong(), 1L, result.size.toLong()))



// logger.debug("getRevisions PagedModel: {}", qPage)
// val revisions = repository.findRevisions(uri, qPage).map { rev -> entityRevisionModelBuilder(rev) }
// return PagedModel.of(revisions.content, pageMetadataBuilder(revisions))
}

@ResponseBody
Expand Down Expand Up @@ -124,24 +148,6 @@ abstract class AbstractRestController<T : AbstractEntityAudit>(val repository: B
return getByUri(UriId.fromAny(uri))
}

protected fun <T : AbstractEntityAudit> loadRevisionEntity(
uri: UriId,
repository: RevisionRepository<T, UUID, Int>
): T {
return with(uri) {
if (rev != null)
repository.findRevision(id, rev!!).map {
it.entity.version.rev = it.revisionNumber.get()
it.entity
}.get()
else
repository.findLastChangeRevision(id).map {
it.entity.version.rev = it.revisionNumber.get()
it.entity
}.get()
}
}

protected fun pageMetadataBuilder(revisions: Page<RepresentationModel<EntityModel<T>>>): PagedModel.PageMetadata {
return PagedModel.PageMetadata(
revisions.size.toLong(),
Expand Down Expand Up @@ -171,6 +177,20 @@ abstract class AbstractRestController<T : AbstractEntityAudit>(val repository: B

companion object {
val logger: Logger = LoggerFactory.getLogger(this::class.java)
fun <T : AbstractEntityAudit> loadRevisionEntity(uri: UriId,repository: RevisionRepository<T, UUID, Int>): T {
return with(uri) {
if (rev != null)
repository.findRevision(id, rev!!).map {
it.entity.version.rev = it.revisionNumber.get()
it.entity
}.get()
else
repository.findLastChangeRevision(id).map {
it.entity.version.rev = it.revisionNumber.get()
it.entity
}.get()
}
}
}


Expand Down
108 changes: 108 additions & 0 deletions src/main/kotlin/no/nsd/qddt/controller/CategoryController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package no.nsd.qddt.controller

import no.nsd.qddt.model.Category
import no.nsd.qddt.model.classes.UriId
import no.nsd.qddt.repository.CategoryRepository
import org.hibernate.Hibernate
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Pageable
import org.springframework.data.rest.webmvc.BasePathAwareController
import org.springframework.hateoas.EntityModel
import org.springframework.hateoas.Link
import org.springframework.hateoas.LinkRelation
import org.springframework.hateoas.RepresentationModel
import org.springframework.hateoas.mediatype.hal.HalModelBuilder
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import java.util.*


@BasePathAwareController
class CategoryController(@Autowired repository: CategoryRepository) : AbstractRestController<Category>(repository) {

@Transactional(propagation = Propagation.REQUIRED)
@GetMapping("/category/revision/{uri}", produces = ["application/hal+json"])
override fun getRevision(@PathVariable uri: String): RepresentationModel<*> {
return super.getRevision(uri)
}

@Transactional(propagation = Propagation.REQUIRED)
@GetMapping("/category/revisions/{uri}", produces = ["application/hal+json"])
fun getRevisions(
@PathVariable uri: UUID,
pageable: Pageable
): RepresentationModel<*> {
return super.getRevisions(uri, pageable,Category::class.java)
}

@Transactional(propagation = Propagation.REQUIRED)
@GetMapping("/category/revisions/byparent/{uri}", produces = ["application/hal+json"])
fun getStudies(@PathVariable uri: String, pageable: Pageable): RepresentationModel<*> {
logger.debug("get category by parent rev...")
return super.getRevisionsByParent(uri, Category::class.java, pageable)
}

@Transactional(propagation = Propagation.REQUIRED)
@GetMapping("/category/{uri}", produces = [MediaType.APPLICATION_PDF_VALUE])
override fun getPdf(@PathVariable uri: String): ByteArray {
return super.getPdf(uri)
}

@Transactional(propagation = Propagation.REQUIRED)
@GetMapping("/category/{uri}", produces = [MediaType.APPLICATION_XML_VALUE])
override fun getXml(@PathVariable uri: String): String {
return super.getXml(uri)
}


@Transactional(propagation = Propagation.NESTED)
@PutMapping("/category/{uri}/children", produces = ["application/hal+json"])
fun putChildren(
@PathVariable uri: UUID,
@RequestBody category: Category
): ResponseEntity<RepresentationModel<EntityModel<Category>>> {
logger.debug("put category CategoryController...")

var parent = repository.findById(uri).orElseThrow()
val entity = repository.findById(category.id!!).orElseThrow()
if (category.code!= null) {
entity.code = category.code
}
parent.addChildren(entity)
parent = repository.saveAndFlush(parent)

return ResponseEntity.ok(entityModelBuilder(parent.children.last()))
}

override fun entityModelBuilder(entity: Category): RepresentationModel<EntityModel<Category>> {
val uriId = UriId.fromAny("${entity.id}:${entity.version.rev}")
logger.debug("entityModelBuilder Category : {}", uriId)
val baseUrl = if (uriId.rev != null)
"${baseUri}/category/revision/${uriId}"
else
"${baseUri}/category/${uriId.id}"

entity.comments.size
entity.children.size
Hibernate.initialize(entity.agency)
Hibernate.initialize(entity.modifiedBy)
return HalModelBuilder.halModel()
.entity(entity)
.link(Link.of(baseUrl))
// .link(Link.of("${baseUri}/category/topics/${uriId}","topics"))

.embed(entity.agency, LinkRelation.of("agency"))
.embed(entity.modifiedBy, LinkRelation.of("modifiedBy"))
.embed(entity.comments, LinkRelation.of("comments"))
.embed(entity.children.map {
entityModelBuilder(it)
}, LinkRelation.of("children"))
.build()
}
}
12 changes: 9 additions & 3 deletions src/main/kotlin/no/nsd/qddt/controller/ConceptController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ class ConceptController(@Autowired repository: ConceptRepository) : AbstractRest

// @Transactional(propagation = Propagation.REQUIRED)
@GetMapping("/concept/revisions/{uri}", produces = ["application/hal+json"])
override fun getRevisions(@PathVariable uri: UUID, pageable: Pageable): RepresentationModel<*> {
return super.getRevisions(uri, pageable)
fun getRevisions(
@PathVariable uri: UUID,
pageable: Pageable
): RepresentationModel<*> {
return super.getRevisions(uri, pageable,Concept::class.java)
}

// @Transactional(propagation = Propagation.REQUIRED)
Expand Down Expand Up @@ -86,7 +89,7 @@ class ConceptController(@Autowired repository: ConceptRepository) : AbstractRest
parent.addQuestionItem(questionItem)

val result = repository.saveAndFlush(parent).questionItems.map {
it.element = loadRevisionEntity(it.getUri(), qRepository)
it.element = Companion.loadRevisionEntity(it.getUri(), qRepository)
it
}.toMutableList()

Expand Down Expand Up @@ -150,6 +153,9 @@ class ConceptController(@Autowired repository: ConceptRepository) : AbstractRest
"${baseUri}/concept/${uriId.id}"

entity.children.size
if (entity.children== null) {
logger.error("wrf null children")
}
entity.authors.size
entity.comments.size
entity.questionItems.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import no.nsd.qddt.model.interfaces.IBasedOn
import no.nsd.qddt.repository.ControlConstructRepository
import no.nsd.qddt.service.OtherMaterialService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.rest.webmvc.BasePathAwareController
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
Expand All @@ -27,29 +28,34 @@ class ControlConstructController(@Autowired repository: ControlConstructReposito


@ResponseBody
@Modifying
@PostMapping(value = ["/controlconstruct/condition"])
fun update(@RequestBody instance: ConditionConstruct): ConditionConstruct {
return repository.save(instance)
return repository.save<ConditionConstruct>(instance)
}

@ResponseBody
@Modifying
@PostMapping(value = ["/controlconstruct/question"])
fun update(@RequestBody instance: QuestionConstruct): QuestionConstruct {
return repository.save(instance)
}

@ResponseBody
@Modifying
@PostMapping(value = ["/controlconstruct/sequence"])
fun update(@RequestBody instance: Sequence): Sequence {
return repository.save(instance)
}

@ResponseBody
@Modifying
@PostMapping(value = ["/controlconstruct/statement"])
fun update(@RequestBody instance: StatementItem): StatementItem {
return repository.save(instance)
}

@Modifying
@PostMapping(value = ["/controlconstruct/createfile"], headers = ["content-type=multipart/form-data"])
@Throws(FileUploadException::class, IOException::class)
fun createWithFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ class PublicationController(@Autowired repository: PublicationRepository) :
}

@GetMapping("/publication/revisions/{uri}", produces = ["application/hal+json"])
override fun getRevisions(@PathVariable uri: UUID, pageable: Pageable): RepresentationModel<*> {
return super.getRevisions(uri, pageable)
fun getRevisions(
@PathVariable uri: UUID,
pageable: Pageable
): RepresentationModel<*> {
return super.getRevisions(uri, pageable,Publication::class.java)
}


Expand Down
28 changes: 17 additions & 11 deletions src/main/kotlin/no/nsd/qddt/controller/QuestionItemController.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.nsd.qddt.controller

import no.nsd.qddt.model.QuestionItem
import no.nsd.qddt.model.ResponseDomain
import no.nsd.qddt.model.classes.UriId
import no.nsd.qddt.repository.QuestionItemRepository
import org.hibernate.Hibernate
Expand All @@ -26,24 +27,29 @@ class QuestionItemController(@Autowired repository: QuestionItemRepository) :


@Transactional(propagation = Propagation.REQUIRED)
@GetMapping("/questionitem/revision/{uri}", produces = ["application/hal+json"])
@GetMapping("/questionitem/revision/{uri}", produces = ["application/hal+json;charset=UTF-8"])
override fun getRevision(@PathVariable uri: String): RepresentationModel<*> {
return super.getRevision(uri)
}

@Transactional(propagation = Propagation.REQUIRED)
@GetMapping("/questionitem/revisions/{uri}", produces = ["application/hal+json"])
override fun getRevisions(@PathVariable uri: UUID, pageable: Pageable): RepresentationModel<*> {
return super.getRevisions(uri, pageable)
}

@Transactional(propagation = Propagation.REQUIRED)
@GetMapping("/questionitem/revisions/{uri}", produces = ["application/hal+json;charset=UTF-8"])
@ResponseBody
@GetMapping("/questionitem/{uri}", produces = ["application/hal+json"])
fun getById(@PathVariable uri: String): RepresentationModel<*> {
return entityModelBuilder(super.getByUri(uri))
fun getRevisions(
@PathVariable uri: UUID,
pageable: Pageable
): RepresentationModel<*> {
return super.getRevisions(uri, pageable, QuestionItem::class.java)
}


// @Transactional(propagation = Propagation.REQUIRED)
// @ResponseBody
// @GetMapping("/questionitem/{uri}", produces = ["application/hal+json;charset=UTF-8"])
// fun getById(@PathVariable uri: UUID): RepresentationModel<*> {
// return entityModelBuilder(repository.getById(uri))
// }

@GetMapping("/questionitem/{uri}", produces = [MediaType.APPLICATION_PDF_VALUE])
override fun getPdf(@PathVariable uri: String): ByteArray {
return super.getPdf(uri)
Expand All @@ -70,7 +76,7 @@ class QuestionItemController(@Autowired repository: QuestionItemRepository) :
.link(Link.of(baseUrl))
.embed(entity.agency, LinkRelation.of("agency"))
.embed(entity.modifiedBy, LinkRelation.of("modifiedBy"))
// .embed(entity.responseDomainListe!!,LinkRelation.of("responseDomain") )
// .embed(entity.responseDomain!!,LinkRelation.of("responseDomain") )
.build()
}

Expand Down
Loading

0 comments on commit ee97d96

Please sign in to comment.