BrightSide Workbench Full Report + Source Code
ContractFilter.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.contract;
19 
20 import java.util.Collection;
21 import java.util.List;
22 import org.turro.contacts.Contact;
23 import org.turro.elephant.db.WhereClause;
24 import org.turro.financials.db.FinancialsPU;
25 import org.turro.financials.entity.Contract;
26 import org.turro.financials.entity.ContractDefinition;
27 import org.turro.financials.model.contract.ContractWrapper;
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 ContractFilter {
39 
40  private String mainOrderField, mode = "all", searchText = "";
41  private ContractDefinition contractDefinition;
42  private Contact contact;
43 
44  private long lastId = 0;
45  private String lastContact = null;
46 
47  public ContractFilter() {
48  }
49 
50  public Contact getContact() {
51  return contact;
52  }
53 
54  public void setContact(Contact contact) {
55  this.contact = contact;
56  }
57 
59  return contractDefinition;
60  }
61 
62  public void setContractDefinition(ContractDefinition contractDefinition) {
63  this.contractDefinition = contractDefinition;
64  }
65 
66  public String getMode() {
67  return mode;
68  }
69 
70  public void setMode(String mode) {
71  this.mode = mode;
72  }
73 
74  public String getSearchText() {
75  return searchText;
76  }
77 
78  public void setSearchText(String searchText) {
79  this.searchText = searchText;
80  }
81 
82  public Collection<Contract> getContracts(List<IFilterValue> values) {
83  lastId = 0;
84  lastContact = null;
85  Dao dao = new FinancialsPU();
86  WhereClause wc = createCriteria(values);
87  Collection<Contract> list = dao.getResultList(wc);
88  if("active".equals(mode)) {
90  } else if("inactive".equals(mode)) {
92  }
93  return list;
94  }
95 
96  public void checkGroup(Grid grid, Contract contract, boolean open) {
97  if("ctc.contractDefinition.id".equals(mainOrderField)) {
98  if(lastId == 0 || lastId != contract.getContractDefinition().getId()) {
99  lastId = contract.getContractDefinition().getId();
100  Group group = new GroupExtended(contract.getContractDefinition().getName());
101  group.setOpen(open);
102  grid.getRows().appendChild(group);
103  }
104  } else if("ctc.contractor".equals(mainOrderField)) {
105  if(lastContact == null || !lastContact.equals(contract.getContractor())) {
106  lastContact = contract.getContractor();
107  Group group = new GroupExtended(contract.getIContractor().getName());
108  group.setOpen(open);
109  grid.getRows().appendChild(group);
110  }
111  }
112  }
113 
114  private WhereClause createCriteria(List<IFilterValue> values) {
115  mainOrderField = "ctc.contractDefinition.id";
116  WhereClause wc = new WhereClause();
117  wc.addClause("select ctc from Contract as ctc");
118  wc.addClause("where 1=1");
119  for(IFilterValue v : values) {
120  if(v.hasValue() && "Contract model".equals(v.getLabel())) {
121  mainOrderField = "ctc.contractor";
122  } else if(v.hasValue() && "Contact".equals(v.getLabel())) {
123  mainOrderField = "ctc.contractDefinition.id";
124  }
125  v.addConstraint(wc);
126  }
127  wc.addClause("order by " + mainOrderField + ", ctc.id");
128  return wc;
129  }
130 
131 }
void setContractDefinition(ContractDefinition contractDefinition)
Collection< Contract > getContracts(List< IFilterValue > values)
void checkGroup(Grid grid, Contract contract, boolean open)
ContractDefinition getContractDefinition()
Definition: Contract.java:125