Announcement Announcement Module
Collapse
No announcement yet.
Using a collection of simple variables as a domain property Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using a collection of simple variables as a domain property

    I am some what new to Grails but I have been working on this issue for over a week and I have gotten no where: I am trying to store one array of integers and two arrays of BigDecimals into the property of my domain. The domain will be populated once then the data will only be returned in the form of a JSON.

    Here is my code:
    domain:
    class HeatMap {

    BigDecimal measure
    def idKey
    def latPath
    def lngPath

    HeatMap() {}

    HeatMap(idKey, latPath, lngPath) {
    this.idKey = idKey
    this.latPath = latPath
    this.lngPath = lngPath
    }

    public calculateMeasure(results) {
    def value = 0
    results.each { current ->
    value += current
    }
    this.measure = value
    }

    static constraints = {
    measure(nullable: true)
    idKey(blank: false)
    latPath(blank: false)
    lngPath(blank: false)
    }
    }
    controller:
    def getHeatMap = {
    if(!HeatMap.count()) {
    heatMapService.main(Company.list(), 48280.32)
    HeatMap.list().each { h ->
    //println "idKey - "+h.idKey
    def value = Company.getAll(h.idKey).collect {
    //println "OPAT - "+it.OPAT
    if(it.OPAT == null) {
    return 0
    }
    else {
    return it.OPAT
    }
    }
    h.calculateMeasure(value)
    //println "measure - "+h.measure
    }
    }
    def maxMeasure = 0
    //println HeatMap.getAt(1).idKey
    def paths = HeatMap.list().collect { h ->
    if(h.measure > maxMeasure) {
    maxMeasure = h.measure
    }
    //println h.idKey
    return [
    id: h.id,
    idKey: h.idKey,
    measure: h.measure,
    latPath: h.latPath,
    lngPath: h.lngPath,
    ]
    }
    def results = ["maxMeasure":maxMeasure, "paths"aths]
    render results as JSON
    }
    service:
    public createRegion(ID, center) {
    def latPath = []
    def lngPath = []
    for(def i=0; i<numOfSides; i++) {
    def point = center.travelFor((radius / ID.size()), ((360/numOfSides) * (i+1)))
    latPath.push(point.lat)
    lngPath.push(point.lng)
    }
    def trueID = ID.collect { objectList[it as int].id }

    new HeatMap(trueID, latPath, lngPath).save(flush:true, failOnError: true)
    }
    What Happens:
    the first time my controller is called and the domain is populated I get the correct information back, which is this:
    {"maxMeasure":1318439,"paths":[{"id":1,"idKey":[1],"measure":149649,"latPath":[41.50808513097997, 41.507...],"lngPath":[-74.03234239041699, -74.02...]},{"id":2,...},{...},...]}
    The Problem:
    my problem is when I refresh the page(or any time the controller is called from then on) I get back null values:
    {"maxMeasure":1318439,"paths":[{"id":1, "idKey":null, "measure":149649, "latPath":null, "lngPath":null},{"id":2, "idKey":null, "measure":174382, "latPath":null, "lngPath":null}, ...]}
    I don't understand why this is happen when I refresh the page. Can anyone Help?

  • #2
    You're being way too groovy - avoid overusing 'def' like this. You need to type the variables, otherwise Hibernate doesn't know how to persist them. It doesn't figure out what the type is at runtime - it configures everything at startup. Anything declared with 'def' in a domain class is ignored. You can set the values, but they won't be persisted.

    Here's an updated class:

    Code:
    class HeatMap {
       BigDecimal measure
       String idKey
       String latPath
       String lngPath
    
       void calculateMeasure(results) {
          int sum = 0
          for (result in results) {
             sum += current
          }
          measure = sum
       }
    
       static constraints = {
          measure(nullable: true)
          idKey(blank: false)
          latPath(blank: false)
          lngPath(blank: false)
       }
    }
    You're specifying blank: false, so I assumed the types should be Strings, but given your sample output it looks like they're arrays of numbers. If so, you need to configure that. See the user manual for how to do this - it's section "5.2.1.4 Basic Collection Types" at http://grails.org/doc/latest/

    Also - I removed the constructors. You can use them (as long as you leave in the default constructor like you did) but in general people don't. Use the Map constructor instead, e.g. "new HeatMap(idKey: foo, latPath: bar, lngPath: baz)"

    Comment


    • #3
      First, Thank you for your response burtbeckwith. Second thank you very much I thought I had tried this approach but I guess I didn't because now it works. I ended up going with:
      class HeatMap {

      BigDecimal measure
      def idKey
      static hasMany = [latPathouble, lngPathouble]

      public calculateMeasure(results) {
      def value = 0
      results.each { current ->
      value += current
      }
      this.measure = value
      }

      static constraints = {
      measure(nullable: true)
      idKey(blank: false)
      latPath(blank: false)
      lngPath(blank: false)
      }
      }
      I know the idKey doesn't persist but I really I don't need it to as long a measure persists(which it does). I couldn't use the String variable types because I meed them to be a collection of numbers and not a String when passed with the JSON. Thanks for your help!

      Comment

      Working...
      X