Magento Integration Specification ERP Trade Agreement - Product Pricing & Discounts Extract

Magento Integration Specification ERP Trade Agreement - Product Pricing & Discounts Extract:

Main table pricediscadmtrans, need to add NoYesId field.
And write the code by taking extension of the table for Insert and Update methods

 [ExtensionOf(tableStr(PriceDiscAdmTrans))]
final class KGSPMIPriceDiscAdmTrans_Extension
{
    public void  insert()
    {
        this.KGSPMIIsProcessed  = NoYes::No;
        next insert();            
    }
    public void update()
    {
        this.KGSPMIIsProcessed  = NoYes::No;
        next update();  
    }
}
--------------------------------------

For reset for the pricediscadmtrans table of which added field of NoYesId
class KGSPMIPriceDiscAdmTransProcessAllData
{       
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        PriceDiscAdmTrans   priceDiscAdmTrans;;
        int countRec = 0;
        ttsbegin;
        while select forupdate priceDiscAdmTrans
        {
            priceDiscAdmTrans.KGSPMIIsProcessed    = NoYes::No;
            priceDiscAdmTrans.doUpdate();
            countRec++;
        }
        ttscommit;
        Info(strFmt("@KGSPMI:KGSPMIJobUpdate", countRec));
    }
}
------------------------------------

1. Create a BaseEnums with IncrementalExport and FullExport
2. Create a table which to do temporary records comparing from pricediscadmtrans table
    ItemRelation,relation,accountcode,accountrelation,quantityamountfrom,quantityamountto,itemcode

3. Create a Data Contract class for initializing parameters
   [DataContractAttribute]
class KGSPMIPriceDiscAdmTransExportDataContract
{
    KGSPMIExportTradeAgreement       exportTradeAgreement;
    [DataMemberAttribute]
    public KGSPMIExportTradeAgreement parmExportTradeAgreement(KGSPMIExportTradeAgreement _exportTradeAgreement = exportTradeAgreement)
    {
        exportTradeAgreement = _exportTradeAgreement;
        return exportTradeAgreement;
    }
}
--------------------------------------------------------------------
4. Create a service class
  using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using OfficeOpenXml.Table;
