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.query;
027:
028: import org.cougaar.lib.aggagent.util.InverseSax;
029: import org.cougaar.lib.aggagent.util.Enum.Language;
030: import org.cougaar.lib.aggagent.util.Enum.ScriptType;
031: import org.w3c.dom.Element;
032: import org.w3c.dom.NodeList;
033:
034: /**
035: * This is a client-side description of an alert.
036: */
037: public class AlertDescriptor extends Alert {
038: private UpdateObservable updateObservable = new UpdateObservable();
039: private String queryId = null;
040:
041: private ScriptSpec alertSpec = null;
042:
043: /**
044: * Default constructor
045: */
046: public AlertDescriptor() {
047: super ();
048: }
049:
050: public AlertDescriptor(Alert a) {
051: super ();
052: setName(a.getName());
053: setAlerted(a.isAlerted());
054: setQueryId(a.getQueryAdapter().getID());
055: }
056:
057: public AlertDescriptor(Element alertRoot) {
058: super ();
059: setName(alertRoot.getAttribute("name"));
060: setAlerted(Boolean.valueOf(alertRoot.getAttribute("alerted"))
061: .booleanValue());
062: setQueryId(alertRoot.getAttribute("query_id"));
063: NodeList nl = alertRoot.getElementsByTagName("alert_script");
064: if (nl.getLength() > 0)
065: alertSpec = new ScriptSpec((Element) nl.item(0));
066: }
067:
068: public AlertDescriptor(Language language, String script) {
069: alertSpec = new ScriptSpec(ScriptType.ALERT, language, script);
070: }
071:
072: public void setQueryAdapter(QueryResultAdapter qra) {
073: super .setQueryAdapter(qra);
074: queryId = qra.getID();
075: }
076:
077: public void setQueryId(String queryId) {
078: this .queryId = queryId;
079: }
080:
081: public String getQueryId() {
082: return queryId;
083: }
084:
085: public void setAlerted(boolean f) {
086: super .setAlerted(f);
087: fireObjectChanged();
088: }
089:
090: /**
091: * Create a functional alert based on this descriptor
092: *
093: * @return a functional alert based on this descriptor
094: */
095: public Alert createAlert() throws Exception {
096: Alert a = alertSpec.toAlert();
097: a.setName(getName());
098:
099: return a;
100: }
101:
102: public String getScript() {
103: if (alertSpec != null)
104: return alertSpec.getText();
105: else
106: return "";
107: }
108:
109: public void handleUpdate() {
110: // never actually used as a functional alert
111: }
112:
113: protected void includeXmlBody(InverseSax doc) {
114: if (alertSpec != null)
115: alertSpec.includeXml(doc);
116: }
117:
118: /**
119: * Add an update listener to observe this object
120: */
121: public void addUpdateListener(UpdateListener ul) {
122: updateObservable.addUpdateListener(ul);
123: }
124:
125: /**
126: * Remove an update listener such that it no longer gets notified of changes
127: * to this object
128: */
129: public void removeUpdateListener(UpdateListener ul) {
130: updateObservable.removeUpdateListener(ul);
131: }
132:
133: /**
134: * Send event to all update listeners indicating that object has been added
135: * to the log plan.
136: */
137: public void fireObjectAdded() {
138: updateObservable.fireObjectAdded(this );
139: }
140:
141: /**
142: * Send event to all update listeners indicating that object has been removed
143: * from the log plan.
144: */
145: public void fireObjectRemoved() {
146: updateObservable.fireObjectRemoved(this );
147: }
148:
149: /**
150: * Send event to all update listeners indicating that object has been changed
151: * on the log plan.
152: */
153: private void fireObjectChanged() {
154: updateObservable.fireObjectChanged(this);
155: }
156: }
|