Commit 041a679b authored by mjadhav's avatar mjadhav

add upload file using SFTP


git-svn-id: http://15.206.35.175/svn/proteus/business-java/trunk@97033 ce508802-f39f-4f6c-b175-0d175dae99d5
parent 67216f13
......@@ -33,13 +33,53 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import ibase.webitm.ejb.dis.DistCommon;
import ibase.webitm.ejb.sys.UtilMethods;
import ibase.utility.CommonConstants;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class GenInvoiceSchedule implements Schedule {
private String invHost = "";
private String invUser = "";
private String invPwd = "";
//private String remoteFilePath = "base_out/Stock.csv";
private String remoteFilePath = "";
private String xmlFileNameUpd = "";
private String invRmFilePath="";
private String invLocFilePath="";
private String invBackupFilePath="";
@Override
public String schedule(HashMap arg0) throws Exception {
// TODO Auto-generated method stub
......@@ -66,7 +106,7 @@ public class GenInvoiceSchedule implements Schedule {
conn = connDriver.getConnectDB("DriverITM");
String sql = "",retString="";
String sql = "",retString="",finParamTranType="",filePath="",updResult="";
PreparedStatement pstmt = null;
ResultSet rs = null;
String siteCodeList="",siteCode="",invoiceId="";
......@@ -76,7 +116,7 @@ public class GenInvoiceSchedule implements Schedule {
/*String queryString = "",suppAssignCode="";
String sql = "",suppCode="",retString="",tranId="",siteCode="",suppCodeList="",;
*/
FinCommon finCommon = new FinCommon();
try
......@@ -133,20 +173,26 @@ public class GenInvoiceSchedule implements Schedule {
{
invoiceIdList.add(rs.getString("INVOICE_ID"));
}
System.out.println(">>>>>>>>For siteCode :"+siteCode+" of invoiceIdList for invoice :"+invoiceIdList);
System.out.println("invoiceIdList for invoice :"+invoiceIdList);
rs.close();
rs = null;
pstmt.close();
pstmt = null;
/*----------------------------------------------------------------------------------------------------------------*/
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse("/IBASEHOME/ibase.xml");
invLocFilePath = dom.getElementsByTagName("INV_EXPORT_PATH").item(0).getFirstChild().getNodeValue();
System.out.println("invLocFilePath :"+invLocFilePath);
for (int i = 0; i < invoiceIdList.size(); i++)
{
invoiceId = invoiceIdList.get(i);
System.out.println(invoiceId);
retString = generateInvoiceXML(invoiceId,conn);
filePath=invLocFilePath+"/invoice_"+invoiceId.trim()+".xml";
System.out.println("filePath :"+filePath);
retString = generateInvoiceXML(filePath,invoiceId,conn);
......@@ -169,6 +215,42 @@ public class GenInvoiceSchedule implements Schedule {
conn.commit();
invoiceIdSuccList.add(invoiceId);
System.out.println("invoice Id : "+invoiceId+" updated TRACE_STATUS 'Y' succefully!!!");
/*--------------added by mahendra dated 24-DEC-2014-------------------*/
finParamTranType=checkNull(finCommon.getFinparams("999999","INV_EDI_AUTO_UPLOAD",conn));
System.out.println("finParamTranType :"+finParamTranType);
if(finParamTranType.equalsIgnoreCase("Y"))
{
//filePath=this is local path where invoice xml is generated.
//invRmFilePath = this is remote path where invoice xml is uploaded
//invUser=SFTP username
//invHost=SFTP Host
//invPwd=SFTP Password
setSFTPConfig();
invRmFilePath=invRmFilePath+"/invoice_"+invoiceId.trim()+".xml";
invBackupFilePath=invBackupFilePath+"/invoice_"+invoiceId.trim()+".xml";
System.out.println("invRmFilePath by appending invoice xml @@@@"+invRmFilePath);
updResult=putFile(invUser, invHost,invPwd,invRmFilePath,filePath);
if(updResult.equalsIgnoreCase("success"))
{
File fromFile = new File(filePath);
File toFile = new File(invBackupFilePath);
moveUploadFile(fromFile,toFile);
System.out.println("invoice"+invoiceId.trim()+".xml uploaded sucessfully and move to backup folder!!!");
}
else
{
System.out.println("invoice"+invoiceId.trim()+".xml uploaded failed!!!");
}
}
/*-----------------------------------------------------------------------------*/
}
catch(Exception e)
{
......@@ -249,21 +331,21 @@ public class GenInvoiceSchedule implements Schedule {
public String generateInvoiceXML(String invoiceId,Connection conn)throws RemoteException, ITMException
public String generateInvoiceXML(String filePath,String invoiceId,Connection conn)throws RemoteException, ITMException
{
String errString="",filepath="";
String errString="";
ITMDBAccessLocal itmDBAccessLocal = new ITMDBAccessEJB();
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
/* DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse("/IBASEHOME/ibase.xml");
filepath = dom.getElementsByTagName("JBOSSHOME").item(0).getFirstChild().getNodeValue();
filepath=filepath +"/export";
System.out.println("filepath :::: "+filepath);
/*-------------------------------------------------*/
File directory = new File(filepath);
if (directory.exists())
{
......@@ -279,12 +361,14 @@ public class GenInvoiceSchedule implements Schedule {
System.out.println("directory :"+directory);
}
/*---------------------------------------------------------------*/
//filepath=filepath+"/export/facturae"+invoiceId+".xml";
filepath=directory+"/invoice_"+invoiceId.trim()+".xml";
System.out.println("filepath :"+filepath);
filepath=directory+"/invoice_"+invoiceId.trim()+".xml";*/
System.out.println("filepath :"+filePath);
DocumentBuilderFactory icFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder icBuilder;
......@@ -304,7 +388,7 @@ public class GenInvoiceSchedule implements Schedule {
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
// StreamResult streamResult = new StreamResult(new File("//home//basedv-2//mahendra//work//xmlFile//Sample_Facturae11.xml"));
StreamResult streamResult = new StreamResult(new File(filepath));
StreamResult streamResult = new StreamResult(new File(filePath));
transformer.transform(domSource, streamResult);
......@@ -384,7 +468,7 @@ public class GenInvoiceSchedule implements Schedule {
sql = "select iso4217 from TARODEV.currency where curr_code=? " ;
sql = "select iso4217 from currency where curr_code=? " ;
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, currencyCode);
......@@ -415,7 +499,28 @@ public class GenInvoiceSchedule implements Schedule {
// fileHeader.setAttribute("id", id);
fileHeader.appendChild(getElementValue(doc, fileHeader, "SchemaVersion", "3.2"));
fileHeader.appendChild(getElementValue(doc, fileHeader, "Modality", "I"));
fileHeader.appendChild(getElementValue(doc, fileHeader, "InvoiceIssuerType", "EM"));
fileHeader.appendChild(getElementValue(doc, fileHeader, "InvoiceIssuerType", "TE"));
Element thirdParty = doc.createElement("ThirdParty");
Element thirdPartyTaxId = doc.createElement("TaxIdentification");
thirdPartyTaxId.appendChild(getElementValue(doc, fileHeader, "PersonTypeCode","J"));
thirdPartyTaxId.appendChild(getElementValue(doc, fileHeader, "ResidenceTypeCode","R"));
thirdPartyTaxId.appendChild(getElementValue(doc, fileHeader, "TaxIdentificationNumber","ESB96490867"));
thirdParty.appendChild(thirdPartyTaxId);
Element thirdPartyEntity = doc.createElement("LegalEntity");
thirdPartyEntity.appendChild(getElementValue(doc, fileHeader, "CorporateName","Intercambio Electrónico de Datos,SL"));
Element thirdPartyAddInSpain = doc.createElement("AddressInSpain");
thirdPartyAddInSpain.appendChild(getElementValue(doc, fileHeader, "Address","Ronda de Auguste y Louis Lumiere 12"));
thirdPartyAddInSpain.appendChild(getElementValue(doc, fileHeader, "PostCode","46980"));
thirdPartyAddInSpain.appendChild(getElementValue(doc, fileHeader, "Town","Paterna"));
thirdPartyAddInSpain.appendChild(getElementValue(doc, fileHeader, "Province","Valencia"));
thirdPartyAddInSpain.appendChild(getElementValue(doc, fileHeader, "CountryCode","ESP"));
thirdPartyEntity.appendChild(thirdPartyAddInSpain);
fileHeader.appendChild(thirdPartyEntity);
fileHeader.appendChild(thirdParty);
Element batch = doc.createElement("Batch");
batch.appendChild(getElementValue(doc, fileHeader, "BatchIdentifier",invoiceId));
batch.appendChild(getElementValue(doc, fileHeader, "InvoicesCount","1"));
......@@ -653,7 +758,7 @@ public class GenInvoiceSchedule implements Schedule {
Element administrativeCentresparent = doc.createElement("AdministrativeCentres");
Element admCustDlv = doc.createElement("AdministrativeCentres");
Element admCustDlv = doc.createElement("AdministrativeCentre");
admCustDlv.appendChild(getElementValue(doc, parties, "CentreCode", custCodeDlv));
admCustDlv.appendChild(getElementValue(doc, parties, "RoleTypeCode", "01"));
// administrativeCentres.appendChild(getElementValue(doc, parties, "RoleTypeCode", "03"));
......@@ -672,7 +777,7 @@ public class GenInvoiceSchedule implements Schedule {
administrativeCentresparent.appendChild(admCustDlv);
/*-------------------------------------------------------------------*/
Element admCustBill = doc.createElement("AdministrativeCentres");
Element admCustBill = doc.createElement("AdministrativeCentre");
if(custCodeBill != "")
{
admCustBill.appendChild(getElementValue(doc, parties, "CentreCode", custCodeBill));
......@@ -695,7 +800,7 @@ public class GenInvoiceSchedule implements Schedule {
Element administrativeCentres = doc.createElement("AdministrativeCentres");
Element administrativeCentres = doc.createElement("AdministrativeCentre");
administrativeCentres.appendChild(getElementValue(doc, parties, "CentreCode", custCode));
administrativeCentres.appendChild(getElementValue(doc, parties, "RoleTypeCode", "03"));
// administrativeCentres.appendChild(getElementValue(doc, parties, "RoleTypeCode", "03"));
......@@ -798,9 +903,9 @@ public class GenInvoiceSchedule implements Schedule {
System.out.println("invoiceDtHd : "+invoiceDtHd);
paymentMeans=getSiteRegvalue(conn,"IBAN",siteCode);
//paymentMeans=getSiteRegvalue(conn,"IBAN",siteCode);
accToBeCredited=getSiteRegvalue(conn,"ACCTNO",siteCode);
System.out.println("paymentMeans : "+paymentMeans);
//System.out.println("paymentMeans : "+paymentMeans);
System.out.println("accToBeCredited : "+accToBeCredited);
......@@ -948,7 +1053,8 @@ public class GenInvoiceSchedule implements Schedule {
invoiceIssueData.appendChild(invoicingPeriod);
invoiceIssueData.appendChild(getElementValue(doc, invoiceParent, "InvoiceCurrencyCode", invoiceISOCode));
//invoiceIssueData.appendChild(getElementValue(doc, invoiceParent, "InvoiceCurrencyCode", "EUR"));
invoiceIssueData.appendChild(getElementValue(doc, invoiceParent, "TaxCurrencyCode", "EUR"));
//invoiceIssueData.appendChild(getElementValue(doc, invoiceParent, "TaxCurrencyCode", "EUR"));
invoiceIssueData.appendChild(getElementValue(doc, invoiceParent, "TaxCurrencyCode", invoiceISOCode));
invoiceIssueData.appendChild(getElementValue(doc, invoiceParent, "LanguageName", "es"));
invoice.appendChild(invoiceIssueData);
......@@ -981,8 +1087,8 @@ public class GenInvoiceSchedule implements Schedule {
invoiceTotals.appendChild(getElementValue(doc, invoiceParent, "TotalGrossAmount",utilMethods.getReqDecString(NetAmtHd,2)));
Element generalDiscounts = doc.createElement("GeneralDiscounts");
Element discount = doc.createElement("Discount");
discount.appendChild(getElementValue(doc, invoiceParent, "DiscountReason", "Sales Promotions"));
//discount.appendChild(getElementValue(doc, invoiceParent, "DiscountReason", "Sales Promotions"));
discount.appendChild(getElementValue(doc, invoiceParent, "DiscountReason", "Descuentos Varios"));
discount.appendChild(getElementValue(doc, invoiceParent, "DiscountAmount", utilMethods.getReqDecString(discAmtHd,2)));
generalDiscounts.appendChild(discount);
invoiceTotals.appendChild(generalDiscounts);
......@@ -1012,8 +1118,8 @@ public class GenInvoiceSchedule implements Schedule {
Element discountsAndRebates = doc.createElement("DiscountsAndRebates");
Element discountItem = doc.createElement("Discount");
discountItem.appendChild(getElementValue(doc, invoiceParent, "DiscountReason", "Sales Promotions"));
//discountItem.appendChild(getElementValue(doc, invoiceParent, "DiscountReason", "Sales Promotions"));
discountItem.appendChild(getElementValue(doc, invoiceParent, "DiscountReason", "Descuentos varios"));
if(!(tempMap.get("disc_amt" + cnt).equalsIgnoreCase("0.0")))
{
......@@ -1039,11 +1145,11 @@ public class GenInvoiceSchedule implements Schedule {
Element taxesWithheldItem = doc.createElement("TaxesWithheld");
Element taxHeldItem = doc.createElement("TaxesWithheld");
Element taxHeldItem = doc.createElement("Tax");
taxHeldItem.appendChild(getElementValue(doc, invoiceParent, "TaxTypeCode", "01"));
taxHeldItem.appendChild(getElementValue(doc, invoiceParent, "TaxRate", "0.00"));
Element taxableTotAmt = doc.createElement("TotalAmount");
taxableTotAmt.appendChild(getElementValue(doc, invoiceParent, "TaxableBase","0.00"));
Element taxableTotAmt = doc.createElement("TaxableBase");
taxableTotAmt.appendChild(getElementValue(doc, invoiceParent, "TotalAmount","0.00"));
taxableTotAmt.appendChild(taxesWithheldItem);
taxHeldItem.appendChild(getElementValue(doc, invoiceParent, "TaxAmount", "0.00"));
taxesWithheldItem.appendChild(taxHeldItem);
......@@ -1056,8 +1162,8 @@ public class GenInvoiceSchedule implements Schedule {
Element taxItem = doc.createElement("Tax");
taxItem.appendChild(getElementValue(doc, invoiceParent, "TaxTypeCode", "01"));
taxItem.appendChild(getElementValue(doc, invoiceParent, "TaxRate",utilMethods.getReqDecString(taxPersc,2)));
Element taxableTotAmtOutputs = doc.createElement("TotalAmount");
taxableTotAmtOutputs.appendChild(getElementValue(doc, invoiceParent, "TaxableBase",utilMethods.getReqDecString(taxBaseAmt,2)));
Element taxableTotAmtOutputs = doc.createElement("TaxableBase");
taxableTotAmtOutputs.appendChild(getElementValue(doc, invoiceParent, "TotalAmount",utilMethods.getReqDecString(taxBaseAmt,2)));
taxItem.appendChild(taxableTotAmtOutputs);
taxItem.appendChild(getElementValue(doc, invoiceParent, "TaxAmount", (String) tempMap.get("tax_amt" + cnt)));
......@@ -1080,7 +1186,7 @@ public class GenInvoiceSchedule implements Schedule {
installment.appendChild(getElementValue(doc, invoiceParent, "InstallmentAmount", (String) tempMap.get("net_amt1")));
installment.appendChild(getElementValue(doc, invoiceParent, "PaymentMeans", "04"));
Element accountToBeCredited = doc.createElement("AccountToBeCredited");
accountToBeCredited.appendChild(getElementValue(doc, invoiceParent, "IBAN",accToBeCredited));
accountToBeCredited.appendChild(getElementValue(doc, invoiceParent, "IBAN",accToBeCredited.replaceAll("-","")));
installment.appendChild(accountToBeCredited);
paymentDetails.appendChild(installment);
......@@ -1137,6 +1243,139 @@ public class GenInvoiceSchedule implements Schedule {
return result;
}
/*------------added by mahendra dated 24-DEC-2014 for file upload-------------------------*/
private String putFile(String username, String host, String password, String remotefile, String localfile)
{
JSch jsch = new JSch();
Session session = null;
try {
System.out.println("inside putFile ...........");
System.out.println("username :"+username);
System.out.println("host :"+host);
System.out.println("password :"+password);
System.out.println("remotefile :"+remotefile);
System.out.println("localfile :"+localfile);
//getSession(String username, String host, int port)
session = jsch.getSession(username, host, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
System.out.println("channel success...........");
ChannelSftp sftpChannel = (ChannelSftp) channel;
System.out.println("process starts for upload localfile to remote path.......");
sftpChannel.put(localfile, remotefile);
System.out.println("The first file is uploaded successfully.");
sftpChannel.exit();
session.disconnect();
return "success";
} catch (JSchException e) {
e.printStackTrace();
return "false";
} catch (SftpException e) {
e.printStackTrace();
return "false";
}
}
private void setSFTPConfig()
{
CommonConstants objCommon =new CommonConstants();
GenericUtility genericUtility=GenericUtility.getInstance();
String fileName = null,xmlString = "";
Document dom=null;
String sCurrentLine = "";
//String xmlFile="DDSalesConfig";
System.out.println(" in setSFTPConfig -------------------------");
//String xmlFileName = CommonConstants.APPLICATION_CONTEXT + "setting" + File.separator + "DDSalesConfig.xml";
// String xmlFileName = CommonConstants.APPLICATION_CONTEXT + "setting" + File.separator + "DDSalesConfig.xml";
//fileName = ".." + File.separator + "setting" + File.separator + xmlFile + ".xml";
StringBuilder sb = new StringBuilder();
// System.out.println("xmlFileName-->>["+xmlFileName+"]");
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse("/IBASEHOME/ibase.xml");
this.invRmFilePath = dom.getElementsByTagName("INV_SFTP_PATH").item(0).getFirstChild().getNodeValue();
System.out.println("invRmFilePath :::: "+invRmFilePath);
this.invLocFilePath = dom.getElementsByTagName("INV_EXPORT_PATH").item(0).getFirstChild().getNodeValue();
System.out.println("invLocFilePath :::: "+invLocFilePath);
this.invBackupFilePath = dom.getElementsByTagName("INV_BACK_PATH").item(0).getFirstChild().getNodeValue();
System.out.println("invBackupFilePath :::: "+invBackupFilePath);
this.invHost = dom.getElementsByTagName("INV_SFTP_HOST").item(0).getFirstChild().getNodeValue();
System.out.println("invHost :::: "+invHost);
this.invUser = dom.getElementsByTagName("INV_SFTP_USER").item(0).getFirstChild().getNodeValue();
System.out.println("invUser :::: "+invUser);
this.invPwd = dom.getElementsByTagName("INV_SFTP_PWD").item(0).getFirstChild().getNodeValue();
System.out.println("invPwd :::: "+invPwd);
/*
BufferedReader br = new BufferedReader(new FileReader(xmlFileName));
while (( sCurrentLine = br.readLine()) != null)
{
sb.append(sCurrentLine);
}
xmlString =sb.toString();
System.out.println("DDSalesConfig xmlString---->>["+xmlString+"]");
dom = genericUtility.parseString(xmlString);
if(dom != null)
{
this.xmlFileNameUpd = genericUtility.getColumnValue("STOCK_TEMP_UPD_PATH",dom);
this.hostName = genericUtility.getColumnValue("SFTP_HOST",dom);
this.username = genericUtility.getColumnValue("SFTP_USER",dom);
this.password = genericUtility.getColumnValue("SFTP_PWD",dom);
}*/
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void moveUploadFile(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File is copied successful!");
src.delete();
System.out.println("original file delete successful !");
}
/*----------------------------------------------------------------------------------------------*/
private String checkNull(String input)
{
if (input == null)
{
input = "";
}
return input;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment