• Maneesh J
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
PLEASE GIVE ME RESOLUTION AS SOON AS POSSIBLE:

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.

Your task is to understand and refactor the code as it contains some more or less critical issues.
Additionally you should add a new feature:
   You 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 our solution.
Admincontroller:

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;}
        }
}
        
}

DropzonePageController:


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;
        }
    }
    
}