2011-07-05 109 views
3

這是我在這裏的第一篇文章:)我有一個CRUD模塊的問題。我想在列表中添加更多過濾器,但我無法理解Factory Model。我有這樣的代碼:過濾crud列表問題玩!框架

@With(Secure.class) 
public class Contacts extends CRUD { 


public static void list(int page,String search,int origine,String searchFields, String orderBy, String order) { 
    ObjectType type = ObjectType.get(getControllerClass()); 
    notFoundIfNull(type); 
    if (page < 1) { 
     page = 1; 
    } 

    //System.out.println(type); 

    List<Model> contacts = Model.Manager.factoryFor(Contact.class).fetch((page - 1) * getPageSize(), getPageSize(), orderBy, order, searchFields == null ? new ArrayList<String>() : Arrays.asList(searchFields.split("[ ]")), search, (String) request.args.get("where")); 
    System.out.println(contacts); 

    List<Model> objects = type.findPage(page, search, searchFields, orderBy, order, (String) request.args.get("where")); 
    Long count = type.count(search, searchFields, (String) request.args.get("where")); 
    Long totalCount = type.count(null, null, (String) request.args.get("where")); 


    // Liste des origines 
    List<Origine> origines = Origine.find("order by nom asc").fetch(); 
    List<Employe> employes = Employe.find("order by nom asc").fetch(); 

    try { 
     render(type, objects, count, totalCount, page, orderBy, order, origines,employes); 
    } catch (TemplateNotFoundException e) { 
     render("CRUD/list.html", type, objects, count, totalCount, page, orderBy, order, origines, employes); 
    } 
} 

}

我想搜索的申請「原產」和「僱工」我該怎麼辦呢?感謝您的幫助。 :)

+0

你希望做什麼樣的過濾器? – mandubian

+1

也許我沒有使用好的術語...這是更多的添加字段來搜索數據。例如,我有一個聯繫人,在這張表中我與僱主(OnetoMany)和Origine有關係。我想顯示與特定僱員的所有聯繫人(在SQL等於SELECT * FROM聯繫人地址id_employe = my_post_value) –

+0

您是否看過JPAPlugin.JPAModelLoader類中的函數fetch和getSearchQueries?它在那裏,搜索字段被分析! – mandubian

回答

1

我在我的代碼中進步!你的建議很有幫助!現在我創建了一個擴展CRUD的新類Recherche。我想改變字段「聯繫」與動態字段,因爲我想擴展接觸和Compte和其他類的Recherche!我已經測試對象類型寬度不sucess ...

