Friday, August 6, 2021

x++ code to move files from one folder to another after processing business logic

 public void run()

{

    CommaTextIo     commaTextIo;

    container values;

      FolderName      sourceFolder = @"\\test\Source";

    FolderName      destinationFolder = @"\\test1\Archive";

    System.Array    files;

    int fileCount, i;

    Filename    filepath;

    Filename    filename;

    Filename    fileType;

    // code added by dilip on 8/1/2018

           // str         currentFile;    - Declared the cuurentFile and destinationFile in classDeclaration Method

            #File

         InteropPermission   interopPerm;

         FileIOPermission    filepermission;

         Set                 permissionSet;


            // code ended by dilip on 8/1/2018



    InventJournalTable  inventJournalTable;

    InventJournalTrans  inventJournalTrans;

    InventDim           inventDim;

    JournalCheckPost    journalCheckPost;

    InventJournalName   inventJournalName;

    InventBatch         inventBatch;


    boolean             isFirst;


    new InteropPermission(InteropKind::ClrInterop).assert();


    try

    {

        files = System.IO.Directory::GetFiles(sourceFolder);


        fileCount = files.get_Length();


        for(i = 0; i < fileCount; i++)

        {

            currentFile = files.GetValue(i);

            commaTextIo = new CommaTextIo(currentFile,'R');

            isFirst = true;


            values = commaTextIo.read();

            values = commaTextIo.read();


            ttsBegin;


            while(values != conNull())

            {

                if(isFirst)

                {

                    inventJournalTable.clear();


                    inventJournalName = InventJournalName::find(conPeek(values, 2));


                    if(!inventJournalName)

                    {

                        break;

                    }


                    inventJournalTable.initFromInventJournalName(inventJournalName);

                    inventJournalTable.insert();


                    isFirst = false;

                }


                inventJournalTrans.clear();


                inventJournalTrans.initFromInventJournalTable(inventJournalTable);

                inventJournalTrans.TransDate    = str2Date(conPeek(values, 4), 213);

                inventJournalTrans.ItemId       = conPeek(values, 5);

                inventJournalTrans.initFromInventTable(InventTable::find(inventJournalTrans.ItemId));

                inventJournalTrans.Qty          = str2num(conPeek(values, 11));


                inventDim.InventColorId         = conPeek(values, 6);

                inventDim.InventSizeId          = conPeek(values, 7);

                inventDim.InventLocationId      = conPeek(values, 8);

                inventDim.InventSiteId          = conPeek(values, 9);

                inventDim.inventBatchId         = conPeek(values, 10);


                select firstOnly RecId from inventBatch

                    where inventBatch.inventBatchId ==  inventDim.inventBatchId

                    && inventBatch.itemId ==  inventJournalTrans.ItemId;


                if(!inventBatch.RecId)

            {

                   inventBatch.clear();


                  inventBatch.initValue();

                  inventBatch.itemId = inventJournalTrans.ItemId;

                  inventBatch.inventBatchId = inventDim.inventBatchId;

                  inventBatch.insert();

               }


                inventJournalTrans.InventDimId = InventDim::findOrCreate(inventDim).inventDimId;


                  inventJournalTrans.setCostPrice('', inventJournalTrans.inventDim(),  inventJournalTrans.inventTableModuleInvent());


                inventJournalTrans.insert();


                values = commaTextIo.read();

                  inventJournalTable.NumOfLines += 1;

        inventJournalTable.update();

            }


            //if(inventJournalTable)

            //{

                //journalCheckPost = InventJournalCheckPost::newPostJournal(inventJournalTable);

                //journalCheckPost.run();

            //}


            ttsCommit;


            [filepath, filename, fileType] = fileNameSplit(currentFile);


           // code added by dilip on 8/1/2018

             destinationFile = destinationFolder + "\\" + filename + fileType;


                permissionSet =  new Set(Types::Class);

                permissionSet.add(new InteropPermission(InteropKind::ClrInterop));

                permissionSet.add(new FileIOPermission(destinationFile,'w'));

                permissionSet.add(new FileIOPermission(currentFile,'rw'));

                CodeAccessPermission::assertMultiple(permissionSet);


                    //copy the file from source folder to destination folder

                    WINAPIServer::copyFile(currentFile, destinationFile);


                    //delete the file from source folder

                    commaTextIo.finalize();

                    commaTextIo = null;

                    filepermission = new FileIOPermission(currentFile, 'w');

                    filepermission.demand();

                    System.IO.File::Delete(currentFile);

                    CodeAccessPermission::revertAssert();

        // code ended by dilip on 8/1/2018

        }

    }

    catch

    {

        error("Import process failed");

    }


    CodeAccessPermission::revertAssert();

}

No comments:

Post a Comment