package com.bloatit.model.data;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.util.Version;
import org.hibernate.metadata.ClassMetadata;

import com.bloatit.common.PageIterable;
import com.bloatit.model.data.util.SessionManager;

public class DBRequests {

    @SuppressWarnings("unchecked")
    public static <T> T getById(Class<T> persistant, Integer id) {
        return (T) SessionManager.getSessionFactory().getCurrentSession().get(persistant, id);
    }

    public static <T> PageIterable<T> getAll(Class<T> persistent) {
        final ClassMetadata meta = SessionManager.getSessionFactory().getClassMetadata(persistent);
        return new QueryCollection<T>("from " + meta.getEntityName());
    }

    public static PageIterable<DaoDemand> searchDemands(String searchStr) {
        return search(DaoDemand.class,
                new String[] { "description.translations.title","description.translations.text", "offers.description.translations.title" },
                searchStr);
    }

    /**
     * Create a search on the db using Hibernate Search and Lucene
     * 
     * @param <T> is a persistent class (something like Dao...)
     * @param persistent is the class object associated with T.
     * @param fields is a list of field on which we are doing the search. These
     *        field are relative to the persistent class.
     * @param searchStr is the string we are looking for.
     * @return a PageIterable with the search results.
     */
    private static <T> PageIterable<T> search(Class<T> persistent, String[] fields, String searchStr) {
        MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_29, fields, new StandardAnalyzer(Version.LUCENE_29));
        try {
            org.apache.lucene.search.Query query = parser.parse(searchStr);
            return new SearchCollection<T>(SessionManager.getCurrentFullTextSession().createFullTextQuery(query, persistent));

        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> int count(Class<T> persistent) {
        final ClassMetadata meta = SessionManager.getSessionFactory().getClassMetadata(persistent);
        return ((Long) SessionManager.getSessionFactory().getCurrentSession()
                .createQuery("select count(*) from " + meta.getEntityName()).uniqueResult()).intValue();
    }

}

@@ -22,24 +22,66 @@
 odel.data;%0A%0A
+import java.util.NoSuchElementException;%0A%0A
 import org.a
@@ -888,32 +888,377 @@
 Name());%0A    %7D%0A%0A
+    public static %3CT%3E int count(Class%3CT%3E persistent) %7B%0A        final ClassMetadata meta = SessionManager.getSessionFactory().getClassMetadata(persistent);%0A        return ((Long) SessionManager.getSessionFactory().getCurrentSession()%0A                .createQuery(%22select count(*) from %22 + meta.getEntityName()).uniqueResult()).intValue();%0A    %7D%0A%0A
     public stati
@@ -1355,32 +1355,16 @@
 d.class,
-%0A               
  new Str
@@ -1404,16 +1404,17 @@
 .title%22,
+ 
 %22descrip
@@ -1437,16 +1437,32 @@
 s.text%22,
+%0A               
  %22offers
@@ -1496,32 +1496,16 @@
 itle%22 %7D,
-%0A               
  searchS
@@ -2203,16 +2203,33 @@
 nalyzer(
+%0A                
 Version.
@@ -2593,333 +2593,620 @@
 tic 
-%3CT%3E int count(Class%3CT%3E persistent) %7B%0A        final ClassMetadata meta = SessionManager.getSessionFactory().getClassMetadata(persistent);%0A        return ((Long) SessionManager.getSessionFactory().g
+PageIterable%3CDaoDemand%3E DemandsOrderByPopularity() %7B%0A        return demandsOrderBy(%22popularity%22);%0A    %7D%0A%0A    public static PageIterable%3CDaoDemand%3E DemandsOrderByContribution() %7B%0A        return demandsOrderBy(%22contribution%22);%0A    %7D%0A    %0A    public static PageIterable%3CDaoDemand%3E DemandsOrderByDate() %7B%0A        r
 et
-C
 ur
-rentSession()
+n demandsOrderBy(%22creationDate%22);
 %0A    
+%7D%0A%0A
     
-        .createQuery(%22select count(*) from %22 + meta.getEntityName()).uniqueResult()).intValue();%0A    %7D%0A
+private static PageIterable%3CDaoDemand%3E demandsOrderBy(String field) %7B%0A        return new QueryCollection%3CDaoDemand%3E(%22from DaoDemand where state == PENDING order by %22 + field);%0A    %7D%0A%0A    // By kudosable%0A    // by near end%0A    // by date%0A    // by contrib
 %0A%7D%0A

add some requests to have ordered demands