001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.impl;
018:
019: import org.compass.core.CompassException;
020: import org.compass.core.CompassHits;
021: import org.compass.core.CompassQuery;
022: import org.compass.core.CompassQueryFilter;
023: import org.compass.core.engine.SearchEngineHits;
024: import org.compass.core.engine.SearchEngineQuery;
025: import org.compass.core.engine.SearchEngineQuery.SearchEngineSpanQuery;
026: import org.compass.core.mapping.ResourceMapping;
027: import org.compass.core.mapping.ResourcePropertyLookup;
028: import org.compass.core.spi.InternalCompassSession;
029:
030: /**
031: * @author kimchy
032: */
033: public class DefaultCompassQuery implements CompassQuery, Cloneable {
034:
035: public static class DefaultCompassSpanQuey extends
036: DefaultCompassQuery implements CompassSpanQuery {
037:
038: private SearchEngineSpanQuery spanQuery;
039:
040: public DefaultCompassSpanQuey(
041: SearchEngineSpanQuery searchEngineQuery,
042: InternalCompassSession session) {
043: super (searchEngineQuery, session);
044: this .spanQuery = searchEngineQuery;
045: }
046:
047: public SearchEngineSpanQuery getSearchEngineSpanQuery() {
048: return spanQuery;
049: }
050: }
051:
052: private SearchEngineQuery searchEngineQuery;
053:
054: private InternalCompassSession session;
055:
056: private CompassQueryFilter filter;
057:
058: public DefaultCompassQuery(SearchEngineQuery searchEngineQuery,
059: InternalCompassSession session) {
060: this .searchEngineQuery = searchEngineQuery;
061: this .session = session;
062: }
063:
064: public CompassQuery setBoost(float boost) {
065: searchEngineQuery.setBoost(boost);
066: return this ;
067: }
068:
069: public CompassQuery addSort(String name) {
070: ResourcePropertyLookup lookup = session.getMapping()
071: .getResourcePropertyLookup(name);
072: searchEngineQuery.addSort(lookup.getPath());
073: return this ;
074: }
075:
076: public CompassQuery addSort(String name, SortDirection direction) {
077: ResourcePropertyLookup lookup = session.getMapping()
078: .getResourcePropertyLookup(name);
079: searchEngineQuery.addSort(lookup.getPath(), direction);
080: return this ;
081: }
082:
083: public CompassQuery addSort(String name, SortPropertyType type) {
084: ResourcePropertyLookup lookup = session.getMapping()
085: .getResourcePropertyLookup(name);
086: searchEngineQuery.addSort(lookup.getPath(), type);
087: return this ;
088: }
089:
090: public CompassQuery addSort(String name, SortPropertyType type,
091: SortDirection direction) {
092: ResourcePropertyLookup lookup = session.getMapping()
093: .getResourcePropertyLookup(name);
094: searchEngineQuery.addSort(lookup.getPath(), type, direction);
095: return this ;
096: }
097:
098: public CompassQuery addSort(SortImplicitType implicitType) {
099: searchEngineQuery.addSort(implicitType);
100: return this ;
101: }
102:
103: public CompassQuery addSort(SortImplicitType implicitType,
104: SortDirection direction) {
105: searchEngineQuery.addSort(implicitType, direction);
106: return this ;
107: }
108:
109: public CompassQuery setSubIndexes(String... subIndexes) {
110: searchEngineQuery.setSubIndexes(subIndexes);
111: return this ;
112: }
113:
114: public CompassQuery setAliases(String... aliases) {
115: searchEngineQuery.setAliases(aliases);
116: return this ;
117: }
118:
119: public CompassQuery setTypes(Class... types) {
120: if (types == null) {
121: searchEngineQuery.setAliases(null);
122: return this ;
123: }
124: String[] aliases = new String[types.length];
125: for (int i = 0; i < types.length; i++) {
126: ResourceMapping resourceMapping = session.getMapping()
127: .getRootMappingByClass(types[i]);
128: aliases[i] = resourceMapping.getAlias();
129: }
130: setAliases(aliases);
131: return this ;
132: }
133:
134: public CompassQuery setFilter(CompassQueryFilter filter) {
135: this .filter = filter;
136: searchEngineQuery
137: .setFilter(((DefaultCompassQueryFilter) filter)
138: .getFilter());
139: return this ;
140: }
141:
142: public CompassQueryFilter getFilter() {
143: return this .filter;
144: }
145:
146: public CompassQuery rewrite() {
147: searchEngineQuery.rewrite();
148: return this ;
149: }
150:
151: public CompassQuery getSuggestedQuery() {
152: if (session.getCompass().getSpellCheckManager() == null) {
153: return this ;
154: }
155: return session.getCompass().getSpellCheckManager()
156: .suggest(this );
157: }
158:
159: public boolean isSuggested() {
160: return searchEngineQuery.isSuggested();
161: }
162:
163: public long count() {
164: return searchEngineQuery.count();
165: }
166:
167: public CompassHits hits() throws CompassException {
168: SearchEngineHits searchEngineHits = searchEngineQuery.hits();
169: return new DefaultCompassHits(searchEngineHits, session, this );
170: }
171:
172: public SearchEngineQuery getSearchEngineQuery() {
173: return searchEngineQuery;
174: }
175:
176: public String toString() {
177: return searchEngineQuery.toString();
178: }
179:
180: public Object clone() throws CloneNotSupportedException {
181: DefaultCompassQuery clone = (DefaultCompassQuery) super .clone();
182: clone.searchEngineQuery = (SearchEngineQuery) searchEngineQuery
183: .clone();
184: return clone;
185: }
186: }
|