/*=============================================================================*
* 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.impl;
import java.text.ParseException;
import java.util.Date;
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.xml.XmlUtils;
import org.apache.muse.util.xml.XsdUtils;
import org.apache.muse.ws.dm.muws.StateTransition;
import org.apache.muse.ws.dm.muws.MuwsConstants;
import org.apache.muse.ws.dm.muws.State;
import org.apache.muse.ws.dm.muws.StateType;
import org.apache.muse.ws.resource.basefaults.BaseFault;
import org.apache.muse.ws.resource.ext.faults.InvalidMessageFormatFault;
/**
*
* SimpleStateTransition represents the muws2:StateTransition property defined
* by the WSDM {@linkplain State State capability}.
*
* @author Dan Jemiolo (danj)
*
*/
public class SimpleStateTransition implements StateTransition
{
//
// Used to lookup all exception messages
//
private static Messages _MESSAGES =
MessagesFactory.get(SimpleStateTransition.class);
//
// The current state value - equivalent to State.getCurrentState()
//
private StateType _entered = null;
//
// The last state value - this is null for a resource's first transition.
//
private StateType _previous = null;
//
// The time that the transition was performed. The default is "right now".
//
private Date _transitionTime = new Date();
/**
*
* @param xml
* The DOM Element representing the state transition.
*
*/
public SimpleStateTransition(Element xml)
throws BaseFault
{
if (xml == null)
throw new NullPointerException(_MESSAGES.get("NullSTElement"));
//
// first get the time - we have to add a lot of exception cruft
// to make sure we get a valid date
//
String timeString = xml.getAttribute(MuwsConstants.TIME);
if (timeString == null || timeString.length() == 0)
throw new InvalidMessageFormatFault(_MESSAGES.get("NoTransitionTime"));
try
{
_transitionTime = XsdUtils.getLocalTime(timeString);
}
catch (ParseException error)
{
throw new InvalidMessageFormatFault(error);
}
//
// now grab the two state values - the "previous" one is optional
//
Element previousXML = XmlUtils.getElement(xml, MuwsConstants.PREVIOUS_QNAME);
if (previousXML != null)
_previous = new SimpleStateType(previousXML);
Element enteredXML = XmlUtils.getElement(xml, MuwsConstants.ENTERED_QNAME);
if (enteredXML == null)
throw new InvalidMessageFormatFault(_MESSAGES.get("NoEnteredState"));
_entered = new SimpleStateType(enteredXML);
}
/**
*
* @param previous
* The state the owning resource was in previously. This can be
* null for new resources that had no initial state.
*
* @param entered
* The state the owning resource just entered. This is usually
* equivalent to its current state.
*
*/
public SimpleStateTransition(StateType previous, StateType entered)
{
if (entered == null)
throw new NullPointerException(_MESSAGES.get("NullState"));
_previous = previous;
_entered = entered;
}
/**
*
* @return The state the resource entered during the transition.
*
*/
public StateType getEnteredState()
{
return _entered;
}
/**
*
* @return The state the resource left during the transition. This is
* null if there was no previous state.
*
*/
public StateType getPreviousState()
{
return _previous;
}
/**
*
* @return The time that the transition occurred.
*
*/
public Date getTransitionTime()
{
return _transitionTime;
}
public Element toXML()
{
return toXML(XmlUtils.EMPTY_DOC);
}
public Element toXML(Document doc)
{
if (doc == null)
throw new NullPointerException(_MESSAGES.get("NullDocument"));
Element root = XmlUtils.createElement(doc, MuwsConstants.TRANSITION_QNAME);
Date time = getTransitionTime();
root.setAttribute(MuwsConstants.TIME, XsdUtils.getLocalTimeString(time));
StateType previous = getPreviousState();
if (previous != null)
{
Element previousXML = previous.toXML(doc);
XmlUtils.setElement(root, MuwsConstants.PREVIOUS_QNAME, previousXML, true);
}
StateType entered = getEnteredState();
Element enteredXML = entered.toXML(doc);
XmlUtils.setElement(root, MuwsConstants.ENTERED_QNAME, enteredXML, true);
return root;
}
}