class KGSPMIPriceDiscAdmTransExportService
{
    PriceDiscAdmTrans                   priceDiscAdmTrans;
    PriceDiscGroup                      priceDiscGroup;
    PriceDiscAdmTrans                   priceDiscAdmTransLoc,priceDiscAdmTransLocValue;
    EcoResProductCategory               prodCategory;
    InventTable                         inventTable;
    str                                 itemValueTable, journalValue;
    KGSPMITradeAgreementItemRelation    tradeAgreementItemRelation;
    KGSPMIExportTradeAgreement          exportTradeAgreement;
    KGSPMIIntegrationParameters         integrationParameters = KGSPMIIntegrationParameters::find();
    void process(KGSPMIPriceDiscAdmTransExportDataContract  _contract)
    {
        KGSPMIBlobFileShareDataWriter   integrationDataWriter;
        const str                       fileName        = "TradeAgreementTransaction.xlsx";
        MemoryStream                    memoryStream    = new MemoryStream();
        ttsbegin;
        using (var package = new ExcelPackage(memoryStream))
        {
            var currentRow = 1;
            var worksheets = package.get_Workbook().get_Worksheets();
            var CustTableWorksheet = worksheets.Add("@KGSPMI:KGSPMIExport");
            var cells = CustTableWorksheet.get_Cells();
           
            OfficeOpenXml.ExcelRange cell = cells.get_Item(currentRow, 1);
            System.String value = "@KGSPMI:KGSPMICustPriceGroup";
            cell.set_Value(value);
           
            cell = null;
            value = "@KGSPMI:KGSPMIItemItemGroup";
            cell = cells.get_Item(currentRow, 2);
            cell.set_Value(value);
            cell = null;
            value = "@SYS152640";
            cell = cells.get_Item(currentRow, 3);
            cell.set_Value(value);
            cell = null;
            value = "@SYS309396";
            cell = cells.get_Item(currentRow, 4);
            cell.set_Value(value);
           
            cell = null;
            value = "@SYS16617";
            cell = cells.get_Item(currentRow, 5);
            cell.set_Value(value);
            cell = null;
            value = "@KGSPMI:KGSPMIAmount";
            cell = cells.get_Item(currentRow, 6);
            cell.set_Value(value);
            cell = null;
            value = "@KGSPMI:KGSPMIDiscountPercent1";
            cell = cells.get_Item(currentRow, 7);
            cell.set_Value(value);
            cell = null;
            value = "@KGSPMI:KGSPMIDiscountPercent2";
            cell = cells.get_Item(currentRow, 8);
            cell.set_Value(value);
            cell = null;
            value = "@KGSPMI:KGSPMICurrency";
            cell = cells.get_Item(currentRow, 9);
            cell.set_Value(value);
           
            cell = null;
            value = "@KGSPMI:KGSPMIFromDate";
            cell = cells.get_Item(currentRow, 10);
            cell.set_Value(value);
            cell = null;
            value = "@KGSPMI:KGSPMIToDate";
            cell = cells.get_Item(currentRow, 11);
            cell.set_Value(value);
            cell = null;
            value = "@SYS329986";
            cell = cells.get_Item(currentRow, 12);
            cell.set_Value(value);
            cell = null;
            value = "@KGSPMI:KGSPMIToQty";
            cell = cells.get_Item(currentRow, 13);
            cell.set_Value(value);
            cell = null;
            value = "@SYS328585";
            cell = cells.get_Item(currentRow, 14);
            cell.set_Value(value);
            cell = null;
            value = "@SYS101146";
            cell = cells.get_Item(currentRow, 15);
            cell.set_Value(value);
            exportTradeAgreement    = _contract.parmExportTradeAgreement();
            if(exportTradeAgreement == KGSPMIExportTradeAgreement::FullExport)
            {
                update_recordset priceDiscAdmTrans
                    setting KGSPMIIsProcessed = NoYes::No;
            }
            while select forupdate priceDiscAdmTrans
                where priceDiscAdmTrans.KGSPMIIsProcessed == NoYes::No
                    && priceDiscAdmTrans.KGSPMIPosted == NoYes::Yes
            {
                //Table
                if(priceDiscAdmTrans.ItemCode == PriceDiscProductCodeType::Table)
                {
                    select firstonly1 tradeAgreementItemRelation
                        where tradeAgreementItemRelation.ItemRelation       ==  priceDiscAdmTrans.ItemRelation
                            && tradeAgreementItemRelation.relation          ==  priceDiscAdmTrans.relation
                            && tradeAgreementItemRelation.AccountCode       ==  priceDiscAdmTrans.AccountCode
                            && tradeAgreementItemRelation.AccountRelation   ==  priceDiscAdmTrans.AccountRelation;
                    if(tradeAgreementItemRelation.ItemRelation == '')
                    {
                        select firstonly1 priceDiscAdmTransLocValue
                            order by RecId desc
                                where priceDiscAdmTransLocValue.ItemRelation            ==  priceDiscAdmTrans.ItemRelation
                                    && priceDiscAdmTransLocValue.relation               ==  priceDiscAdmTrans.relation
                                    && priceDiscAdmTransLocValue.AccountCode            ==  priceDiscAdmTrans.AccountCode
                                    && priceDiscAdmTransLocValue.AccountRelation        ==  priceDiscAdmTrans.AccountRelation
                                    && priceDiscAdmTransLocValue.KGSPMIIsProcessed      ==  priceDiscAdmTrans.KGSPMIIsProcessed
                                    && priceDiscAdmTransLocValue.KGSPMIPosted           ==  priceDiscAdmTrans.KGSPMIPosted;//fetching latest item
                        select firstonly1 inventTable
                            where inventTable.ItemId == priceDiscAdmTransLocValue.ItemRelation
                                notexists  join prodCategory
                                    where prodCategory.Product == inventTable.Product
                                        && prodCategory.Category == integrationParameters.MagentoCategory;//filtering from ProductCategory
                        if(inventTable)
                        {
                            currentRow ++;
                            cell = null;
                            cell = cells.get_Item(currentRow, 1);
                            cell.set_Value(this.getPriceGroupName(priceDiscAdmTransLocValue));
               
                            cell = null;
                            cell = cells.get_Item(currentRow, 2);
                            cell.set_Value(priceDiscAdmTransLocValue.ItemRelation);
                            cell = null;
                            cell = cells.get_Item(currentRow, 3);
                            cell.set_Value(enum2Str(priceDiscAdmTransLocValue.ItemCode));
                            cell = null;
                            cell = cells.get_Item(currentRow, 4);
                            cell.set_Value(enum2Str(priceDiscAdmTransLocValue.relation));
               
                            cell = null;
                            cell = cells.get_Item(currentRow, 5);
                            cell.set_Value(priceDiscAdmTransLocValue.UnitId);
                            cell = null;
                            cell = cells.get_Item(currentRow, 6);
                            cell.set_Value(priceDiscAdmTransLocValue.Amount);
                            cell = null;
                            cell = cells.get_Item(currentRow, 7);
                            cell.set_Value(priceDiscAdmTransLocValue.Percent1);
                            cell = null;
                            cell = cells.get_Item(currentRow, 8);
                            cell.set_Value(priceDiscAdmTransLocValue.Percent2);
                            cell = null;
                            cell = cells.get_Item(currentRow, 9);
                            cell.set_Value(priceDiscAdmTransLocValue.Currency);
                            cell = null;
                            cell = cells.get_Item(currentRow, 10);
                            cell.set_Value(DateTimeUtil::date(priceDiscAdmTransLocValue.FromDate));
                            cell = null;
                            cell = cells.get_Item(currentRow, 11);
                            cell.set_Value(DateTimeUtil::date(priceDiscAdmTransLocValue.ToDate));
                               
                            cell = null;
                            cell = cells.get_Item(currentRow, 12);
                            cell.set_Value(priceDiscAdmTransLocValue.QuantityAmountFrom);
                            cell = null;
                            cell = cells.get_Item(currentRow, 13);
                            cell.set_Value(priceDiscAdmTransLocValue.QuantityAmountTo);
                            cell = null;
                            cell = cells.get_Item(currentRow, 14);
                            cell.set_Value(priceDiscAdmTransLocValue.createdby);
                            cell = null;
                            cell = cells.get_Item(currentRow, 15);
                            cell.set_Value(this.getPostedDate(priceDiscAdmTransLocValue));
                            priceDiscAdmTrans.KGSPMIIsProcessed = NoYes::yes;
                            priceDiscAdmTrans.doUpdate();
                            tradeAgreementItemRelation.ItemRelation     =   priceDiscAdmTransLocValue.ItemRelation;
                            tradeAgreementItemRelation.relation         =   priceDiscAdmTransLocValue.relation;
                            tradeAgreementItemRelation.AccountCode      =   priceDiscAdmTransLocValue.AccountCode;
                            tradeAgreementItemRelation.AccountRelation  =   priceDiscAdmTransLocValue.AccountRelation;
                            tradeAgreementItemRelation.insert();
                        }
                    }
                    else
                    {
                        priceDiscAdmTrans.KGSPMIIsProcessed = NoYes::yes;
                        priceDiscAdmTrans.doUpdate();
                    }
                }
                //Group
                else if(priceDiscAdmTrans.ItemCode == PriceDiscProductCodeType::GroupId)
                {
                    select firstonly1 tradeAgreementItemRelation
                        where tradeAgreementItemRelation.ItemRelation           ==  priceDiscAdmTrans.ItemRelation
                            && tradeAgreementItemRelation.relation              ==  priceDiscAdmTrans.relation
                            && tradeAgreementItemRelation.QuantityAmountFrom    ==  priceDiscAdmTrans.QuantityAmountFrom
                            && tradeAgreementItemRelation.QuantityAmountTo      ==  priceDiscAdmTrans.QuantityAmountTo
                            && tradeAgreementItemRelation.AccountCode           ==  priceDiscAdmTrans.AccountCode
                            && tradeAgreementItemRelation.AccountRelation       ==  priceDiscAdmTrans.AccountRelation;
                    if(tradeAgreementItemRelation.ItemRelation == '')
                    {
                        select firstonly1 priceDiscAdmTransLocValue
                            order by RecId desc
                                where priceDiscAdmTransLocValue.ItemRelation            ==  priceDiscAdmTrans.ItemRelation
                                    && priceDiscAdmTransLocValue.relation               ==  priceDiscAdmTrans.relation
                                    && priceDiscAdmTransLocValue.AccountCode            ==  priceDiscAdmTrans.AccountCode
                                    && priceDiscAdmTransLocValue.AccountRelation        ==  priceDiscAdmTrans.AccountRelation
                                    && priceDiscAdmTransLocValue.QuantityAmountFrom     ==  priceDiscAdmTrans.QuantityAmountFrom
                                    && priceDiscAdmTransLocValue.QuantityAmountTo       ==  priceDiscAdmTrans.QuantityAmountTo
                                    && priceDiscAdmTransLocValue.KGSPMIIsProcessed      ==  priceDiscAdmTrans.KGSPMIIsProcessed
                                    && priceDiscAdmTransLocValue.KGSPMIPosted           ==  priceDiscAdmTrans.KGSPMIPosted;//fetching latest item
                        if(priceDiscAdmTransLocValue)
                        {
                            currentRow ++;
                            cell = null;
                            cell = cells.get_Item(currentRow, 1);
                            cell.set_Value(this.getPriceGroupName(priceDiscAdmTransLocValue));
               
                            cell = null;
                            cell = cells.get_Item(currentRow, 2);
                            cell.set_Value(priceDiscAdmTransLocValue.ItemRelation);
                            cell = null;
                            cell = cells.get_Item(currentRow, 3);
                            cell.set_Value(enum2Str(priceDiscAdmTransLocValue.ItemCode));
                            cell = null;
                            cell = cells.get_Item(currentRow, 4);
                            cell.set_Value(enum2Str(priceDiscAdmTransLocValue.relation));
               
                            cell = null;
                            cell = cells.get_Item(currentRow, 5);
                            cell.set_Value(priceDiscAdmTransLocValue.UnitId);
                            cell = null;
                            cell = cells.get_Item(currentRow, 6);
                            cell.set_Value(priceDiscAdmTransLocValue.Amount);
                            cell = null;
                            cell = cells.get_Item(currentRow, 7);
                            cell.set_Value(priceDiscAdmTransLocValue.Percent1);
                            cell = null;
                            cell = cells.get_Item(currentRow, 8);
                            cell.set_Value(priceDiscAdmTransLocValue.Percent2);
                            cell = null;
                            cell = cells.get_Item(currentRow, 9);
                            cell.set_Value(priceDiscAdmTransLocValue.Currency);
                            cell = null;
                            cell = cells.get_Item(currentRow, 10);
                            cell.set_Value(DateTimeUtil::date(priceDiscAdmTransLocValue.FromDate));
                            cell = null;
                            cell = cells.get_Item(currentRow, 11);
                            cell.set_Value(DateTimeUtil::date(priceDiscAdmTransLocValue.ToDate));
                               
                            cell = null;
                            cell = cells.get_Item(currentRow, 12);
                            cell.set_Value(priceDiscAdmTransLocValue.QuantityAmountFrom);
                            cell = null;
                            cell = cells.get_Item(currentRow, 13);
                            cell.set_Value(priceDiscAdmTransLocValue.QuantityAmountTo);
                            cell = null;
                            cell = cells.get_Item(currentRow, 14);
                            cell.set_Value(priceDiscAdmTransLocValue.createdby);
                            cell = null;
                            cell = cells.get_Item(currentRow, 15);
                            cell.set_Value(this.getPostedDate(priceDiscAdmTransLocValue));
                            priceDiscAdmTrans.KGSPMIIsProcessed = NoYes::yes;
                            priceDiscAdmTrans.doUpdate();
                            tradeAgreementItemRelation.ItemRelation         =   priceDiscAdmTransLocValue.ItemRelation;
                            tradeAgreementItemRelation.QuantityAmountFrom   =   priceDiscAdmTransLocValue.QuantityAmountFrom;
                            tradeAgreementItemRelation.QuantityAmountTo     =   priceDiscAdmTransLocValue.QuantityAmountTo;
                            tradeAgreementItemRelation.relation             =   priceDiscAdmTransLocValue.relation;
                            tradeAgreementItemRelation.AccountCode          =   priceDiscAdmTransLocValue.AccountCode;
                            tradeAgreementItemRelation.AccountRelation      =   priceDiscAdmTransLocValue.AccountRelation;
                            tradeAgreementItemRelation.insert();
                        }
                    }
                    else
                    {
                        priceDiscAdmTrans.KGSPMIIsProcessed = NoYes::yes;
                        priceDiscAdmTrans.doUpdate();
                    }
                }
                //All
                else
                {
                    select firstonly1 tradeAgreementItemRelation
                        where tradeAgreementItemRelation.AccountCode        ==  priceDiscAdmTrans.AccountCode
                            && tradeAgreementItemRelation.AccountRelation   ==  priceDiscAdmTrans.AccountRelation
                            && tradeAgreementItemRelation.ItemCode          ==  priceDiscAdmTrans.ItemCode
                            && tradeAgreementItemRelation.relation          ==  priceDiscAdmTrans.relation;
                    if(!tradeAgreementItemRelation.RecId)
                    {
                        select firstonly1 priceDiscAdmTransLocValue
                            order by RecId desc
                                where priceDiscAdmTransLocValue.ItemCode            ==  PriceDiscProductCodeType::All
                                    && priceDiscAdmTransLocValue.relation           ==  priceDiscAdmTrans.relation
                                    && priceDiscAdmTransLocValue.AccountCode        ==  priceDiscAdmTrans.AccountCode
                                    && priceDiscAdmTransLocValue.AccountRelation    ==  priceDiscAdmTrans.AccountRelation
                                    && priceDiscAdmTransLocValue.KGSPMIIsProcessed  ==  priceDiscAdmTrans.KGSPMIIsProcessed
                                    && priceDiscAdmTransLocValue.KGSPMIPosted       ==  priceDiscAdmTrans.KGSPMIPosted;//fetching latest item
                        if(priceDiscAdmTransLocValue)
                        {
                            currentRow ++;
                            cell = null;
                            cell = cells.get_Item(currentRow, 1);
                            cell.set_Value(this.getPriceGroupName(priceDiscAdmTransLocValue));
               
                            cell = null;
                            cell = cells.get_Item(currentRow, 2);
                            cell.set_Value(priceDiscAdmTransLocValue.ItemRelation);
                            cell = null;
                            cell = cells.get_Item(currentRow, 3);
                            cell.set_Value(enum2Str(priceDiscAdmTransLocValue.ItemCode));
                            cell = null;
                            cell = cells.get_Item(currentRow, 4);
                            cell.set_Value(enum2Str(priceDiscAdmTransLocValue.relation));
               
                            cell = null;
                            cell = cells.get_Item(currentRow, 5);
                            cell.set_Value(priceDiscAdmTransLocValue.UnitId);
                            cell = null;
                            cell = cells.get_Item(currentRow, 6);
                            cell.set_Value(priceDiscAdmTransLocValue.Amount);
                            cell = null;
                            cell = cells.get_Item(currentRow, 7);
                            cell.set_Value(priceDiscAdmTransLocValue.Percent1);
                            cell = null;
                            cell = cells.get_Item(currentRow, 8);
                            cell.set_Value(priceDiscAdmTransLocValue.Percent2);
                            cell = null;
                            cell = cells.get_Item(currentRow, 9);
                            cell.set_Value(priceDiscAdmTransLocValue.Currency);
                            cell = null;
                            cell = cells.get_Item(currentRow, 10);
                            cell.set_Value(DateTimeUtil::date(priceDiscAdmTransLocValue.FromDate));
                            cell = null;
                            cell = cells.get_Item(currentRow, 11);
                            cell.set_Value(DateTimeUtil::date(priceDiscAdmTransLocValue.ToDate));
                               
                            cell = null;
                            cell = cells.get_Item(currentRow, 12);
                            cell.set_Value(priceDiscAdmTransLocValue.QuantityAmountFrom);
                            cell = null;
                            cell = cells.get_Item(currentRow, 13);
                            cell.set_Value(priceDiscAdmTransLocValue.QuantityAmountTo);
                            cell = null;
                            cell = cells.get_Item(currentRow, 14);
                            cell.set_Value(priceDiscAdmTransLocValue.createdby);
                            cell = null;
                            cell = cells.get_Item(currentRow, 15);
                            cell.set_Value(this.getPostedDate(priceDiscAdmTransLocValue));
                            priceDiscAdmTrans.KGSPMIIsProcessed = NoYes::yes;
                            priceDiscAdmTrans.doUpdate();
                            tradeAgreementItemRelation.relation             =   priceDiscAdmTransLocValue.relation;
                            tradeAgreementItemRelation.AccountCode          =   priceDiscAdmTransLocValue.AccountCode;
                            tradeAgreementItemRelation.AccountRelation      =   priceDiscAdmTransLocValue.AccountRelation;
                            tradeAgreementItemRelation.ItemCode             =   priceDiscAdmTransLocValue.ItemCode;
                            tradeAgreementItemRelation.insert();
                        }
                    }
                    else
                    {
                        priceDiscAdmTrans.KGSPMIIsProcessed = NoYes::yes;
                        priceDiscAdmTrans.doUpdate();
                    }
                }
            }
            package.Save();
            ttscommit;
            if(isRunningOnBatch())
            {
                integrationDataWriter.exportToBlob(memoryStream, fileName);
            }
            else
            {
                file::SendFileToUser(memoryStream, fileName);
            }
        }
    }
    /// <summary>
    /// Gets a PriceGroupName for a record based on the title fields of the record.
    /// </summary>
    /// <returns>
    /// A PriceGroupName for the specified record.
    /// <param name = "_priceDiscAdmTrans"></param>
    /// <returns></returns>
    public PriceDiscName  getPriceGroupName(PriceDiscAdmTrans    _priceDiscAdmTrans)
    {
        priceDiscAdmTransLoc    =   _priceDiscAdmTrans;//assigning pricediscadmtrans data for fetching the price group name.
        if(priceDiscAdmTransLoc.AccountCode    ==  PriceDiscPartyCodeType::Table)
        {
            return priceDiscAdmTransLoc.AccountRelation;
        }
        else
        {
            //fetching the name instead of the actual price group from pricediscgroup table.
            select priceDiscGroup
                        where priceDiscGroup.GroupId            ==  priceDiscAdmTransLoc.AccountRelation
                            && priceDiscAdmTransLoc.relation    ==  _priceDiscAdmTrans.relation;
            return priceDiscGroup.Name;
        }
    }
    /// <summary>
    /// Gets a PostedDate for a record based on the title fields of the record.
    /// </summary>
    /// <returns>
    /// A PostedDate for the specified record.
    /// <param name = "_priceDiscAdmTrans"></param>
    /// <returns></returns>
    public  TransDate   getPostedDate(PriceDiscAdmTrans     _priceDiscAdmTrans)
    {
        return PriceDiscAdmTable::find(_priceDiscAdmTrans.JournalNum).PostedDate;//fetching the posted date.
    }
}
------------------------------------------------------
5. create a class - Export integrations to Azure blob file share

 using Microsoft.WindowsAzure.Storage.File;
