Lesson 5 : Fetch Customerdetails

 

The Plan

 

  • Getting record details of a particular customer on the basis of its id.
  • Validating the input request.
  • Transforming the server response to a solid xml-document.

 

Files

 

 

 Create this file structure in your editor, and add the files and directories in your editor. The actual adapter resides in the file ConfigurationGetCustomerDetails.xml ; we are sending a POST request to our Ibis, in which we wrap a client-id that we want to investigate. We could use a GET request with a request parameter, but we will covert that in another section of the course.

 

 

 

 

 

Question Time

In CreateCustomer we saw how to post a request and how we should validate that request. It should be a piece of cake now to write your klantDetailsRequest.xsd with the following prerequisites : A root element called “Request” that contains one simple element called “Klantnummer” that contains a numerical positive value with at most 10 digits.

 

Planning

 Our database output has to be transformed to a document that gives all the details of that specific record. When talking about transformation, this is xslt. And the corresponding data mapping table looks straightforward :
Destination: output Source: table customers
Element Level M/O Type Path Element M/O Type Condition
Response 0
  Klantnummer 1 M customers CLIENTNR
  Voorletters 1 O customers INITIALS
  Voornaam 1 O customers FIRSTNAME
  Achternaam 1 M customers LASTNAME
  Titel 1 O customers TITLE
  Straat 1 O customers STREET
  Postcode 1 M customers POSTALCODE
  Huisnummer 1 O customers HOUSENR
  Huisnummer2 1 O customers ADDITION
  Plaats 1 O customers City
  Aanmaakdatum 1 O customers CREATIONDATE

 

Our full Configuration.xml will look as follows now :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration [
    <!ENTITY CreateCustomer SYSTEM "ConfigurationCreateCustomer.xml">
    <!ENTITY GetDetails SYSTEM "ConfigurationGetCustomerDetails.xml">
    <!ENTITY GetOverview SYSTEM "ConfigurationGetCustomerOverview.xml"> ]>

<configuration name="YOUR CONFIGURATIONNAME HERE">
    <jmsRealms>
        <jmsRealm realmName="jdbc" datasourceName="jdbc/${instance.name.lc}" />
    </jmsRealms>
    &CreateCustomer;
    &GetOverview;
      &GetDetails;
</configuration>

 

Through the pipeline !

 

 

Let’s start coding!

STEP 1  As usual we will create an Adapter with a GenericReceiver that holds an ApiListener inside. The listener should consume and produce xml.

<module>
    <adapter name="GetCustomerDetails" description="Adapter om klantgegevens uit een backend systeem te halen">
        <receiver className="nl.nn.adapterframework.receivers.GenericReceiver" name="GetCustomerDetails_Receiver">
            <listener className="nl.nn.adapterframework.http.rest.ApiListener" 
                      name="GetCustomerDetails_Listener"
                      uriPattern="customerdetails"
                      method="POST"
                      consumes="XML"
                      produces="XML"
                    />
        </receiver>
</module>        

STEP 2   Now we will add a pipeline that holds a FixedQuerySender that makes a db query for the given CustomerId; In lesson 1 we already used parameters in a FixedQuerySender. One thing we didn’t speak of was the sessionKey that could refer to our original message. Note that this a reserverd keyword: “originalMessage“. The parameter also gets an xpath expression that queries this original message. A name has to be supplied, and there we go !

<pipeline firstPipe="ExecuteSelectQuery">
    <pipe name="ExecuteSelectQuery" 
        className="nl.nn.adapterframework.pipes.GenericMessageSendingPipe">
        <sender name="ExecuteInsertQuery" 
            className="nl.nn.adapterframework.jdbc.FixedQuerySender"
            queryType="select"
            query="SELECT * FROM CUSTOMERS WHERE ClientNr=?" 
            jmsRealm="jdbc">
            <param  sessionKey="originalMessage" 
                    xpathExpression="Request/Klantnummer"
                    name="Klantnummer"/>
        </sender>
        <forward name="success" path="MapResponse"/>
    </pipe>
</pipeline>

STEP 3   We continue to the familiar XsltPipe to write the db response into a message that we will return.

<pipe name="MapResponse" 
        className="nl.nn.adapterframework.pipes.XsltPipe" 
        styleSheetName="GetCustomerDetails/xsl/mapResponse.xsl">
        <forward name="success" path="EXIT"/>
</pipe>

STEP 4   An input- and an outputValidator will be added to the adapter, as well as the now familiar exits. You can place them all at the beginning of the pipeline.

<exits>
    <exit path="EXIT" state="success"/>
</exits>
<inputValidator
    className="nl.nn.adapterframework.pipes.XmlValidator"
    schema="GetCustomerDetails/xsd/klantDetailsRequest.xsd"
    root="Request"
    throwException="true">
</inputValidator>
<outputValidator
    className="nl.nn.adapterframework.pipes.XmlValidator"
    schema="GetCustomerDetails/xsd/klantDetailsResponse.xsd"
    root="Response"
    throwException="true">
</outputValidator>

 

 

Testing the app

 

When we have posted our Adapter we can test it under “test a pipeline”; We have to give it a plausible input and the result should be the record of one of our customers. Only what would they buy ? Let’s keep that for another lesson !

 

 

 

 

A succesful postman test will look like this.

Solutions

ConfigurationGetCustomerDetails.xml

klantdetailsrequest.xml

klantdetailsresponse.xml

mapresponse.xsl

troubleshooting

 

 

 


Leave a Comment