public class Recherche extends CRUD { 


public static List findPage(int page, String search, String searchFields, String orderBy, String order, String where) throws ClassNotFoundException{ 

    int pageLength = getPageSize(); 
    String q = "from Contact where 1=1 "; 
    if (search != null && !search.equals("")) { 
     String searchQuery = getSearchQuery(); 
     if (!searchQuery.equals("")) { 
      q += " and (" + searchQuery + ")"; 
     } 
     q += (where != null ? " and " + where : ""); 
    } else { 
     q += (where != null ? " and " + where : ""); 
    } 

    /** 
    * Ajout des champs auxiliaires 
    */ 
    List<Property> fields = Contact.Manager.factoryFor(Contact.class).listProperties(); 

    String qaux = ""; 
    List<Integer> relationArray = new ArrayList<Integer>(); 

    for (Property field : fields) { 
     if(field.isRelation){ 

      if(request.params.get(field.name) != null){ 
       int requestArg = Integer.parseInt(request.params.get(field.name)); 

       if(requestArg != 0){ 
        if (!qaux.equals("")) { 
         qaux += " and "; 
        } 
        relationArray.add(requestArg); 
        qaux += " "+field.name+"_id = ?"+(relationArray.size()+1)+" "; 
       } 
      } 
     } 
    } 
    if(!qaux.equals("")){ 
     q+= " and ("+qaux+") "; 
    } 
    /** 
    * Fin ajout champs auxiliaires 
    */ 


    if (orderBy == null && order == null) { 
      orderBy = "nom"; 
      order = "ASC"; 
    } 
    if (orderBy == null && order != null) { 
      orderBy = "nom"; 
    } 
    if (order == null || (!order.equals("ASC") && !order.equals("DESC"))) { 
      order = "ASC"; 
    } 
    q += " order by " + orderBy + " " + order; 
    Query query = Contact.em().createQuery(q); 
    if (search != null && !search.equals("") && q.indexOf("?1") != -1) { 
     query.setParameter(1, "%" + search.toLowerCase() + "%"); 
    } 

    // Champs auxiliaires 
    for (int i = 0; i < relationArray.size(); i++) { 
     query.setParameter((i+2), relationArray.get(i)); 
    } 


    query.setFirstResult((page - 1) * pageLength); 
    query.setMaxResults(pageLength); 

    return query.getResultList(); 
} 


public static Long count(String search, String searchFields, String where) { 
    String q = "select count(c.id) from Contact c where 1=1 "; 

    if (search != null && !search.equals("")) { 
     String searchQuery = getSearchQuery(); 
     if (!searchQuery.equals("")) { 
      q += " and (" + searchQuery + ")"; 
     } 
     q += (where != null ? " and " + where : ""); 
    } else { 
     q += (where != null ? " and " + where : ""); 
    } 
    /** 
    * Ajout des champs auxiliaires 
    */ 
    List<Property> fields = Contact.Manager.factoryFor(Contact.class).listProperties(); 

    String qaux = ""; 
    List<Integer> relationArray = new ArrayList<Integer>(); 

    for (Property field : fields) { 
     if(field.isRelation){ 

      if(request.params.get(field.name) != null){ 
       int requestArg = Integer.parseInt(request.params.get(field.name)); 

       if(requestArg != 0){ 
        if (!qaux.equals("")) { 
         qaux += " and "; 
        } 
        relationArray.add(requestArg); 
        qaux += " "+field.name+"_id = ?"+(relationArray.size()+1)+" "; 
       } 
      } 
     } 
    } 
    if(!qaux.equals("")){ 
     q+= " and ("+qaux+") "; 
    } 

    /** 
    * Fin ajout champs auxiliaires 
    */ 


    Query query = Contact.em().createQuery(q); 
    if (search != null && !search.equals("") && q.indexOf("?1") != -1) { 
     query.setParameter(1, "%" + search.toLowerCase() + "%"); 
    } 
    // Champs auxiliaires 
    for (int i = 0; i < relationArray.size(); i++) { 
     query.setParameter((i+2), relationArray.get(i)); 
    } 

    return Long.decode(query.getSingleResult().toString()); 
} 


public static void list(int page,String search,int origine,String searchFields, String orderBy, String order) throws ClassNotFoundException { 
    ObjectType type = ObjectType.get(getControllerClass()); 
    notFoundIfNull(type); 

    if (page < 1) { 
     page = 1; 
    } 

    List<Contact> objects = Contacts.findPage(page, search, searchFields, orderBy, order, (String) request.args.get("where")); 
    Long count = Contacts.count(search, searchFields, (String) request.args.get("where")); 
    Long totalCount = Contacts.count(null, null, (String) request.args.get("where")); 


    // Liste des origines 
    List<Origine> origines = Origine.find("order by nom asc").fetch(); 
    // Liste des employes 
    List<Employe> employes = Employe.find("order by nom asc").fetch(); 
    // Liste des villes 
    List<Ville> villes = Ville.find("order by nom asc").fetch(); 

    try { 
     render(type, objects, count, totalCount, page, orderBy, order, origines,employes,villes); 
    } catch (TemplateNotFoundException e) { 
     render("CRUD/list.html", type, objects, count, totalCount, page, orderBy, order, origines, employes,villes); 
    } 
} 


private static String getSearchQuery() { 
    List<Property> fields = Contact.Manager.factoryFor(Contact.class).listProperties(); 

    ObjectType type = ObjectType.get(getControllerClass()); 
    notFoundIfNull(type); 



    String q = ""; 
    for (Property field : fields) { 
     if(field.isSearchable){ 
      if (!q.equals("")) { 
       q += " or "; 
      } 

      q += "lower(" + field.name + ") like ?1"; 
     } 
    } 
    return q; 
} 

}

+0

如果它適合你,你可以接受你自己的答案。 – ripper234