Coverage report

  %line %branch
org.apache.portals.graffito.jcr.query.impl.FilterImpl
0% 
0% 

 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.ArrayList;
 22  
 import java.util.Iterator;
 23  
 import java.util.Map;
 24  
 import java.util.List;
 25  
 
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
 29  
 import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter;
 30  
 import org.apache.portals.graffito.jcr.query.Filter;
 31  
 
 32  
 /**
 33  
  * {@link org.apache.portals.graffito.jcr.query.Filter}
 34  
  *
 35  
  * @author <a href="mailto:christophe.lombart@sword-technologies.com">Christophe Lombart</a>
 36  
  * @author <a href="mailto:the_mindstorm[at]evolva[dot]ro">Alex Popescu</a>
 37  
  */
 38  
 public class FilterImpl implements Filter {
 39  0
     private final static Log log = LogFactory.getLog(FilterImpl.class);
 40  
 
 41  
     private Class claszz;
 42  0
     private String scope = "";
 43  0
     private List jcrExpressions = new ArrayList();
 44  
 
 45  
     private ClassDescriptor classDescriptor;
 46  
     private Map atomicTypeConverters;
 47  
 
 48  
     private String orJcrExpression;
 49  
 
 50  
     /**
 51  
      * Constructor
 52  
      *
 53  
      * @param classDescriptor
 54  
      * @param atomicTypeConverters
 55  
      * @param clazz
 56  
      */
 57  0
     public FilterImpl(ClassDescriptor classDescriptor, Map atomicTypeConverters, Class clazz) {
 58  0
         this.claszz = clazz;
 59  0
         this.atomicTypeConverters = atomicTypeConverters;
 60  0
         this.classDescriptor = classDescriptor;
 61  0
     }
 62  
 
 63  
     /**
 64  
      *
 65  
      * @see org.apache.portals.graffito.jcr.query.Filter#getFilterClass()
 66  
      */
 67  
     public Class getFilterClass() {
 68  0
         return claszz;
 69  
     }
 70  
 
 71  
     /**
 72  
      * @see org.apache.portals.graffito.jcr.query.Filter#setScope(java.lang.String)
 73  
      */
 74  
     public void setScope(String scope) {
 75  0
         this.scope = scope;
 76  0
     }
 77  
 
 78  
     /**
 79  
      * @see org.apache.portals.graffito.jcr.query.Filter#getScope()
 80  
      */
 81  
     public String getScope() {
 82  0
         return this.scope;
 83  
     }
 84  
 
 85  
     /**
 86  
      * @see org.apache.portals.graffito.jcr.query.Filter#addContains(java.lang.String, java.lang.String)
 87  
      */
 88  
     public Filter addContains(String scope, String fullTextSearch) {
 89  0
         String jcrExpression = null;
 90  0
         if (scope.equals(".")) {
 91  0
             jcrExpression = "jcr:contains(., '" + fullTextSearch + "')";
 92  
         }
 93  
         else {
 94  0
             jcrExpression = "jcr:contains(@" + this.getJcrFieldName(scope) + ", '" + fullTextSearch
 95  
                 + "')";
 96  
         }
 97  
 
 98  0
         jcrExpressions.add(jcrExpression);
 99  
 
 100  0
         return this;
 101  
     }
 102  
 
 103  
     /**
 104  
      * @see org.apache.portals.graffito.jcr.query.Filter#addBetween(java.lang.String, java.lang.Object, java.lang.Object)
 105  
      */
 106  
     public Filter addBetween(String fieldAttributeName, Object value1, Object value2) {
 107  0
         String jcrExpression = "( @" + this.getJcrFieldName(fieldAttributeName) + " >= "
 108  
             + this.getStringValue(value1)
 109  
             + " and @" + this.getJcrFieldName(fieldAttributeName) + " <= "
 110  
             + this.getStringValue(value2) + ")";
 111  
 
 112  0
         jcrExpressions.add(jcrExpression);
 113  
 
 114  0
         return this;
 115  
     }
 116  
 
 117  
     /**
 118  
      * @see org.apache.portals.graffito.jcr.query.Filter#addEqualTo(java.lang.String, java.lang.Object)
 119  
      */
 120  
     public Filter addEqualTo(String fieldAttributeName, Object value) {
 121  0
         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " = "
 122  
             + this.getStringValue(value);
 123  0
         jcrExpressions.add(jcrExpression);
 124  
 
 125  0
         return this;
 126  
     }
 127  
 
 128  
     /**
 129  
      * @see org.apache.portals.graffito.jcr.query.Filter#addGreaterOrEqualThan(java.lang.String, java.lang.Object)
 130  
      */
 131  
     public Filter addGreaterOrEqualThan(String fieldAttributeName, Object value) {
 132  0
         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " >= "
 133  
             + this.getStringValue(value);
 134  0
         jcrExpressions.add(jcrExpression);
 135  
 
 136  0
         return this;
 137  
     }
 138  
 
 139  
     /**
 140  
      * @see org.apache.portals.graffito.jcr.query.Filter#addGreaterThan(java.lang.String, java.lang.Object)
 141  
      */
 142  
     public Filter addGreaterThan(String fieldAttributeName, Object value) {
 143  0
         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " > "
 144  
             + this.getStringValue(value);
 145  0
         jcrExpressions.add(jcrExpression);
 146  
 
 147  0
         return this;
 148  
     }
 149  
 
 150  
     /**
 151  
      * @see org.apache.portals.graffito.jcr.query.Filter#addLessOrEqualThan(java.lang.String, java.lang.Object)
 152  
      */
 153  
     public Filter addLessOrEqualThan(String fieldAttributeName, Object value) {
 154  0
         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " <= "
 155  
             + this.getStringValue(value);
 156  0
         jcrExpressions.add(jcrExpression);
 157  
 
 158  0
         return this;
 159  
     }
 160  
 
 161  
     /**
 162  
      * @see org.apache.portals.graffito.jcr.query.Filter#addLessOrEqualThan(java.lang.String, java.lang.Object)
 163  
      */
 164  
     public Filter addLessThan(String fieldAttributeName, Object value) {
 165  0
         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " < "
 166  
             + this.getStringValue(value);
 167  0
         jcrExpressions.add(jcrExpression);
 168  
 
 169  0
         return this;
 170  
     }
 171  
 
 172  
     /**
 173  
      * @see org.apache.portals.graffito.jcr.query.Filter#addLike(java.lang.String, java.lang.Object)
 174  
      */
 175  
     public Filter addLike(String fieldAttributeName, Object value) {
 176  0
         String jcrExpression = "jcr:like(" + "@" + this.getJcrFieldName(fieldAttributeName) + ", '"
 177  
             + value + "')";
 178  0
         jcrExpressions.add(jcrExpression);
 179  
 
 180  0
         return this;
 181  
     }
 182  
 
 183  
     /**
 184  
      * @see org.apache.portals.graffito.jcr.query.Filter#addNotEqualTo(java.lang.String, java.lang.Object)
 185  
      */
 186  
     public Filter addNotEqualTo(String fieldAttributeName, Object value) {
 187  0
         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName) + " != "
 188  
             + this.getStringValue(value);
 189  0
         jcrExpressions.add(jcrExpression);
 190  
 
 191  0
         return this;
 192  
     }
 193  
 
 194  
     /**
 195  
      * @see org.apache.portals.graffito.jcr.query.Filter#addNotNull(java.lang.String)
 196  
      */
 197  
     public Filter addNotNull(String fieldAttributeName) {
 198  0
         String jcrExpression = "@" + this.getJcrFieldName(fieldAttributeName);
 199  0
         jcrExpressions.add(jcrExpression);
 200  
 
 201  0
         return this;
 202  
     }
 203  
 
 204  
     /**
 205  
      * @see org.apache.portals.graffito.jcr.query.Filter#addIsNull(java.lang.String)
 206  
      */
 207  
     public Filter addIsNull(String fieldAttributeName) {
 208  0
         String jcrExpression = "not(@" + this.getJcrFieldName(fieldAttributeName) + ")";
 209  0
         jcrExpressions.add(jcrExpression);
 210  
 
 211  0
         return this;
 212  
     }
 213  
 
 214  
     /**
 215  
      * @see org.apache.portals.graffito.jcr.query.Filter#addOrFilter(org.apache.portals.graffito.jcr.query.Filter)
 216  
      */
 217  
     public Filter addOrFilter(Filter filter) {
 218  0
         orJcrExpression = ((FilterImpl) filter).getJcrExpression();
 219  
 
 220  0
         return this;
 221  
     }
 222  
 
 223  
     public Filter addJCRExpression(String jcrExpression) {
 224  0
         jcrExpressions.add(jcrExpression);
 225  
 
 226  0
         return this;
 227  
     }
 228  
 
 229  
     private String getJcrFieldName(String fieldAttribute) {
 230  0
         String jcrFieldName = classDescriptor.getJcrName(fieldAttribute);
 231  0
         if (jcrFieldName == null) {
 232  0
             log.error("Impossible to find the jcrFieldName for the attribute :" + fieldAttribute);
 233  
         }
 234  
 
 235  0
         return jcrFieldName;
 236  
 
 237  
     }
 238  
 
 239  
     private String getStringValue(Object value) {
 240  0
         AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters.get(
 241  
                 value.getClass());
 242  
 
 243  0
         return atomicTypeConverter.getStringValue(value);
 244  
     }
 245  
 
 246  
     public String getJcrExpression() {
 247  0
         if ((orJcrExpression == null) || orJcrExpression.equals("")) {
 248  0
             return buildJcrExpression();
 249  
         }
 250  
         else {
 251  0
             return "(" + buildJcrExpression() + ") or (" + this.orJcrExpression + ")";
 252  
         }
 253  
     }
 254  
 
 255  
     private String buildJcrExpression() {
 256  0
         int count = 1;
 257  0
         String jcrExp = "";
 258  
 
 259  0
         Iterator criteriaIterator = jcrExpressions.iterator();
 260  0
         while (criteriaIterator.hasNext()) {
 261  0
             if (count > 1) {
 262  0
                 jcrExp += " and ";
 263  
             }
 264  0
             jcrExp += (String) criteriaIterator.next();
 265  0
             count++;
 266  
 
 267  
         }
 268  
 
 269  0
         return jcrExp;
 270  
     }
 271  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.