/// <summary>
/// Export integrations to Azure blob file share
/// </summary>
class KGSPMIBlobFileShareDataWriter
{
    KGSPMIIntegrationType                       integrationType;
    CloudFileShare                              fileShare;
    KGSPMIIntegrationDataImportExportSettings   integrationDataImportExportSettings;
    /// <summary>
    /// Construct the object for KGSPMIBlobFileShareDataWriter based on integration type
    /// </summary>
    /// <param name="_integrationType">
    /// Integration type enumeration
    /// </param>
    public static KGSPMIBlobFileShareDataWriter construct(KGSPMIIntegrationType _integrationType)
    {
        KGSPMIBlobFileShareDataWriter integrationDataExport = new KGSPMIBlobFileShareDataWriter();
        integrationDataExport.parmIntegrationType(_integrationType);
        integrationDataExport.fetchBlobSettings();

        return integrationDataExport;
    }
    /// <summary>
    /// Parm method for integration type
    /// </summary>
    /// <param name="_integrationType">
    /// Integration type enumeration
    /// </param>
    public KGSPMIIntegrationType parmIntegrationType(KGSPMIIntegrationType _integrationType = integrationType)
    {
        integrationType = _integrationType;
        return integrationType;
    }
    /// <summary>
    /// Initialyses the blob settings based on integration type
    /// </summary>
    public void fetchBlobSettings()
    {
       
        KGSPMIIntegrationParameters         integrationParameters = KGSPMIIntegrationParameters::find();
        integrationDataImportExportSettings = KGSPMIIntegrationDataImportExportSettings::construct(this.parmIntegrationType());
       
        fileShare = KGSPMIBlobFileShareUtility::connectToFileShare(integrationParameters.BlobConnectionString, integrationParameters.BlobFileShareName);
       
    }
    /// <summary>
    /// Export the file stream to blob file share
    /// </summary>
    /// <param name="_stream">
    /// File stream to export to blob file share
    /// </param>
    /// <param name="_fileName">
    /// Name of the file to be generated
    /// </param>
    public void exportToBlob(System.IO.Stream _stream, str _fileName)
    {     
        KGSPMIBlobFileShareUtility::uploadStreamToFileShare(_stream, fileShare, integrationDataImportExportSettings.parmOutFilePath(), _fileName);
    }
    /// <summary>
    /// Get all the files in a list from blob FileShare path
    /// </summary>
    public  List  getFileList()
    {
        return KGSPMIBlobFileShareUtility::getFileNamesFromFileShare(fileShare, integrationDataImportExportSettings.parmInFilePath());
    }
    /// <summary>
    /// Get specific file from blob FileShare path based on file name
    /// </summary>
    /// <param name="_fileName">
    /// Name of the file to read from blob
    /// </param>
    public  System.IO.Stream  getFileStream(str _fileName)
    {
        return KGSPMIBlobFileShareUtility::downloadFileFromFileShare(fileShare, integrationDataImportExportSettings.parmInFilePath(), _fileName);
    }
    /// <summary>
    /// Moves the file from processing folder to success folder
    /// </summary>
    /// <param name="_fileName">
    /// Name of the file to move to success path
    /// </param>
    public  void  moveToSuccessPath(str _fileName)
    {
        KGSPMIBlobFileShareUtility::movefilefromFileShare(fileShare, fileShare, integrationDataImportExportSettings.parmInFilePath(), integrationDataImportExportSettings.parmSuccessFilePath(), _fileName, _fileName);
    }
    /// <summary>
    /// Moves the file from processing folder to error folder
    /// </summary>
    /// <param name="_fileName">
    /// Name of the file to move to error path
    /// </param>
    public  void  moveToErrorPath(str _fileName)
    {
        KGSPMIBlobFileShareUtility::movefilefromFileShare(fileShare, fileShare, integrationDataImportExportSettings.parmInFilePath(), integrationDataImportExportSettings.parmErrorFilePath(), _fileName, _fileName);
    }
    /// <summary>
    /// Export the file to bob FileShare based on execution from data entity
    /// </summary>
    /// <param name="_definitionGroupId">
    /// Definition group id for data entity job
    /// </param>
    /// <param name="_executionId">
    /// Execution id for data entity job
    /// </param>
    public static void exportToBlobFromDefinitionGroup(DMFDefinitionGroupName _definitionGroupId, DMFExecutionId _executionId)
    {
        DMFEntityName                               entityName;
        DMFEntityExportDetails                      exportDetails;
        DMFEntity                                   dMFEntity;
        DMFDefinitionGroupExecution                 definitionGroupExecution;
        KGSPMIIntegrationParameters                 integrationParameters;
        KGSPMIIntegrationDataImportExportSettings   integrationDataImportExportSettings;
        KGSPMIIntegrationType                       integrationType;
        integrationParameters = KGSPMIIntegrationParameters::find();
        if(integrationParameters.EnableAll)
        {
            select firstonly definitionGroupExecution
                where definitionGroupExecution.ExecutionId  == _executionId &&
                definitionGroupExecution.DefinitionGroup    == _definitionGroupId;
            dMFEntity = DMFEntity::find(definitionGroupExecution.Entity);
            if (dMFEntity.ParentEntityName != '')
            {
                entityName = dMFEntity.ParentEntityName;
            }
            else
            {
                entityName = definitionGroupExecution.Entity;
            }
            if(KGSPMIIntegrationDataImportExportSettings::isPMIIntegration(entityName))
            {
                select firstonly1 SampleFilePath, DefinitionGroup, Entity from exportDetails
                    where exportDetails.DefinitionGroup == _definitionGroupId
                        && exportDetails.ExecutionId == _executionId
                        && exportDetails.Entity == entityName;
                if (exportDetails.RecId && exportDetails.SampleFilePath)
                {
                    integrationType                     = KGSPMIIntegrationDataImportExportSettings::getIntegrationTypeFromDataEntity(entityName);
                    integrationDataImportExportSettings = KGSPMIIntegrationDataImportExportSettings::construct(integrationType);               
                    integrationParameters               = KGSPMIIntegrationParameters::find();
                    CloudFileShare fileShare            = KGSPMIBlobFileShareUtility::connectToFileShare(integrationParameters.BlobConnectionString, integrationParameters.BlobFileShareName);
                    KGSPMIDataEntityBlobFileShareUtility::exportToFileShare(exportDetails, fileShare, integrationDataImportExportSettings.parmOutFilePath());
                }
                else
                {
                    warning(strFmt("@KGSPMI:KGSPMINoExportFileForEntityTxt", entityName, definitionGroupExecution.ExecutionId));
                }
            }
        }
    }
}
--------------------------------
6. Create a class - Utility class for Azure blob file share connection

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.File;
using System.IO;
/// <summary>
/// Utility class for Azure blob file share connection.
/// </summary>
class KGSPMIBlobFileShareUtility
{
    public static CloudFileShare connectToFileShare(str _connectionString, str _fileShareName)
    {
        CloudStorageAccount     storageAccount;     
        CloudFileClient         fileClient;
        CloudFileShare          fileShare;
       
        storageAccount      = CloudStorageAccount::Parse(_connectionString);
        fileClient          = storageAccount.CreateCloudFileClient();
        fileShare           = fileClient.GetShareReference(_fileShareName);
        return fileShare;
    }
    public static void uploadStreamToFileShare(System.IO.Stream _stream, CloudFileShare _fileShare, str _filepath, str _fileName)
    {
        CloudFileDirectory          fileRootDirectory;
        CloudFileDirectory          fileDirectory ;
        CloudFile                   file;
        fileRootDirectory       = _fileShare.GetRootDirectoryReference();
        fileDirectory           = fileRootDirectory.GetDirectoryReference(_filepath);
        file                    = fileDirectory.GetFileReference(_fileName);
       
        file.UploadFromStream(_stream, null, null, null);
    }
    public static void uploadTextToFileShare(str _stringData, CloudFileShare _fileShare, str _filepath, str _fileName, System.Text.Encoding _encoding = null)
    {
        CloudFileDirectory          fileRootDirectory;
        CloudFileDirectory          fileDirectory ;
        CloudFile                   file;
        fileRootDirectory       = _fileShare.GetRootDirectoryReference();
        fileDirectory           = fileRootDirectory.GetDirectoryReference(_filepath);
        file                    = fileDirectory.GetFileReference(_fileName);
       
        file.UploadText(_stringData, _encoding, null, null, null);
    }
    public static list getFileNamesFromFileShare(CloudFileShare _fileShare, str _filepath)
    {
        container                       con;
        List                            filenames           = new List(Types::String);
        CloudFileDirectory              fileRootDirectory   = _fileShare.GetRootDirectoryReference();
        CloudFileDirectory              fileDirectory       = fileRootDirectory.GetDirectoryReference(_filepath);
        System.Collections.IEnumerable  lstEnumarable       = fileDirectory.ListFilesAndDirectories(null, null);
        System.Collections.IEnumerator  lstEnumarator       = lstEnumarable.GetEnumerator();
        while(lstEnumarator.MoveNext())
        {
            IListFileItem item = lstEnumarator.Current;
           
            if(item is CloudFile)
            {
                CloudFile  file = item;
                file.FetchAttributes(null, null, null);
                con = str2con(file.name, "/");
                filenames.addStart(conPeek(con,conlen(con)));
            }
        }
        return filenames;
    }
    public static void movefilefromFileShare(CloudFileShare _srcFileShare, CloudFileShare _dstFileShare,
                                        str _srcFilePath, str _destFilePath, str _srcFilename, str _destFilename)
    {
        CloudFileDirectory          srcfileRootDirectory    = _srcFileShare.GetRootDirectoryReference();
        CloudFileDirectory          srcfileDirectory        = srcfileRootDirectory.GetDirectoryReference(_srcFilePath);
        CloudFileDirectory          dstfileRootDirectory    = _dstFileShare.GetRootDirectoryReference();
        CloudFileDirectory          dstfileDirectory        = dstfileRootDirectory.GetDirectoryReference(_destFilePath);
        CloudFile                   srcfile = srcfileDirectory.GetFileReference(_srcFilename);
        CloudFile                   dstfile = dstfileDirectory.GetFileReference(_destFilename);
       
        dstfile.UploadFromStream(srcfile.OpenRead(null,null,null),null,null,null);
        srcfile.Delete(null,null,null);
    }
    public static System.IO.Stream downloadFileFromFileShare(CloudFileShare _fileShare, str _filepath, str _filename)
    {
        CloudFileDirectory          fileRootDirectory   = _fileShare.GetRootDirectoryReference();
        CloudFileDirectory          fileDirectory       = fileRootDirectory.GetDirectoryReference(_filepath);
        CloudFile                   file                = fileDirectory.GetFileReference(_filename);
        System.IO.Stream            stream              = file.OpenRead(null,null,null);
       
        return stream;
    }
    /// <summary>
    ///
    /// </summary>
    public static MemoryStream convertStringToStream(str _record, System.Text.Encoding _encoding)
    {
        System.Byte[]  byteArray =   new System.Byte[0]();
        if(_encoding == System.Text.Encoding::Unicode)
        {
            byteArray = System.Text.Encoding::Unicode.GetBytes(_record);
        }
        else
        {
            byteArray = System.Text.Encoding::ASCII.GetBytes(_record);
        }
        return  new MemoryStream( byteArray );
    }
}
----------------------------------------------------------


 

Comments

Popular posts from this blog

Data entites method calling sequence in D365FO

How to Extend Sales Order Update Functionality to Custom Fields in D365 Finance and Operations

To generate dimension and find the account value - KGSPMIDimensionUtility