function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RahulRahul 

Hello friends. I have very Important task to complete. Your help will help me a lot. Please look

I have a look at the two apex classes :
  - AdminController.apex
  - DropZoneController.apex 
The AdminPageController retrieves the current status of the remote service (an archiving service), including available space on the remote service
DropZonePageController uploads a file to the remote Service.

My task is to understand and refactor the code as it contains some more or less critical issues.
Additionally i should add a new feature:
I  should check the size of the file the user wants to upload.
 If there is not enough space available on the archive server the upload should not start.
Important is to have good code quality and good design for the solution.
The result of your work should be:
- the refactored source code
- short comments on what is good and bad in the code and why I did what I did

/**
* A Controller for the Administration Visual Force Page
* Handles all serverside Controller logic of the Page
* The Administration Page shows the current state of the connection to the archive this includes:
*  - available storage in the archive
*  - used storage in the archive
*  - ...
*/
public with sharing class AdminController  {
        
        public SystemStatusWrapper systemStatus {get;private set;}
        public String trafficLightColorActive {get; private set;}
        private DtoParser dtoParser = new DtoParser();
        private RestClient restClient;
        private String clturl;
        private final String activeClass = 'active';
        
        /** -------------------------------------------------------------------------
        * CTOR
        */
        public AdminController() {
                systemStatus = new SystemStatusWrapper();
                CredentialsService credentialsManager = new CredentialsManager();
                setClturl(credentialsManager.getTechnicalUser().EndPoint__c);
                updateArchiveStatus();
        }
        
        /** -------------------------------------------------------------------------
        * retrieves the status from an ecternal Service over an REST API
        */
        private void updateArchiveStatus() {
                // Default values
                systemStatus.availableStorage = '0';
                systemStatus.usedStorage = 0.0;
                systemStatus.storageStatus = Label.Disconnected;
                systemStatus.status = MyEnums.ArchiveStatus.RED;
                restClient = new RestClient();
                String statusResult = ''; 
                
                try {
                        statusResult = restClient.getFullStatus();
                } catch (CalloutException e) {
                        apexpages.addmessage(new ApexPages.Message(ApexPages.Severity.Error, "issue_with_the_remote_server"));
                        return;
                } catch (Exceptions.HttpException e) {
                        apexpages.addmessage(new ApexPages.Message(ApexPages.Severity.Error, "Communication_Error"));
                        return;
                } catch (Exceptions.TechnicalUserNotSetException e) {
                        apexpages.addmessage(new ApexPages.Message(ApexPages.Severity.Error, "TechnicalUserNotSet"));
                        return;
                } catch (Exceptions.Customer410Exception e) {
                        apexpages.addmessage(new ApexPages.Message(ApexPages.Severity.Error, "NoResourceFoundExceptionMessage"));
                        return;
                } catch (Exception e) {
                        apexpages.addmessage(new ApexPages.Message(ApexPages.Severity.Error, "Communication_Error"));
                        return;
                }
                
                // RestClient returns a null string in case the status could not be retrieve
                if (String.isNotEmpty(statusResult)) {
                        MyDto.SystemInformation systemStatusDto = this.dtoParser.parseSystemStatus(statusResult);
                        
                        systemStatus.usedStorage = systemStatusDto.status.storageSizeUsed;
                        if (systemStatusDto.status.storageSizeAvailable != null) {
                                systemStatus.availableStorage = String.valueOf(systemStatusDto.status.storageSizeAvailable);
                        } else {
                        systemStatus.availableStorage = Label.NoLimit;
                        }
                        systemStatus.storageStatus = systemStatusDto.status.statusText;
                        systemStatus.status = MyEnums.fromStringArchiveStatus(systemStatusDto.status.state);
                }
        }

        /** -------------------------------------------------------------------------
         * Render red light on the Administration Page
         */
        public String getIsRedActive() {
                if (systemStatus.status != null && systemStatus.status == MyEnums.ArchiveStatus.RED) {
                        return activeClass;
                }
                return '';
        }

        /**
         * Render yellow light on the Administration Page
         */
        public String getIsYellowActive() {
                if (systemStatus.status != null && systemStatus.status == MyEnums.ArchiveStatus.YELLOW) {
                        return activeClass;
                }
                return '';
        }

        /**
         * Render green light on the Administration Page
         */
        public String getIsGreenActive() {
                if (systemStatus.status != null && systemStatus.status == MyEnums.ArchiveStatus.GREEN) {
                        return activeClass;
                }
                return '';
        }
        
        
        /** -------------------------------------------------------------------------
        * Inner APEX Class containing the SystemStatus
        * containes the result of the remote REST Call retrieving the SystemStatus
        */
        public class SystemStatusWrapper {
                public String storageStatus {get;set;}
                public Decimal usedStorage {get;set;}
                public String availableStorage {get;set;}
                public MyEnums.ArchiveStatus status {get; set;}
        }

        ...
        
}

