BrightSide Workbench Full Report + Source Code
ModelSearch.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.model;
20 
21 import java.util.HashSet;
22 import java.util.Set;
23 import org.turro.string.Strings;
24 import org.turro.contacts.organigram.RelationType;
25 import org.turro.elephant.db.SQLHelper;
26 import org.turro.elephant.db.WhereClause;
27 
32 public class ModelSearch {
33 
34  private String fieldContact, fieldRelation, fieldConnector, fieldAddress, fieldSyndication, fieldComment;
35  private boolean useConnector, useAddress, useComment;
36 
37  public ModelSearch() {
38  this.fieldContact = "contact";
39  this.fieldRelation = "relation";
40  this.fieldConnector = "connector";
41  this.fieldAddress = "address";
42  this.fieldSyndication = "syndication";
43  this.fieldComment = "comment";
44  this.useConnector = false;
45  this.useAddress = false;
46  this.useComment = false;
47  }
48 
49  public String getFieldContact() {
50  return fieldContact;
51  }
52 
53  public void setFieldContact(String fieldContact) {
54  this.fieldContact = fieldContact;
55  }
56 
57  public String getFieldRelation() {
58  return fieldRelation;
59  }
60 
61  public void setFieldRelation(String fieldRelation) {
62  this.fieldRelation = fieldRelation;
63  }
64 
65  public String getFieldConnector() {
66  return fieldConnector;
67  }
68 
69  public void setFieldConnector(String fieldConnector) {
70  this.fieldConnector = fieldConnector;
71  }
72 
73  public String getFieldAddress() {
74  return fieldAddress;
75  }
76 
77  public void setFieldAddress(String fieldAddress) {
78  this.fieldAddress = fieldAddress;
79  }
80 
81  public String getFieldSyndication() {
82  return fieldSyndication;
83  }
84 
85  public void setFieldSyndication(String fieldSyndication) {
86  this.fieldSyndication = fieldSyndication;
87  }
88 
89  public String getFieldComment() {
90  return fieldComment;
91  }
92 
93  public void setFieldComment(String fieldComment) {
94  this.fieldComment = fieldComment;
95  }
96 
97  public boolean isUseConnector() {
98  return useConnector;
99  }
100 
101  public void setUseConnector(boolean useConnector) {
102  this.useConnector = useConnector;
103  }
104 
105  public boolean isUseAddress() {
106  return useAddress;
107  }
108 
109  public void setUseAddress(boolean useAddress) {
110  this.useAddress = useAddress;
111  }
112 
113  public boolean isUseComment() {
114  return useComment;
115  }
116 
117  public void setUseComment(boolean useComment) {
118  this.useComment = useComment;
119  }
120 
121  public void apply(String operator, String value, WhereClause wc, boolean stopIfEmpty) {
122  if(Strings.isBlank(value)) {
123  wc.addClause(operator + (stopIfEmpty ? " 1=2" : " 1=1"));
124  } else if(value.indexOf("=") > 0) {
125  applyPair(operator, value, wc);
126  } else {
127  Set<String> fields = new HashSet<>();
128  fields.add("contact.name");
129  fields.add("contact.globalIdentifier");
130  if(useConnector) fields.add("connector.value");
131  if(useAddress) {
132  fields.add("address.city");
133  fields.add("address.zipCode");
134  fields.add("address.province");
135  fields.add("address.state");
136  fields.add("address.street");
137  }
138  if(useComment) fields.add("comment.comment");
139  wc.setPrefix(operator);
140  wc.addLikeFields(fields.toArray(new String[0]), value);
141  }
142  }
143 
144  private void applyPair(String operator, String value, WhereClause wc) {
145  String[] v = value.split("\\s*=\\s*");
146  if(v.length == 1) {
147  wc.addClause(operator + " (");
148  wc.addClause("connector.description = :variable");
149  wc.addClause("or address.description = :variable");
150  wc.addClause(")");
151  wc.addNamedValue("variable", v[0]);
152  } else {
153  if("Grouping".equalsIgnoreCase(v[0])) {
154  wc.addClause(operator + " contact.grouping like :value");
155  wc.addNamedValue("value", SQLHelper.convertToPartialLike(v[1]));
156  } else if("Activity".equalsIgnoreCase(v[0])) {
157  wc.addClause(operator + " contact.idActivity like :value");
158  wc.addNamedValue("value", SQLHelper.convertToPartialLike(v[1]));
159  } else if("Relation".equalsIgnoreCase(v[0])) {
160  wc.addClause(operator + " relation.relationType = :typevalue");
161  wc.addNamedValue("typevalue", RelationType.getRelationPrefix() + v[1].toUpperCase());
162  } else {
163  wc.addClause(operator + " (");
164  wc.addClause("(connector.description = :variable and connector.value like :value)");
165  wc.addClause("or (address.description = :variable and (");
166  wc.addClause("address.city like :value or address.zipCode like :value or address.province like :value or address.state like :value or address.street like :value))");
167  wc.addClause(")");
168  wc.addNamedValue("variable", v[0]);
169  wc.addNamedValue("value", SQLHelper.convertToPartialLike(v[1]));
170  }
171  }
172  }
173 
174 }
void setFieldContact(String fieldContact)
void setFieldComment(String fieldComment)
void setUseComment(boolean useComment)
void setFieldSyndication(String fieldSyndication)
void setFieldAddress(String fieldAddress)
void setUseConnector(boolean useConnector)
void setFieldRelation(String fieldRelation)
void apply(String operator, String value, WhereClause wc, boolean stopIfEmpty)
void setUseAddress(boolean useAddress)
void setFieldConnector(String fieldConnector)
void addLikeFields(String[] fields, String value)
void addNamedValue(String name, Object value)