001: /*
002: * <copyright>
003: *
004: * Copyright 2003-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.lib.aggagent.servlet;
027:
028: import java.io.PrintWriter;
029: import java.util.Collection;
030: import java.util.Iterator;
031: import java.util.StringTokenizer;
032:
033: import javax.servlet.http.HttpServletRequest;
034:
035: import org.cougaar.lib.aggagent.query.AggregationQuery;
036: import org.cougaar.lib.aggagent.query.AggregationResultSet;
037: import org.cougaar.lib.aggagent.query.Alert;
038: import org.cougaar.lib.aggagent.query.AlertDescriptor;
039: import org.cougaar.lib.aggagent.query.QueryResultAdapter;
040: import org.cougaar.lib.aggagent.query.ResultSetDataAtom;
041: import org.cougaar.lib.aggagent.query.ScriptSpec;
042: import org.cougaar.lib.aggagent.util.Enum.Language;
043: import org.cougaar.lib.aggagent.util.Enum.QueryType;
044: import org.cougaar.lib.aggagent.util.Enum.ScriptType;
045: import org.cougaar.lib.aggagent.util.Enum.UpdateMethod;
046: import org.cougaar.lib.aggagent.util.Enum.XmlFormat;
047:
048: /**
049: * HTML utilities to assist with HTML presentation and form post interpretation
050: * related to core aggregation query objects. This could be extended to become
051: * non-static and to include look and feel description fields
052: * (e.g. colors, fonts to use)
053: */
054: public class HTMLPresenter {
055: public static String toHTML(QueryResultAdapter qra) {
056: StringBuffer s = new StringBuffer();
057: s.append("<B>Query #" + qra.getID() + "</B><P>");
058: s.append(toHTML(qra.getResultSet()));
059:
060: return s.toString();
061: }
062:
063: public static String toHTML(AggregationResultSet rs) {
064: StringBuffer s = new StringBuffer(
065: "<TABLE BORDER=\"1\" BGCOLOR=\"WHITE\">\n");
066: boolean firstElement = true;
067:
068: Iterator dataAtoms = rs.getAllAtoms();
069: while (dataAtoms.hasNext()) {
070: ResultSetDataAtom d = (ResultSetDataAtom) dataAtoms.next();
071: if (firstElement) {
072: firstElement = false;
073: s.append("<TR BGCOLOR=\"#888888\"><TH COLSPAN=\"");
074: s.append(countElements(d.getIdentifierNames()));
075: s
076: .append("\"><FONT COLOR=\"WHITE\">Identifier(s)</FONT></TH><TH COLSPAN=\"");
077: s.append(countElements(d.getValueNames()));
078: s
079: .append("\"><FONT COLOR=\"WHITE\">Value(s)</FONT></TH></TR>\n");
080: s.append("<TR BGCOLOR=\"NAVY\">");
081: addHeaderCells(s, d.getIdentifierNames());
082: addHeaderCells(s, d.getValueNames());
083: s.append("</TR>\n");
084: }
085:
086: s.append("<TR>");
087: Iterator ids = d.getIdentifierNames();
088: while (ids.hasNext()) {
089: s
090: .append("<TD>" + d.getIdentifier(ids.next())
091: + "</TD>");
092: }
093: Iterator values = d.getValueNames();
094: while (values.hasNext()) {
095: s.append("<TD>" + d.getValue(values.next()) + "</TD>");
096: }
097: s.append("</TR>\n");
098: }
099: s.append("</TABLE>");
100:
101: return s.toString();
102: }
103:
104: public static void sendAlertSummary(Collection alerts,
105: PrintWriter out) {
106: out.println("<table BORDER=\"1\" BGCOLOR=\"WHITE\">");
107: sendAlertRow(out, "000080", "FFFFFF", "Query", "Name",
108: "Triggered", true);
109: for (Iterator i = alerts.iterator(); i.hasNext();)
110: sendAlert(out, (Alert) i.next());
111: out.println("</table></center>");
112: }
113:
114: public static void sendQueryForm(String selfName, PrintWriter out,
115: boolean persistent) {
116: QueryType queryType = persistent ? QueryType.PERSISTENT
117: : QueryType.TRANSIENT;
118: out.println("<CENTER>");
119: out
120: .println("<TABLE BORDER=1 cellpadding=8 BGCOLOR=\"#DDDDDD\">");
121: out.print("<TR><TH>Create New ");
122: out.print(queryType);
123: out.println(" Query</TH></TR>");
124: out.println("<TR><TD>");
125: out.println("<form action=\"" + selfName + "\" ");
126: out.println("method=\"GET\" "
127: + (persistent ? "target=\"menu\">" : ">"));
128: out
129: .println("<input type=\"HIDDEN\" name=\"CREATE_QUERY\" value =\"1\">");
130: out
131: .print("<input name=\"query_type\" type=\"HIDDEN\" value=\"");
132: out.println(queryType + "\"/>");
133: if (persistent) {
134: out.println("Query Name: ");
135: out
136: .println("<input name=\"query_name\" type=\"TEXT\" size=\"15\"/>");
137: out.println(" Update Method: ");
138: out.println("<SELECT NAME=\"update_method\">");
139: for (Iterator i = UpdateMethod.getValidValues().iterator(); i
140: .hasNext();) {
141: UpdateMethod updateMethod = (UpdateMethod) i.next();
142: out.print("<OPTION VALUE=\"");
143: out.print(updateMethod);
144: out.print("\" ");
145: out
146: .print(updateMethod == UpdateMethod.PUSH ? "SELECTED>"
147: : ">");
148: out.println(updateMethod);
149: }
150: out.println("</SELECT>");
151: out.println(" Pull Wait Period: ");
152: out
153: .println("<input name=\"pull_rate\" value=\"60\" type=\"TEXT\"");
154: out.println(" size=\"3\"/> sec.");
155: out.println("<BR><BR>");
156: }
157: out.println("List of Source Cluster IDs:<br>");
158: out
159: .println("<input name=\"source_cluster\" type=\"TEXT\" size=\"70\"/>");
160: out.println("<BR><BR>");
161: out.println("Unary Predicate Script: ");
162: sendLanguageSelector("unary_predicate_lang", out);
163: out.println("<BR>");
164: out
165: .println("<TEXTAREA name=\"unary_predicate\" ROWS=\"4\" COLS=\"70\">");
166: out.println("</TEXTAREA><BR><BR>");
167: out.println("XML Encoder Script: ");
168: sendCoderTypeSwitch("xml_encoder_type", "Full Format", out);
169: sendLanguageSelector("xml_encoder_lang", out);
170: out.println("<BR>");
171: out
172: .println("<TEXTAREA name=\"xml_encoder\" ROWS=\"8\" COLS=\"70\">");
173: out.println("</TEXTAREA>");
174: out.println("</TD></TR><TR><TD ALIGN=\"CENTER\">");
175: out.println("<input value=\"Create Query\" type=\"SUBMIT\"/>");
176: out.println("</form>");
177: out.println("</TD></TR>");
178: out.println("</TABLE>");
179: out.println("</CENTER>");
180: }
181:
182: public static AggregationQuery processQueryForm(
183: HttpServletRequest request) {
184: // get query type
185: QueryType queryType = QueryType.fromString(request
186: .getParameter("query_type"));
187:
188: // publish request to blackboard for Aggregation Plugin
189: AggregationQuery aq = new AggregationQuery(queryType);
190: if (queryType == QueryType.PERSISTENT) {
191: aq.setName(request.getParameter("query_name"));
192: aq.setUpdateMethod(UpdateMethod.fromString(request
193: .getParameter("update_method")));
194: aq.setPullRate(Integer.parseInt(request
195: .getParameter("pull_rate")));
196: }
197: String sourceClusters = request.getParameter("source_cluster");
198: StringTokenizer t = new StringTokenizer(sourceClusters, ",");
199: while (t.hasMoreTokens()) {
200: aq.addSourceCluster(t.nextToken().trim());
201: }
202:
203: String pred = request.getParameter("unary_predicate");
204: Language pred_l = Language.fromString(request
205: .getParameter("unary_predicate_lang"));
206: aq.setPredicateSpec(new ScriptSpec(ScriptType.UNARY_PREDICATE,
207: pred_l, pred));
208: String coder = request.getParameter("xml_encoder");
209: Language coder_l = Language.fromString(request
210: .getParameter("xml_encoder_lang"));
211: XmlFormat coder_f = (request.getParameter("xml_encoder_type") != null) ? XmlFormat.INCREMENT
212: : XmlFormat.XMLENCODER;
213: aq.setFormatSpec(new ScriptSpec(coder_l, coder_f, coder));
214:
215: return aq;
216: }
217:
218: public static void sendAlertForm(String selfName,
219: HttpServletRequest request, PrintWriter out) {
220: String name = request.getParameter("QUERY");
221: String id = request.getParameter("SESSION_ID");
222: out.println("<center>");
223: out.println("<form action=\"" + selfName
224: + "\" method=\"GET\" target=\"data\">");
225: out
226: .println("<input type=\"HIDDEN\" name=\"ADD_ALERT\" value =\"1\">");
227: out
228: .println("<table border=1 cellpadding=8 bgcolor=\"#DDDDDD\">");
229: out.print("<tr><th>Create new Alert for Query");
230: if (name != null && name.length() > 0)
231: out.print(" " + name);
232: out.println(" (" + id + ")</th></tr>");
233: out.println("<tr><td>");
234: out.println("<input name=\"query_id\" type=\"hidden\" value=\""
235: + id + "\"/>");
236: out.println("Alert Name: ");
237: out
238: .println("<input name=\"alert_name\" type=\"text\" size=\"15\"/>");
239: out.println("<br><br>Alert Specification: ");
240: sendLanguageSelector("alert_script_lang", out);
241: out.println("<br>");
242: sendTextArea("alert_script", 8, 70, out);
243:
244: out.println("</td></tr><tr><td align=\"center\">"
245: + "<input value=\"Create Alert\" type=\"submit\"/>");
246:
247: out.println("</td></tr>");
248: out.println("</table>");
249: out.println("</form>");
250: out.println("</center>");
251: }
252:
253: public static AlertDescriptor processAlertForm(
254: HttpServletRequest request) {
255: String name = request.getParameter("alert_name");
256: String query = request.getParameter("query_id");
257: String lang = request.getParameter("alert_script_lang");
258: String script = request.getParameter("alert_script");
259:
260: // create the Alert
261: AlertDescriptor ad = new AlertDescriptor(Language
262: .fromString(lang), script);
263: ad.setName(name);
264: ad.setQueryId(query);
265:
266: return ad;
267: }
268:
269: private static void sendAlert(PrintWriter out, Alert a) {
270: StringBuffer buf = new StringBuffer();
271: QueryResultAdapter qra = a.getQueryAdapter();
272: AggregationQuery q = qra.getQuery();
273:
274: String query = q.getName();
275: if (query != null)
276: buf.append(query);
277: buf.append(" (");
278: buf.append(qra.getID());
279: buf.append(")");
280:
281: String status = String.valueOf(a.isAlerted());
282:
283: sendAlertRow(out, "FFFFFF", "000000", buf.toString(), a
284: .getName(), status, false);
285: }
286:
287: private static void sendAlertRow(PrintWriter out, String bg,
288: String fg, String query, String name, String status,
289: boolean header) {
290: out.println("<tr>");
291: sendTableCell(out, bg, fg, query, header);
292: sendTableCell(out, bg, fg, name, header);
293: sendTableCell(out, bg, fg, status, header);
294: out.println("</tr>");
295: }
296:
297: private static void sendTableCell(PrintWriter out, String bg,
298: String fg, String x, boolean header) {
299: String tagName = header ? "th" : "td";
300: out.print("<" + tagName);
301: if (bg != null)
302: out.print(" bgcolor=\"#" + bg + "\"");
303: out.print(">");
304: if (fg != null)
305: out.print("<font color=\"#" + fg + "\">");
306: if (x != null)
307: out.print(x);
308: if (fg != null)
309: out.print("</font>");
310: out.println("</" + tagName + ">");
311: }
312:
313: private static void sendTextArea(String name, int rows, int cols,
314: PrintWriter out) {
315: out.println("<textarea name=\"" + name + "\" rows = \"" + rows
316: + "\" cols=\"" + cols + "\"></textarea>");
317: }
318:
319: private static void sendCoderTypeSwitch(String paramName,
320: String appearance, PrintWriter out) {
321: out.print("(");
322: out.print("<input name=\"");
323: out.print(paramName);
324: out.print("\" type=\"checkbox\"/>");
325: out.print(appearance);
326: out.println(")");
327: }
328:
329: private static void sendLanguageSelector(String paramName,
330: PrintWriter out) {
331: out.print("(");
332: for (Iterator i = Language.getValidValues().iterator(); i
333: .hasNext();) {
334: Language lang = (Language) i.next();
335: String langString = lang.toString();
336: sendRadio(paramName, langString, langString,
337: (lang == Language.JAVA), out);
338: if (i.hasNext())
339: out.println(" ");
340: }
341: out.println(")");
342: }
343:
344: private static void sendRadio(String paramName, String appearance,
345: String value, boolean selected, PrintWriter out) {
346: out.print("<input name=\"");
347: out.print(paramName);
348: out.print("\" type=\"radio\" value=\"");
349: out.print(value);
350: out.print("\"");
351: if (selected)
352: out.print(" CHECKED ");
353: out.print("/>");
354: out.println(appearance);
355: }
356:
357: private static void addHeaderCells(StringBuffer s, Iterator names) {
358: while (names.hasNext()) {
359: s.append("<TH><FONT COLOR=\"WHITE\">" + names.next()
360: + "</FONT></TH>");
361: }
362: }
363:
364: private static int countElements(Iterator i) {
365: int count = 0;
366: while (i.hasNext()) {
367: count++;
368: i.next();
369: }
370: return count;
371: }
372: }
|