BrightSide Workbench Full Report + Source Code
ContactFilterField.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.contacts.filter;
20 
21 import java.util.Date;
22 import java.util.List;
23 import org.turro.string.Strings;
24 import org.turro.contacts.db.ContactsPU;
25 import org.turro.elephant.db.SQLHelper;
26 import org.turro.elephant.db.WhereClause;
27 import org.turro.zkoss.filter.FilterField;
28 import org.turro.zkoss.filter.FilterFieldOperator;
29 
34 public abstract class ContactFilterField extends FilterField {
35 
36  public ContactFilterField(String label, String value) {
37  super(label, value);
38  }
39 
40  public ContactFilterField(String label, Number value) {
41  super(label, value);
42  }
43 
44  public ContactFilterField(String label, Date value) {
45  super(label, value);
46  }
47 
48  public ContactFilterField(String label, Boolean value) {
49  super(label, value);
50  }
51 
52  public ContactFilterField(String label, Enum value) {
53  super(label, value);
54  }
55 
56  public ContactFilterField(String label) {
57  super(label);
58  }
59 
60  public ContactFilterField(String label, String value, FilterFieldOperator operator) {
61  super(label, value, operator);
62  }
63 
64  public ContactFilterField(String label, Number value, FilterFieldOperator operator) {
65  super(label, value, operator);
66  }
67 
68  public ContactFilterField(String label, Date value, FilterFieldOperator operator) {
69  super(label, value, operator);
70  }
71 
72  public ContactFilterField(String label, Boolean value, FilterFieldOperator operator) {
73  super(label, value, operator);
74  }
75 
76  public ContactFilterField(String label, Enum value, FilterFieldOperator operator) {
77  super(label, value, operator);
78  }
79 
80  public ContactFilterField(String label, FilterFieldOperator operator) {
81  super(label, operator);
82  }
83 
84  @Override
85  public void addConstraint(WhereClause wc) {
86  String inClause = getInClause();
87  if(!Strings.isBlank(inClause)) {
88  wc.addClause(getChain().getOpSQL() + " " + getContactField() + " in " + inClause);
89  } else {
90  wc.addClause(getChain().getOpSQL() + " 1 = 2");
91  }
92  }
93 
94  public abstract String getContactField();
95 
96  protected abstract String getInternalField();
97 
98  private String getInClause() {
99  WhereClause wc = new WhereClause();
100  wc.addClause("select distinct contact.id from Contact as contact");
101  wc.addClause("left join contact.addresses address");
102  wc.addClause("left join contact.connectors connector");
103  wc.addClause("left join contact.comments comment");
104  wc.addClause("where 1=1");
106  List<String> list = new ContactsPU().getResultList(wc);
107  String inClause = SQLHelper.convertToIn(list);
108  if(!Strings.isBlank(inClause)) {
109  return "(" + inClause + ")";
110  }
111  return null;
112  }
113 
114 }
ContactFilterField(String label, Boolean value, FilterFieldOperator operator)
ContactFilterField(String label, Enum value, FilterFieldOperator operator)
ContactFilterField(String label, Date value, FilterFieldOperator operator)
ContactFilterField(String label, String value, FilterFieldOperator operator)
ContactFilterField(String label, Number value, FilterFieldOperator operator)
ContactFilterField(String label, FilterFieldOperator operator)
static String convertToIn(Collection collection)
Definition: SQLHelper.java:43
void doAddConstraint(WhereClause wc, String field)