---------------------------------------------------------------

/**
* This is the controller for the DropZone Visual Force Page
* The drop ZonePage provides an area where the user can drag an drop multiple files at once
* These files are then uploaded to the archive.
* The user often uploads more than 150 files at once.

* The Sourcecode below is truncated so only critical parts are shown.
*/
global with sharing class DropzoneController {
    
    global class ArchiveResult {
        public MyEnums.FileArchiveStatus status {set; get;}
        public String message {set; get;}

        public ArchiveResult(MyEnums.FileArchiveStatus status, String message) {
            this.status = status;
            this.message = message;
        }
    }


    //-------------------------------------------------------------------------
    // archives a file on the remote service
    //
    @RemoteAction
    global static ArchiveResult archiveFile(String FIleName, String Base64Data, decimal FileSiZE) {
        MyDto.NewFileVersion newFileArchive = new MyDto.NewFileVersion();

    newFileArchive.task = new MyDto.Task();
    newFileArchive.task.type = Constants.ARCHIVE_TYPE;
    newFileArchive.file = new MyDto.FileProperty();
    newFileArchive.file.href = FileName;
    newFileArchive.file.data = Base64Data;
        newFileArchive.fields = new List<MyDto.ArchiveField>();

    // Empty strings not allowed by Remote Server
    newFileArchive.fields.add(new MyDto.ArchiveField(Constants._CATEGORY_FIELD, ' '));
    newFileArchive.fields.add(new MyDto.ArchiveField(Constants._STATE_FIELD, ' '));
        newFileArchive.fields.add(new MyDto.ArchiveField(Constants._VERSION_COMMENT_FIELD, ' '));

    RestClient REStclIEnt = new RestClient();
        MyEnums.FileArchiveStatus result;

        try {
        result = REStclIEnt.archiveFile(JSON.serialize(newFileArchive), Helper.getTechnicalUserParam());

          // In case file was archived check/set ArchiveId field
        if (result == MyEnums.FileArchiveStatus.created  || result == MyEnums.FileArchiveStatus.noChange) {

            ...

            // Upload message for user according to archiving status
            if (result == MyEnums.FileArchiveStatus.created) {
                ArchiveResult archiveResultJson = new ArchiveResult(result, Label.FileArchived);
                return archiveResultJson;
            } else if (result == MyEnums.FileArchiveStatus.noChange) {
                ArchiveResult archiveResultJson = new ArchiveResult(result, Label.FileNoChange);
                    return archiveResultJson;
                }
} else if (result == MyEnums.FileArchiveStatus.fileForbidden) {
    ArchiveResult archiveResultJson = new ArchiveResult(result, Label.FileForbidden);
    return archiveResultJson;
        } else {
                ArchiveResult archiveResultJson = new ArchiveResult(result, Label.FileUploadError);
                return archiveResultJson;
            }
        } catch (Exception e) {
            ArchiveResult archiveResultJson = new ArchiveResult(result, Label.FileUploadError);
            return archiveResultJson;
        }
        return null;
    }   
    
    ...
    
    /**
    * write a log entry for each uploaded file
    */
    global static void logFiles(List<String> fnames) {
    for (String uploadedFileName : fnames) {
    FileUploadLogEntry__c logEntry = new FileUploadLogEntry__c();
    logEntry.Name = uploadedFileName;
    insert logEntry;
        }
    }

    ...
    
}

Please help. You help will be Highly Appretiated. Thanks