Advertisement
erwindegula

Sample PI 7.1 Java Mapping with Dynamic Configuraiton

Jun 27th, 2011
1,958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.53 KB | None | 0 0
  1.  
  2. /**------------------------------------------------------------------------------*
  3.  * Interface ID : SWIFTFIN02
  4.  * Description : Inbound FIN MT Messages
  5.  *
  6.  * Input: SwiftMessage
  7.  * Input Format: XML File
  8.  *
  9.  * Output: SwiftMessage
  10.  * Output Format: XML File
  11.  *
  12.  * Desription : This mapping program will retrieve information from the PDU data
  13.  *        to generate a filename and set it via Dynamic Configuration
  14.  *               - Uses StAX Reader
  15.  *               - Uses Dynamic Configuration to set the Filename
  16.  *------------------------------------------------------------------------------*/
  17. import java.io.*;
  18. import java.util.*;
  19.  
  20.  
  21. import javax.xml.stream.XMLInputFactory;
  22. import javax.xml.stream.XMLStreamConstants;
  23. import javax.xml.stream.XMLStreamException;
  24. import javax.xml.stream.XMLStreamReader;
  25.  
  26. import com.sap.aii.mapping.api.AbstractTransformation;
  27. import com.sap.aii.mapping.api.StreamTransformationException;
  28. import com.sap.aii.mapping.api.TransformationInput;
  29. import com.sap.aii.mapping.api.TransformationOutput;
  30. import com.sap.aii.mapping.api.DynamicConfiguration;
  31. import com.sap.aii.mapping.api.DynamicConfigurationKey;
  32.  
  33. public class MappingImplementation extends AbstractTransformation {
  34.    
  35.      private static final DynamicConfigurationKey KEY_FILENAME =
  36.          DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
  37.                
  38.     String format = null;
  39.     String messageID = null;
  40.     String senderBIC = null;
  41.  
  42.     /**--------------------------------------------*
  43.      *  Transform : called by PI 7.1 IE            *
  44.      *---------------------------------------------*/  
  45.     public void transform (TransformationInput input, TransformationOutput output)
  46.     throws StreamTransformationException {
  47.        
  48.         getTrace().addInfo("Java Mapping started.");
  49.                        
  50.         // Build filename using the following values separated by '_'
  51.         // 1. Format
  52.         // 2. Message Identifier
  53.         // 3. Sender BIC - Institution Code (first 4 characters)
  54.         // 4. Date Time
  55.         // 5. Counter - to be added by File Adapter
  56.         //
  57.         // Sample: MT_fin.199_CITI_20110627_152658_NNNNN
  58.    
  59.         this.execute(input.getInputPayload().getInputStream(), output.getOutputPayload().getOutputStream());       
  60.                
  61.         DynamicConfiguration conf = input.getDynamicConfiguration();
  62.         String dateTime = String.format("%tY%<tm%<td_%<tH%<tM%<tS", new Date());
  63.         String filename = format + "_" +
  64.                           messageID + "_" +
  65.                           senderBIC.substring(0,4) + "_" +
  66.                           dateTime + "_.out" ;
  67.         getTrace().addInfo("Generating filename: " + filename);
  68.         conf.put(KEY_FILENAME, filename);                      
  69.        
  70.         // return the same payload
  71.         String inData = convertStreamToString(input.getInputPayload().getInputStream());
  72.         try {      
  73.             output.getOutputPayload().getOutputStream().write(inData.getBytes("UTF-8"));
  74.         } catch(Exception exception1) { }
  75.                
  76.         getTrace().addInfo("Java Mapping finished.");
  77.                
  78.     }  // transform
  79.    
  80.     /**--------------------------------------------*
  81.      *  convertStreamToString                      *
  82.      *---------------------------------------------*/      
  83.     public String convertStreamToString(InputStream in){
  84.         StringBuffer sb = new StringBuffer();
  85.         try
  86.         {
  87.         InputStreamReader isr = new InputStreamReader(in);
  88.         Reader reader =
  89.         new BufferedReader(isr);
  90.         int ch;
  91.         while((ch = in.read()) > -1) {
  92.             sb.append((char)ch);}
  93.             reader.close();
  94.         }
  95.         catch(Exception exception) { }
  96.         return sb.toString();
  97.        
  98.     }
  99.  
  100.     /**--------------------------------------------*
  101.      *  execute                                    *
  102.      *---------------------------------------------*/          
  103.     public void execute(InputStream in, OutputStream out)
  104.     throws StreamTransformationException {
  105.  
  106.         XMLInputFactory inputFactory = XMLInputFactory.newInstance();
  107.         inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
  108.         try {
  109.             XMLStreamReader xmlStreamReader = inputFactory.createXMLStreamReader(in);
  110.            
  111.             while (xmlStreamReader.hasNext()) {
  112.                 int event = xmlStreamReader.next();
  113.                 if (event == XMLStreamConstants.START_ELEMENT) {
  114.                    
  115.                     if ( xmlStreamReader.getLocalName().equals("Format") ) {                       
  116.                         event = xmlStreamReader.next();
  117.                         if (event == XMLStreamConstants.CHARACTERS) {
  118.                             format = xmlStreamReader.getText();
  119.                             System.out.println("Format : " + format);
  120.                             continue;
  121.                         }                      
  122.                     }
  123.                    
  124.                     if ( xmlStreamReader.getLocalName().equals("MessageIdentifier") ) {                    
  125.                         event = xmlStreamReader.next();
  126.                         if (event == XMLStreamConstants.CHARACTERS) {
  127.                             messageID = xmlStreamReader.getText();
  128.                             System.out.println("Message ID : " + messageID );
  129.                             continue;
  130.                         }                      
  131.                     }                  
  132.                    
  133.                     if ( xmlStreamReader.getLocalName().equals("SenderBIC12") ) {                      
  134.                         event = xmlStreamReader.next();
  135.                         if (event == XMLStreamConstants.CHARACTERS) {
  136.                             senderBIC = xmlStreamReader.getText();
  137.                             System.out.println("Sender BIC : " + senderBIC );
  138.                             continue;
  139.                         }                      
  140.                     }
  141.                      
  142.                 }
  143.                                                
  144.             }    // while (xmlStreamReader.hasNext())
  145.                    
  146.         } catch ( XMLStreamException x ) {
  147.             x.printStackTrace();
  148.         }
  149.            
  150.     }
  151.     /*
  152.      * main method for testing
  153.      */
  154.     public static void main(String[] args) {
  155.         try {
  156.             InputStream in = new FileInputStream(new File("in.xml"));
  157.             OutputStream out = new FileOutputStream(new File("out.xml"));
  158.             MappingImplementation myMapping = new MappingImplementation();
  159.             myMapping.execute(in, out);
  160.             System.out.print("Success!");
  161.         } catch (Exception e) {
  162.             e.printStackTrace();
  163.         }
  164.     }
  165.            
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement