2011-07-05 2 views
3

그것은 내 첫 게시물 여기 :) CRUD 모듈에 문제가 있습니다. 목록에 필터를 추가하고 싶지만 공장 모델을 이해하는 데 성공하지 못했습니다.필터링 crud list problem play! 프레임 워크

@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); 
    } 
} 

}

가 나는 것을 어떻게 할 수있는 제출 "origine"와 "직원당"를 검색하고자

:이 코드가? 도와 줘서 고마워. :)

+0

필터 어떤 종류의 할 기대 ... 개체 유형 폭이 더 성공 테스트 한? – mandubian

+1

어쩌면 나는 좋은 용어를 사용하지 않았을 것입니다 ... 그것은 검색 데이터에 더 많은 필드를 추가합니다. 예를 들어 Contact가 있으며이 테이블에는 Employe (OnetoMany) 및 Origine과의 관계가 있습니다. 특정 Employe와의 모든 연락처를 표시하고 싶습니다. (SQL은 SELECT * FROM Contact와 같습니다. id_employe = my_post_value) –

+0

JPAPlugin.JPAModelLoader 클래스에서 function fetch 및 getSearchQueries를 보셨습니까? 검색 필드가 분석됩니다! – mandubian

답변

1

내 코드가 진행됩니다! 귀하의 조언은 도움이되었다! 이제 CRUD를 확장하는 Recherche 클래스를 새로 만들었습니다. 연락처 및 Compte 및 다른 클래스를위한 Recherche를 확장하고 싶기 때문에 나는 동적 필드와 필드 "연락처"를 변경하고 싶습니다! 나는

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

관련 문제