Sunday 16 June 2019

AX Integration, Get data from SQL db to AX

AX Integration, Get data from SQL db to AX

//This class created by Ki  11-June-19 for PSA-AX integration
class PSA_ProjectMasterInboundProcess extends RunBaseBatch
{
    System.Data.OleDb.OleDbConnection objConn;
    System.Data.OleDb.OleDbCommand cmdSelect,cmdUpdate;
    System.Data.OleDb.OleDbDataReader reader,readerUpdate;
    container resultCon;
    str connectStr;
    ProjParameters projParameters;
    Map dimAttributeMap;
    ProjId  projID;
    Name    projName;

}

.............

public boolean canGoBatch()
{
    return true;

}
..........>

protected boolean canGoBatchJournal()
{
    return true;

}

........................>

public void createProject(container _c)
{
    boolean ret = true;
    ProjTable           newProjTable;
    ProjId              projIdNumSeq,projIdLoc;
    Name                projNameLoc;
    str                 projGroupLoc,projContractLoc,projTypeLoc,projStatusLoc,buLoc,deptLoc,custIdLoc,ccLoc,purposeLoc;
    int                 projTypeValue,projStatusValue;
    ProjTable           projTableLoc;
    Description         guidLoc;

    container           ledgerdimensionheader;
    DimensionDefault    dimensiondefaultheader;
    STG_ProjMaster  projMasterLoc;
    Struct structHdr = new Struct();

    NumberSeq projNumberSeq;
    CompanyInfo companyInfo = companyInfo::find();

    ;
    projNameLoc     = conpeek(_c, 1);
    projGroupLoc    = conpeek(_c, 2);
    projContractLoc = conpeek(_c, 3);
    projTypeLoc     = conpeek(_c, 4);
    projStatusLoc   = conpeek(_c, 5);
    buLoc           = conpeek(_c, 6);
    deptLoc         = conpeek(_c, 7);
    custIdLoc       = conpeek(_c, 8);
    ccLoc           = conpeek(_c, 9);
    purposeLoc      = conpeek(_c, 10);
    guidLoc         = conpeek(_c, 11);
    projIdLoc      = conpeek(_c, 12);


    projTableLoc = ProjTable::find(projIdLoc);
    if(!projTableLoc.ProjId)
    {
        projTypeValue = str2int(projTypeLoc);
        projStatusValue = str2int(projStatusLoc);

        projNumberSeq = NumberSeq::newGetNum(ProjParameters::numRefProjId(), true);

        if (projNumberSeq)
        {
            projIdNumSeq    = projNumberSeq.num();
        }
        projID                  = projIdNumSeq;
        newProjTable.ProjId     = projIdNumSeq;
        newProjTable.Name       = projNameLoc;
        newProjTable.ProjGroupId = projGroupLoc;
        newProjTable.ProjInvoiceProjId = projContractLoc;
        newProjTable.Type       = projTypeValue;
        newProjTable.Status     = projStatusValue;
        newProjTable.CustAccount = custIdLoc;

        if (buLoc && deptLoc && custIdLoc && ccLoc && purposeLoc)
        {
            structHdr.add('BusinessUnit', buLoc);
            structHdr.add('Department', deptLoc);
            structHdr.add('Customer', custIdLoc);
            structHdr.add('CostCenter', ccLoc);
            structHdr.add('Purpose', purposeLoc);
            structHdr.add('Project', projID);


            ledgerDimensionHeader += structHdr.fields();
            ledgerDimensionHeader += structHdr.fieldName(1);
            ledgerDimensionHeader += structHdr.valueIndex(1);
            ledgerDimensionHeader += structHdr.fieldName(2);
            ledgerDimensionHeader += structHdr.valueIndex(2);
            ledgerDimensionHeader += structHdr.fieldName(3);
            ledgerDimensionHeader += structHdr.valueIndex(3);
            ledgerDimensionHeader += structHdr.fieldName(4);
            ledgerDimensionHeader += structHdr.valueIndex(4);
            ledgerDimensionHeader += structHdr.fieldName(5);
            ledgerDimensionHeader += structHdr.valueIndex(5);
            ledgerDimensionHeader += structHdr.fieldName(6);
            ledgerDimensionHeader += structHdr.valueIndex(6);

            dimensionDefaultHeader = AxdDimensionUtil::getDimensionAttributeValueSetId(ledgerDimensionHeader);
        }

        newProjTable.DefaultDimension = dimensionDefaultHeader;
        newProjTable.insert();

        select forUpdate projMasterLoc where projMasterLoc.GUID == guidLoc;
        projMasterLoc.ProjId = newProjTable.ProjId;//projMasterLoc.ProjId = projID;
        projMasterLoc.update();
    }
    else
    {
        ttsBegin;
        select forUpdate newProjTable where newProjTable.ProjId ==   projTableLoc.ProjId;
        projTypeValue = str2int(projTypeLoc);
        projStatusValue = str2int(projStatusLoc);

        newProjTable.ProjGroupId = projGroupLoc;
        newProjTable.ProjInvoiceProjId = projContractLoc;
        newProjTable.Type       = projTypeValue;
        newProjTable.Status     = projStatusValue;
        newProjTable.CustAccount = custIdLoc;
        newProjTable.DefaultDimension = dimensionDefaultHeader;
        newProjTable.update();
        ttsCommit;

    }

}

