View Javadoc

1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
20   */
21  package org.synchronoss.cpo.jdbc;
22  
23  import org.synchronoss.cpo.*;
24  import org.synchronoss.cpo.meta.domain.*;
25  
26  import java.util.Collection;
27  
28  /**
29   * JdbcCpoWhere is an interface for specifying the sort order in which objects are returned from the Datasource.
30   *
31   * @author david berry
32   */
33  public class JdbcCpoWhere extends Node implements CpoWhere {
34  
35    /**
36     * Version Id for this class.
37     */
38    private static final long serialVersionUID = 1L;
39    static final String[] comparisons = {
40      "=", //COMP_EQ
41      "<", //COMP_LT
42      ">", //COMP_GT
43      "<>", //COMP_NEQ
44      "IN", //COMP_IN
45      "LIKE", //COMP_LIKE
46      "<=", //COMP_LTEQ
47      ">=", //COMP_GTEQ
48      "EXISTS", //COMP_EXISTS
49      "IS NULL" //COMP_ISNULL
50    };
51    static final String[] logicals = {
52      "AND", //LOGIC_AND
53      "OR" //LOGIC_OR
54    };
55    private int comparison = CpoWhere.COMP_NONE;
56    private int logical = CpoWhere.LOGIC_NONE;
57    private String attribute = null;
58    private String rightAttribute = null;
59    private Object value = null;
60    private String attributeFunction = null;
61    private String rightAttributeFunction = null;
62    private String valueFunction = null;
63    private boolean not = false;
64    private String staticValue_ = null;
65    private String name = "__CPO_WHERE__";
66  
67    public <T> JdbcCpoWhere(int logical, String attr, int comp, T value) {
68      setLogical(logical);
69      setAttribute(attr);
70      setComparison(comp);
71      setValue(value);
72    }
73  
74    public <T> JdbcCpoWhere(int logical, String attr, int comp, T value, boolean not) {
75      setLogical(logical);
76      setAttribute(attr);
77      setComparison(comp);
78      setValue(value);
79      setNot(not);
80    }
81  
82    public JdbcCpoWhere() {
83    }
84  
85    @Override
86    public void setComparison(int i) {
87      if (i < 0 || i >= comparisons.length) {
88        this.comparison = CpoWhere.COMP_NONE;
89      } else {
90        this.comparison = i;
91      }
92    }
93  
94    @Override
95    public int getComparison() {
96      return this.comparison;
97    }
98  
99    @Override
100   public void setLogical(int i) {
101     if (i < 0 || i >= logicals.length) {
102       this.logical = CpoWhere.LOGIC_NONE;
103     } else {
104       this.logical = i;
105     }
106   }
107 
108   @Override
109   public int getLogical() {
110     return this.logical;
111   }
112 
113   @Override
114   public void setAttribute(String s) {
115     this.attribute = s;
116   }
117 
118   @Override
119   public String getAttribute() {
120     return this.attribute;
121   }
122 
123   @Override
124   public void setRightAttribute(String s) {
125     this.rightAttribute = s;
126   }
127 
128   @Override
129   public String getRightAttribute() {
130     return this.rightAttribute;
131   }
132 
133   @Override
134   public void setValue(Object s) {
135     this.value = s;
136   }
137 
138   @Override
139   public Object getValue() {
140     return this.value;
141   }
142 
143   @Override
144   public void setStaticValue(String staticValue) {
145     this.staticValue_ = staticValue;
146   }
147 
148   @Override
149   public String getStaticValue() {
150     return this.staticValue_;
151   }
152 
153   @Override
154   public boolean getNot() {
155     return this.not;
156   }
157 
158   @Override
159   public void setNot(boolean b) {
160     this.not = b;
161   }
162 
163   public String toString(CpoClass cpoClass) throws CpoException {
164     StringBuilder sb = new StringBuilder();
165     JdbcCpoAttribute jdbcAttribute = null;
166 
167 
168     if (getLogical() != CpoWhere.LOGIC_NONE) {
169       sb.append(" ");
170       sb.append(logicals[getLogical()]);
171     } else if (!hasParent()) {
172       // This is the root where clause
173       sb.append("WHERE");
174     }
175 
176     if (getNot()) {
177       sb.append(" NOT");
178     }
179 
180     if (getAttribute() != null) {
181       if (sb.length() > 0) {
182         sb.append(" ");
183       }
184       String fullyQualifiedColumn;
185 
186       jdbcAttribute = (JdbcCpoAttribute) cpoClass.getAttributeJava(getAttribute());
187       if (jdbcAttribute == null) {
188         // This is not an attribute on the cpo bean passed to the retrieveBeans method.
189         // treat it as the column name
190         fullyQualifiedColumn = getAttribute();
191       } else {
192         fullyQualifiedColumn = buildColumnName(jdbcAttribute);
193       }
194 
195       if (getAttributeFunction() != null) {
196         if (jdbcAttribute != null) {
197           sb.append(buildFunction(getAttributeFunction(), jdbcAttribute.getJavaName(), fullyQualifiedColumn));
198         } else {
199           sb.append(getAttributeFunction());
200         }
201       } else {
202         sb.append(fullyQualifiedColumn);
203       }
204     }
205 
206     if (getComparison() != CpoWhere.COMP_NONE) {
207       sb.append(" ");
208       sb.append(comparisons[getComparison()]);
209     }
210 
211     if (getComparison() != CpoWhere.COMP_ISNULL && (getValue() != null || getRightAttribute() != null || getStaticValue() != null)) {
212       sb.append(" ");
213 
214       if (getValue() != null) {
215         if (getValueFunction() != null) {
216           if (jdbcAttribute == null) {
217             jdbcAttribute = (JdbcCpoAttribute) cpoClass.getAttributeJava(getRightAttribute());
218           }
219           sb.append(buildFunction(getValueFunction(), getAttributeName(jdbcAttribute, getAttribute(), getRightAttribute()), "?"));
220         } else if (getComparison() == CpoWhere.COMP_IN && getValue() instanceof Collection) {
221           Collection coll = (Collection) getValue();
222           sb.append("(");
223           if (coll.size() > 0) {
224             sb.append("?"); // add the parameter, we will bind it later.
225             for (int i = 1; i < coll.size(); i++) {
226               sb.append(", ?"); // add the parameter, we will bind it later.
227             }
228           }
229           sb.append(")");
230         } else {
231           sb.append("?"); // add the parameter, we will bind it later.
232         }
233       } else if (getRightAttribute() != null) {
234         jdbcAttribute = (JdbcCpoAttribute) cpoClass.getAttributeJava(getRightAttribute());
235         String fullyQualifiedColumn;
236         if (jdbcAttribute == null) {
237           fullyQualifiedColumn = getRightAttribute();
238         } else {
239           fullyQualifiedColumn = buildColumnName(jdbcAttribute);
240         }
241 
242         if (getRightAttributeFunction() != null) {
243           sb.append(buildFunction(getRightAttributeFunction(), getAttributeName(jdbcAttribute, getAttribute(), getRightAttribute()), fullyQualifiedColumn));
244         } else {
245           sb.append(fullyQualifiedColumn);
246         }
247       } else if (getStaticValue() != null) {
248         sb.append(getStaticValue());
249       }
250     }
251     return sb.toString();
252   }
253 
254   private String getAttributeName(CpoAttribute jdbcAttribute, String leftAttribute, String rightAttribute) {
255     String attrName = null;
256 
257     if (jdbcAttribute != null) {
258       attrName = jdbcAttribute.getJavaName();
259     }
260 
261     if (attrName == null && leftAttribute != null) {
262       attrName = leftAttribute;
263     }
264 
265     if (attrName == null && rightAttribute != null) {
266       attrName = rightAttribute;
267     }
268 
269     return attrName;
270   }
271 
272   @Override
273   public void addWhere(CpoWhere cw) throws CpoException {
274     try {
275       this.addChild((Node) cw);
276     } catch (ChildNodeException cne) {
277       throw new CpoException("Error Adding Where Statement");
278     }
279   }
280 
281   @Override
282   public void setAttributeFunction(String s) {
283     this.attributeFunction = s;
284   }
285 
286   @Override
287   public String getAttributeFunction() {
288     return this.attributeFunction;
289   }
290 
291   @Override
292   public void setValueFunction(String s) {
293     this.valueFunction = s;
294   }
295 
296   @Override
297   public String getValueFunction() {
298     return this.valueFunction;
299   }
300 
301   @Override
302   public void setRightAttributeFunction(String s) {
303     this.rightAttributeFunction = s;
304   }
305 
306   @Override
307   public String getRightAttributeFunction() {
308     return this.rightAttributeFunction;
309   }
310 
311   private String buildFunction(String function, String match, String value) {
312     StringBuilder sb = new StringBuilder();
313     int attrOffset;
314     int fromIndex = 0;
315 
316     if (function != null && function.length() > 0) {
317       while ((attrOffset = function.indexOf(match, fromIndex)) != -1) {
318         sb.append(function.substring(0, attrOffset));
319         sb.append(value);
320         fromIndex += attrOffset + match.length();
321       }
322       sb.append(function.substring(fromIndex));
323     }
324 
325     return sb.toString();
326   }
327 
328   private String buildColumnName(JdbcCpoAttribute attribute) {
329     StringBuilder columnName = new StringBuilder();
330 
331     if (attribute.getDbTable() != null) {
332       columnName.append(attribute.getDbTable());
333       columnName.append(".");
334     }
335     if (attribute.getDbColumn() != null) {
336       columnName.append(attribute.getDbColumn());
337     } else {
338       columnName.append(attribute.getDataName());
339     }
340 
341     return columnName.toString();
342   }
343 
344   @Override
345   public String getName() {
346     return name;
347   }
348 
349   @Override
350   public void setName(String name) {
351     this.name = name;
352   }
353 }