On 06/07/2011 02:58 PM, Michael Weber wrote:
Hello!

I have a problem which might sound trivial to most of you, but I fail
at figuring out a solution myself.

I have a bean which stores some information from a database in a
Collection. The content of the Collection is shown on a JSP page. Now
I want to allow the users to sort the content (for example by name,
by date or by title etc).

To do this I created an ActionBean named SortActionBean, which is
supposed to take a parameter that tells it which way to sort. So far
so good, but I cannot figure out how to go on from that point.

I would really appreciate it if you could give me any hints or
suggestions that can help me to achieve this.

In the attachment you can find my way of doing it (I shortened the Java files a little). There is a BaseDBItemActionBean that implements the logic that all the ActionBeans that care about working with a certain model class share. The only criterion that is used on all actionBeans to change the resulting dataset is the "limit"-Parameter (automatically bound to the actionBean from URL Parameter by stripes). Thus is parameter is included in the basic class. But since every time the list of items is requested, the addSearchRestrictions method is called, deriving classes that override this method can add their own restrictions. You can see this applied in the TeamActionBean that allows to supply a name to search for. Note that I always check for null-values. Thus the same ActionBean can be used for filtered and unfiltered pages. If no limiting parameters are supplied then the result won't be limited.

It should easily be possible to extend this system with an order by statement. I am using the convenience of Hibernate's Criteria-class in this example. If you don't use hibernate you need to find another solution for that part which should always be possible.

By the way: I am also interested on Feedback on my system. Do you (everyone) like it? Is it good practice? How could I improve it?



Thank you

Michael

Thomas
abstract public class BaseDBItemActionBean<M> extends BaseActionBean
{
	private Integer limit;

	@SuppressWarnings("unchecked")
	private Collection<? extends M> getItems(Class<? extends M> itemClass)
	{
		Session session = (Session) Stripersist.getEntityManager()
				.getDelegate();
		Criteria criteria = session.createCriteria(itemClass);
		addSearchRestrictions(criteria);
		if (limit != null)
			criteria.setMaxResults(limit);
		return criteria.list();
	}

	/**
	 * Filter all elements of the current model class with the restrictions that
	 * addSearchRestrictions adds and limit the number to the limit URL
	 * parameter if one was supplied and return the filtered list.
	 * 
	 * @return result list with items
	 */
	public Collection<? extends M> getItems()
	{
		return getItems(getCurrentModelClass());
	}

	/**
	 * Adds search restrictions. Can be overridden by subclasses to add
	 * additional search criteria.
	 * 
	 * @param criteria
	 *            the criteria that will eventually be used to filter result
	 *            lists
	 */
	protected void addSearchRestrictions(Criteria criteria)
	{

	}

	/**
	 * @return the ActionBean class that's list-method is called after
	 *         saving/deleting etc. items. Can be overridden by subclasses.
	 *         Defaults to the getClass().
	 */
	protected Class<? extends ActionBean> getListClass()
	{
		return getClass();
	}

	public Resolution list()
	{
		getContext().getRequest().setAttribute("itemList", getItems());
		return new ForwardResolution(jsp("itemlist_"
				+ getCurrentModelClass().getSimpleName() + ".jsp"));
	}

	public Resolution delete()
	{
		Stripersist.getEntityManager().remove(getItem());
		Stripersist.getEntityManager().getTransaction().commit();
		return new RedirectResolution(getListClass(), "list");
	}
	
	public Resolution details()
	{
		return new ForwardResolution(jsp("itemdetails_"
				+ getCurrentModelClass().getSimpleName() + ".jsp"));
	}

	public Resolution form()
	{
		return new ForwardResolution(jsp("itemform_"
				+ getCurrentModelClass().getSimpleName() + ".jsp"));
	}

	public Resolution save()
	{
		Stripersist.getEntityManager().persist(getItem());
		Stripersist.getEntityManager().getTransaction().commit();
		return new RedirectResolution(getListClass(), "list");
	}

	public Integer getLimit()
	{
		return limit;
	}

	public void setLimit(Integer limit)
	{
		this.limit = limit;
	}

	abstract public Object getItem();

	abstract public Class<? extends M> getCurrentModelClass();
}
public class TeamActionBean extends BaseDBItemActionBean<Team>
{
	private Team item;
	private String searchName;
	
	@Override
	public Class<Team> getCurrentModelClass()
	{
		return Team.class;
	}

	@Override
	protected void addSearchRestrictions(Criteria criteria)
	{
		super.addSearchRestrictions(criteria);
		if (searchName != null)
			criteria.add(Restrictions.ilike("name", searchName));
	}

	@Override
	public Team getItem()
	{
		return item;
	}

	public void setItem(Team item)
	{
		this.item = item;
	}

	public String getSearchName()
	{
		return searchName;
	}

	public void setSearchName(String searchName)
	{
		this.searchName = searchName;
	}
	
}
------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to