-6

I am building a web application with Grails and need to model a complex organisational structure. i.e.

  • Organisation
  • Department
  • Office
  • People

And retrieve a list of people at each level of the heirarchy. The problem is that Grails does not seem to have good support for polymorphism (no interfaces, etc) and this is how I would normally go about modelling a structure like this.

How would you model a structure like this in Grails? Do you know of any good examples?

EDIT:

Regarding interfaces JIRA it's not that it doesn't its that from what I read GORM doesn't support them properly yet. And the Reference Guide only mentions abstract classes when related to GORM.

What I am looking for is something like Martin Fowlers Organisational Patterns. I need a way to GetPeople from any level in the heirarchy without having to explicitly traverse the structure and as far as I can see GORM doesn't support this very well.

PS Please prove me wrong I would be very happy.

eaglestorm
  • 1,192
  • 1
  • 13
  • 32
  • Actually, Grails does allow you to use interfaces and inheritance in domain classes. What other kinds of support are you referring to? And what common interfaces do you wish to have in these classes? I don't see one - besides of that each of them "has an Address". – Victor Sergienko Mar 02 '11 at 10:56
  • Do you actually have any experience with using GORM with abstract base classes and interfaces because from what I find on google and from trying to implement them personally GORM doesn't handle them properly. And from reading between the lines of the documentation I would be suprised to find the developers considered interfaces and abstract classes important. – eaglestorm Mar 04 '11 at 07:40
  • I do have certain Grails experience, and though it wasn't me who downvoted you, I can guess. First, you're not specific enough in your question - 1) it took you that long to mention "Organization Structures" and 2) a specific task ("GetPeople from any level in the heirarchy"), and still 3) you didn't describe the relations of your classes. People DO take time to describe things like that if they want a deep answer. A better question would be "I have such a class model (description or a diagram) and I need to do queries/updates like these: (samples)". – Victor Sergienko Apr 17 '13 at 12:27

3 Answers3

2

A people can have null or many offices but normally one office. A office is in one department and the department is in one organisation. In the simplest case each sub level has a attribute of the type from the next level and the next level has a list of Objects from the sub level.

class People{
   Office office 
}

class Office{
    static hasMany = [employee:People]
    Department department
}

class Department{
    static hasMany = [offices:Office]
    Organisation organisation
}

class Organisation{
    static hasMany = [departments:Department] 
}

If you like to know in which organisation is a people you can do this:

def people = ...
def organisation = people.office.department.organisation
Medrod
  • 986
  • 7
  • 17
  • this doesn't really cover what I am looking for as any standard organisational structure will need some kind of interface or abstract base class see edit. – eaglestorm Mar 04 '11 at 07:37
0

As to the abstract classes, I've actually used them in the domain in GORM. Abstract classes should lie outside of the whole domain folder. Also note that because of that, GORM doesn't perform any of its magic on them. Therefore if you supply fields like "static constaints = ..." or "static mapping = ..." they might be taken into consideration only as long as the derived class doesn't shadow them. One more thing worth noting: base classes outside of the domain package SHOULD be abstract. If they aren't, GORM complains about it.

As to interfaces, I don't really see much of the problem. You can implement as many interfaces as you wish on your domain classes. You cannot declare fields that use interfaces as their types, not unless you give a hint about their actual type (not entirely sure this is possible). This is a limitation of an underlying Hibernate stack, and is a direct effect of how it handles inheritance (i.e. table per hierarchy).

However bare in mind that dynamic languages such as Groovy REALLY make interfaces much less useful. If you supply a method with a common name to all of your domain classes, you will be able to write something like organisationStructureItem.findPeople() and the compiler won't complain about it. This wouldn't be the case in a strongly typed language, like Java.

julx
  • 8,694
  • 6
  • 47
  • 86
  • I'm a big fan of software engineering tools that just work and I'm sorry to say that this is one area where grails doesn't. And just becausee you can doesn't mean you should in regard to using the magic of dynamic languages. – eaglestorm Mar 10 '11 at 10:30
0

I think the Grails 2 support for abstract inheritance and polymorphic querying meets your need ... see section 5.2.3 of http://grails.org/doc/2.0.0.M1/guide/GORM.html#inheritanceInGORM

Wayne
  • 1,075
  • 1
  • 10
  • 21