001: /*
002: * $Header$
003: * $Revision: 9073 $
004: * $Date: 2007-11-22 09:04:41 -0800 $
005: *
006: * ====================================================================
007: *
008: * Copyright 1999-2002 The Apache Software Foundation
009: *
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: *
022: */
023:
024: package org.apache.slide.index.lucene;
025:
026: import java.util.Iterator;
027: import java.util.List;
028:
029: import org.apache.lucene.search.Sort;
030: import org.apache.lucene.search.SortField;
031: import org.apache.slide.common.PropertyName;
032: import org.apache.slide.search.InvalidQueryException;
033: import org.apache.slide.search.basic.Literals;
034: import org.apache.slide.search.basic.OrderBy;
035: import org.jdom.Attribute;
036: import org.jdom.Element;
037: import org.jdom.Namespace;
038:
039: /**
040: * Encapsulate an OrderBy expression. Also supplies a Comparator according
041: * to the specified parameters in the OrderBy expression,
042: *
043: * @version $Revision: 9073 $
044: */
045: public class LuceneOrderBy extends OrderBy {
046:
047: private Sort theSorter = null;
048:
049: //private Index index = null;
050:
051: public LuceneOrderBy() {
052: super ();
053: //this.index = index;
054: }
055:
056: /**
057: * initializes an OrderBy.
058: *
059: * @param orderByElement the JDOM element containing the
060: * orderBy expression
061: */
062: public void init(Element orderByElement)
063: throws InvalidQueryException {
064:
065: super .init(orderByElement);
066: orderByElements.clear();
067:
068: Namespace nameSpace = orderByElement.getNamespace();
069: Iterator it = orderByElement.getChildren(Literals.ORDER,
070: nameSpace).iterator();
071:
072: while (it.hasNext()) {
073: Element order = (Element) it.next();
074: PropertyName p = getProperty(order);
075: boolean isAscending = isAscending(order);
076:
077: // WE DON'T SUPPORT CASE SENSITIVITY PER REQUEST/ORDER BY
078: if (p.getName().equals("score")
079: && p.getNamespace().equals("DAV:")) {
080: orderByElements.add(SortField.FIELD_SCORE);
081: } else {
082: orderByElements.add(new SortField(IndexConfiguration
083: .generateFieldName(p.getNamespace(), p
084: .getName()), SortField.STRING,
085: !isAscending));
086: }
087:
088: }
089: if (orderByElements.size() > 0)
090: theSorter = new Sort((SortField[]) orderByElements
091: .toArray(new SortField[0]));
092: }
093:
094: public Sort getSorter() {
095: return theSorter;
096: }
097:
098: /**
099: * Method isCaseSensitive
100: *
101: * @param order an order Element
102: *
103: * @return true if this order element shall regard case
104: *
105: */
106: private boolean isCaseSensitive(Element order) {
107: boolean result = true;
108: Attribute caseSens = order.getAttribute(Literals.CASESENSITIVE,
109: order.getNamespace());
110:
111: if (caseSens != null) {
112: try {
113: result = caseSens.getBooleanValue();
114: } catch (org.jdom.DataConversionException e) {
115: e.printStackTrace();
116: }
117: }
118: return result;
119: }
120:
121: /**
122: * Method isAscending
123: *
124: * @param order an Element
125: *
126: * @return a boolean
127: *
128: * @throws InvalidQueryException if ascending and descending is supplied
129: *
130: */
131: private boolean isAscending(Element order)
132: throws InvalidQueryException {
133: Element asc = order.getChild(Literals.ASCENDING, order
134: .getNamespace());
135: Element desc = order.getChild(Literals.DESCENDING, order
136: .getNamespace());
137: boolean result = true;
138:
139: if (asc != null && desc != null)
140: throw new InvalidQueryException(
141: "either ascending or descending may be supplied");
142:
143: if (desc != null)
144: result = false;
145:
146: return result;
147: }
148:
149: /**
150: * Method getPropName
151: *
152: * @param order an Element
153: *
154: * @return a String
155: *
156: * @throws InvalidQueryException
157: *
158: */
159: private PropertyName getProperty(Element orderElem)
160: throws InvalidQueryException {
161:
162: List propList = orderElem.getChild(Literals.PROP,
163: orderElem.getNamespace()).getChildren();
164:
165: if (propList.size() != 1)
166: throw new InvalidQueryException(
167: "Expected exactly 1 prop element, found "
168: + propList.size());
169:
170: Element propElem = (Element) propList.get(0);
171:
172: return createProperty(propElem);
173: }
174:
175: }
|