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 java.util.HashMap;
020:
021: import org.compass.core.CompassDetachedHits;
022: import org.compass.core.CompassException;
023: import org.compass.core.CompassHighlightedText;
024: import org.compass.core.CompassHighlighter;
025: import org.compass.core.CompassHit;
026: import org.compass.core.CompassQuery;
027: import org.compass.core.Resource;
028: import org.compass.core.engine.SearchEngineHits;
029: import org.compass.core.spi.InternalCompassHighlightedText;
030: import org.compass.core.spi.InternalCompassHits;
031: import org.compass.core.spi.InternalCompassSession;
032: import org.compass.core.spi.InternalResource;
033: import org.compass.core.spi.ResourceKey;
034:
035: /**
036: * @author kimchy
037: */
038: public class DefaultCompassHits extends AbstractCompassHits implements
039: InternalCompassHits {
040:
041: private final SearchEngineHits hits;
042:
043: private final InternalCompassSession session;
044:
045: private final CompassQuery query;
046:
047: private HashMap<Integer, InternalCompassHighlightedText> highlightedTextHolder;
048:
049: private CompassQuery suggestedQuery;
050:
051: public DefaultCompassHits(SearchEngineHits hits,
052: InternalCompassSession session, CompassQuery query) {
053: this .hits = hits;
054: this .session = session;
055: this .query = query;
056: }
057:
058: public SearchEngineHits getSearchEngineHits() {
059: return this .hits;
060: }
061:
062: public CompassHit hit(int n) throws CompassException {
063: return new DefaultCompassHit(this , n);
064: }
065:
066: public Object data(int n) throws CompassException {
067: Resource resource = resource(n);
068: return session.getByResource(resource);
069: }
070:
071: public Resource resource(int n) throws CompassException {
072: Resource resource = hits.getResource(n);
073: ResourceKey key = ((InternalResource) resource).resourceKey();
074: Resource cachedResource = session.getFirstLevelCache()
075: .getResource(key);
076: if (cachedResource != null) {
077: return cachedResource;
078: }
079: session.getFirstLevelCache().setResource(key, resource);
080: return resource;
081: }
082:
083: public CompassQuery getQuery() {
084: return this .query;
085: }
086:
087: public CompassQuery getSuggestedQuery() {
088: if (suggestedQuery == null) {
089: suggestedQuery = getQuery().getSuggestedQuery();
090: }
091: return suggestedQuery;
092: }
093:
094: public int getLength() {
095: return hits.getLength();
096: }
097:
098: public float score(int n) throws CompassException {
099: return hits.score(n);
100: }
101:
102: public CompassHighlighter highlighter(int n)
103: throws CompassException {
104: return new DefaultCompassHighlighter(session, this , n);
105: }
106:
107: public CompassDetachedHits detach() throws CompassException {
108: return detach(0, hits.getLength());
109: }
110:
111: public CompassDetachedHits detach(int from, int size)
112: throws CompassException, IllegalArgumentException {
113: return new DefaultCompassDetachedHits(this , session, from,
114: size, getQuery(), getSuggestedQuery());
115: }
116:
117: public CompassHighlightedText highlightedText(int n)
118: throws CompassException {
119: if (highlightedTextHolder == null) {
120: return null;
121: }
122: return highlightedTextHolder.get(new Integer(n));
123: }
124:
125: public void setHighlightedText(int n, String propertyName,
126: String highlihgtedText) {
127: if (highlightedTextHolder == null) {
128: highlightedTextHolder = new HashMap<Integer, InternalCompassHighlightedText>();
129: }
130:
131: InternalCompassHighlightedText hitHighlightedText = highlightedTextHolder
132: .get(n);
133: if (hitHighlightedText == null) {
134: hitHighlightedText = new DefaultCompassHighlightedText();
135: highlightedTextHolder.put(n, hitHighlightedText);
136: }
137:
138: hitHighlightedText.setHighlightedText(propertyName,
139: highlihgtedText);
140: }
141:
142: public void close() throws CompassException {
143: hits.close();
144: }
145: }
|