19 package org.turro.contacts.model;
21 import java.util.HashSet;
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;
34 private String fieldContact, fieldRelation, fieldConnector, fieldAddress, fieldSyndication, fieldComment;
35 private boolean useConnector, useAddress, useComment;
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;
54 this.fieldContact = fieldContact;
62 this.fieldRelation = fieldRelation;
66 return fieldConnector;
70 this.fieldConnector = fieldConnector;
78 this.fieldAddress = fieldAddress;
82 return fieldSyndication;
86 this.fieldSyndication = fieldSyndication;
94 this.fieldComment = fieldComment;
102 this.useConnector = useConnector;
110 this.useAddress = useAddress;
118 this.useComment = useComment;
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);
127 Set<String> fields =
new HashSet<>();
128 fields.add(
"contact.name");
129 fields.add(
"contact.globalIdentifier");
130 if(useConnector) fields.add(
"connector.value");
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");
138 if(useComment) fields.add(
"comment.comment");
144 private void applyPair(String
operator, String value,
WhereClause wc) {
145 String[] v = value.split(
"\\s*=\\s*");
148 wc.
addClause(
"connector.description = :variable");
149 wc.
addClause(
"or address.description = :variable");
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());
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))");
169 wc.
addNamedValue(
"value", SQLHelper.convertToPartialLike(v[1]));
void setPrefix(String prefix)
void addClause(String clause)
void addLikeFields(String[] fields, String value)
void addNamedValue(String name, Object value)