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.demo;
007:
008: import com.sun.portal.search.soif.*;
009:
010: import java.applet.Applet;
011: import java.awt.Button;
012: import java.awt.FlowLayout;
013: import java.awt.Frame;
014: import java.awt.Panel;
015: import java.awt.TextField;
016: import java.awt.Event;
017: import java.awt.AWTEvent;
018:
019: import java.io.*;
020:
021: /**
022: * Performs a simple search and displays its results.
023: */
024: class SimpleSearch {
025:
026: String RDMServer;
027: String dbname;
028: String SOIFOutputFile;
029:
030: /**
031: * SimpleSearch constructor
032: * @param rdm - the rdm search server, eg, http://portal.siroe.com:2222/mySearch/search
033: */
034: public SimpleSearch(String rdm, String db) {
035: System.out.println("Sun ONE Search Java Demo");
036: RDMServer = rdm;
037: dbname = db;
038: }
039:
040: public void doSearch(String scope) throws IOException {
041:
042: /* The Search class encapsulates the search.
043: ** It's parameters are:
044: ** 1) the search string
045: ** 2) the attributes you want returned, comma delimited
046: ** 3) sort order, comma delimited, - descending, + ascending
047: ** 4) first hit
048: ** 5) number of hits
049: ** 6) query language, eg search, taxonomy-basic, schema-basic, etc
050: ** 7) database to search
051: ** 8) The RDM server URL, eg, http://portal.siroe.com:2222/mySearch/search
052: ** 9) Access token (null for anonymous access, or valid
053: ** Sun ONE Identity Server session id)
054: */
055: Search search = new Search(scope,
056: "score,url,title,description", "-score", 1, 20,
057: "search", dbname, RDMServer, null);
058:
059: /* Execute the query. */
060:
061: System.out.println("\nSearch results for \"" + scope + "\"");
062:
063: DataOutputStream sos = null;
064: if (SOIFOutputFile != null) {
065: try {
066: sos = new DataOutputStream(new FileOutputStream(
067: SOIFOutputFile));
068: } catch (Exception e1) {
069: System.out
070: .println("Error: failed to create output file: "
071: + e1);
072: }
073: }
074:
075: int pagenum = 1;
076: int pagesize = 10;
077: SOIFBuffer firstPageSOIF = new SOIFBuffer();
078:
079: for (; pagenum <= 5; pagenum++) {
080:
081: int firstHit = (pagenum - 1) * pagesize + 1;
082:
083: try {
084: search.doQuery(firstHit, pagesize);
085: } catch (Exception ex) {
086: ex.printStackTrace();
087: break;
088: }
089:
090: // Check the result count. -1 indicates an error.
091: if (search.getResultCount() <= 0)
092: break;
093:
094: System.out
095: .println("==========================================");
096: System.out
097: .println("page "
098: + pagenum
099: + ": hits "
100: + search.getFirstHit()
101: + " to "
102: + (search.getFirstHit()
103: + search.getResultCount() - 1)
104: + " out of " + search.getHitCount()
105: + " across " + search.getDocumentCount()
106: + " documents");
107: System.out
108: .println("==========================================");
109: System.out.println();
110:
111: SOIFInputStream resultStream = search.getResultStream();
112: SOIF soif;
113:
114: /* Examine the results of the search. The
115: ** results can also be returned as a string
116: ** using getResult(), but the SOIF version
117: ** parses the string for you. The following
118: ** block loops through a list of SOIF instances.
119: */
120: for (soif = resultStream.readSOIF(); soif != null; soif = resultStream
121: .readSOIF()) {
122:
123: if (pagenum == 1)
124: firstPageSOIF.write(soif.toByteArray());
125:
126: /* use the getValue() method to get
127: ** a value associated w/ the specified
128: ** attribute.
129: */
130: String u = soif.getURL();
131: String t = soif.getValue("title");
132: String d = soif.getValue("description");
133: String sc = soif.getValue("score");
134:
135: /* do something with the results */
136: System.out
137: .println("TITLE: "
138: + t
139: + "\n"
140: + "URL: "
141: + u
142: + "\n"
143: + "SCORE: "
144: + sc
145: + "\n"
146: + "DESCRIPTION: "
147: + d
148: + "\n"
149: + "--------------------------------------------\n");
150: if (sos != null) {
151: try {
152: sos.writeBytes(soif.toString());
153: } catch (Exception e1) {
154: System.out
155: .println("Error: failed to write to SOIF output file: "
156: + e1);
157: }
158: }
159:
160: }
161:
162: /* Break if largest hit already displayed */
163: if (search.getHitCount() <= (firstHit + pagesize - 1))
164: break;
165:
166: }
167:
168: if (firstPageSOIF == null)
169: System.out.println("No matching documents found.");
170:
171: }
172:
173: /**
174: * @param filename - a file to dump raw SOIF results into - only
175: * use if running from the comand line or an applet with file
176: * system access
177: */
178: public void setSOIFfile(String filename) {
179: SOIFOutputFile = filename;
180: }
181:
182: }
183:
184: /**
185: * A panel with a search box that calls the simple search class.
186: */
187: class SearchPanel extends Panel implements
188: java.awt.event.ActionListener {
189:
190: Button searchBtn;
191: TextField searchTF;
192: SimpleSearch ss;
193:
194: /** C'tor */
195: public SearchPanel(SimpleSearch ss) {
196: this .ss = ss;
197: searchBtn = new Button("Search");
198: searchTF = new TextField("java", 40);
199: setLayout(new FlowLayout(FlowLayout.CENTER));
200: add(searchTF);
201: add(searchBtn);
202: searchTF.addActionListener(this );
203: searchBtn.addActionListener(this );
204: }
205:
206: public void actionPerformed(java.awt.event.ActionEvent e) {
207: if (e.getID() == Event.ACTION_EVENT) {
208: searchTF.setEditable(false);
209: searchBtn.setEnabled(false);
210: try {
211: ss.doSearch(searchTF.getText());
212: } catch (Exception ex) {
213: System.out.println("Exception during search: " + ex);
214: }
215: searchTF.setEditable(true);
216: searchBtn.setEnabled(true);
217: } else
218: super .processEvent(e);
219: }
220:
221: }
222:
223: /**
224: * Applet/application for simple query interface. Can be used as an
225: * example for those who want to create their own java interface.
226: * This example demonstrates search only. Browse, determining
227: * the schema of the search server and obtaining the taxonomy
228: * of the search server will be demonstrated in other examples.
229: */
230: public class SearchDemo extends Applet {
231:
232: /** Run as an applet. */
233: public void init() {
234: String rdm = getParameter("RDMServer");
235: String db = getParameter("Database");
236: SimpleSearch ss = new SimpleSearch(rdm, db);
237: SearchPanel sp = new SearchPanel(ss);
238: setLayout(new FlowLayout(FlowLayout.CENTER));
239: add(sp);
240: }
241:
242: /** Run as an application. */
243: public static void main(String argv[]) throws Exception {
244:
245: int args = argv.length;
246: String SOIFOutputFile = null;
247:
248: if (args != 1 && args != 2 && args != 3) {
249: System.out
250: .println("args: RDMServer [query] [soif_output_file_name]");
251: return;
252: }
253:
254: String rdm = argv[0]; // rdm search server, eg, http://portal.siroe.com:2222/mySearch/search
255: String db = null; // server will use default db
256:
257: SimpleSearch ss = new SimpleSearch(rdm, db);
258:
259: if (args == 3) {
260: --args;
261: ss.setSOIFfile(argv[2]); // dump raw soif results to this file
262: }
263:
264: if (args == 1) {
265: // run from a search box
266: Frame f = new Frame();
267: SearchPanel sp = new SearchPanel(ss);
268: f.add(sp);
269: f.pack();
270: f.show();
271: } else {
272: // run from command line
273: String query = argv[1];
274: ss.doSearch(query);
275: }
276: }
277:
278: }
|