View Javadoc

1   /*
2    * Copyright 2004-2005 The Apache Software Foundation or its licensors,
3    *                     as applicable.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.portals.graffito.jcr.query.impl;
19  
20  
21  import java.util.Map;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
26  import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter;
27  import org.apache.portals.graffito.jcr.query.Filter;
28  
29  /***
30   * {@link org.apache.portals.graffito.jcr.query.Filter}
31   *
32   * @author <a href="mailto:christophe.lombart@sword-technologies.com">Christophe Lombart</a>
33   * @author <a href="mailto:the_mindstorm[at]evolva[dot]ro">Alex Popescu</a>
34   */
35  public class FilterImpl implements Filter {
36      private final static Log log = LogFactory.getLog(FilterImpl.class);
37  
38      private Class claszz;
39      private String scope = "";
40      private String jcrExpression = "";
41  
42      private ClassDescriptor classDescriptor;
43      private Map atomicTypeConverters;
44  
45      /***
46       * Constructor
47       *
48       * @param classDescriptor
49       * @param atomicTypeConverters
50       * @param clazz
51       */
52      public FilterImpl(ClassDescriptor classDescriptor, Map atomicTypeConverters, Class clazz) {
53          this.claszz = clazz;
54          this.atomicTypeConverters = atomicTypeConverters;
55          this.classDescriptor = classDescriptor;
56      }
57  
58      /***
59       *
60       * @see org.apache.portals.graffito.jcr.query.Filter#getFilterClass()
61       */
62      public Class getFilterClass() {
63          return claszz;
64      }
65  
66      /***
67       * @see org.apache.portals.graffito.jcr.query.Filter#setScope(java.lang.String)
68       */
69      public void setScope(String scope) {
70          this.scope = scope;
71      }
72  
73      /***
74       * @see org.apache.portals.graffito.jcr.query.Filter#getScope()
75       */
76      public String getScope() {
77          return this.scope;
78      }
79  
80      /***
81       * @see org.apache.portals.graffito.jcr.query.Filter#addContains(java.lang.String, java.lang.String)
82       */
83      public Filter addContains(String scope, String fullTextSearch) {
84          String jcrExpression = null;
85          if (scope.equals(".")) {
86              jcrExpression = "jcr:contains(., '" + fullTextSearch + "')";
87          }
88          else {
89              jcrExpression = "jcr:contains(@" + this.getJcrFieldName(scope) + ", '" + fullTextSearch
90                  + "')";
91          }
92  
93          addExpression(jcrExpression);
94  
95          return this;
96      }
97  
98      /***
99       * @see org.apache.portals.graffito.jcr.query.Filter#addBetween(java.lang.String, java.lang.Object, java.lang.Object)
100      */
101     public Filter addBetween(String fieldAttributeName, Object value1, Object value2) {
102         String jcrExpression = "( @" + this.getJcrFieldName(fieldAttributeName) + " >= "
103             + this.getStringValue(value1)
104             + " and @" + this.getJcrFieldName(fieldAttributeName) + " <= "
105             + this.getStringValue(value2) + ")";
106 
107         addExpression(jcrExpression);
108 
109         return this;
110     }
111 
112     /***
113      * @see org.apache.portals.graffito.jcr.query.Filter#addEqualTo(java.lang.String, java.lang.Object)
114      */
115     public Filter addEqualTo(String fieldAttributeName, Object value) {
116         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " = "
117             + this.getStringValue(value);
118         addExpression(jcrExpression);
119 
120         return this;
121     }
122 
123     /***
124      * @see org.apache.portals.graffito.jcr.query.Filter#addGreaterOrEqualThan(java.lang.String, java.lang.Object)
125      */
126     public Filter addGreaterOrEqualThan(String fieldAttributeName, Object value) {
127         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " >= "
128             + this.getStringValue(value);
129         addExpression(jcrExpression);
130 
131         return this;
132     }
133 
134     /***
135      * @see org.apache.portals.graffito.jcr.query.Filter#addGreaterThan(java.lang.String, java.lang.Object)
136      */
137     public Filter addGreaterThan(String fieldAttributeName, Object value) {
138         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " > "
139             + this.getStringValue(value);
140         addExpression(jcrExpression);
141 
142         return this;
143     }
144 
145     /***
146      * @see org.apache.portals.graffito.jcr.query.Filter#addLessOrEqualThan(java.lang.String, java.lang.Object)
147      */
148     public Filter addLessOrEqualThan(String fieldAttributeName, Object value) {
149         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " <= "
150             + this.getStringValue(value);
151         addExpression(jcrExpression);
152 
153         return this;
154     }
155 
156     /***
157      * @see org.apache.portals.graffito.jcr.query.Filter#addLessOrEqualThan(java.lang.String, java.lang.Object)
158      */
159     public Filter addLessThan(String fieldAttributeName, Object value) {
160         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " < "
161             + this.getStringValue(value);
162         addExpression(jcrExpression);
163 
164         return this;
165     }
166 
167     /***
168      * @see org.apache.portals.graffito.jcr.query.Filter#addLike(java.lang.String, java.lang.Object)
169      */
170     public Filter addLike(String fieldAttributeName, Object value) {
171         String jcrExpression = "jcr:like(" + "@" + this.getJcrFieldName(fieldAttributeName) + ", '"
172             + value + "')";
173         addExpression(jcrExpression);
174 
175         return this;
176     }
177 
178     /***
179      * @see org.apache.portals.graffito.jcr.query.Filter#addNotEqualTo(java.lang.String, java.lang.Object)
180      */
181     public Filter addNotEqualTo(String fieldAttributeName, Object value) {
182         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " != "
183             + this.getStringValue(value);
184         addExpression(jcrExpression);
185 
186         return this;
187     }
188 
189     /***
190      * @see org.apache.portals.graffito.jcr.query.Filter#addNotNull(java.lang.String)
191      */
192     public Filter addNotNull(String fieldAttributeName) {
193         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName);
194         addExpression(jcrExpression);
195 
196         return this;
197     }
198 
199     /***
200      * @see org.apache.portals.graffito.jcr.query.Filter#addIsNull(java.lang.String)
201      */
202     public Filter addIsNull(String fieldAttributeName) {
203         String jcrExpression = "not(@" + this.getJcrFieldName(fieldAttributeName) + ")";
204         addExpression(jcrExpression);
205 
206         return this;
207     }
208 
209     /***
210      * @see org.apache.portals.graffito.jcr.query.Filter#addOrFilter(org.apache.portals.graffito.jcr.query.Filter)
211      */
212     public Filter addOrFilter(Filter filter) {
213     	   if ( null == jcrExpression || "".equals(jcrExpression) )
214     	   {
215     		   jcrExpression =    ((FilterImpl) filter).getJcrExpression() ;    		   
216     	   }
217     	   else
218     	   {
219     	         jcrExpression =   "(" + jcrExpression + ")  or ( "  +  ((FilterImpl) filter).getJcrExpression() + ")";
220     	   }
221         return this;
222     }
223 
224     /***
225      * @see org.apache.portals.graffito.jcr.query.Filter#addAndFilter(Filter)
226      */
227     public Filter addAndFilter(Filter filter) {
228  	   if ( null == jcrExpression || "".equals(jcrExpression) )
229 	   {
230 		   jcrExpression =    ((FilterImpl) filter).getJcrExpression() ;    		   
231 	   }
232 	   else
233 	   {
234 	         jcrExpression =   "(" + jcrExpression + ") and  ( "  +  ((FilterImpl) filter).getJcrExpression() + ")";
235 	   }
236        return this;
237 
238     }
239     
240 
241     public Filter addJCRExpression(String jcrExpression) {
242        addExpression(jcrExpression);
243 
244         return this;
245     }
246 
247     private String getJcrFieldName(String fieldAttribute) {
248         String jcrFieldName = classDescriptor.getJcrName(fieldAttribute);
249         if (jcrFieldName == null) {
250             log.error("Impossible to find the jcrFieldName for the attribute :" + fieldAttribute);
251         }
252 
253         return jcrFieldName;
254 
255     }
256 
257     private String getStringValue(Object value) {
258         AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters.get(
259                 value.getClass());
260 
261         return atomicTypeConverter.getStringValue(value);
262     }
263 
264     public String getJcrExpression() {
265     	     return this.jcrExpression;
266     }
267 
268     private void addExpression(String jcrExpression) {
269             
270     	     if (this.jcrExpression.length() >0) {
271               	this.jcrExpression += " and ";
272         }
273         this.jcrExpression += jcrExpression ;
274     }
275 
276 	public String toString() {
277 		return getJcrExpression();
278 	}
279     
280    
281 }