Home > Tutorial

Add Programmatic Service

In this chapter we will demonstrate, in a simple way, how to create and use programmatic Services.

Programmatic Services allow to execute a procedure directly in Java and out of EBX.Manager context, through the ProgrammaticService Java class.

An easy way to use it is through a JSP as decribed in the following example, which replicates the service described in the UI Service chapter, but outside EBX.Manager. This implies that the process integrates login features so that EBX.Platform can determine whether the user is allowed to access the adaptation or not.

<%-- sampleProgrammaticService.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.orchestranetworks.instance.*"%>
<%@page import="com.onwbp.adaptation.*"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="java.util.Random"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>EBX.Platform Programmatic Service Sample</title>
</head> <body> <%
             /*Retrieves parameters */
            String login = request.getParameter("login");
            String password = request.getParameter("password");
            String branch = request.getParameter("branch");
            String adaptationName = request.getParameter("adaptation");


            /*Retrieves adaptation from parameters */
            Repository defaultRepository = Repository.getDefault();
            HomeKey homeKey = HomeKey.forBranchName(branch);
            AdaptationHome home = defaultRepository.lookupHome(homeKey);
            final Adaptation ada = home.findAdaptationOrNull(AdaptationName.forName(adaptationName));
            if (ada == null)
                   throw new ServletException("Adaptation with name " + adaptationName
                   + " does not exist.");
            AdaptationTable aTable = (AdaptationTable) ada.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);
             /*Creates a container for runnning services */
            Session aSession = defaultRepository.createSessionFromLoginPassword(login, password);
            ProgrammaticService svc = ProgrammaticService.createForSession(aSession, home);
            svc.setSessionTrackingInfo("Script Sample");             

             /*Defines the procedure */
            Procedure proc = 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);
               }
            };
             /* Requests the execution in the container and handles the result */
            ProcedureResult result = svc.execute(proc);
            if (result.hasFailed())
            throw new ServletException(result.getException());
            else
            {
            price = (BigDecimal) target.get(Path.parse("/unit_price"));
%>
<b>New book price: </b><%=price%><br />
<br />
<%
}
%>

</body>

</html>

Please refer to the API Javadoc for more information.

To run the programmatic service, please open an internet browser and specify the path to the jsp script with the required parameters. The path to the jsp script starts from the module public path , which is the parent directory of the WEB-INF/ directory.

Example:

http://localhost:8080/ebx-tutorial/sampleProgrammaticService.jsp?branch=BooksBranch&adaptation=Books&login=admin&password=admin

Where ebx-tutorial/ directory is the module public path for the considered java project.

Since we have to access the repository outside of EBX.Manager, we need to specify several parameters:

These parameters are required in order to find the Master Data to be updated and authentify the user. In the previous section, we did not need such information since the service was called from the current adaptation in EBX.Manager.

Home > Tutorial