BrightSide Workbench Full Report + Source Code
DocumentFilter.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.financials.document;
19 
20 import java.util.Collection;
21 import java.util.List;
22 import org.turro.elephant.db.WhereClause;
23 import org.turro.financials.db.FinancialsPU;
24 import org.turro.financials.entity.Document;
25 import org.turro.jpa.Dao;
26 import org.turro.plugin.filter.IFilterValue;
27 import org.turro.zkoss.grid.GroupExtended;
28 import org.zkoss.zul.Grid;
29 import org.zkoss.zul.Group;
30 
35 public class DocumentFilter {
36 
37  private String mainOrderField;
38 
39  private long lastId = 0;
40 
41  public DocumentFilter() {
42  }
43 
44  public Collection getDocuments(List<IFilterValue> values, boolean onlyIds) {
45  lastId = 0;
46  Dao dao = new FinancialsPU();
47  WhereClause wc = createCriteria(values, onlyIds);
48  return dao.getResultList(wc);
49  }
50 
51  public void checkGroup(Grid grid, Document doc, boolean open) {
52  if("doc.contract.id".equals(mainOrderField)) {
53  if(lastId == 0 || lastId != doc.getContract().getId()) {
54  lastId = doc.getContract().getId();
55  Group group = new GroupExtended(doc.getContract().getFullDescription());
56  group.setOpen(open);
57  grid.getRows().appendChild(group);
58  }
59  } else if("doc.documentDefinition.id".equals(mainOrderField)) {
60  if(lastId == 0 || lastId != doc.getDocumentDefinition().getId()) {
61  lastId = doc.getDocumentDefinition().getId();
62  Group group = new GroupExtended(doc.getDocumentDefinition().getName());
63  group.setOpen(open);
64  grid.getRows().appendChild(group);
65  }
66  }
67  }
68 
69  private WhereClause createCriteria(List<IFilterValue> values, boolean onlyIds) {
70  mainOrderField = "doc.contract.id";
71  WhereClause wc = new WhereClause();
72  if(onlyIds) {
73  wc.addClause("select doc.id from Document as doc");
74  } else {
75  wc.addClause("select doc from Document as doc");
76  }
77  wc.addClause("where 1=1");
78  for(IFilterValue v : values) {
79  if(v.hasValue() && "Contract".equals(v.getLabel())) {
80  mainOrderField = "doc.documentDefinition.id";
81  }
82  v.addConstraint(wc);
83  }
84  wc.addClause("order by " + mainOrderField + ", doc.documentDate, doc.documentNumber");
85  return wc;
86  }
87 
88 }
Collection getDocuments(List< IFilterValue > values, boolean onlyIds)
void checkGroup(Grid grid, Document doc, boolean open)
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167