1 23 package org.apache.slide.store.impl.rdbms.expression; 24 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 29 import org.apache.slide.common.SlideException; 30 import org.apache.slide.content.NodeProperty.NamespaceCache; 31 import org.apache.slide.search.BadQueryException; 32 import org.apache.slide.search.InvalidQueryException; 33 import org.apache.slide.search.PropertyProvider; 34 import org.apache.slide.search.basic.BasicExpressionFactory; 35 import org.apache.slide.search.basic.ComparableResourcesPool; 36 import org.apache.slide.search.basic.IBasicExpression; 37 import org.apache.slide.search.basic.IBasicQuery; 38 import org.apache.slide.search.basic.Literals; 39 import org.apache.slide.store.impl.rdbms.AbstractRDBMSStore; 40 import org.apache.slide.store.impl.rdbms.RDBMSComparableResourcesPool; 41 import org.jdom.Element; 42 43 47 public class RDBMSExpressionFactory extends BasicExpressionFactory { 48 49 private final AbstractRDBMSStore _store; 50 private final RDBMSQueryContext _context; 51 52 public RDBMSExpressionFactory(AbstractRDBMSStore store) { 53 _store = store; 54 _context = new RDBMSQueryContext(new RDBMSResultSet(this)); 55 } 56 57 public void init(IBasicQuery query, PropertyProvider propertyProvider) 58 throws BadQueryException { 59 super.init(query, propertyProvider); 60 } 61 62 public IBasicExpression createMergeExpression(String name, String namespace, Collection members) 63 throws BadQueryException { 64 RDBMSExpression result = null; 65 if (name == null) { 66 result = new RDBMSNOPExpression(_context); 67 } 68 else { 69 ArrayList otherExpressions = null; 70 ArrayList rdbmsExpressions = null; 71 final Iterator iter = members.iterator(); 72 while (iter.hasNext()) { 73 final IBasicExpression expression = (IBasicExpression) iter.next(); 74 if (expression instanceof RDBMSExpression) { 75 if (rdbmsExpressions == null) { 76 rdbmsExpressions = new ArrayList (members.size()); 77 } 78 rdbmsExpressions.add(expression); 79 } 80 else { 81 if (otherExpressions == null) { 82 otherExpressions = new ArrayList (members.size()); 83 } 84 otherExpressions.add(expression); 85 } 86 } 87 result = new RDBMSMergeExpression(name, namespace, _context, rdbmsExpressions, otherExpressions); 88 } 89 result.setFactory(this); 90 return result; 91 } 92 93 IBasicExpression createStandardMergeExpression(String name, String namespace, Collection members) 94 throws BadQueryException { 95 return super.createMergeExpression(name, namespace, members); 96 } 97 98 public IBasicExpression createExpression(Element element) throws BadQueryException { 99 IBasicExpression result = null; 100 if (element == null) { 101 result = new RDBMSNOPExpression(_context); 102 } 103 else { 104 final String namespace = element.getNamespace().getURI(); 105 final String name = element.getName(); 106 if (namespace.equals(NamespaceCache.DEFAULT_URI)) { 107 result = createDAVExpression(element); 108 } 109 else if (namespace.equals(NamespaceCache.SLIDE_URI)) { 110 result = createSlideExpression(element); 111 } 112 else { 113 throw new InvalidQueryException 114 ("operator <" + namespace + ":" + name + "> is an unprocessable entity"); 115 116 } 117 } 118 result.setFactory(this); 119 return result; 120 } 121 122 protected IBasicExpression createDAVExpression(Element element) throws BadQueryException { 123 if (isSQLCompilableProperty(element)) { 124 if (RDBMSCompareExpression.COMPARE_OPERATORS.containsKey(element.getName())) { 125 return new RDBMSCompareExpression(element, _context); 126 } 127 else if (element.getName().equals(Literals.ISDEFINED)) { 128 return new RDBMSIsDefinedExpression(element, _context); 129 } 130 else if (element.getName().equals(Literals.NOT_ISDEFINED)) { 131 return new RDBMSNotIsDefinedExpression(element, _context); 132 } 133 } 134 else if (element.getName().equals(Literals.ISCOLLECTION)) { 135 return new RDBMSIsCollectionExpression(element, _context); 136 } 137 else if (element.getName().equals(Literals.NOT_ISCOLLECTION)) { 138 return new RDBMSNotIsCollectionExpression(element, _context); 139 } 140 return super.createExpression(element); 141 } 142 143 protected IBasicExpression createSlideExpression(Element element) throws BadQueryException { 144 if (isSQLCompilableProperty(element)) { 145 if (element.getName().equals(Literals.PROPCONTAINS)) { 146 return new RDBMSPropContainsExpression(element, _context); 147 } 148 else if (element.getName().equals(Literals.NOT_PROPCONTAINS)) { 149 return new RDBMSNotPropContainsExpression(element, _context); 150 } 151 } 152 if (element.getName().equals(Literals.ISPRINCIPAL)) { 153 return new RDBMSIsPrincipalExpression(element, _context); 154 } 155 else if (element.getName().equals(Literals.NOT_ISPRINCIPAL)) { 156 return new RDBMSNotIsPrincipalExpression(element, _context); 157 } 158 return super.createExpression(element); 159 } 160 161 protected ComparableResourcesPool getRequestedResourcePool() { 162 if (requestedResourcePool == null) { 163 requestedResourcePool = new RDBMSComparableResourcesPool(_store, _context, getQuery()); 164 } 165 return requestedResourcePool; 166 } 167 168 public boolean isSQLCompilableProperty(Element element) { 169 Element davProp = element.getChild(Literals.PROP, NamespaceCache.DEFAULT_NAMESPACE); 170 if (davProp != null) { 171 Element property = (Element) davProp.getChildren().get(0); 172 return isSQLCompilableProperty(property.getNamespaceURI(), property.getName()); 173 } 174 return false; 175 } 176 177 public boolean isSQLCompilableProperty(String namespace, String name) { 178 if (getPropertyProvider() == null) return false; 179 try { 180 final String uri = getQuery().getSearchToken().getSlideContext() 181 .getSlidePath(getQuery().getScope().getHref()); 182 return !getPropertyProvider().isSupportedProperty(uri, name, namespace); 183 } 184 catch (SlideException e) { 185 e.printStackTrace(); 186 return false; 187 } 188 } 189 190 193 public static String propertyToAlias(String propertyName) { 194 197 StringBuffer buffer = new StringBuffer (propertyName.length() + 5); 198 buffer.append("prop_"); 199 for (int i = 0, l = propertyName.length(); i < l; i++) { 200 char c = propertyName.charAt(i); 201 if (c == '-') c = '_'; 202 buffer.append(c); 203 } 204 return buffer.toString(); 205 } 206 207 } 208 | Popular Tags |