6.1 Accessing Element Properties

This example uses RPC to manage session and REST to fetch a single element using the rest/element/value/{identity} URI. When the application is executed a field is available to enter the DName of an element. Clicking Find submits the REST element/value request and display the standard properties inherent to all elements.

Using the Flex HTTPService component, the XML response returned by the REST API is preparsed, providing easy access to the element properties through the ResultEvent object.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
  xmlns:mx="http://www.adobe.com/2006/mxml"
  verticalGap="16"
  horizontalGap="16"
  layout="absolute"
  height="256" width="830"
  >
  <mx:Script>
  <![CDATA[
    import mx.rpc.http.HTTPService;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import com.mosol.moweb2cn.Web2Connect;
    import com.mosol.moweb2cn.ElementValue;
    import com.mosol.moweb2cn.Web2Connect.CreateSessionResultEvent;
    import com.mosol.moweb2cn.Web2Connect.CloseSessionResultEvent;
    private var rpc : Web2Connect = new Web2Connect();
    private var httpService : HTTPService = new HTTPService();
    private const MO_WEB2CONNECT_REST_ROOT : String = "/moweb2cn/rest/";
    private function createSession() : void
    {
      rpc.addEventListener("createSession", createSessionHandler);
      rpc.addEventListener("fault", rpcFaultHandler);
      rpc.createSession("admin", "formula");
    }
    private function createSessionHandler(event : CreateSessionResultEvent) : void
    {
      responseText.text = "Session created\n" + event.result.value + "\n";
    }
    private function closeSession() : void
    {
      rpc.addEventListener("closeSession", closeSessionHandler);
      rpc.addEventListener("fault", rpcFaultHandler);
      rpc.closeSession();
    }
    private function closeSessionHandler(event : CloseSessionResultEvent) : void
    {
      responseText.text = "Session closed\n" + event.result.value;
    }
     private function getElementValue() : void
    {
      // There is no RPC method for ElementValue so use REST API
      httpService.url = MO_WEB2CONNECT_REST_ROOT + "element/value/" + elementIdentity.text;
      httpService.addEventListener(ResultEvent.RESULT, getElementValueHandler);
      httpService.resultFormat = "object";
      httpService.send(null);
    }
    private function getElementValueHandler(event : ResultEvent) : void
    {
      // Note that the response XML is pre-processed
      // providing easy access to element properties
      responseText.text += "Element data\n"
        + "Identity    : " + event.result.element.identity + "\n"
        + "Name        : " + event.result.element.name + "\n"
        + "Class       : " + event.result.element.elementClassName + "\n"
        + "Condition   : " + event.result.element.condition + "\n"
        + "Last Update : " + event.result.element.lastupdateString + "\n";
    }
     private function httpServiceFaultHandler(event : FaultEvent) : void
    {
      responseText.text = event.fault.toString();
    }
    private function rpcFaultHandler(event : Event) : void
    {
      responseText.text = event.toString();
    }
   ]]>
  </mx:Script>
       <mx:TextArea id="responseText" text="Click button to create a session"
         fontSize="12" fontFamily="Courier New" x="10" y="10"
         width="811" height="208" wordWrap="true" />
       <mx:TextInput id="elementIdentity"
         text="formulaServer=Server/root=Administration"
         x="10" y="224" width="451" />
       <mx:Button label="Find" click="getElementValue()"
         textAlign="center" x="470" y="226" />
       <mx:Button label="Create Session" click="createSession()"
         textAlign="center" x="591" y="226" />
       <mx:Button label="Close Session" click="closeSession()"
         textAlign="center" x="711" y="226" />
</mx:Application>