BrightSide Workbench Full Report + Source Code
WorkOrderFilter.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.workorder;
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.WorkOrder;
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 WorkOrderFilter {
39 
40  private long lastId = 0;
41 
42  public WorkOrderFilter() {
43  }
44 
45  public Collection<WorkOrder> getWorkOrders(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, WorkOrder workOrder, boolean open) {
63  if(lastId == 0 || lastId != getGroupId(workOrder)) {
64  lastId = getGroupId(workOrder);
65  if(lastId < 0) {
66  Group group = new GroupExtended(workOrder.getProduct().getProductString());
67  group.setOpen(open);
68  grid.getRows().appendChild(group);
69  } else {
70  Group group = new GroupExtended(workOrder.getContract().getFullDescription());
71  group.setOpen(open);
72  grid.getRows().appendChild(group);
73  }
74  }
75  }
76 
77  private WhereClause createCriteria(List<IFilterValue> values) {
78  WhereClause wc = new WhereClause();
79  wc.addClause("select distinct wo from WorkOrder as wo");
80  wc.addClause("left join wo.orderReferences as r");
81  wc.addClause("where 1=1");
82  for(IFilterValue v : values) {
83  v.addConstraint(wc);
84  }
85  wc.addClause("order by wo.productId, wo.contractId, wo.workOrderDate, wo.workOrderId");
86  return wc;
87  }
88 
89  private long getGroupId(WorkOrder workOrder) {
90  if(workOrder.getProductId() > 0) {
91  return -workOrder.getProductId();
92  } else if(workOrder.getContractId() > 0) {
93  return workOrder.getContractId();
94  }
95  return 0;
96  }
97 
98 }
void checkGroup(Grid grid, WorkOrder workOrder, boolean open)
Collection< WorkOrder > getWorkOrders(List< IFilterValue > values)
void close()
Definition: Dao.java:71
Query createQuery(String query)
Definition: Dao.java:61