package org.fedorahosted.flies.core.action;

import java.util.Arrays;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.fedorahosted.flies.core.model.Project;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.log.Log;

@Name("projectBrowser")
@Scope(ScopeType.EVENT)
public class ProjectBrowser {

	private static final int DEFAULT_LIMIT = 20;
	private static final String ORDERBY_NAME = "name";
	private static final String ORDERBY_ID = "id";
	private static final String ORDERBY_TIMESTAMP = "timestamp";
	private static final List<String> ORDERBY_VALUES = Arrays.asList(
			ORDERBY_NAME, ORDERBY_TIMESTAMP, ORDERBY_ID);

	@RequestParameter
	private Integer page;

	@RequestParameter("q")
	private String query;

	private Integer limit;

	@RequestParameter
	private String orderBy;

	@Logger
	private Log log;

	@In
	private EntityManager entityManager;

	@Out(required = false)
	private List<Project> projects;

	@Out(required = false)
	private List<Project> latestProjects;

	@SuppressWarnings("unchecked")
	@Factory("latestProjects")
	public void getLatestProjects() {
		Query q = entityManager
				.createQuery("select p from Project p order by :order");
		q.setParameter("order", ORDERBY_NAME);
		q.setMaxResults(DEFAULT_LIMIT);
		latestProjects = q.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Factory("projects")
	public void getProjects() {
		String order;
		Integer pageNumber;
		if (orderBy == null || !ORDERBY_VALUES.contains(orderBy)) {
			order = ORDERBY_ID;
		} else {
			order = orderBy;
		}
		if (page == null || page < 1) {
			pageNumber = 1;
		} else {
			pageNumber = page;
		}

		Query q = entityManager
				.createQuery("select p from Project p order by :order");
		log.debug("setting order by to '{0}'", order);
		q.setParameter("order", order);
		q.setFirstResult((pageNumber - 1) * DEFAULT_LIMIT);
		q.setMaxResults(DEFAULT_LIMIT);
		projects = q.getResultList();
	}

	public Integer getSize() {
		return (Integer) entityManager.createQuery(
				"select count(*) from Project p").getSingleResult();
	}

}

@@ -210,16 +210,115 @@
 roject;%0A
+import org.hibernate.Criteria;%0Aimport org.hibernate.Session;%0Aimport org.hibernate.criterion.Order;%0A
 import o
@@ -952,17 +952,20 @@
  = %22
-timestamp
+creationDate
 %22;%0A%09
@@ -1287,37 +1287,26 @@
 ate 
-EntityManager entityManager
+Session session
 ;%0A
+%09
 %0A%09@O
@@ -1525,119 +1525,86 @@
 %7B%0A%09%09
-Query q = entityManager%0A%09%09%09%09.createQuery(%22select p from Project p order by :order%22);%0A%09%09q.setParameter(%22o
+latestProjects = session.createCriteria(Project.class)%0A%09%09%09.addOrder(O
 rder
-%22, 
+.asc(
 ORDE
@@ -1612,21 +1612,21 @@
 BY_NAME)
-;%0A%09%09q
+)%0A%09%09%09
 .setMaxR
@@ -1650,41 +1650,67 @@
 MIT)
-;
 %0A%09%09
-latestProjects = q.getResultL
+%09.setComment(%22ProjectBrowser.getLatestProjects()%22)%0A%09%09%09.l
 ist(
@@ -2054,179 +2054,124 @@
 %0A%0A%09%09
-Query q = entityManager%0A%09%09%09%09.createQuery(%22select p from Project p order by :order%22);%0A%09%09log.debug(%22setting order by to '%7B0%7D'%22, 
+projects = session.createCriteria(Project.class)%0A%09%09%09.addOrder(Order.asc(
 order)
-;%0A%09%09q
+)%0A%09%09%09
 .set
-Parameter(%22order%22, order);%0A%09%09q
+MaxResults(DEFAULT_LIMIT)%0A%09%09%09
 .set
@@ -2219,207 +2219,205 @@
 MIT)
-;
 %0A%09%09
-q
+%09
 .set
-MaxResults(DEFAULT_LIMIT);%0A%09%09projects = q.getResultList();%0A%09%7D%0A%0A%09public Integer getSize() %7B%0A%09%09return (Integer) entityManager.createQuery(%0A%09%09%09%09%22select count(*) from Project p%22).getSingleResult
+Comment(%22ProjectBrowser.getProjects()%22)%0A%09%09%09.list();%0A%09%7D%0A%0A%09public Integer getSize() %7B%0A%09%09return session.createCriteria(Project.class)%0A%09%09%09.setComment(%22ProjectBrowser.getSize()%22)%0A%09%09%09.list().size
 ();%0A

make ProjectBrowser work in hsqldb