Home > Tutorial

Add UI Services

In this chapter, we will demonstrate, in a simple way, how to declare and create a service.

What is important to notice is that EBX.Platform provides all necessary features to connect and interact with the Master Data through its API .

See also:

Sample Service

The given example creates a service that selects a random occurrence in the Titles table, then modifies its price randomly. The old price and the new price are displayed to the user. Furthermore, some direct links to the Title table and the modified occurrence are offered.

First, we will see how to define the service in the schema, then we will have a look at the JSP code implementation.

Schema definition

< xs:complexType name="SampleUI">
    <xs:annotation>
        <xs:appinfo>
            <osd:service resourcePath="/sampleUI/sampleUI.jsp"/>
        </xs:appinfo>
    </xs:annotation >
    <xs:documentation xml:lang="en-US">
           <osd:label>UIServices examples</osd:label >
           <osd:description>This service randomly modifies the price from a random book </osd:description >
    </xs:documentation >
    <xs:documentation xml:lang="fr-FR">
           <osd:label>Démonstration des UI services</osd:label >
           <osd:description>Ce service modifie de façon aléatoire le prix d'un livre sélectionné au hasard </osd:description >
    </xs:documentation >
</xs:complexType >

The service is declared with an osd:service element under the xs:annotation/xs:appinfo element of a complex type. It accepts resourcePath as an attribute to specify the directory in the module where the code lies.

Note that, for this example, the directory sampleUI/ is located at the same level as the WEB-INF/ directory.

Now that the service is defined, EBX.Manager handles the service:

Code implementation

The last thing to do is then to implement the code in the JSP

<%-- sampleUI.jsp --%>

<%@page language="java" contentType="text/html; charset=UTF-8"
   pageEncoding="UTF-8"%>
<%@page import="com.orchestranetworks.service.*"%>
<%@page import="com.orchestranetworks.schema.*"%>
<%@page import="com.onwbp.adaptation.*"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="java.util.Random"%>
<%
             /*ServiceContext provides the necessary context and navigation functions for integrating UI services into EBX.Manager.*/
            ServiceContext sContext = ServiceContext.getServiceContext(request);

            /*Selects Titles table*/
            Adaptation adaptation = sContext.getCurrentAdaptation();
            AdaptationTable aTable = (AdaptationTable) adaptation.get(Path.parse("/root/Titles"));

            /*Selects random occurrence*/
            Request tableRequest = aTable.createRequest();
            RequestResult requestResult = tableRequest.execute();
            int random = new Random().nextInt(requestResult.getSize());
            final Adaptation target = requestResult.getAdaptation(random);
%>
<p class="title">Random price modification of a random book</p>
<%
            String title = (String) target.get(Path.parse("/title"));
            BigDecimal price = (BigDecimal) target.get(Path.parse("/unit_price"));
%>
<b>Book title: </b><%=title%><br />
<b>Old book price: </b><%=price%><br />
<br />
<i>Changing book price...</i>
<br />
<%
            BigDecimal newPriceTmp = new BigDecimal(Math.random() * 100);
            final BigDecimal newprice = newPriceTmp.setScale(2, BigDecimal.ROUND_CEILING);
             /* Defines the procedure that modifies the price */
            Procedure procedure = new Procedure()
            {
             public void execute(ProcedureContext aContext) throws Exception
               {
                  ValueContextForUpdate valueContext = aContext.getContext(target.getAdaptationName());
                  valueContext.setValue(newprice, Path.parse("/unit_price"));
                  aContext.doModifyContent(target, valueContext);
               }
            };
            /* Executes procedure from ServiceContext */
            sContext.execute(procedure);
%>
<%
            price = (BigDecimal) target.get(Path.parse("/unit_price"));
%>
<b>New book price: </b><%=price%><br />
<br />
<%
            /* Generates direct link URLs*/
            String tableURL = sContext.getURLForSelection(adaptation, Path.parse("/root/Titles"));
            String occurrenceURL = sContext.getURLForSelection(target);
%>
The following points directly to the table
<br />
<a href="<%=tableURL%>">Here is the link to the table</a>
<br />
<br />
The following points directly to the occurrence:
<br />
<a href="<%=occurrenceURL%>">Here is the link to the modified occurrence</a>

It is to be noticed that we are creating a Procedure in order to update the price value. This interface must be implemented for performing operations on a repository in a safe way. See Procedure in the JavaDoc.

Result

The following screenshot matches the display after we executed the service.

Next: Add Programmatic Service >

Home > Tutorial