<commit_before>package net.nemerosa.ontrack.boot.ui

import net.nemerosa.ontrack.model.form.Form
import net.nemerosa.ontrack.model.form.Selection
import net.nemerosa.ontrack.model.security.SecurityService
import net.nemerosa.ontrack.model.structure.*
import net.nemerosa.ontrack.ui.controller.AbstractResourceController
import net.nemerosa.ontrack.ui.resource.Pagination
import net.nemerosa.ontrack.ui.resource.PaginationCountException
import net.nemerosa.ontrack.ui.resource.PaginationOffsetException
import net.nemerosa.ontrack.ui.resource.Resources
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.on

@RestController
@RequestMapping("/structure")
class ValidationRunController
@Autowired
constructor(
        private val structureService: StructureService,
        private val validationRunStatusService: ValidationRunStatusService,
        private val propertyService: PropertyService,
        private val securityService: SecurityService
) : AbstractResourceController() {

    @GetMapping("builds/{buildId}/validationRuns/view")
    fun getValidationStampRunViews(@PathVariable buildId: ID): Resources<ValidationStampRunView> {
        // Build
        val build = structureService.getBuild(buildId)
        // Gets the views
        val views = structureService.getValidationStampRunViewsForBuild(build)
        // Converts into a view
        val uri = uri(on(javaClass).getValidationStampRunViews(buildId))
        return Resources.of(
                views,
                uri
        ).forView(ValidationStampRunView::class.java)
    }

    @GetMapping("builds/{buildId}/validationRuns")
    fun getValidationRuns(@PathVariable buildId: ID): Resources<ValidationRun> {
        return Resources.of(
                structureService.getValidationRunsForBuild(buildId),
                uri(on(ValidationRunController::class.java).getValidationRuns(buildId))
        ).forView(Build::class.java)
    }

    @GetMapping("builds/{buildId}/validationRuns/create")
    fun newValidationRunForm(@PathVariable buildId: ID): Form {
        val build = structureService.getBuild(buildId)
        return Form.create()
                .with(
                        Selection.of("validationStampId")
                                .label("Validation stamp")
                                .items(structureService.getValidationStampListForBranch(build.branch.id))
                )
                .with(
                        Selection.of("validationRunStatusId")
                                .label("Status")
                                .items(validationRunStatusService.validationRunStatusRoots)
                )
                .description()
    }

    @PostMapping("builds/{buildId}/validationRuns/create")
    @ResponseStatus(HttpStatus.CREATED)
    fun newValidationRun(@PathVariable buildId: ID, @RequestBody validationRunRequest: ValidationRunRequest): ValidationRun {
        // Gets the build
        val build = structureService.getBuild(buildId)
        // Gets the validation stamp
        val validationStamp = getValidationStamp(
                build.branch,
                validationRunRequest.validationStampId,
                validationRunRequest.actualValidationStampName)
        // Gets the validation run status
        val validationRunStatusID = validationRunStatusService.getValidationRunStatus(validationRunRequest.validationRunStatusId)
        // Validation run to create
        var validationRun = ValidationRun.of(
                build,
                validationStamp,
                0,
                securityService.currentSignature,
                validationRunStatusID,
                validationRunRequest.description
        )
        // Creation
        validationRun = structureService.newValidationRun(validationRun)
        // Saves the properties
        for ((propertyTypeName, propertyData) in validationRunRequest.properties) {
            propertyService.editProperty(
                    validationRun,
                    propertyTypeName,
                    propertyData
            )
        }
        // OK
        return validationRun
    }

    protected fun getValidationStamp(branch: Branch, validationStampId: Int?, validationStampName: String?): ValidationStamp {
        return structureService.getOrCreateValidationStamp(branch, validationStampId, validationStampName)
    }

    @GetMapping("validationRuns/{validationRunId}")
    fun getValidationRun(@PathVariable validationRunId: ID): ValidationRun =
        structureService.getValidationRun(validationRunId)

    // Validation run status

    @GetMapping(value = "validationRuns/{validationRunId}/status/change")
    fun getValidationRunStatusChangeForm(@PathVariable validationRunId: ID): Form {
        val validationRun = structureService.getValidationRun(validationRunId)
        return Form.create()
                .with(
                        Selection.of("validationRunStatusId")
                                .label("Status")
                                .items(
                                        validationRunStatusService.getNextValidationRunStatusList(validationRun.lastStatus.statusID.id)
                                )
                )
                .description()
    }

    @PostMapping("validationRuns/{validationRunId}/status/change")<commit_msg>#176 Validation run creation form<commit_after>package net.nemerosa.ontrack.boot.ui

import net.nemerosa.ontrack.model.form.Form
import net.nemerosa.ontrack.model.form.Selection
import net.nemerosa.ontrack.model.form.ServiceConfigurator
import net.nemerosa.ontrack.model.security.SecurityService
import net.nemerosa.ontrack.model.structure.*
import net.nemerosa.ontrack.ui.controller.AbstractResourceController
import net.nemerosa.ontrack.ui.resource.Pagination
import net.nemerosa.ontrack.ui.resource.PaginationCountException
import net.nemerosa.ontrack.ui.resource.PaginationOffsetException
import net.nemerosa.ontrack.ui.resource.Resources
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.on

@RestController
@RequestMapping("/structure")
class ValidationRunController
@Autowired
constructor(
        private val structureService: StructureService,
        private val validationRunStatusService: ValidationRunStatusService,
        private val validationDataTypeService: ValidationDataTypeService,
        private val propertyService: PropertyService,
        private val securityService: SecurityService
) : AbstractResourceController() {

    @GetMapping("builds/{buildId}/validationRuns/view")
    fun getValidationStampRunViews(@PathVariable buildId: ID): Resources<ValidationStampRunView> {
        // Build
        val build = structureService.getBuild(buildId)
        // Gets the views
        val views = structureService.getValidationStampRunViewsForBuild(build)
        // Converts into a view
        val uri = uri(on(javaClass).getValidationStampRunViews(buildId))
        return Resources.of(
                views,
                uri
        ).forView(ValidationStampRunView::class.java)
    }

    @GetMapping("builds/{buildId}/validationRuns")
    fun getValidationRuns(@PathVariable buildId: ID): Resources<ValidationRun> {
        return Resources.of(
                structureService.getValidationRunsForBuild(buildId),
                uri(on(ValidationRunController::class.java).getValidationRuns(buildId))
        ).forView(Build::class.java)
    }

    @GetMapping("builds/{buildId}/validationRuns/create")
    fun newValidationRunForm(@PathVariable buildId: ID): Form {
        val build = structureService.getBuild(buildId)
        return Form.create()
                .with(
                        ServiceConfigurator.of("validationStampData")
                                .label("Validation stamp")
                                .sources(
                                        structureService.getValidationStampListForBranch(build.branch.id)
                                                .map {
                                                    ServiceConfigurationSource(
                                                            it.name,
                                                            it.name,
                                                            it.dataType?.let { dataType ->
                                                                validationDataTypeService
                                                                        .getValidationDataType<Any, Any>(dataType.id)
                                                                        ?.getForm(null)
                                                            } ?: Form.create()
                                                    )
                                                }
                                )
                )
                .with(
                        Selection.of("validationRunStatusId")
                                .label("Status")
                                .items(validationRunStatusService.validationRunStatusRoots)
                )
                .description()
    }

    @PostMapping("builds/{buildId}/validationRuns/create")
    @ResponseStatus(HttpStatus.CREATED)
    fun newValidationRun(@PathVariable buildId: ID, @RequestBody validationRunRequest: ValidationRunRequest): ValidationRun {
        // Gets the build
        val build = structureService.getBuild(buildId)
        // Gets the validation stamp
        val validationStamp = getValidationStamp(
                build.branch,
                validationRunRequest.validationStampId,
                validationRunRequest.actualValidationStampName)
        // Gets the validation run status
        val validationRunStatusID = validationRunStatusService.getValidationRunStatus(validationRunRequest.validationRunStatusId)
        // Validation run to create
        var validationRun = ValidationRun.of(
                build,
                validationStamp,
                0,
                securityService.currentSignature,
                validationRunStatusID,
                validationRunRequest.description
        )
        // Creation
        validationRun = structureService.newValidationRun(validationRun)
        // Saves the properties
        for ((propertyTypeName, propertyData) in validationRunRequest.properties) {
            propertyService.editProperty(
                    validationRun,
                    propertyTypeName,
                    propertyData
            )
        }
        // OK
        return validationRun
    }

    protected fun getValidationStamp(branch: Branch, validationStampId: Int?, validationStampName: String?): ValidationStamp {
        return structureService.getOrCreateValidationStamp(branch, validationStampId, validationStampName)
    }

    @GetMapping("validationRuns/{validationRunId}")
    fun getValidationRun(@PathVariable validationRunId: ID): ValidationRun =
            structureService.getValidationRun(validationRunId)

    // Validation run status

    @GetMapping(value = "validationRuns/{validationRunId}/status/change")
    fun getValidationRunStatusChangeForm(@PathVariable validationRunId: ID): Form {
        val validationRun = structureService.getValidationRun(validationRunId)
        return Form.create()
                .with(
                        Selection.of("validationRunStatusId")
                                .label("Status")
                                .items(
                                        validationRunStatusService.getNextValidationRunStatusList(validationRun.lastStatus.statusID.id)
                                )
                )
                .description()
    }

    @PostMapping("validationRuns/{validationRunId}/status/change")
{'fraction_non_alphanumeric': 0.07467371551296878, 'fraction_numerical': 0.00041301833801420785, 'mean_word_length': 2.090096988259316, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 27, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7663531', 'n_tokens_mistral': 2783, 'n_tokens_neox': 2695, 'n_words': 457}