FTP, SFTP explore , placing/pushing files integration through code with d365fo
class SSC_DataEntityXmlGeneration
{
// Generate XML file from DMF Project
public static System.IO.StreamReader generateXml(Name entityName,DMFSourceName sourceName,container queryData = conNull())
{
SharedServiceUnitFileID fileId;
System.IO.Stream fileStream;
Description255 filepath;
str definitionGroupName = "SSC XML Generation";
#DMF
DMFDefinitionGroup::find(definitionGroupName, true).delete();
DMFEntityExporter exporter = new DMFEntityExporter();
fileId = exporter.exportToFile(entityName,definitionGroupName,"",sourceName,#FieldGroupName_AllFields,queryData);
if(fileId)
{
str downloadUrl = DMFDataPopulation::getAzureBlobReadUrl(str2Guid(fileId));
filestream = File::UseFileFromURL(downloadUrl);
System.IO.StreamReader sReader = new System.IO.StreamReader(filestream);
DMFDefinitionGroup::find(definitionGroupName, true).delete();
return sReader;
}
else
{
throw Error("@SSC:NOFileid");
}
}
}
//Upload a file to SFTP
using Renci.SshNet;
Using System.IO;
class SSC_SFTPFileUpload
{
public static void fileUploadWithStream(System.IO.Stream _memoryStream,Description255 _SFTPserverName , Description255 _SFTPfileName, Description _SFTPuserId,Description _SFTPPassword,int _portNo=22)
{
System.Exception ex;
try
{
using(SftpClient sftp = new Renci.SshNet.SftpClient(_SFTPserverName,_portNo,_SFTPuserId,_SFTPPassword))
{
sftp.Connect();
sftp.BufferSize = 4 * 1024;
sftp.UploadFile(_memoryStream,_SFTPfileName,true,null);
info("@SSC:FileUploadSuccessMsg");
}
}
catch(Exception::CLRError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ex = ex.get_InnerException();
if (ex != null)
{
throw error(ex.ToString());
}
}
}
}
// Browse all files in SFTP Folder
public static container exploreFiles(Description255 _SFTPserverName , Description255 _SFTPfileName, Description _SFTPuserId,Description _SFTPPassword,int _portNo=22)
{
System.Exception ex;
container conFTPFilesDownload;
try
{
using(SftpClient sftp = new Renci.SshNet.SftpClient(_SFTPserverName,_portNo,_SFTPuserId,_SFTPPassword))
{
sftp.Connect();
container con;
Description255 filetxtName;
var files = sftp.ListDirectory(_SFTPfileName,null);
var enumerator = files.GetEnumerator();
while (enumerator.MoveNext())
{
con = str2con(enumerator.current.ToString(),",");
filetxtName = conPeek(con,1);
filetxtName = subStr(filetxtName,5,strLen(filetxtName)-4);
if(filetxtName && strAlpha(filetxtName) && strFind(filetxtName,".",0,strLen(filetxtName)))
{
conFTPFilesDownload += filetxtName;
}
}
sftp.Disconnect();
}
}
catch(Exception::CLRError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ex = ex.get_InnerException();
if (ex != null)
{
throw error(ex.ToString());
}
}
}
return conFTPFilesDownload;
}
// Move a file from one SFTP folder to another
public static void moveSftpFile(Description255 _SFTPserverName , Description _SFTPuserId,Description _SFTPPassword,Filename _origFilepath, Filename _destfilePath,int _portNo=22)
{
using(SftpClient sftp = new Renci.SshNet.SftpClient(_SFTPserverName,_portNo,_SFTPuserId,_SFTPPassword))
{
sftp.Connect();
sftp.RenameFile(_origFilepath,_destfilePath);
sftp.Disconnect();
}
}
}
using Renci.SshNet;
Using System.IO;
class SSC_Salary2WaysPaymentTotals
{
Description project,costCentre,resource,activity;
container con;
SSC_Salary2WaysPaymentTotalsStagingTable stagingTable;
str fileNameType;
SSC_Salary2WaysFTPExportParameters exportParameters = SSC_Salary2WaysFTPExportParameters::find() ;
RecordInsertList insList = new RecordInsertList(tableNum(SSC_Salary2WaysPaymentTotalsStagingTable));
[SysEntryPointAttribute(false)]
public void createGLJEntry()
{
if(exportParameters.SSC_IsSFTP)
{
con = SSC_SFTPFileUpload::exploreFiles(exportParameters.FilePath,exportParameters.PaymentFolderName,exportParameters.FileUserName,exportParameters.FilePwd,exportParameters.SSC_PortNum);
this.readSFtpFiles();
}
else
{
con = SSC_FTPFileUpload::exploreFiles(exportParameters.FilePath+exportParameters.PaymentFolderName,exportParameters.FileUserName,exportParameters.FilePwd);
this.readFtpFiles();
}
}
public DimensionDefault createDefaultDimension()
{
DimensionAttributeValueSetStorage valueSetStorage = new DimensionAttributeValueSetStorage();
DimensionDefault result;
int i;
DimensionAttribute dimensionAttribute;
DimensionAttributeValue dimensionAttributeValue;
container conAttr = ["Project", "Resource", "CostCenter","Activity"];
container conValue = [project,resource,costCentre,activity];
str dimValue;
for (i = 1; i <= conLen(conAttr); i++)
{
dimensionAttribute = dimensionAttribute::findByName(conPeek(conAttr,i));
if (dimensionAttribute.RecId == 0)
{
continue;
}
dimValue = conPeek(conValue,i);
if (dimValue != "")
{
dimensionAttributeValue = dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,dimValue,false,true);
valueSetStorage.addItem(dimensionAttributeValue);
}
}
result = valueSetStorage.save();
//It reutrns the value of type DimensionDefault
return result;
}
// Read FTP files
public void readFtpFiles()
{
System.Object ftpo;
System.Net.FtpWebRequest request;
System.IO.StreamReader reader;
System.Net.NetworkCredential credential;
System.Net.FtpWebResponse response;
System.String text;
for (int i = 1 ; i <= conLen(con) ; i++)
{
fileNameType = conPeek(con,i);
select firstonly stagingTable
where stagingTable.FileName == fileNameType;
if(!stagingTable.RecId)
{
ftpo = System.Net.WebRequest::Create(exportParameters.FilePath+exportParameters.PaymentFolderName+ @"/" + fileNameType);
request = ftpo;
if(request)
{
credential = new System.Net.NetworkCredential(exportParameters.FileUserName,exportParameters.FilePwd);
request.set_Credentials(credential);
response = request.GetResponse();
if(response)
{
reader = new System.IO.StreamReader(response.GetResponseStream());
text = reader.ReadToEnd();
container constr = str2con(text,"\n");
for (int j = 1; j <= conLen(constr) ; j++)
{
stagingTable.FileString = conPeek(constr,i);
stagingTable.FileName = fileNameType;
insList.add(stagingTable);
}
}
}
}
insList.insertDatabase();
this.createrecords();
}
}
// Read SFTP files
public void readSFtpFiles()
{
for (int i = 1 ; i <= conLen(con) ; i++)
{
fileNameType = conPeek(con,i);
fileNameType = strRTrim(strLTrim(fileNameType));
select firstonly stagingTable
where stagingTable.FileName == fileNameType;
if(!stagingTable.RecId)
{
using(SftpClient sftp = new Renci.SshNet.SftpClient(exportParameters.FilePath,exportParameters.SSC_PortNum,exportParameters.FileUserName,exportParameters.FilePwd))
{
sftp.Connect();
var files = sftp.ReadLines(exportParameters.PaymentFolderName+fileNameType);
var enumerator = files.GetEnumerator();
while (enumerator.MoveNext())
{
str text = enumerator.current.ToString();
stagingTable.FileString = text;
stagingTable.FileName = fileNameType;
insList.add(stagingTable);
}
sftp.Disconnect();
}
}
insList.insertDatabase();
this.createrecords();
}
}
public void createrecords()
{
int recordInserted;
MCRLedgerJournal journalTable;
LedgerJournalTable ledgerJournalTable;
Counter recordsInserted;
TransDate transDate;
DimensionDynamicAccount mainAcc;
Str1260 txt;
Amount crAmt,dbAmt;
TaxGroup taxGroup;
TaxItemGroup taxItemGroup;
CurrencyCode currency;
Str1260 fileName;
while select forupdate stagingTable
where stagingTable.IsFileDataProcessed == NoYes::No
&& stagingTable.HasDataError == NoYes::No
{
container condata = str2con(stagingTable.FileString,";");
transDate = str2Date(conPeek(condata,1),321);
mainAcc = conPeek(condata,2);
txt = conPeek(condata,3);
crAmt = str2Num(conPeek(condata,4));
dbAmt = str2num(conPeek(condata,5));
project = conPeek(condata,7);;
costCentre = conPeek(condata,6);
resource = conPeek(condata,8);
activity = conPeek(condata,9);
taxGroup = conPeek(condata,10);
taxItemGroup = conPeek(condata,11);
currency = conPeek(condata,13);
try
{
journalTable = new MCRLedgerJournal_Daily(LedgerJournalType::Daily,exportParameters.PaymentJournalName);
//Instatiate the General Journal Table
ledgerJournalTable = journalTable.createLedgerJournalTable(exportParameters.PaymentJournalName);
journalTable.parmLedgerJournalTable(ledgerJournalTable);
journalTable.parmMCRCCGeneralLedgerId();
journalTable.parmLedgerAccountType(LedgerJournalACType::Ledger);
journalTable.parmLedgerOffsetAccountType(LedgerJournalACType::Ledger);
journalTable.parmTransTxt(txt);
journalTable.parmTransDate(transDate);
journalTable.parmLedgerAccount(mainAcc);
journalTable.parmDefaultDimension(this.createDefaultDimension());
journalTable.parmTaxGroup(taxGroup);
journalTable.parmTaxItemGroup(taxItemGroup);
journalTable.parmCurrencyCode(currency);
journalTable.createLedgerJournalTrans(abs(crAmt),abs(dbAmt),LedgerJournalACType::Ledger);
recordsInserted++;
ttsbegin;
stagingTable.IsFileDataProcessed = NoYes::Yes;
stagingTable.update();
ttscommit;
if(exportParameters.SSC_IsSFTP)
{
SSC_SFTPFileUpload::moveSftpFile(exportParameters.FilePath,exportParameters.FileUserName,exportParameters.FilePwd,exportParameters.PaymentFolderName+fileNameType,exportParameters.PaymentProcessedFilePath+fileNameType,exportParameters.SSC_PortNum);
}
else
{
SSC_FTPFileUpload::moveFile(exportParameters.FilePath+exportParameters.PaymentFolderName,exportParameters.PaymentProcessedFilePath);
}
}
catch(Exception::Error)
{
SysInfoLogEnumerator infoLogEnum = SysInfoLogEnumerator::newData(infolog.infologData());
SysInfologMessageStruct infoMessageStruct;
ttsbegin;
while(infoLogEnum.moveNext())
{
infoMessageStruct = SysInfologMessageStruct::construct(infoLogEnum.currentMessage());
stagingTable.HasDataError = NoYes::Yes;
stagingTable.ErrorString = infoMessageStruct.message();
stagingTable.update();
throw error(strFmt("%1",infoMessageStruct.message()));
}
ttscommit;
if(exportParameters.SSC_IsSFTP)
{
SSC_SFTPFileUpload::moveSftpFile(exportParameters.FilePath,exportParameters.FileUserName,exportParameters.FilePwd,exportParameters.PaymentFolderName+fileNameType,exportParameters.PaymentErrorFilePath+fileNameType, exportParameters.SSC_PortNum);
}
else
{
SSC_FTPFileUpload::moveFile(exportParameters.FilePath+exportParameters.PaymentFolderName,exportParameters.PaymentErrorFilePath);
}
}
}
}
}
class SSC_FTPFileUpload
{
public static void fileUploadWithStream(System.IO.StreamReader _sReader, Description255 _FTPfileName, Description _FTPuserId,Description _FTPPassword)
{
System.Object ftpo;
System.Object ftpResponse;
System.Net.FtpWebRequest request;
System.IO.Stream requestStream;
System.Text.Encoding utf8;
System.Object credential;
System.Byte[] bytes;
System.Net.FtpWebResponse response;
System.Exception ex;
try
{
ftpo = System.Net.WebRequest::Create(_FTPfileName);
request = ftpo;
credential = new System.Net.NetworkCredential(_FTPuserId,_FTPPassword);
utf8 = System.Text.Encoding::get_UTF8();
bytes = utf8.GetBytes( _sReader.ReadToEnd() );
request.set_Credentials(credential);
request.set_ContentLength(bytes.get_Length());
request.set_Method('STOR');
requestStream = request.GetRequestStream();
requestStream.Write(bytes,0,bytes.get_Length());
requestStream.Close();
ftpResponse = request.GetResponse();
response = ftpResponse;
info("@SSC:FileUploadSuccessMsg");
}
catch(Exception::CLRError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ex = ex.get_InnerException();
if (ex != null)
{
throw error(ex.ToString());
}
}
}
}
public static container exploreFiles(Description255 _FTPfileName, Description _FTPuserId,Description _FTPPassword)
{
System.Object ftpo;
System.Object ftpResponse;
System.Net.FtpWebRequest request;
System.Object credential;
System.Net.FtpWebResponse response;
System.Exception ex;
System.IO.StreamReader ioStreamReader;
System.String strReadLine;
ListIterator ftpFilesListIterator;
container conFTPFilesDownload;
try
{
ftpo = System.Net.WebRequest::Create(_FTPfileName);
request = ftpo;
credential = new System.Net.NetworkCredential(_FTPuserId,_FTPPassword);
request.set_KeepAlive(false);
request.set_UsePassive(true);
request.set_UseBinary(true);
request.set_Method('NLST');
request.set_Credentials(credential);
ftpResponse = request.GetResponse();
response = ftpResponse;
if(response)
{
ioStreamReader = new System.IO.StreamReader(response.GetResponseStream());
if (ioStreamReader)
{
strReadLine = ioStreamReader.ReadLine();
while (!System.String::IsNullOrEmpty(strReadLine))
{
ftpFilesListIterator = new Listiterator(strsplit(strReadLine, '/'));
while (ftpFilesListIterator.more())
{
if(strReadLine.ToString().Contains("."))//<To Exclude folders and considering only files >
{
conFTPFilesDownload += ftpFilesListIterator.value();
}
ftpFilesListIterator.next();
}
strReadLine = ioStreamReader.ReadLine();
}
ioStreamReader.Close();
}
}
}
catch(Exception::CLRError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ex = ex.get_InnerException();
if (ex != null)
{
throw error(ex.ToString());
}
}
}
return conFTPFilesDownload;
}
public static void moveFile(Filename _origFilepath, Filename _destfilePath)
{
InteropPermission permission;
permission = new InteropPermission(InteropKind::ClrInterop);
permission.assert();
if (System.IO.File::Exists(_destfilePath))
System.IO.File::Delete(_destfilePath);
System.IO.File::Copy(_origFilepath,_destfilePath);
System.IO.File::Delete(_origFilepath);
CodeAccessPermission::revertAssert();
}
}
Comments
Post a Comment