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.*;
012: import java.net.*;
013:
014: /**
015: * Applet for populating the Search database. It uses the static
016: * strings defined below for input. The basic process is to convert
017: * the input to SOIF and then add it to the database using rdsubmit.
018: * If you are adding a lot of data, you will probably want to batch
019: * the input to rdsubmit. You are not limited to just the DB fields
020: * used in this example, you can add any fields at all and they will
021: * behave according to their schema definitions, if any.
022: */
023:
024: class SimpleSubmit {
025:
026: public static final String DATA_URL[] = { "http://url1",
027: "http://url2", "http://url3" };
028: public static final String DATA_CLASS[] = { "Cars", "Boats",
029: "Planes" };
030: public static final String DATA_DESC[] = { "Document 1",
031: "Document 2", "Document 3" };
032:
033: String RDMServer;
034: String dbname;
035: String SOIFOutputFile;
036:
037: /**
038: * SimpleSubmit constructor
039: * @param rdm - url of rdm server, eg, http://search_server:port
040: * @param db - the database to submit to on this rdm server
041: * if null, server will use the default db
042: */
043: public SimpleSubmit(String rdm, String db) {
044: System.out.println("Sun ONE Search Java Submit Demo\n");
045: RDMServer = rdm;
046: dbname = db;
047: }
048:
049: public void doSubmit() throws Exception {
050:
051: try {
052: URLConnection pc = new URL(RDMServer).openConnection();
053: pc.setAllowUserInteraction(true);
054: pc.setUseCaches(false);
055: pc.setDoOutput(true);
056: pc.setDoInput(true);
057: SOIFOutputStream sos = new SOIFOutputStream(pc
058: .getOutputStream());
059: pc.connect();
060:
061: System.out.println(">>> Request\n");
062: SOIF hdr = new SOIF("RDMHEADER", "-");
063: hdr.insert("rdm-type", "rd-submit-request");
064: if (dbname != null)
065: hdr.insert("submit-database", dbname);
066: sos.write(hdr);
067: System.out.print(hdr);
068:
069: /*
070: // submit request header
071: // - specifies the operation to be performed on each submitted RD
072: String SUBMIT_TYPE = "submit-type"; // see below
073: String SUBMIT_OPER = "submit-operation"; // see below
074: String SUBMIT_VIEW = "submit-view"; // attribute list, eg, title,author,classification
075:
076: // submit types
077: // - controls the type of data processed
078: String SUBMIT_PERSISTENT = "persistent"; // persistent RD data only
079: String SUBMIT_NONPERSISTENT = "nonpersistent"; // non-persistent RD data only
080: String SUBMIT_MERGED = "merged"; // merged P/NP data (with P attributes 'covering' NP attributes)
081:
082: // submit operations
083: String SUBMIT_RETRIEVE = "retrieve"; // fetch RDs filtered by submit-view
084: String SUBMIT_INSERT = "insert"; // insert/replace entire RDs
085: String SUBMIT_DELETE = "delete"; // delete entire RDs
086: String SUBMIT_UPDATE = "update"; // update the given RD attributes only, can also be filtered by submit-view
087: */
088:
089: SOIF req = new SOIF("Request", "-");
090: req.insert("submit-type", "nonpersistent");
091: req.insert("submit-operation", "retrieve");
092: req.insert("submit-view", "classification,description");
093: sos.write(req);
094: System.out.print(req);
095:
096: for (int i = 0; i < DATA_URL.length; i++) {
097: SOIF doc = new SOIF("DOCUMENT", DATA_URL[i]);
098: doc.insert("classification", DATA_CLASS[i]);
099: doc.insert("description", DATA_DESC[i]);
100: sos.write(doc);
101: System.out.print(doc);
102: }
103: sos.close();
104:
105: System.out.println(">>> Response\n");
106: SOIFInputStream sis = new SOIFInputStream(pc
107: .getInputStream());
108: SOIF s;
109: while ((s = sis.readSOIF()) != null)
110: System.out.println(s);
111:
112: System.out.println("Done: " + DATA_URL.length
113: + " RDs processed.\n");
114:
115: } catch (Exception e) {
116: System.out.println("Error: " + e);
117: throw e;
118: }
119:
120: }
121:
122: /**
123: * @param filename - a file to dump raw SOIF results into - only
124: * use if running from the command line or an applet with file
125: * system access
126: */
127: public void setSOIFfile(String filename) {
128: SOIFOutputFile = filename;
129: }
130:
131: }
132:
133: class SubmitPanel extends Panel {
134:
135: Button submitBtn;
136: TextField submitLog;
137: SimpleSubmit ss;
138:
139: /** C'tor */
140: public SubmitPanel(SimpleSubmit ss) {
141: this .ss = ss;
142: submitBtn = new Button("Submit");
143: submitLog = new TextField("", 40);
144: submitLog.setEditable(false);
145: setLayout(new FlowLayout(FlowLayout.CENTER));
146: add(submitBtn);
147: add(submitLog);
148: }
149:
150: public void actionPerformed(java.awt.event.ActionEvent e) {
151: if (e.getID() == Event.ACTION_EVENT) {
152: submitBtn.setEnabled(false);
153: try {
154: ss.doSubmit();
155: } catch (Exception ex) {
156: System.out.println("Exception during submit: " + ex);
157: }
158: submitBtn.setEnabled(true);
159: } else
160: super .processEvent(e);
161: }
162:
163: }
164:
165: public class SubmitDemo extends Applet {
166:
167: /** Run as an applet. */
168: public void init() {
169: String rdm = getParameter("RDMServer");
170: String db = getParameter("Database");
171: SimpleSubmit ss = new SimpleSubmit(rdm, db);
172: SubmitPanel sp = new SubmitPanel(ss);
173: setLayout(new FlowLayout(FlowLayout.CENTER));
174: add(sp);
175: }
176:
177: /** Run as an application. */
178: public static void main(String argv[]) throws Exception {
179:
180: int args = argv.length;
181: String SOIFOutputFile = null;
182:
183: if (args != 1 && args != 2 && args != 3) {
184: System.out
185: .println("args: RDMServer [soif_output_file_name]");
186: return;
187: }
188:
189: String rdm = argv[0]; // rdm server, eg, http://server:port
190: String db = null; // server will use default db
191:
192: SimpleSubmit ss = new SimpleSubmit(rdm, db);
193:
194: if (args == 2) {
195: --args;
196: ss.setSOIFfile(argv[1]); // dump raw soif results to this file
197: }
198:
199: // run from command line
200: ss.doSubmit();
201:
202: }
203:
204: }
|