...............................>

public Str1260 formatString(Str1260 _msg)
{
    str replace(str _source, str _what, str _with)
    {
        int found = 0;
        str target = _source;
        do
        {
            found = strscan(target, _what, found, strlen(target));
            if (found != 0)
            {
                target = strdel(target, found, strlen(_what));
                target = strins(target, _with, found);
                found += strlen(_with);
            }
        } while (found != 0);
        return target;
    }
    ;
    _msg = replace(_msg, '\n', '');
    _msg = replace(_msg, '\r', '');
    _msg = replace(_msg, '\t', '');
    _msg = replace(_msg, '\'', '');
    return _msg;

}

.............................>

public container pack()
{
    return conNull();

}

....................>

public container processProjTable(System.Data.OleDb.OleDbDataReader readerProject)
{
    int startLine = infologline(), currentLine, axStatus;
    int             i;
    Name            name;
    ProjId          projIDLoc,psaProjId;
    ProjGroupId     psaProjGroupId;
    CategoryId      psaCategoryId;
    ProjTable       projTable;
    str             projStatus,projType;
    AccountNum      custId;
    Description     psaGuid;

    str             businessUnit, department,msg,purpose,costCenter;
    ProjInvoiceProjId   contractId;
    STG_ProjMaster   projMaster;


    try
    {
        if (!readerProject.IsDBNull(0))
            psaProjGroupId = readerProject.GetString(0);
        else
            psaProjGroupId = "";

        if (!readerProject.IsDBNull(1))
            psaCategoryId = readerProject.GetString(1);
        else
            psaCategoryId = "";

        if (!readerProject.IsDBNull(2))
            contractId = readerProject.GetString(2);
        else
            contractId = "";

        if (!readerProject.IsDBNull(3))
            projType = readerProject.GetString(3);
        else
            projType = "";

        if (!readerProject.IsDBNull(4))
            projStatus = readerProject.GetString(4);
        else
            projStatus = "";

        if (!readerProject.IsDBNull(5))
            businessUnit = readerProject.GetString(5);
        else
            businessUnit = "";

        if (!readerProject.IsDBNull(6))
            department = readerProject.GetString(6);
        else
            department = "";

        if (!readerProject.IsDBNull(7))
            custId = readerProject.GetString(7);
        else
            custId = "";

        if (!readerProject.IsDBNull(8))
            costCenter = readerProject.GetString(8);
        else
            costCenter = "";

        if (!readerProject.IsDBNull(9))
            purpose = readerProject.GetString(9);
        else
            purpose = "";

        if (!readerProject.IsDBNull(10))
            name = readerProject.GetString(10);
        else
            name = "";

        if (!readerProject.IsDBNull(11))
            psaProjId = readerProject.GetString(11);
        else
            psaProjId = "";
        if (!readerProject.IsDBNull(12))
            psaGuid = readerProject.GetString(12);
        else
            psaGuid = "";
        //Insert in AX staging table-start

        if(ALE_STG_ProjMaster::findByGUID(psaGuid))
        {
            ttsBegin;
            select forUpdate projMaster where projMaster.GUID == psaGuid;
            projMaster.Name         = name;
            projMaster.ProjGroupId  = psaProjGroupId;
            projMaster.ProjContractId = contractId;
            projMaster.CategoryId   = psaCategoryId;
            projMaster.ALE_PRojType = any2str(projType);
            projMaster.BusinessUnit = businessUnit;
            projMaster.CostCentre   = costCenter;
            projMaster.Department   = department;
            projMaster.ProjStage    = projStatus;
            projMaster.CustAccount  = custId;
            projMaster.Purpose      = purpose;

            projMaster.update();
            ttsCommit;
        }
        else
        {
            projMaster.clear();
            projMaster.ProjId       = "";
            projMaster.Name         = name;
            projMaster.ProjGroupId  = psaProjGroupId;
            projMaster.ProjContractId = contractId;
            projMaster.CategoryId   = psaCategoryId;
            projMaster.ALE_PRojType = any2str(projType);
            projMaster.BusinessUnit = businessUnit;
            projMaster.CostCentre   = costCenter;
            projMaster.Department   = department;
            projMaster.ProjStage    = projStatus;
            projMaster.CustAccount  = custId;
            projMaster.Purpose      = purpose;
            projMaster.GUID         = psaGuid;

            projMaster.insert();
        }

        //Insert in AX staging table-start

        ttsbegin;

        // Project name
        if (readerProject.IsDBNull(10))
        {
            msg = 'Project name cannot be null';
            ttsAbort;
            axStatus = -1;
            return [axStatus, msg];
        }

        // Project group
        if (readerProject.IsDBNull(0))
        {
            msg = 'Project group cannot be null';
            ttsAbort;
            axStatus = -1;
            return [axStatus, msg];
        }

        // Project category
        if (readerProject.IsDBNull(1))
        {
            msg = 'Project category cannot be null';
            ttsAbort;
            axStatus = -1;
            return [axStatus, msg];
        }
        // Project Contract Id
        if (readerProject.IsDBNull(2))
        {
            msg = 'Project Contract Id cannot be null';
            ttsAbort;
            axStatus = -1;
            return [axStatus, msg];
        }

        // Project type
        if (readerProject.IsDBNull(3))
        {
            msg = 'Project type cannot be null';
            ttsAbort;
            axStatus = -1;
            return [axStatus, msg];
        }

        // Project Status
        if (readerProject.IsDBNull(4))
        {
            msg = 'Project Status cannot be null';
            ttsAbort;
            axStatus = -1;
            return [axStatus, msg];
        }
        // businessUnit
        if (readerProject.IsDBNull(5))
        {
            msg = 'Business Unit cannot be null';
            ttsAbort;
            axStatus = -1;
            return [axStatus, msg];
        }
        // Department
        if (readerProject.IsDBNull(6))
        {
            msg = 'Department cannot be null';
            ttsAbort;
            axStatus = -1;
            return [axStatus, msg];
        }

        // Customer Id
        if (readerProject.IsDBNull(7))
        {
            msg = ' Customer Id cannot be null';
            ttsAbort;
            axStatus = -1;
            return [axStatus, msg];
        }

        // Cost Center
        if (readerProject.IsDBNull(8))
        {
            msg = 'Cost center cannot be null';
            ttsAbort;
            axStatus = -1;
            return [axStatus, msg];
        }

        // Purpose
        if (readerProject.IsDBNull(9))
        {
            msg = 'Purpose cannot be null';
            ttsAbort;
            axStatus = -1;
            return [axStatus, msg];
        }

        //Financial dimension
        if (!readerProject.IsDBNull(5) && !readerProject.IsDBNull(6)
            && !readerProject.IsDBNull(7) && !readerProject.IsDBNull(8) && !readerProject.IsDBNull(9))
        {
            businessUnit = readerProject.GetString(5);
            department = readerProject.GetString(6);
            custId = readerProject.GetString(7);
            costCenter = readerProject.GetString(8);
            purpose = readerProject.GetString(9);

            if (custId == '0' || businessUnit == '0' || department == '0' ||  costCenter == '0' || purpose == '0' )
            {
                msg = 'Financial dimension value 0 is not valid';
                ttsAbort;
                axStatus = -1;
                return [axStatus, msg];
            }

            /*if (!DimensionFinancialTag::findByFinancialTagCategoryAndValue(5637144576,purpose) && purpose)
            {
                msg = strFmt("Business line %1 does not exists in purpose master.",purpose);
                axStatus = -1;
                ttsAbort;
                return [axStatus, msg];

            }*/

            else if (!OMOperatingUnit::findNumber(businessUnit).OMOperatingUnitType == OMOperatingUnitType::OMBusinessUnit && businessUnit)
            {
                msg = strFmt("Business unit %1 does not exists in Business unit master.",businessUnit);
                axStatus = -1;
                ttsAbort;
                return [axStatus, msg];
            }
            else if (!OMOperatingUnit::findNumber(department).OMOperatingUnitType == OMOperatingUnitType::OMDepartment && department)
            {
                msg = strFmt("Department %1 does not exists in department master.",department);
                axStatus = -1;
                ttsAbort;
                return [axStatus, msg];
            }
            else if (!OMOperatingUnit::findNumber(costCenter).OMOperatingUnitType == OMOperatingUnitType::OMCostCenter && costCenter)
            {
                msg = strFmt("Department %1 does not exists in costCenter master.",costCenter);
                axStatus = -1;
                ttsAbort;
                return [axStatus, msg];
            }

            else if (!ProjCategory::find(psaCategoryId) && psaCategoryId)
            {
                msg = strFmt("Project category ID %1 does not exists in project Category master.",psaCategoryId);
                axStatus = -1;
                ttsAbort;
                return [axStatus, msg];
            }
            else if (!ProjGroup::find(psaProjGroupId) && psaProjGroupId)
            {
                msg = strFmt("Project group ID %1 does not exists in project group master.",psaProjGroupId);
                axStatus = -1;
                ttsAbort;
                return [axStatus, msg];
            }
            else if (!ProjInvoiceTable::find(contractId) && contractId)
            {
                msg = strFmt("Project contract ID %1 does not exists in project contract master.",contractId);
                axStatus = -1;
                ttsAbort;
                return [axStatus, msg];
            }
            else if (!CustTable::find(custId) && custId)
            {
                msg = strFmt("Customer ID %1 does not exists in customer site master.",custId);
                axStatus = -1;
                ttsAbort;
                return [axStatus, msg];
            }
        }
        select forUpdate projTable
            where projTable.Name == name;

        if (!projMaster.ProjId)
        {
            this.createProject([name,psaProjGroupId,contractId,projType,projStatus,businessUnit,department,custId,costCenter,purpose,psaGuid]);
            axStatus = 2;
        }
        else
        {
            this.createProject([name,psaProjGroupId,contractId,projType,projStatus,businessUnit,department,custId,costCenter,purpose,psaGuid,projMaster.ProjId]);
            axStatus = 1;
        }

        msg = strFmt('Project created successfully ');
        ttscommit;

    }
    catch
    {
        ttsAbort;
        axStatus = -1;
        for (currentLine = startLine + 1; currentLine <= infologline(); currentLine++)
        {
            msg += infolog.text(currentLine);
        }
        msg = this.formatString(msg);

        infolog.clear();
    }
    return [axStatus, msg];

}

