Similar to class RoundtripHandler in the yFiles FLEX client API, the yFiles
FLEX server API provides the class
GraphRoundtripSupport
that makes it easy to deserialize GraphML data that was sent from the client
and to send a serialized graph instance back to the client.
GraphRoundtripSupport offers the following benefits:
There are three overloaded methods that can be used for reading a graph into an IGraph instance:
API Excerpt 7.1. GraphRoundtripSupport graph reading
// Read a Graph from a GraphML Stream public virtual void ReadGraph(Stream, IGraph) // Read a Graph from a StringReader public virtual void ReadGraph(StringReader, IGraph) // Read a Graph from a HTTP Request using the // GraphRoundtripSupport.ParamGraph form parameter. public virtual bool ReadGraph(HttpRequest, IGraph)
If the graph is read directly from a HTTP request, the
ParamGraph
parameter is used to retrieve the GraphML string from the request.
All graph reading methods will use
CreateHandler
to retrieve a preconfigured GraphML I/O handler.
There are two overloaded methods that can be used for serializing a graph using the roundtrip support:
API Excerpt 7.2. GraphRoundtripSupport graph writing
// Write a Graph to a HTTP response public virtual void SendGraph(IGraph graph, HttpResponse response) // Send a Graph to a stream public virtual void SendGraph(IGraph graph, Stream stream)
Proper IDs are needed for client-server communication so the client graph can
be incrementally updated and the server knows which graph items to act upon.
If
CreateRoundtripGraph
is used for creating graph instances on the server, the roundtrip support
registers IMappers for node and edge
IDs with the mapper registry of the graph. The corresponding mappers can be
obtained from the mapper registry using the mapper keys
Node2IdMapperKey
and
Edge2IdMapperKey
.
Custom attributes associated with graph items can be easily added to the (de)serialization mechanism using the roundtrip support:
API Excerpt 7.3. Adding custom attribute mappers
// Add an attribute with the given name that can be queried using the
// given tag. scope type and key type are derived from the given
// type parameters.
public virtual void AddMapper<K, V>(object tag,
string name) where K : class, IModelItem
// Add an attribute with the given name that can be queried using the
// given tag. The attribute will be added to the I/O handler for the given
// scope type and key type.
// The type parameters are used for the corresponding IMapper that is created
// and registered with the graph's IMapperRegistry.
public virtual void AddMapper<K, V>(object tag,
string name,
KeyScope scopeType, KeyType keyType)
Example 7.1. Adding custom graph item data
// Add an attribute mapper that contains string values for nodes.
roundtripSupport.AddMapper<INode,string>("myNodeData", "myNodeData");
// create the IGraph instance that is used for roundtripping
IGraph graph = roundtripSupport.createRoundtripGraph();
// (populate the graph)
// retrieve the graph's mapper registry
IMapperRegistry registry =
graph.Lookup(typeof (IMapperRegistry)) as IMapperRegistry;
if (registry != null) {
// Retrieve the IMapper from the graph's mapper registry using the tag
// defined above
IMapper nodeDataMapper = registry.GetMapper<INode, string>("myNodeData");
// set the node data for each node
int i = 0;
foreach (INode node in graph.Nodes) {
nodeDataMapper.SetValue(node, "node " + (i++));
}
}
// send the graph to the client.
// the custom node data will be serialized as a custom GraphML attribute.
roundtripSupport.sendGraph(graph, httpResponse);
GraphRoundtripSupport's send and read methods use a
preconfigured GraphMLIOHandler that is created using
the protected method
CreateHandler
.
For additional customization of the (de)serialization process, this I/O handler
can be customized by overriding the factory method.
The default configuration of the I/O handler can also be replaced by a custom
configuration by overriding
ConfigureHandler
.
API Excerpt 7.4. I/O handler configuration
// Create a preconfigured GraphMLIOHandler protected virtual GraphMLIOHandler CreateHandler() // Configure the I/O handler protected virtual void ConfigureHandler(GraphMLIOHandler handler)
The roundtrip support class can also be used to send XML formatted error messages to the client:
API Excerpt 7.5. Sending error messages
// Send an Exception's message to the client public virtual void SendError(Exception ex, HttpContext context) // Send a custom error message to the client public virtual void SendError(string message, HttpContext context)
The XML data sent to the client is formatted as follows:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<errors>
<error>
error message
<error>
</errors>
</response>
|
Copyright ©2007-2008, yWorks GmbH. All rights reserved. |