BrightSide Workbench Full Report + Source Code
TaskFilter.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.erp.task;
19 
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
23 import javax.persistence.Query;
24 import org.turro.string.Strings;
25 import org.turro.elephant.db.WhereClause;
26 import org.turro.erp.db.ErpPU;
27 import org.turro.erp.entity.Task;
28 import org.turro.jpa.Dao;
29 import org.turro.plugin.filter.IFilterValue;
30 import org.turro.zkoss.grid.GroupExtended;
31 import org.zkoss.zul.Grid;
32 import org.zkoss.zul.Group;
33 
38 public class TaskFilter {
39 
40  private long lastId = 0;
41 
42  public TaskFilter() {
43  }
44 
45  public Collection<Task> getTasks(List<IFilterValue> values) {
46  lastId = 0;
47  Dao dao = new ErpPU();
48  try {
49  WhereClause wc = createCriteria(values);
50  if(!Strings.isEmpty(wc.getClause())) {
51  Query q = dao.createQuery(wc.getClause());
52  wc.setNamedParameters(q);
53  return q.getResultList();
54  } else {
55  return Collections.EMPTY_LIST;
56  }
57  } finally {
58  dao.close();
59  }
60  }
61 
62  public void checkGroup(Grid grid, Task task, boolean open) {
63  if(lastId == 0 || lastId != task.getOrderReference().getId()) {
64  lastId = task.getOrderReference().getId();
65  Group group = new GroupExtended(task.getOrderReference().getFullDescription());
66  group.setOpen(open);
67  grid.getRows().appendChild(group);
68  }
69  }
70 
71  private WhereClause createCriteria(List<IFilterValue> values) {
72  WhereClause wc = new WhereClause();
73  wc.addClause("select t from Task as t");
74  wc.addClause("where 1=1");
75  for(IFilterValue v : values) {
76  v.addConstraint(wc);
77  }
78  wc.addClause("order by t.orderReference, t.startDate, t.id");
79  return wc;
80  }
81 
82 }
void checkGroup(Grid grid, Task task, boolean open)
Definition: TaskFilter.java:62
Collection< Task > getTasks(List< IFilterValue > values)
Definition: TaskFilter.java:45
void close()
Definition: Dao.java:71
Query createQuery(String query)
Definition: Dao.java:61