...............>

public void run()
{
    str datestr;
    //ProjMaster   projMasterUpdate;
    ProcessingStatus status;
    DataAreaId  psaCompanyId;
    string255 err;
    projParameters = ProjParameters::find();

    connectStr = 'Provider=SQLNCLI11;Integrated Security=SSPI;' +'Persist Security Info=False;Initial Catalog=' +
                    projParameters.ALE_PSA_StagingDBName + ';Data Source=' + projParameters.ALE_PSA_StagingDBServer + ';'+ 'User id=xxxxx;Password=xxxxx';
    try
    {
        objConn = new System.Data.OleDb.OleDbConnection(connectStr);
        objConn.Open();

        cmdSelect = objConn.CreateCommand();
        cmdSelect.set_CommandText('SELECT PM.ProjGroupID, PM.ProjCategoryId, PM.ProjContractID, PM.ProjType, PM.ProjStatus, PM.BUSINESSUNIT, PM.DEPARTMENT, PM.Customer, PM.CostCenter, PM.Purpose, PM.ProjName,PM.ProjId,PM.GUID,PM.CompanyId '+
                                    'FROM ProjMaster AS PM '+ 'WHERE PM.STATUS = 0');
        reader = cmdSelect.ExecuteReader();

        while (reader.Read())
        {
            psaCompanyId = reader.GetString(13);

            changeCompany(psaCompanyId)

            resultCon = this.processProjTable(reader);
            projName = reader.GetString(12);

            cmdUpdate = objConn.CreateCommand();
            cmdUpdate.set_CommandText(strFmt('UPDATE ProjMaster SET STATUS = %1,' + 'projId = ' + "'%2'" +', ERRORMESSAGE = ' + "'%3'" + 'WHERE GUID = ' + "'%4'",
                                                conPeek(resultCon, 1),projID, conPeek(resultCon, 2), projName));
            readerUpdate = cmdUpdate.ExecuteReader();

            if (conPeek(resultCon, 1) == 1)
                status = Ale_ProcessingStatus::Processed;
            else if (conPeek(resultCon, 1) == -1)
                status = Ale_ProcessingStatus::Error;
            else
                status = Ale_ProcessingStatus::Unprocessed;

            err = conPeek(resultCon, 2);

            //Update AX staging employee master-start
            //update_recordSet projMasterUpdate
                //setting processingstatus = status,ErrorMsg = err
            //where projMasterUpdate. == emplID
                //&& employeeMasterUpdate.ProcessingStatus == Ale_ProcessingStatus::Unprocessed;
            //Update AX staging employee master-end
        }
    }
    catch(Exception::Error)
    {
        error('An Exception has occurred');
    }

    if(objConn)
    {
        objConn.Close();
    }
}

