String RequestDataStr = "";
RequestDataStr = ReqStr_FileName ;
try
{
// set to dynamic config:
DynamicConfiguration conf = (DynamicConfiguration) container .getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/SOAP", "FileName_StartKey" );
if ( conf != null){
conf.put(key, RequestDataStr);
}
}
catch (Exception ex)
{
;
}
return RequestDataStr;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;
public class Example_01 implements StreamTransformation {
private String ArchivalPath = "";
private String SourcePath = "";
private AbstractTrace trace = null;
private Map param;
public void setParameter(Map map){
param = map;
if (param == null)
{
param = new HashMap();
}
}
public void execute(InputStream in, OutputStream out) throws StreamTransformationException{
try{
trace = (AbstractTrace) param.get(StreamTransformationConstants.MAPPING_TRACE);
trace.addInfo("Starting JavaMap 'Example_01'");
//Get FileNameKey using DynamicConfig
String FileNameKey = "";
FileNameKey = getFileNameKey();
trace.addInfo("FileName starting key found as: " + FileNameKey);
//Get FolderPath from ResourceFile 'FolderPath_details.xml'
getFolderPath();
trace.addInfo("SourcePath: " + SourcePath);
trace.addInfo("ArchivalPath: " + ArchivalPath);
//----Start:Read File from SAPDirectory & Append it to OutputXml -------
String FileName = "";
String FileContent = "";
String ErrorStr = "";
File theDir = new File(SourcePath);
if (!theDir.exists()){
//Error message 'SourcePath not found'
ErrorStr = "SourcePath '" + SourcePath + "' not found";
}else{
//If directory found, then check if it is empty
File[] dirFileList = new File(SourcePath).listFiles();
if (dirFileList.length < 1){
//Error message 'No file found in SourcePath'
ErrorStr = "No file found in SourcePath '" + SourcePath + "'";
}else{
//If folder is not empty, the search for files
for (int i=0; i<dirFileList.length; i++){
FileName = dirFileList[i].getName(); //Get file name
//check if file is having CSV extensions
if ((FileName.indexOf(".CSV") != -1) || (FileName.indexOf(".csv") != -1)){
//Now check if FileName is having starting key word
if(FileName.startsWith(FileNameKey)){
FileContent = getFileContent(SourcePath + FileName ); //Read file content
//Archive file to other directory
File file = new File(FileName);
String arcFNm = ArchivalPath + "ARC_" + file.getName();
file.renameTo(new File(arcFNm));
ErrorStr = "File found and its content has been read";
break; //break the loop if file found | To return one fileContent at a time
}
}else{
ErrorStr = "No .CSV/.csv file found";
}
}
}
}
//----End :Read File from SAPDirectory & Append it to OutputXml -------
//----Start: Create output XML Document -------------------
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
Document docOut = dbBuilder.newDocument();
//Create XML RootElement
Element rootElmnt = docOut.createElement("ns0:MT_TargetMsg");
docOut.appendChild(rootElmnt); //Append rootElement to Output document
//Set Attribute info to RootElement
Attr attr = docOut.createAttribute("xmlns:ns0");
attr.setValue("http://Test-01");
rootElmnt.setAttributeNode(attr);
//Create child Node
Element Chld1 = docOut.createElement("Item");
rootElmnt.appendChild(Chld1);
//Create sub child node
Element subChld1 = docOut.createElement("FileName");
Chld1.appendChild(subChld1);
Node nd1 = docOut.createTextNode(FileName);
subChld1.appendChild(nd1);
Element subChld2 = docOut.createElement("FileContent");
Chld1.appendChild(subChld2);
CDATASection cdataFCS = docOut.createCDATASection(FileContent);
subChld2.appendChild(cdataFCS);
Element subChld3 = docOut.createElement("Error");
Chld1.appendChild(subChld3);
Node nd3 = docOut.createTextNode(ErrorStr);
subChld3.appendChild(nd3);
//----End : Create output XML Document -------------------
trace.addInfo("Message: " + ErrorStr);
//Transform Output Document
trace.addInfo("Transforming results to output xml payload");
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transform = tf.newTransformer();
transform.transform(new DOMSource(docOut), new StreamResult(out));
} catch (Exception e) {
trace.addInfo(e.getMessage());
}
}
private void getFolderPath() throws IOException, ParserConfigurationException, SAXException {
/*
This function helps to get folder path details from resource 'FolderPath_details.xml'
Jar file of this map will include resource file 'FolderPath_details.xml'
*/
//Get reference of ResourceFile 'FolderPath_details.xml'
InputStream is_ShpCrd = getClass().getResourceAsStream("FolderPath_details.xml");
//Parse input to create document tree
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
Document xmlDoc_ShpCrd = dbBuilder.parse(is_ShpCrd);
//Read XmlElement tag to fetch SharePoint_Credentials
NodeList ndList_1 = xmlDoc_ShpCrd.getElementsByTagName("Path");
for(int i1 = 0; i1 < ndList_1.getLength(); i1++){
Node nd_1 = ndList_1.item(i1);
for(Node nd_2 = nd_1.getFirstChild(); nd_2 != null; nd_2 = nd_2.getNextSibling()){
if(nd_2.getNodeName().equals("SourcePath")){
SourcePath = nd_2.getFirstChild().getNodeValue();
}
if(nd_2.getNodeName().equals("ArchivalPath")){
ArchivalPath = nd_2.getFirstChild().getNodeValue();
}
}
}
}
private String getFileNameKey() throws IOException {
/*
This function helps to get FileName starting Key word
using DynamicConfiguration variable which is set from SoapRequestPayload
*/
String FileNameKey = "";
try{
DynamicConfiguration conf = (DynamicConfiguration)param.get("DynamicConfiguration");
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/SOAP", "FileName_StartKey");
if(conf != null){
FileNameKey = conf.get(key);
}
}catch(Exception e) {
trace.addInfo(e.getMessage());
}
return FileNameKey;
}
private String getFileContent(String filePath) throws IOException {
/*
* This Function read file content into String
*
*/
FileInputStream fin = new FileInputStream(new File(filePath));
java.util.Scanner scanner = new java.util.Scanner(fin,"UTF-8").useDelimiter("\\A");
String theString = scanner.hasNext() ? scanner.next() : "";
scanner.close();
return theString;
}
/*
public static void main(String[] args) {
try{
Example_01 myClass = new Example_01();
FileInputStream in = new FileInputStream("C:/Request_Input_2.xml");
FileOutputStream out = new FileOutputStream("C:/Output.xml");
myClass.execute(in, out);
}catch (Exception e){
e.printStackTrace();
}
}
*/
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
5 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 | |
2 |