JD Edwards

JD Edwards
Lokesh Reddy Blog

Saturday, May 9, 2015

Get Files From the Directory For EDI Inbound Process

Business Function : Get Files From Directory
 Description: This is used to read the files from the particular directory and read the each file having the number of records.After reading the files , it will store the the file names,record count,transaction id,audit fields in to the custom table F567101.That will use to read the Inbound files using the inbound EDI programs.


/************************* HEADER FILE ******************************************
 *    Header File:  B567101.h
 *
 *    Description:  Inbound Directory Monitoring BSFN Header File
 *
 *        History:
 *          Date        Programmer  SAR# - Description
 *          ----------  ----------  -------------------------------------------
 *   Author 1/2/2015   Lokesh Reddy    Unknown  - Created 
 *
 *
 * Copyright (c) J.D. Edwards World Source Company, 1996
 *
 * This unpublished material is proprietary to J.D. Edwards World Source 
 * Company.  All rights reserved.  The methods and techniques described 
 * herein are considered trade secrets and/or confidential.  Reproduction
 * or distribution, in whole or in part, is forbidden except by express
 * written permission of J.D. Edwards World Source Company.
 ****************************************************************************/

#ifndef __B567101_H
#define __B567101_H

/*****************************************************************************
 * Table Header Inclusions
 ****************************************************************************/

 #include <f567101.h>

/*****************************************************************************
 * External Business Function Header Inclusions
 ****************************************************************************/
  #include <b9800100.h>        /* For Audit Information */

/*****************************************************************************
 * Global Definitions
 ****************************************************************************/

/*****************************************************************************
 * Structure Definitions
 ****************************************************************************/

/*****************************************************************************
 * DS Template Type Definitions
 ****************************************************************************/
 /**
 * TYPEDEF for Data Structure
 * D567101 : DS For Inbound Directory Monitoring BSFN
 *
 * Copyright Oracle USA
 *
 * This is a JDEdwards EnterpriseOne generated file.
 * Do not modify this file.
 * Only re-generate with the appropriate tool.
 * Generation Date : 1/2/2015
 */

#ifndef DATASTRUCTURE_D567101
#define DATASTRUCTURE_D567101

typedef struct tagDSD567101
{
  JCHAR          szDirectoryPath_DRPT[255];
  JCHAR          cErrorCode_ERRC;
  JCHAR          szNameObject_OBNM[11];
  JCHAR          cInitialUploadFlag_EDSP;
  MATH_NUMERIC   mnUniqueKeyID_UKID;
} DSD5847101 , *LPDSD5847101;

#define IDERRszDirectoryPath_DRPT_1               1L
#define IDERRcErrorCode_ERRC_2                    2L
#define IDERRszNameObject_OBNM_3                  3L
#define IDERRcInitialUploadFlag_EDSP_4            4L
#define IDERRmnUniqueKeyID_UKID_5                 5L

#endif /* DATASTRUCTURE_D567101 */
/*****************************************************************************
 * Source Preprocessor Definitions
 ****************************************************************************/
#if defined (JDEBFRTN)
    #undef JDEBFRTN
#endif

#if defined (WIN32)
    #if defined (WIN32)
        #define JDEBFRTN(r) __declspec(dllexport) r
    #else
        #define JDEBFRTN(r) __declspec(dllimport) r
    #endif
#else
    #define JDEBFRTN(r) r
#endif

/*****************************************************************************
 * Business Function Prototypes
 ****************************************************************************/
 JDEBFRTN (ID) JDEBFWINAPI GetFileNamesInDirectoryN         (LPBHVRCOM lpBhvrCom, LPVOID lpVoid, LPDSD567101 lpDS);


/*****************************************************************************
 * Internal Function Prototypes
 ****************************************************************************/

#endif    /* __B567101_H */


 ****************SOURCE FILE********************************************

#include <jde.h>

#ifdef JDENV_PC
#include <windows.h>
#else
#include <dirent.h>
#include <sys/types.h>
#endif

#define b567101_c


/*****************************************************************************
*    Source File:  b567101
*
*    Description:  Inbound Directory Monitoring BSFN Source File
*
*        History:
*          Date        Programmer  SAR# - Description
*          ----------  ----------  -------------------------------------------
*   Author 1/2/2015   Lokesh Reddy       Unknown  - Created  
*
* Copyright (c) J.D. Edwards World Source Company, 1996
*
* This unpublished material is proprietary to J.D. Edwards World Source Company.
* All rights reserved.  The methods and techniques described herein are
* considered trade secrets and/or confidential.  Reproduction or
* distribution, in whole or in part, is forbidden except by express
* written permission of J.D. Edwards World Source Company.
****************************************************************************/
/**************************************************************************
* Notes:
*
**************************************************************************/

#include <x00022.h>  /* BF: GetNextUniqueKeyID */
#include <b567101.h>


