Java 过滤积垢列表问题玩!框架
这是我在这里的第一篇帖子:)我有一个CRUD模块的问题。我想在列表中添加更多过滤器,但我无法成功理解工厂模型。我有以下代码:Java 过滤积垢列表问题玩!框架,java,web-applications,model,playframework,crud,Java,Web Applications,Model,Playframework,Crud,这是我在这里的第一篇帖子:)我有一个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 t
@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);
}
}
@With(Secure.class)
公共类联系人扩展了CRUD{
公共静态无效列表(整型页面、字符串搜索、整型源代码、字符串搜索字段、字符串排序依据、字符串顺序){
ObjectType=ObjectType.get(getControllerClass());
notFoundIfNull(类型);
如果(第<1页){
page=1;
}
//系统输出打印项次(类型);
List contacts=Model.Manager.factoryFor(Contact.class).fetch((第1页)*getPageSize(),getPageSize(),orderBy,order,searchFields==null?新建ArrayList():Arrays.asList(searchFields.split([]),search,(String)request.args.get(“where”);
系统输出打印项次(联系人);
List objects=type.findPage(页面、搜索、搜索字段、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”);
//原汁原味
List origines=Origine.find(“按nom asc排序”).fetch();
List employes=Employe.find(“按nom asc排序”).fetch();
试一试{
呈现(类型、对象、计数、总数、页面、医嘱人、医嘱、来源、雇员);
}捕获(TemplateNotFounde异常){
呈现(“CRUD/list.html”、类型、对象、计数、总数、页面、orderBy、order、origines、employes);
}
}
}
我想搜索“源代码”和“雇员”字段,我该怎么做?谢谢你的帮助。:) 我的代码有了进步!!!你的建议很有帮助!现在我创建了一个新的类Recherche,它扩展了CRUD。我想用dynamic字段更改字段“Contact”,因为我想扩展Contact和Compte以及其他类的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;
}
公共类Recherche扩展了CRUD{
公共静态列表findPage(int页、字符串搜索、字符串搜索字段、字符串orderBy、字符串顺序、字符串where)抛出ClassNotFoundException{
int pageLength=getPageSize();
字符串q=“从触点,其中1=1”;
if(search!=null&&!search.equals(“”){
字符串searchQuery=getSearchQuery();
如果(!searchQuery.equals(“”){
q+=“和(“+searchQuery+”);
}
q+=(其中!=null?)和“+where:”;
}否则{
q+=(其中!=null?)和“+where:”;
}
/**
*辅助冠军杯
*/
列表字段=Contact.Manager.factoryFor(Contact.class.listProperties();
字符串qaux=“”;
List relationArray=新建ArrayList();
用于(属性字段:字段){
if(字段间关系){
if(request.params.get(field.name)!=null){
int requestArg=Integer.parseInt(request.params.get(field.name));
if(requestArg!=0){
如果(!qaux.equals(“”){
qaux+=“和”;
}
relationArray.add(requestArg);
qaux+=“”+field.name+“_id=?”+(relationArray.size()+1)+“”;
}
}
}
}
如果(!qaux.equals(“”){
q+=“和(“+qaux+”);
}
/**
*Fin ajout champs辅助设备
*/
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+=“按订单”+按订单+”+订单;
Query Query=Contact.em().createQuery(q);
if(search!=null&&!search.equals(“”)&&q.indexOf(“?1”)!=-1){
query.setParameter(1,“%”+search.toLowerCase()+“%”;
}
//辅助冠军
对于(int i=0;i