.............>

public boolean runsImpersonated()
{
    return true;
}

.......................>

private void setDimensionAttribute()
{
   /* dimAttributeMap = new Map(Types::Integer, Types::Record);
    dimAttributeMap.insert(1, dimensionAttribute::findByName('BusinessLine'));
    dimAttributeMap.insert(2, dimensionAttribute::findByName('BusinessUnit'));
    dimAttributeMap.insert(3, dimensionAttribute::findByName('Department'));
    dimAttributeMap.insert(4, dimensionAttribute::findByName('Project'));
    dimAttributeMap.insert(5, dimensionAttribute::findByName('ProjectCategoryItem'));
    dimAttributeMap.insert(6, dimensionAttribute::findByName('Region'));
    dimAttributeMap.insert(7, dimensionAttribute::findByName('Site'));
    dimAttributeMap.insert(8, dimensionAttribute::findByName('Worker'));*/
}
..............>

public boolean unpack(container _packedClass)
{
    return true;
}

...............................>

static ClassDescription description()
{
    return "Project master inbound process";
}

>>>>>>>>>>>>>>>>>>

public static void main(Args args)
{
    ProjectMasterInboundProcess projectMasterInboundProcess = new  ProjectMasterInboundProcess();;

    if (projectMasterInboundProcess.prompt())
    {
        projectMasterInboundProcess.run();
    }
}


No comments:

Post a Comment