I am trying to create a ListView using a detachable model, but I just
can't seem to figure out how to construct my DetachableModel. Basically, I
would like to create a detachable model and then pass it to my constructor
for a CheeseList.  This is built upon the code for the  examples for
Wicket in Action [1] by Dashorst in Chapter 4.3.  My DAO can return the
cheese based upon id or it can also return the list of cheeses. What
code do I need to put in CheeseDetach for my detachable model?


         CheeseDAO myDAO = new CheeseDAOImpl();

         // Can I make CheeseDetach construct it using the DAO as follows?
         CheeseDetach myDetach = new CheeseDetach(myDAO);

          // ListView can take list or Model as constructor.
          // How does the model work for a ListView?
          CheeseList myCheeseList = new CheeseList("cheeses", myDetach, 
getCart());

          // Add ListView to page

          add(myCheeseList);


1. https://code.google.com/p/wicketinaction/downloads/list
-- 
Brian Lavender
http://www.brie.com/brian/

"There are two ways of constructing a software design. One way is to
make it so simple that there are obviously no deficiencies. And the other
way is to make it so complicated that there are no obvious deficiencies."

Professor C. A. R. Hoare
The 1980 Turing award lecture
package com.brie.dtoo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CheeseDAOImpl implements CheeseDAO {
	private static final Map<Long, Cheese> cheeses = new HashMap<Long, Cheese>();

    static {
        cheeses.put(1L, new Cheese());
        cheeses.put(2L, new Cheese());
        cheeses.put(3L, new Cheese());

        Cheese cheese = cheeses.get(1L);
        cheese.setId(1L);
        cheese.setName("Gouda");
        cheese.setDescription("Gouda is a yellowish Dutch cheese named after the city of Gouda. The cheese is made from cow's milk that is cultured and heated until the curd is separate from the whey. About ten percent of the mixture is curds which are pressed into circular moulds for several hours.");
        cheese.setPrice(2.95);

        cheese = cheeses.get(2L);
        cheese.setId(2L);
        cheese.setName("Edam");
        cheese.setDescription("Edam (Dutch Edammer) is a Dutch cheese that is traditionally sold as spheres with pale yellow interior and a coat of paraffin. Its Spanish name is queso de bola, literally 'ball cheese'. It is named after the town of Edam in the province of North Holland[1], where the cheese is coated for export and for tourist high season. Edam which has aged for at least 17 weeks is coated with black wax, rather than the usual red or yellow.");
        cheese.setPrice(1.25);

        cheese = cheeses.get(3L);
        cheese.setId(3L);
        cheese.setName("Old Amsterdam");
        cheese.setDescription("Old Amsterdam is a Dutch gourmet cheese that is ripened to perfection and regularly checked for flavor. It is a gourmet cheese of exceptionally high and consistent quality, with a buttery mature aged Gouda flavor that cuts with ease.");
        cheese.setPrice(3.10);
    }

    
	public Cheese getCheese(Long id) {
		return cheeses.get(id);
	}

	public List<Cheese> getCheeses() {
		List<Cheese> tmpList = new ArrayList<Cheese>(cheeses.values());
		return tmpList ;
	}

}
package com.brie.dtoo;

import java.util.List;

public interface CheeseDAO {
	public Cheese getCheese(Long id);
	public List<Cheese> getCheeses();
}
package com.brie.dtoo;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;

import com.brie.dtoo.callbacks.CheeseDetach;
import com.brie.dtoo.callbacks.CheeseList;



public class Index extends CheesrPage {
  public Index() {
	  
 

	  CheeseList myCheeseList = new CheeseList("cheeses", getCheeses(), getCart());
	  
	  // 
	  
	  add(myCheeseList);
	  
	  
	  add(new ListView("cart", new PropertyModel
				(this, "cart.cheeses")) {

				@Override
				protected void populateItem(ListItem item) {
					Cheese cheese = (Cheese) item.getModelObject();

					item.add(new Label("name", cheese.getName()));
					item.add(new Label("price", "$" + cheese.getPrice()));

					item.add(new Link("remove", item.getModel()) {
						@Override
						public void onClick() {
							Cheese selected = (Cheese) getModelObject();
							getCart().getCheeses().remove(selected);
						}
					});
				}

			});
			add(new Label("total", "$" + getCart().getTotal()));
  }
  
  
}
package com.brie.dtoo.callbacks;

import org.apache.wicket.model.LoadableDetachableModel;



import com.brie.dtoo.Cheese;
import com.brie.dtoo.CheeseDAO;
import com.brie.dtoo.CheeseDAOImpl;

public class CheeseDetach extends LoadableDetachableModel<Cheese> {
	
	private static final long serialVersionUID = 1L;
	private final Long id;
	
	public CheeseDetach(Cheese cheese) 
	{
		super(cheese);
		this.id = cheese.getId();
	}
	
    public CheeseDetach(Long id) {
    	this.id = id;
	}

	@Override
	protected Cheese load() {
        if(id == null) return new Cheese();
        CheeseDAO dao = new CheeseDAOImpl();
        return dao.getCheese(id);
	}

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to