Home > Tutorial

Add Programmatic Service for Import

In this chapter, we will describe another way to tackle the connectivity problem using Programmatic Services.

JavaDoc Programmatic Services allow to execute a procedure directly in Java and out of webMethods Master Data Manager context, through the com.softwareag.mdm.service.ProgrammaticService Java class.

An easy way to use it is through a JSP as decribed in the following example, which replicates the import process described in the UI Service chapter, but out of webMethods Master Data Manager. This implies that the process integrates login features so webMethods MDM can determine whether the user is allowed to access the adaptation or not.

<%-- importScript.jsp --%>

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@ page import="com.softwareag.mdm.service.*"%> 
<%@ page import="com.softwareag.mdm.adaptation.*"%> 
<%@ page import="java.io.*"%> 
<%@ page import="com.softwareag.mdm.instance.*"%> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<html> 
<head> 
<title>webMethods MDM Import Script Sample</title> 
</head> 

<body> 

<% 
            // Retrieves parameters 
             
            String branch = request.getParameter("branch"); 
            String adaptationName = request.getParameter("adaptation"); 
            String login = request.getParameter("login"); 
            String password = request.getParameter("password"); 
            String srcFile = request.getParameter("file"); 
             
            if (adaptationName == null
                throw new ServletException( 
                    "Adaptation name not specified (HTTP parameter 'adaptation' is expected)."); 
             
            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."); 

            if (srcFile == null
                throw new ServletException( 
                    "Source filename not specified (HTTP parameter 'file' is expected)."); 
            final File file = new File(srcFile); 
            if(!file.exists()) 
            { 
                throw new ServletException("Source file with name " + srcFile 
                    + " does not exist."); 
            } 
                         
            // Creates a container for runnning services 
            Session aSession=defaultRepository.createSessionFromLoginPassword(login, password); 
            ProgrammaticService svc = ProgrammaticService.createForSession(aSession, home); 
             
            svc.setSessionTrackingInfo("Script Sample"); 
            final StringBuffer msg = new StringBuffer(); 

            final ImportSpec is = new ImportSpec(); 
            is.setSourceFile(file); 
            is.setTargetAdaptation(ada); 

            // Defines the procedure 
            Procedure proc = new Procedure() 
            { 
                public void execute(ProcedureContext aContext) throws Exception 
                { 
                    aContext.doImport(is); 
                    msg.append("File ["+ file + "] has been imported into adaptation [" + ada.toPublicReference() 
                        + "]."); 
                } 
            }; 
            // Requests the execution in the container and handles the result 
            ProcedureResult result = svc.execute(proc); 
            String summary; 
            if (result.hasFailed()) 
                summary = "Exception: " + result.getException(); 
            else 
                summary = "Execution successful: " + msg; 
%> 
<p>&nbsp;</p> 
<p>Execution UUID : <%=result.getExecutionUUIDString()%></p> 
<p>Execution timestamp : <%=result.getExecutionTimestamp()%></p> 
<p><%=summary%></p> 
<p>&nbsp;</p> 

</body> 
</html> 

JavaDocPlease refer to the API Java doc for more information.

 

To run the import process, 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/mdm-tutorial/importScript.jsp?branch=BooksBranch&adaptation=Books&login=admin&password=admin&file=C:\SalesDetail_to_import.xml

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

Since here we have to access the repository outside of webMethods Master Data Manager, we need to specify several parameters such as: the target branch and the target adaptation where are located the master data to update; login and password in order to verify that the user has the required access rignts to the data; and finally the XML file containing the data to import. In the previous section, we did not need such information since the services (import and export) were called from the current adaptation.

 

Home > Tutorial