/**************************************************************************
*  Business Function:  GetFileNamesInDirectoryN
*
*        Description:  Get File Names In Directory
*
*         Parameters:
*           LPBHVRCOM           lpBhvrCom    Business Function Communications
*           LPVOID              lpVoid       Void Parameter - DO NOT USE!
*           LPDSD584            lpDS         Parameter Data Structure Pointer  
*
*************************************************************************/

JDEBFRTN (ID) JDEBFWINAPI GetFileNamesInDirectoryN (LPBHVRCOM lpBhvrCom, LPVOID lpVoid, LPDSD567101 lpDS) 

{
    /************************************************************************
    *  Variable declarations
    ************************************************************************/
#ifndef JDENV_PC

    int iRet = ER_SUCCESS;
    DIR *dir = NULL;
    struct dirent *de = NULL;       
    JCHAR    szd_name[255] = {0};

    ID  idJDEDBReturn;
    int     counter = 0;
    int nMathReturn=0;

    /************************************************************************
    * Declare structures
    ************************************************************************/
    DSD9800100     GetAuditParms     = {0};   
    F567101    dsF567101 = {0};
    DSDX00022A    dsX00022A = { 0 };
#define NUMMAP_1 1
    CALLMAP cmErrorMap_1[NUMMAP_1] = { { IDERRszNameObject_OBNM_3, IDERRszObjectName_1 } };
    /************************************************************************
    * Declare pointers
    ************************************************************************/
    HREQUEST                 hRequestF567101 =(HREQUEST)NULL;
    HREQUEST                 hRequestFetchSingle =(HREQUEST)NULL;
    HUSER                  hUser =(HUSER)NULL;
    KEY2_F567101          dsF567101Key2  =  {0} ;
    LPJDEAPP              lpObj = (LPJDEAPP)NULL;
    LPAPPVARIABLES        pAppVars = (LPAPPVARIABLES)NULL;

    /************************************************************************
    * Check for NULL pointers
    ************************************************************************/
    if ((lpBhvrCom == (LPBHVRCOM) NULL) ||
        (lpVoid    == (LPVOID)    NULL) ||
        (lpDS      == (LPDSD5847101)    NULL))
    {
        jdeErrorSet (lpBhvrCom, lpVoid, (ID) 0, _J("4363"), (LPVOID) NULL);
        return ER_ERROR;
    }
    idJDEDBReturn = JDB_InitBhvr((void *)lpBhvrCom, &hUser,(JCHAR *) NULL,
        JDEDB_COMMIT_AUTO);

    if (idJDEDBReturn == JDEDB_FAILED)
    {
        jdeWriteLog(_J("B567101"), 0,_J("hUser Failure"));
    }

    /************************************************************************
    * Set pointers
    ************************************************************************/
    lpObj = lpBhvrCom->lpObj;
    pAppVars = lpObj->lpzAppVars;
    /************************************************************************
    * Main Processing
    ************************************************************************/

    idJDEDBReturn =    JDB_OpenTable (hUser,
        NID_F567101,
        ID_F567101_UKID__FILENAME,
        (NULL),
        (ushort) 0,
        (JCHAR *) NULL,
        &hRequestF567101);

    if (idJDEDBReturn == JDEDB_FAILED)
    {
        jdeWriteLog(_J("B567101"), 0,_J("Table Open Failure"));
    }

    if ((lpDS->cInitialUploadFlag_EDSP == _J(' ')) ||
        (lpDS->cInitialUploadFlag_EDSP == _J('\0')))
    {
        lpDS->cInitialUploadFlag_EDSP = _J('I');
    }                                

    dir = jdeOpendir( lpDS->szDirectoryPath_DRPT );

    if (dir)
    {
        //Generate UKID
        jdeStrncpyTerminate( dsX00022A.szObjectName , lpDS->szNameObject_OBNM , DIM( dsX00022A.szObjectName ) );
        pAppVars->iBusFuncRetCode =
            jdeCallObject( _J("GetNextUniqueKeyID") ,
            (LPFNBHVR)NULL , lpBhvrCom , lpVoid ,
            &dsX00022A , cmErrorMap_1 , NUMMAP_1 ,
            (JCHAR*)NULL , (JCHAR*)NULL , 0 );
        MathCopy( &dsF567101.fiukid , &dsX00022A.mnUniqueKeyID );
        MathCopy( &lpDS->mnUniqueKeyID_UKID , &dsX00022A.mnUniqueKeyID );
        /*               End UKID Generation                                     */

        while( (de=readdir(dir)) != NULL )
        {
            jdeToUnicode (szd_name, (ZCHAR *) de->d_name, DIM(szd_name), NULL);   

            if ((jdeStrcmp(szd_name, _J(".")) == 0)  || (jdeStrcmp(szd_name, _J("..")) == 0))
            {
                // Keep Reading...
            }
            else
            {

                jdeStrncpy(dsF567101.fifilename,szd_name,DIM(dsF567101.fifilename)-1);

                // Before Inserting check whether the flat file information has already been written
                // Go to the table using the flat file name
                idJDEDBReturn =    JDB_OpenTable (hUser, NID_F567101, ID_F567101_FILENAME, (NULL), (ushort) 0, (JCHAR *) NULL, &hRequestFetchSingle);
                if (idJDEDBReturn == JDEDB_FAILED)
                {
                    return (ER_ERROR);
                }

                else
                {
                    // Build the Key
                    jdeStrncpy(dsF567101Key2.fifilename, (const JCHAR *)(dsF567101.fifilename), DIM(dsF567101Key2.fifilename));

                    /*Fetch the Database table values based on the Parameters(dsF567101Key2 ).*/

                    idJDEDBReturn = JDB_FetchKeyed(hRequestFetchSingle, ID_F567101_FILENAME,(void *)&dsF567101Key2, (short)1,(void *) &dsF567101,FALSE);

                    if (idJDEDBReturn != JDEDB_PASSED)
                    {

                        //Generate UKID

                        if (MathZeroTest(&lpDS->mnUniqueKeyID_UKID) == 0)
                        {
                            jdeStrncpyTerminate( dsX00022A.szObjectName , lpDS->szNameObject_OBNM , DIM( dsX00022A.szObjectName ) );
                            pAppVars->iBusFuncRetCode =
                                jdeCallObject( _J("GetNextUniqueKeyID") ,
                                (LPFNBHVR)NULL , lpBhvrCom , lpVoid ,
                                &dsX00022A , cmErrorMap_1 , NUMMAP_1 ,
                                (JCHAR*)NULL , (JCHAR*)NULL , 0 );
                            MathCopy( &dsF567101.fiukid , &dsX00022A.mnUniqueKeyID );
                            MathCopy( &lpDS->mnUniqueKeyID_UKID , &dsX00022A.mnUniqueKeyID );
                            /*               End UKID Generation                                     */
                        }
                        else
                        {
                            MathCopy( &dsF567101.fiukid , &lpDS->mnUniqueKeyID_UKID);
                        }

                        nMathReturn = IntToMathNumeric(counter, &dsF567101.ficount);
                        // Set File Path
                        jdeStrncpy((JCHAR *)(dsF567101.fidrpt), (const JCHAR *)(lpDS->szDirectoryPath_DRPT),
                            DIM(dsF567101.fidrpt));
                        // Set EDSP Flag for Initial Upload
                        dsF567101.fiedsp = lpDS->cInitialUploadFlag_EDSP;
                        // Set Audit fields
                        jdeCallObject(_J("GetAuditInfo"), (LPFNBHVR) NULL, lpBhvrCom, lpVoid,
                            (LPVOID) &GetAuditParms, (CALLMAP *) NULL,
                            (int) 0, (JCHAR *) NULL, (JCHAR *) NULL, (int) 0); 

                        jdeStrncpy((JCHAR *)(dsF567101.fijobn), (const JCHAR *)(GetAuditParms.szWorkstation_UserId),
                            DIM(dsF567101.fijobn));
                        jdeStrncpy((JCHAR *)(dsF567101.fipid), (const JCHAR *)(_J("B567101")),
                            DIM(dsF567101.fipid));
                        jdeStrncpy((JCHAR *)(dsF567101.fiuser), (const JCHAR *)(GetAuditParms.szUserName),
                            DIM(dsF567101.fiuser));      
                        memcpy((void *)(&dsF567101.fiupmj), (const void *)(&GetAuditParms.jdDate),
                            sizeof(JDEDATE));              
                        MathCopy(&dsF567101.fitday, &GetAuditParms.mnTime);
                        // Table Insert
                        idJDEDBReturn = JDB_InsertTable(hRequestF567101, NID_F567101, (ID) NULL,
                            (void *) &dsF567101);

                        if (idJDEDBReturn == JDEDB_FAILED)
                        {
                            jdeWriteLog(_J("B567101"), 0,_J("Table Insert Failure"));
                        }

                        jdeWriteLog(_J("B567101"), 0,szd_name);
                        //counter++;
                    }
                } /* Open Table success */
                JDB_CloseTable(hRequestFetchSingle);
            }
        } /* end while */

        jdeClosedir(dir);
        JDB_CloseTable(hRequestF567101);
    }
    else
    {   
        iRet = ER_ERROR;
    }

    return iRet;
#else
    /************************************************************************
    * Function Clean Up
    ************************************************************************/

    return (ER_SUCCESS);

#endif

}

/* Internal function comment block */
/**************************************************************************
*   Function:  Ixxxxxxx_a   // Replace "xxxxxxx" with source file number
*                           // and "a" with the function name
*      Notes:
*
*    Returns:
*
* Parameters:
***********************************

1 comment: