}
public String update()
{
// get the current book
Book book = controller.getBook(getBookId());
// update the book setters based on new form data
book.setAuthor(author);
// split incoming data up based on commas
String genreData[] = StringUtils.split(genre, ",");
if(genreData.length >0)
{
// delete associations - a bit of a nasty way to do things...
for(BookGenre bookGenre: book.getBookGenre())
{
controller.deleteBookGenre(bookGenre.getId());
}
}
List <BookGenre> mapping = new LinkedList <BookGenre>();
final LinkedList <Genre> genres = controller.listGenres(); // get a list of genres already in the database
for(String data: genreData)
{
if(StringUtils.isNotBlank(StringUtils.trimToEmpty(data)))
{
Genre newGenre = new Genre();
newGenre.setGenre(data);
if (!genres.contains(newGenre)) // is this a new genre?
{
controller.addGenre(newGenre);
}
else
{
int idx = genres.indexOf(newGenre);
newGenre = genres.get(idx);
}
BookGenre bookGenre = new BookGenre(); // setup the associative entity
bookGenre.setGenre(newGenre);
bookGenre.setBook(book);
mapping.add(bookGenre);
}
}
book.setBookGenre(mapping);
// save mapping between book and genre
for(BookGenre bookGenre: book.getBookGenre())
{
controller.addBookGenre(bookGenre);
}
// actually update the book using the linkController