File upload and process with RunBaseBatch

Upload file and process with RunBaseBatch framework

/// <summary>
/// Class to delete bogus online refund records in VKOnlineRefund table
/// </summary>
class VKDeleteOnlineRefund extends RunBaseBatch implements BatchRetryable
{
    DialogRunbase   dialog;

    Filename            filename;
    private str         availableTypes = '.csv';
    private const str   OkButtonName   = 'OkButton';
    private const str   FileUploadName = 'FileUpload';
    str                 textFile;

    Set                 set = new Set(Types::Record);
    SetEnumerator       setEnumerator;

    int                    fileLineNumber;
    container           currentLine;
    container            storageResult;

    #define.CurrentVersion(1)
    #localmacro.CurrentList
        filename,
        storageResult
    #endmacro

    public boolean canGoBatch()
    {
        return true;
    }

    protected boolean canGoBatchJournal()
    {
        return true;
    }

    public Object dialog()
  {
        DialogGroup      dialogGroup;
        FormBuildControl formBuildControl;
        FileUploadBuild  dialogFileUpload;

        dialog = new DialogRunbase(VKDeleteOnlineRefund::description(), this);
        dialogGroup = dialog.addGroup(VKDeleteOnlineRefund::description());
        formBuildControl = dialog.formBuildDesign().control(dialogGroup.name());

        dialogFileUpload = formBuildControl.addControlEx(classstr(FileUpload), FileUploadName);
        dialogFileUpload.style(FileUploadStyle::Standard);
        dialogFileUpload.baseFileUploadStrategyClassName(classstr(FileUploadTemporaryStorageStrategy));
        dialogFileUpload.fileTypesAccepted(availableTypes);
        dialogFileUpload.fileNameLabel("@SYS308842");

        return dialog;
    }

    public void dialogPostRun(DialogRunbase _dialog)
    {
        FileUpload fileUpload = this.getFormControl(_dialog, FileUploadName);
        fileUpload.notifyUploadCompleted += eventhandler(this.uploadCompleted);
        this.setDialogOkButtonEnabled(_dialog, false);
    }

    /// <summary>
    /// To handle file upload event and enable OK dialog button
    /// </summary>
    protected void uploadCompleted()
    {
        FileUpload fileUpload = this.getFormControl(dialog, FileUploadName);
        FileUploadTemporaryStorageResult uploadResult = fileUpload.getFileUploadResult();
        storageResult = uploadResult.pack();
        
        if (uploadResult.getUploadStatus())
        {
            fileUpload.notifyUploadCompleted -= eventhandler(this.UploadCompleted);
            filename = fileUpload.fileName();
            this.setDialogOkButtonEnabled(dialog, true);
        }
        else
        {
            warning(uploadResult.getLogMessage());
        }
    }

    protected void setDialogOkButtonEnabled(DialogRunbase _dialog, boolean _isEnabled)
   {
        FormControl okButtonControl = this.getFormControl(_dialog, OkButtonName);

        if (okButtonControl)
        {
            okButtonControl.enabled(_isEnabled);
        }
    }

    protected FormControl getFormControl(DialogRunbase _dialog, str _controlName)
    {
        return _dialog.formRun().control(_dialog.formRun().controlId( _controlName));
    }

    public void run()
    {
        this.processCSVFile();
    }

    public boolean runsImpersonated()
    {
        return true;
    }

    public container pack()
    {
        return [#CurrentVersion, #CurrentList];
    }

    public boolean unpack(container _packedClass)
    {
        Integer version = conPeek(_packedClass, 1);

        switch (version)
        {
            case #CurrentVersion:
                [version, filename, storageResult] = _packedClass;
                break;
            default:
                return false;
        }
        return true;
    }

    public boolean validate(Object _calledFrom = null)
    {
        boolean ret = true;

        if (!filename)
        {
            ret = checkFailed("@SYS18624");
        }

        return ret;
    }

    public static VKDeleteOnlineRefund construct()
    {
        return new VKDeleteOnlineRefund();
    }

    /// <summary>
    /// Description of the job
    /// </summary>
    /// <returns>Description of the job</returns>
    public static ClassDescription description()
    {
        return "Delete bogus online refunds";
    }

    public static void main(Args args)
    {
        VKDeleteOnlineRefund  deleteOnlineRefund;

        deleteOnlineRefund = VKDeleteOnlineRefund::construct();

        if (deleteOnlineRefund.prompt())
        {
            deleteOnlineRefund.runOperation();
        }
    }

    /// <summary>
    /// Processes uploaded CSV file
    /// </summary>
    public void processCSVFile()
    {
        #File
        CommaTextStreamIo localStream;
        FileUpload          fileUploadControl;
        FileUploadTemporaryStorageResult fileUploadResult = new FileUploadTemporaryStorageResult();

        if(this.isInBatch())
        {
            fileUploadResult.unpack(storageResult);
        }
        else
        {
            fileUploadControl = this.getFormControl(dialog, FileUploadName);
            fileUploadResult = fileUploadControl.getFileUploadResult();
        }
         
        if (fileUploadResult != null && fileUploadResult.getUploadStatus())
        {
            textFile = fileUploadResult.getDownloadUrl();
        }

        localStream = CommaTextStreamIo::constructForRead(File::UseFileFromURL(textFile));

        if (localStream.status() != IO_Status::Ok)
        {
            throw error(strfmt("Not possible to open the file. Error: %1", enum2str(localStream.status())));
        }

        currentLine = localStream.read();

        if (conlen(currentLine) < 2)
        {
            throw error("Incorrect file format");
        }

        currentLine = localStream.read();

        DataAreaId  dataAreaId;
        VKRefundID  refundID;

        ttsbegin;
        while (currentLine)
        {
            fileLineNumber++;

            dataAreaId  = conPeek(currentLine, 2);
            refundID    = conPeek(currentLine, 1);

            if (curExt() != dataAreaId)
            {
                changeCompany(dataAreaId)
                {
                    this.deleteOnlineRefundRecord(refundID);
                }
            }
            else
            {
                this.deleteOnlineRefundRecord(refundID);
            }

            currentLine = localStream.read();
        }
        info(strFmt("Total lines processed: %1", fileLineNumber));

        ttscommit;
    }

    private void deleteOnlineRefundRecord(VKRefundID _refundId)
    {
        VKOnlineRefund onlineRefund;

        select firstonly forupdate onlineRefund
            where onlineRefund.RefundID == _refundId;

        if (onlineRefund)
        {
            onlineRefund.delete();
        }
        else
        {
            warning(strFmt("Online refund record %1 not found in legal entity %2", _refundId, curExt()));
        }
    }

    protected boolean canRunInNewSession()
    {
        return false;
    }

    [Hookable(false)]
    public final boolean isRetryable()
   {
        return true;
    }

}


 

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics AX.

Authors are allowed to set their own AdSense units and "buy me a coffee" link.
Join us.

Blog Tags