tags:

views:

276

answers:

2

I'm considering using Grails for my current project. I have a couple of requirements that I'm hoping I can do in Grails.

First, I have the following database table:

TagType
---------
tag_type_id
tag_type


Sample Data: TagType
--------------------
1,title
2,author

Based on that data, I need to generate a data entry form like this which will save its data to another table.

Tile _ _ _ _ _ _ _ _ _ _ _
Author _ _ _ _ _ _ _ _ _ _ _

save cancel

Can I do that in Grails? Can you point me in the right direction?

Thanks!

More Details

I'm building a digital library system that supports OIA-PMH which is a standard for sharing metadata about documents. The standard states that every element is optional and repeatable. To support this requirement I have the following database design.

I need to generate the user GUI (data entry form) based primarily on the contents of the TagType Table (see above). The data from the form then get's saved to the Tags (if the tag is new) and Item_Tags tables.

Items
---------
item_id
last_update

Tags
--------
tag_id
tag_type_id
tag

TagType
---------
tag_type_id
tag_type

Item_tags
---------
item_id
tag_id

Sample Data: Items
------------------
1,2009-06-15

Sample Data: TagType
--------------------
1,title
2,author

Sample Data: Tags
------------------

1,1,The Definitive Guide to Grails
2,2,Graeme Rocher
3,2, Jeff Brown

Sample Data: Item_tags
-----------------------
1,1
1,2
1,3
A: 

I am not completely sure what you are asking here in regards to "save its data to another table", but here are some thoughts.

For the table you have, the domain class you'd need is the following:

class Tag { String type

}

ID field will get generated for you automatically when you create the scaffolding.

Please add more information to your question if this is insufficient.

Jean Barmash
Thanks. I added more details.
Brad Rhoads
A: 

I'm really liking grails. When I first started playing with it a couple of weeks ago, I didn't realize that it's a full fledged language. In fact it's more than that. It's a complete web stack with a web server and a database included. Anyway, the short answer to my question is yes. You might even say yes, of course! Here's the code for the taglib I created:

import org.maflt.flashlit.pojo.Item
import org.maflt.flashlit.pojo.ItemTag
import org.maflt.flashlit.pojo.Metacollection
import org.maflt.flashlit.pojo.SetTagtype
import org.maflt.flashlit.pojo.Tag
import org.maflt.flashlit.pojo.Tagtype

/**
* @return Input form fields for all fields in the given Collection's Metadataset. Does not return ItemTags where TagType is not in the Metadataset.
*  
* In Hibernate, the
*
*    "from ItemTag b, Tag a where b.tag= a"
*
* query is a cross-join. The result of this query is a list of Object arrays where the first item is an ItemTag instance and the second is a Tag instance.
*
* You have to use e.g.
*
*    (ItemTag) theTags2[0][0]
*
* to access the first ItemTag instance.
* (http://stackoverflow.com/questions/1093918/findall-not-returning-correct-object-type)
**/
class AutoFormTagLib {

    def autoForm = {attrs, body ->
     //def masterList  = Class.forName(params.attrs.masterClass,false,Thread.currentThread().contextClassLoader).get(params.attrs.masterId)
     def theItem  = Item.get(attrs.itemId)
     def theCollection = Metacollection.get(attrs.collectionId)
     def masterList  = theCollection.metadataSet.setTagtypes
     def theParams = null
     def theTags  = null
     def itemTag  = null
     def tag   = null
     masterList.each {

      theParams  = [attrs.itemId.toLong(),it.tagtype.id]
      theTags  = ItemTag.findAll("from ItemTag d, Item c, Tag b, Tagtype a where d.item = c and d.tag = b and b.tagtype = a and c.id=? and a.id=?",theParams)

      for (int i=0;i<it.maxEntries;i++) {       
       out << "<tr>\n"
       out << "    <td>${it.tagtype.tagtype}</td>\n"
       out << "    <td>\n"
       if (theTags[i]) {
        itemTag  = (ItemTag) theTags[i][0]
        //item  = (Item)  theTags[i][1]
        tag  = (Tag) theTags[i][2] 
        //itemTag  = (Tagtype) theTags[i][3]
        out << "    <input name='${it.tagtype.tagtype}_${i}_${itemTag.id}' value='${tag.tag}' />\n";
       }
       else
        out << "       <input name='${it.tagtype.tagtype}_${i}' />\n";
       out << "    </td>\n"
       out << "</tr>\n"
      }
     }

    }
}
Brad Rhoads