过滤CRUD列表问题——Play!框架

3

这是我在这里的第一篇帖子 :) 我有一个与CRUD模块相关的问题。我想在列表上添加更多的筛选器,但我无法理解Factory Model。我有以下代码:

@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"和"employe"字段,怎么做呢?谢谢你的帮助。:)


你希望使用什么类型的过滤器? - mandubian
1
也许我没有使用正确的术语...它更多地是添加字段以搜索数据。例如,我有一个联系人,在这个表中我与员工(OnetoMany)和来源有关系。我想显示所有具有特定员工的联系人(在SQL中等于SELECT * FROM Contact Where id_employe=my_post_value)。 - Matthieu Deutscher
你看过JPAPlugin.JPAModelLoader类中的fetch函数和getSearchQueries函数吗?就是在那里分析搜索字段的! - mandubian
1个回答

1

我的代码进展顺利!你的建议很有帮助!现在我创建了一个新类Recherche,它扩展了CRUD。我想通过动态字段更改“联系人”字段,因为我想将Recherche扩展到联系人、账户和其他类!我已经尝试使用ObjectType,但没有成功...

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

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接