/*=============================================================================*
* Copyright 2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*=============================================================================*/
package org.apache.muse.ws.dm.muws.events.impl;
import java.text.ParseException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.apache.muse.util.messages.Messages;
import org.apache.muse.util.messages.MessagesFactory;
import org.apache.muse.util.uuid.RandomUuidFactory;
import org.apache.muse.util.xml.XmlUtils;
import org.apache.muse.util.xml.XsdUtils;
import org.apache.muse.ws.dm.muws.events.Component;
import org.apache.muse.ws.dm.muws.events.ManagementEvent;
import org.apache.muse.ws.dm.muws.events.Situation;
import org.apache.muse.ws.dm.muws.events.WefFactory;
import org.apache.muse.ws.dm.muws.events.WefConstants;
/**
*
* @author Dan Jemiolo (danj)
*
*/
public class SimpleManagementEvent
extends AbstractExtendedElements implements ManagementEvent
{
//
// used to lookup all exception messages
//
private static Messages _MESSAGES = MessagesFactory.get(SimpleManagementEvent.class);
private String _eventID = RandomUuidFactory.getInstance().createUUID();
private Component _reporter = null;
private Date _reportTime = new Date();
private long _sequenceNumber = -1;
private Situation _situation = null;
private Component _source = null;
public SimpleManagementEvent()
{
//
// no action - allow people to construct WEFs starting with
// a blank event. toXML() will validate before serializing
//
}
public SimpleManagementEvent(Element xml, WefFactory factory)
{
//
// NOTE: we can call public get/set methods in the constructor
// because these methods are final
//
String sequenceString = xml.getAttributeNS(WefConstants.MUWS_P2_URI, WefConstants.SEQUENCE_NUMBER);
if (sequenceString != null && sequenceString.length() > 0)
_sequenceNumber = Long.parseLong(sequenceString);
String timeString = xml.getAttribute(WefConstants.REPORT_TIME);
try
{
if (timeString != null && timeString.length() > 0)
setReportTime(XsdUtils.getLocalTime(timeString));
}
catch (ParseException error)
{
throw new RuntimeException(error.getMessage(), error);
}
//
// keep track of all elements in the MUWS namespaces - the
// ones leftover are the "extended elements"
//
Set alreadySeen = new HashSet();
Element idXML = XmlUtils.getElement(xml, WefConstants.EVENT_ID_QNAME);
if (idXML != null)
{
setEventID(XmlUtils.extractText(idXML));
alreadySeen.add(idXML);
}
Element reporterXML = XmlUtils.getElement(xml, WefConstants.REPORTER_COMP_QNAME);
Component reporter = factory.createComponent(reporterXML);
setReporter(reporter);
alreadySeen.add(reporterXML);
Element sourceXML = XmlUtils.getElement(xml, WefConstants.SOURCE_COMP_QNAME);
Component source = factory.createComponent(sourceXML);
setSource(source);
alreadySeen.add(sourceXML);
Element situationXML = XmlUtils.getElement(xml, WefConstants.SITUATION_QNAME);
if (situationXML != null)
{
Situation situation = factory.createSituation(situationXML);
setSituation(situation);
alreadySeen.add(situationXML);
}
Element[] children = XmlUtils.getAllElements(xml);
for (int n = 0; n < children.length; ++n)
if (!alreadySeen.contains(children[n]))
addExtendedElement(children[n]);
}
public final String getEventID()
{
return _eventID;
}
public final Component getReporter()
{
return _reporter;
}
public final Date getReportTime()
{
return _reportTime;
}
public final long getSequenceNumber()
{
return _sequenceNumber;
}
public final Situation getSituation()
{
return _situation;
}
public final Component getSource()
{
return _source;
}
public final void setEventID(String eventID)
{
_eventID = eventID;
}
public final void setReporter(Component reporter)
{
_reporter = reporter;
}
public final void setReportTime(Date reportTime)
{
_reportTime = reportTime;
}
public final void setSequenceNumber(long sequenceNumber)
{
_sequenceNumber = sequenceNumber;
}
public final void setSituation(Situation situation)
{
_situation = situation;
}
public final void setSource(Component source)
{
_source = source;
}
public String toString()
{
return XmlUtils.toString(toXML(), false);
}
public Element toXML()
{
return toXML(XmlUtils.EMPTY_DOC);
}
public Element toXML(Document doc)
{
Element wef = XmlUtils.createElement(doc, WefConstants.MGMT_EVENT_QNAME);
long sequence = getSequenceNumber();
if (sequence >= 0)
{
String name = WefConstants.MUWS_P2_PREFIX + ':' + WefConstants.SEQUENCE_NUMBER;
wef.setAttributeNS(WefConstants.MUWS_P2_URI, name, Long.toString(sequence));
}
Date time = getReportTime();
if (time != null)
wef.setAttribute(WefConstants.REPORT_TIME, XsdUtils.getLocalTimeString(time));
String eventID = getEventID();
if (eventID == null)
throw new RuntimeException(_MESSAGES.get("NoEventID"));
XmlUtils.setElement(wef, WefConstants.EVENT_ID_QNAME, eventID);
Component source = getSource();
if (source == null)
throw new RuntimeException(_MESSAGES.get("NoSourceComponent"));
Element sourceXML = source.toXML(doc);
wef.appendChild(sourceXML);
Component reporter = getReporter();
if (reporter!= null)
{
Element reporterXML = reporter.toXML(doc);
wef.appendChild(reporterXML);
}
Situation situation = getSituation();
if (situation != null)
{
Element situationXML = situation.toXML(doc);
wef.appendChild(situationXML);
}
appendExtendedElements(wef);
return wef;
}
}