001: /*
002: * Copyright 2001 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.admin;
007:
008: import com.sun.portal.search.rdm.*;
009: import com.sun.portal.search.rdmserver.*;
010: import com.sun.portal.search.soif.*;
011: import com.sun.portal.search.util.*;
012: import com.sun.portal.search.db.SToken;
013: import com.sun.portal.log.common.PortalLogger;
014:
015: import java.io.*;
016: import java.util.*;
017: import java.util.logging.Level;
018: import java.util.logging.Logger;
019: import java.text.*;
020: import java.net.*;
021:
022: /**
023: * RDSearch:
024: * Server-side glue for RDEditor Search
025: */
026: public class RDSearch {
027:
028: public int RDM_Results = 0;
029: public int RDM_Hits = 0;
030: public int RDM_Documents = 0;
031:
032: // Create a Logger for this class
033: private static Logger debugLogger = PortalLogger
034: .getLogger(RDSearch.class);
035:
036: public RDSearch() {
037: }
038:
039: /* public ArrayList search(String queryString, int searchBy, int startHits, int viewHits)
040: throws Exception {
041: return search(null, queryString, searchBy, startHits, viewHits, null);
042: }*/
043: public ArrayList search(String database, String queryString,
044: int searchBy, int startHits, int viewHits) throws Exception {
045: return search(database, queryString, searchBy, startHits,
046: viewHits, null);
047: }
048:
049: /*
050: public ArrayList search(String queryString, int searchBy, int startHits, int viewHits, SToken st)
051: throws Exception {
052: return search(null, queryString, searchBy, startHits, viewHits, st, null);
053: }
054: */
055: public ArrayList search(String database, String queryString,
056: int searchBy, int startHits, int viewHits, SToken st)
057: throws Exception {
058: return search(database, queryString, searchBy, startHits,
059: viewHits, st, null);
060: }
061:
062: public ArrayList search(String database, String queryString,
063: int searchBy, int startHits, int viewHits, SToken st,
064: String view) throws Exception {
065:
066: // Convert RDM Header information
067: debugLogger.log(Level.FINER, "PSSH_CSPSA0087", new String[] {
068: Integer.toString(searchBy), queryString });
069: String type = "rd-request";
070: String ql = "search";
071: String scope = queryString;
072:
073: switch (searchBy) {
074: case 1:
075: scope = "*";
076: break;
077: case 2:
078: scope = "<not> (CLASSIFICATION!=\"\")";
079: break;
080: case 3:
081: scope = "(Classification!=\"\")";
082: break;
083: case 4:
084: scope = "(Classification=\"" + queryString + "\")";
085: break;
086: case 5:
087: ql = "url";
088: break;
089: case 6:
090: break;
091: }
092: RDMHeader hdr = null;
093: try {
094: hdr = new RDMHeader(type, null, ql, null);
095: } catch (Exception e) {
096: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
097: .getMessage());
098: }
099:
100: // Convert RDM Query information
101: String hits = startHits + ".." + (startHits + viewHits - 1);
102:
103: RDMQuery qry = new RDMQuery(scope);
104: qry.setViewHits(hits);
105: if (searchBy != 5) {
106: qry.setViewAttr("url,title");
107: }
108: if (view != null) {
109: qry.setViewAttr(view);
110: }
111:
112: if (database != null) {
113: qry.setDatabase(database);
114: }
115:
116: SOIFBuffer sb = new SOIFBuffer();
117: SOIFOutputStream sos = new SOIFOutputStream(sb);
118: sos.write(hdr.getSOIF());
119: debugLogger.log(Level.FINER, "PSSH_CSPSA0092", hdr.getSOIF());
120: sos.write(qry.getSOIF());
121: debugLogger.log(Level.FINER, "PSSH_CSPSA0093", qry.getSOIF());
122: byte[] ba = RDMUIBridge.rdmui_to_srv(sb.toByteArray(), st);
123: SOIFInputStream sis = new SOIFInputStream(ba);
124:
125: RDMHeader rdmHeader = new RDMHeader(sis);
126: String resp = rdmHeader.getResponseInterpret();
127: try {
128: RDM_Results = getKeyedInt(resp, "result");
129: } catch (Exception e) {
130: }
131:
132: try {
133: RDM_Hits = getKeyedInt(resp, "hit");
134: } catch (Exception e) {
135: }
136:
137: try {
138: RDM_Documents = getKeyedInt(resp, "document");
139: } catch (Exception e) {
140: }
141: debugLogger.log(Level.FINER, "PSSH_CSPSA0094", new String[] {
142: String.valueOf(RDM_Results), String.valueOf(RDM_Hits),
143: String.valueOf(RDM_Documents) });
144: ArrayList rs = new ArrayList();
145: while (!sis.isEOS()) {
146: SOIF ss = sis.readSOIF();
147: if (ss != null) {
148: rs.add(ss);
149: }
150: }
151: debugLogger.log(Level.FINER, "PSSH_CSPSA0095", Integer
152: .toString(rs.size()));
153: return rs;
154: }
155:
156: /**
157: * Parses a keyed int out of a string, ie, "... nnn key ..."
158: * throws NumberFormatException on failure
159: */
160: private int getKeyedInt(String str, String rawkey)
161: throws NumberFormatException {
162: int res = 0;
163: try {
164: int i, n1, n2;
165: String key = " " + rawkey; // need space(s) before key
166: if ((i = str.indexOf(key)) <= 0)
167: throw (new Exception());
168: for (--i; i >= 0 && str.charAt(i) == ' '; --i)
169: ;
170: n2 = i + 1;
171: for (; i >= 0 && str.charAt(i) != ' '; --i)
172: ;
173: n1 = i + 1;
174: String s1 = str.substring(n1, n2);
175: //System.out.println("dbg: " + s1 + " " + n1 + " " + n2);
176: res = Integer.parseInt(s1);
177: } catch (Exception e) {
178: throw new NumberFormatException();
179: }
180: return res;
181: }
182: }
|