开发者

Grails constraints GORM-JPA always sorting alphabeticaly

开发者 https://www.devze.com 2023-02-10 19:15 出处:网络
In my grails app, in which I use GORM-JPA, I cannot define the order of the elements of the class using the constraints. If I autogenerate the views, they are all sorted alphabetically, instead of the

In my grails app, in which I use GORM-JPA, I cannot define the order of the elements of the class using the constraints. If I autogenerate the views, they are all sorted alphabetically, instead of the defined order. Here's my source class:

package kbdw



import javax.persistence.*;
// import com.google.appengine.api.datastore.Key;

@Entity
class Organisatie implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id

    @Basic
    String naam

    @Basic
    String telefoonnummer

    @Basic
    String email

    @Basic
    OrganisatieType type

    @Basic
    String adresLijnEen

    @Basic
    String adresLijnTwee

    @Basic
    String gemeente

    @Basic
    String postcode

    @Basic
    String faxnummer



    static constraints = {
        id visible:false
        naam size: 3..75
        telefoonnummer size: 4..18
        email email:true
        type blank:false
        adresLijnEen size:5..250
        adresLijnTwee blank:true
        gemeente size: 2..100
        postcode size: 4..10
        faxnummer size: 4..18
    }
}

enum OrganisatieType {
    School,
    NonProfit,
    Bedrijf
}

The variable names are in Dutch, but it should be clear (Organi开发者_运维百科satie = organisation, naam = name, adres = address, ...).

How do I force the app to use that order of properties? Do I need to use @ annotations?

Thank you!

Yvan (ps: it's for deploying on the Google App Engine ;-) )


Try installing and hacking scaffolding, and use DomainClassPropertyComparator in your gsp-s. Scaffold templates do a Collections.sort() on default comparator, but you can use explicit one.

The absence of Hibernate might be the cause: without it, DomainClassPropertyComparator won't work, and Grails uses SimpleDomainClassPropertyComparator - I'm looking at DefaultGrailsTemplateGenerator.groovy

You can, for sure, provide another Comparator that will compare the order of declared fields.

EDIT:

For example, after installing scaffolding I have a file <project root>\src\templates\scaffolding\edit.gsp. Inside, there are such lines:

props = domainClass.properties.findAll{ ... }
Collections.sort(props, comparator. ... )

where comparator is variable provided by Grails scaffolding. You can do:

props = ...
Collections.sort(props, new PropComparator(domainClass.clazz}))

where PropComparator is something like

class PropComparator implements Comparator {
    private Class clazz
    PropComparator(Class clazz) { this.clazz = clazz }

    int compare(Object o1, Object o2) {
        clazz.declaredFields.findIndexOf{it.name == o1} 
          - clazz.declaredFields.findIndexOf{it.name == o2}
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消