BrightSide Workbench Full Report + Source Code
SaleProspectFilter.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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 
19 package org.turro.crm.zul.sale;
20 
21 import java.util.Collection;
22 import java.util.List;
23 import org.turro.crm.db.CrmPU;
24 import org.turro.crm.entity.SaleProspect;
25 import org.turro.crm.entity.Vendor;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.db.WhereClause;
28 import org.turro.jpa.Dao;
29 import org.turro.plugin.filter.IFilterValue;
30 import org.turro.vendor.VendorUtil;
31 import org.turro.zkoss.filter.Filter;
32 
37 public class SaleProspectFilter extends Filter<SaleProspect> {
38 
39  private boolean restrict = false;
40 
41  public SaleProspectFilter() {
42  }
43 
44  public SaleProspectFilter(boolean restrict) {
45  this.restrict = restrict;
46  }
47 
48  @Override
49  public Collection<SaleProspect> getObjectValues(List<IFilterValue> values) {
50  Dao dao = new CrmPU();
51  WhereClause wc = createCriteria(values);
52  return dao.getResultList(wc);
53  }
54 
55  private WhereClause createCriteria(List<IFilterValue> values) {
56  WhereClause wc = new WhereClause();
57  wc.addClause("select distinct salep from SaleProspect as salep");
58  wc.addClause("left join salep.attendees attendee");
59  if(restrict) {
60  Vendor vendor = VendorUtil.getCurrent();
61  wc.addClause("left join salep.vendorProspects vendorp");
62  wc.addClause("where vendorp.vendor = :vendorlimp");
63  wc.addNamedValue("vendorlimp", vendor);
64  } else if(!Application.getApplication().isInRole("sale-prospect:all")) {
65  Vendor vendor = VendorUtil.getCurrent();
66  wc.addClause("left join salep.vendorProspects vendorp");
67  wc.addClause("left join salep.customer.ownedBy vendoro");
68  wc.addClause("where (vendorp.vendor = :vendorlimp");
69  wc.addClause("or vendoro.vendor = :vendorlimo)");
70  wc.addNamedValue("vendorlimp", vendor);
71  wc.addNamedValue("vendorlimo", vendor);
72  } else {
73  wc.addClause("where 1=1");
74  }
75  for(IFilterValue v : values) {
76  v.addConstraint(wc);
77  }
78  wc.addClause("order by salep.prospectDate");
79  return wc;
80  }
81 
82 }
83 
Collection< SaleProspect > getObjectValues(List< IFilterValue > values)
void addNamedValue(String name, Object value)
static Vendor getCurrent()
Definition: VendorUtil.java:32