001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.db;
007:
008: import com.sun.portal.search.rdm.*;
009: import com.sun.portal.search.soif.*;
010: import com.sun.portal.search.util.*;
011:
012: import javax.naming.*;
013: import javax.naming.directory.*;
014:
015: import java.util.*;
016:
017: /**
018: *
019: * JNDI search result set implementation using RDMResultSet
020: *
021: */
022: public class JNDIResultSet extends RDMResultSet {
023:
024: private List hits;
025:
026: int startHit;
027: int endHit;
028: long hitCount;
029: long docCount;
030: int resultCount;
031:
032: /**
033: * Creates a new instance of JNDIResultSet
034: */
035: JNDIResultSet(SToken st, RDMDb database, String query,
036: RDMTransaction t) {
037: super (st, database, query, t);
038: }
039:
040: /**
041: * Prepare JNDI Search Results
042: */
043: void setJNDISearchResult(NamingEnumeration ne) throws RDMException {
044: startHit = 0;
045: endHit = 0;
046: hitCount = 0;
047: resultCount = 0;
048: docCount = 0;
049:
050: hits = new ArrayList();
051: try {
052: while (ne.hasMoreElements()) {
053: Object o = ne.next();
054: hits.add(o);
055: }
056: } catch (Exception e) {
057: throw new RDMException(e);
058: }
059: int i = hits.size();
060: if (i > 0) {
061: startHit = 1;
062: endHit = i;
063: hitCount = i;
064: resultCount = i;
065: docCount = i;
066: }
067:
068: }
069:
070: /**
071: * Set default attributes
072: */
073: static Set defaultView = new HashSet();
074: static {
075: defaultView.add("hl-description");
076: defaultView.add("hl-title");
077: defaultView.add("url");
078: defaultView.add("content-length");
079: }
080:
081: /**
082: * Retrieve Search Results
083: * @param i Document index
084: * @param view Attributes
085: * @param hltags Highlight tags
086: * @return a SOIF hit, highlighted and filtered by view
087: */
088: public SOIF getResult(int i, Set view, String[] hltags) {
089:
090: SearchResult sr = (SearchResult) hits.get(i);
091: Attributes sra = sr.getAttributes();
092: SOIF s = null;
093:
094: try {
095: s = new SOIF("DOCUMENT", sr.getName());
096: NamingEnumeration avs = sra.getAll();
097: if (avs != null) {
098: while (avs.hasMore()) {
099: Attribute av = (Attribute) avs.next();
100: String name = av.getID();
101: NamingEnumeration n = (NamingEnumeration) av
102: .getAll();
103: int j = 0;
104: while (n.hasMore()) {
105: Object o = n.next();
106: if (o != null && o instanceof String) {
107: s.insert(name, (String) o, j++);
108: }
109: }
110: }
111: }
112:
113: if (view != null && view.contains("score")) {
114: s.insert("score", "100");
115: }
116:
117: } catch (NamingException ne) {
118: //do nothing
119: }
120: return s;
121: }
122:
123: /**
124: * Get hit count
125: * @return total number of hits for this search
126: */
127: public long getHitCount() {
128: return hitCount;
129: }
130:
131: /**
132: * Get document count
133: * @return number of docs searched
134: */
135: public long getDocCount() {
136: return hits.size();
137: }
138:
139: /**
140: * Transform the result set to string
141: * @return tring representation of this result set
142: */
143: public java.lang.String toString() {
144: return super .toString();
145: }
146:
147: /**
148: * Get search result count
149: * @return current number of hits in this set
150: */
151: public int getResultCount() {
152: return resultCount;
153: }
154:
155: }
|