KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > impl > rdbms > expression > RDBMSExpressionFactory


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/expression/RDBMSExpressionFactory.java,v 1.9.2.6 2004/11/24 09:42:27 gcasper Exp $
3  * $Revision: 1.9.2.6 $
4  * $Date: 2004/11/24 09:42:27 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23 package org.apache.slide.store.impl.rdbms.expression;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
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 /**
44  * DASL Basic search expression factory that compiles query expressions
45  * to native SQL queries.
46  */

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 JavaDoc name, String JavaDoc namespace, Collection JavaDoc members)
63     throws BadQueryException {
64         RDBMSExpression result = null;
65         if (name == null) {
66             result = new RDBMSNOPExpression(_context);
67         }
68         else {
69             ArrayList JavaDoc otherExpressions = null;
70             ArrayList JavaDoc rdbmsExpressions = null;
71             final Iterator JavaDoc 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 JavaDoc(members.size());
77                     }
78                     rdbmsExpressions.add(expression);
79                 }
80                 else {
81                     if (otherExpressions == null) {
82                         otherExpressions = new ArrayList JavaDoc(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 JavaDoc name, String JavaDoc namespace, Collection JavaDoc 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 JavaDoc namespace = element.getNamespace().getURI();
105             final String JavaDoc 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 JavaDoc namespace, String JavaDoc name) {
178         if (getPropertyProvider() == null) return false;
179         try {
180             final String JavaDoc 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     /**
191      * converts a property name to a legal SQL alias.
192      */

193     public static String JavaDoc propertyToAlias(String JavaDoc propertyName) {
194         // replaceAll occurs in JDK1.4
195
//return "prop_" + propertyName.replaceAll("-", "_");
196

197         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(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