tags:

views:

57

answers:

2

I'm doing a website for reading stories. My goal is to do save the content of the story into several pages to get a list and then paginate it easily; I did the following:

In the domain I created two domains, Story:

class Story {

    String title

    List pages

    static hasMany=[users:User,pages:Page]
    static belongsTo = [User]

    static mapping={
        users lazy:false
        pages lazy:false

    }
}

And Page:

class Page {
    String Content
    Story story
    static belongsTo = Story
    static constraints = {
        content(blank:false,size:3..300000)
    }

}

The controller save action is:

def save = {
    def storyInstance = new Story(params)
    def pages = new Page(params)
    String content = pages.content
    String[] contentArr = content.split("\r\n")

    int i=0

    StringBuilder page = new StringBuilder()
    for(StringBuilder line:contentArr){
        i++
        page.append(line+"\r\n")

        if(i%10==0){

               pages.content = page
               storyInstance.addToPages(pages)
               page =new StringBuilder()
        }
    }

    if (storyInstance.save(flush:true)) {
        flash.message = "${message(code: 'default.created.message', args: [message(code: 'story.label', default: 'Story'), storyInstance.id])}"
        redirect(action: "viewstory", id: storyInstance.id)
    }else {
        render(view: "create", model: [storyInstance: storyInstance])
    }
}

(I know it looks messy but it's a prototype)

The problem is that I'm waiting for storyInstance.addToPages(pages) to add to the set of pages an instance of the pages every time the condition is true. But what actually happens that it gives me the last instance only with the last page_idx. I thought it would save the pages one by one so I could get a list of pages to every story.

Why does this happen and is there a simpler way to do it than what i did?

Any help is appreciated.

+2  A: 

You are working with only one page... Correct solution:

     def save = {
        def storyInstance = new Story(params)
        def i = 0

        StringBuilder page = new StringBuilder()
        for(StringBuilder line in params?.content?.split("\r\n")){
            i++
            page.append(line+"\r\n")

            if(i%10 == 0){
                   storyInstance.addToPages(new Page(content: page.toString()))
                   page = new StringBuilder()
            }
        }

    if (storyInstance.save(flush:true)) {
        flash.message = "${message(code: 'default.created.message', args: [message(code: 'story.label', default: 'Story'), storyInstance.id])}"
        redirect(action: "viewstory", id: storyInstance.id)
    }else {
        render(view: "create", model: [storyInstance: storyInstance])
    }
}
Olexandr
thank you sir i'll try it ASAP and will tell you the result .. can't tell you how i feel :)
Mohamed Emad Hegab
it's worked sir.. but there's some kind of a problem i can understand.. it's got saved unordered while i've debug it and seen the hashset was is not in order.. so what is the wrong there should i use sorted set.. and how?
Mohamed Emad Hegab
use default sort order mapping - look at this http://www.grails.org/doc/latest/ref/Database%20Mapping/sort.html
Olexandr
yes i kind of tried that..i've set createdDate on page domain with new date each time and sorted from the story domain by it but nothing happened
Mohamed Emad Hegab
try to debug HQL queries. set hibernate log to true and look for query which select pages from DB
Olexandr
could you contact me over skype: rivne2 or email: [email protected] ?
Olexandr
I added you :) thanks so much for the good contribution
Mohamed Emad Hegab