All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cedarsoftware.ncube.rules.RulesController.groovy Maven / Gradle / Ivy

There is a newer version: 5.6.9
Show newest version
package com.cedarsoftware.ncube.rules

import com.cedarsoftware.ncube.ApplicationID
import com.cedarsoftware.ncube.NCube
import groovy.transform.CompileStatic
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

import static com.cedarsoftware.ncube.NCubeAppContext.getNcubeRuntime
import static com.cedarsoftware.ncube.NCubeConstants._OR_
import static org.springframework.http.HttpStatus.OK

@RestController
@CompileStatic
class RulesController
{
    private final RulesConfiguration rulesConfiguration

    RulesController(RulesConfiguration rulesConfiguration)
    {
        this.rulesConfiguration = rulesConfiguration
    }

    @GetMapping(path = ['ui/rulesInfo'])
    ResponseEntity getInfo()
    {
        Map info = rulesConfiguration.info
        return ResponseEntity.status(OK).body(info)
    }

    @GetMapping(path = ['ui/rules'])
    ResponseEntity getBusinessRules(@RequestParam String engine, @RequestParam String group)
    {
        Map rules = rulesConfiguration.getRulesEngine(engine).generateDocumentationForGroups([group])
        return ResponseEntity.status(OK).body(rules)
    }

    @PostMapping(path = ['ui/rulesByCategory'])
    ResponseEntity getRulesByCategories(@RequestBody Map categories)
    {
        String engine = categories['_engine']
        categories.remove('_engine')
        for (Object value : categories.values())
        {
            if (value instanceof List)
            {
                ((List) value).add(0, _OR_)
            }
        }
        Map rules = rulesConfiguration.getRulesEngine(engine).generateDocumentation(categories)
        return ResponseEntity.status(OK).body(rules)
    }

    @GetMapping(path = ['ui/ncube'])
    ResponseEntity getNcubeHtml(@RequestParam String name, @RequestParam String appIdString)
    {
        ApplicationID applicationId = ApplicationID.convert(appIdString)
        NCube ncube = ncubeRuntime.getCube(applicationId, name)
        if (!ncube)
        {
            String error = "NCube: ${name} not found in app: ${applicationId}"
            return ResponseEntity.status(OK).body([html: error] as Map)
        }

        String html = ncube.toHtml('trait', 'traits', 'businessDivisionCode', 'bu', 'month', 'months', 'col', 'column', 'cols', 'columns', 'attribute', 'attributes')
        return ResponseEntity.status(OK).body([html: html] as Map)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy