001    /*
002     * Databinder: a simple bridge from Wicket to Hibernate
003     * Copyright (C) 2006  Nathan Hamblen nathan@technically.us
004    
005     * This library is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public
007     * License as published by the Free Software Foundation; either
008     * version 2.1 of the License, or (at your option) any later version.
009     * 
010     * This library is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * Lesser General Public License for more details.
014     * 
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this library; if not, write to the Free Software
017     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
018     */
019    package net.databinder.components.hib;
020    
021    import java.util.Iterator;
022    
023    import net.databinder.models.hib.HibernateObjectModel;
024    
025    import org.apache.wicket.markup.repeater.RefreshingView;
026    import org.apache.wicket.model.BoundCompoundPropertyModel;
027    import org.apache.wicket.model.IModel;
028    
029    /**
030     * Similar to a PropertyListView, but works with any Iterable collection composed of
031     * persisted entities. This is particularly useful for the common Hibernate set mapping, whose 
032     * contents would otherwise need to be placed in a new List after loading. Contained items
033     * must be Hibernate entities; a Hibernate exeception will be thrown otherwise. 
034     * @author Nathan Hamblen
035     */
036    public abstract class IterableEntityView extends RefreshingView {
037            
038            /**
039             * Contruct with externally bound model whose object must be Iterable.
040             * @param id
041             */
042            public IterableEntityView(String id) {
043                    super(id);
044            }
045            
046            /**
047             * @param id Wicket id
048             * @param model Wrapped object must be Iterable.
049             */
050            public IterableEntityView(String id, IModel model) {
051                    super(id, model);
052            }       
053            
054            @Override
055            protected final Iterator getItemModels() {
056                    return new ModelIterator((Iterable)getModelObject());
057            }
058            
059            /**
060             * Wraps o in a HibernateObjectModel inside a BoundCompoundPropertyModel. Override
061             * if the compound property model is not desired.
062             * @param o object to be wrapped
063             * @return detachable model wrapping object
064             */
065            protected IModel model(Object o) {
066                    return new BoundCompoundPropertyModel(new HibernateObjectModel(o));
067            }
068            
069            private class ModelIterator implements Iterator
070            {
071                    private Iterator iterator;
072    
073                    public ModelIterator(Iterable items)
074                    {
075                            if (items != null)
076                                    this.iterator = items.iterator();
077                    }
078    
079                    public void remove()
080                    {
081                            throw new UnsupportedOperationException();
082                    }
083    
084                    public boolean hasNext()
085                    {
086                            return iterator != null && iterator.hasNext();
087                    }
088    
089                    public Object next()
090                    {
091                            return model(iterator.next());
092                    }
093            }
094    }