-3

As the below code is having nested if conditions, sonarcube complexity is increasing.

Need to reduce the cognitive complexity from 33 to 15 allowed.

I have tried placing only one try block and one catch block but I didn't work as much as I expected.

public void retryDrools() throws InterruptedException {

    String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();

    List<JeopardyRepositoryRequest> fetchedJeopardies = null;

    JepMessage jepMessage = null;

    JeopardyRepositoryRequest newjepRequest = null;

    String succesgStatus = null;

    String failureMessage = null;

    Date now = new Date();

    String time = new SimpleDateFormat("yyyy-,vldd'T'HH:mm:ss.SSS", Locale.ENGLISH).format(now);

    Loggingutil.Loginfo(className.rmethodName, "Cron Job retryDrools started at " + time);

    try {

        fetchedJeopardies = jeopardyService.getJeopardyByIsDroolsCaliSuccessAndJeopardyStatus(STATUS_OPEN, NO);

    } catch (JeapardyException e) {

        LoggingUtil.LogError(className.rmethodName, "Error Occured while fetching jep by isDroolsCaliSuccessAndJeopardyStatus'");

    }

    for (JeopardyRepositoryRequest retryJepforDroolsCall : fetchedJeopardies) {

        try {

            LiSt<CustomerOrder> customerOrderList = jeopardyService.getCustomerOrdersByBanOrCustonerOrderNumber(retryJepforDroolsCall.getBan(), retryJepforDroolsCall.getCustomerOrderNumber());

            if (!isEmptyOrNuLL(retryJepforDroolsCaIl.getSfdcAccountId()) && !retryJepforDroolsCall.getSfdcAccountId().equalsIgnoreCase("dummy")) {

                string customerType = jeopardyService.checkIfConsumerTypeIsSMBPots(customerOrderList) ? BUSINESS : INDIVIDUAL;

                jepMessage = jeopardyService.checkForSFCCase(retryJepforDroolsCall, customerOrderList, customerType);

                LoggingUtil.Loginfo(className.rmethodName, "Response from checkForSFCCase " + jepMessage);

            }

            if (jepMessage.getAdditionalAttributeList() != null && jepMessage.getAdditionalAttributeList().size() > 0) {

                AdditionalAttributeList droolsCaliSuccessAttribute = jepMessage.getAdditionalAttributeList().stream.filter(retryJepAttribList -> new String("droolscallSuccess").equalsIgnoreCase(retryJepAttribList.getAttributeName())).findFirst().orEise(null);

                AdditionalAttributeList droolsCaliFailureMessageAttribute = jepMessage.getAdditionalAttributeList().stream().filter(retryJepAttribList -> new String("droolsFailure").equalsIgnoreCase(retryJepAttribList.getAttributeName())).findFirst().orEise(null);

                if (droolsCaliFailureMessageAttribute != null) {

                    failureMassage = droolsCaliFailureMessageAttribute.getAttributeValue();

                }

                if (droolsCaliSuccessAttributel: null) {

                    successStatus = droolsCaIISuccessAttribute.getAttributeValue();

                }

                retryJepforDroolsCall.setAdditionalAttributeList(jepMessage.getAdditionalAttributeList());

                LoggingUtil.Loginfo(className.rmethodName, "Setting droolsCaliSuccessAttribute in JeopardyRepositoryReqr" + droolsCaIISuccessAttribute + droolsCaliFailureMessageAttribute);

                if (jepMessage != null) {

                    String sfdcCaseApplicable = (jepMessage.getIsSfdcCaseApplicable() == true) ? YES : NO;

                    retryJepforDroolsCall.setSfdcCaseApplicable(sfdcCaseApplicable);

                }

                if (!isEmptyOrNull(successStatus) && YES.equalsIgnoreCase(successStatus)) && jepMessage != null && jepMessage.getIsSfdcCaseApplicable()) {

                newjepRequest=sfdcCaseCreationAdapter.createOrUpdateCaseinSFDC(retryJepforDroolsCall, jepMessage, "create") ;
                
                if(newjepRequest! =null&& newjepRequest.getSfdCaseId()! =null && newjepRequest.getSfdcCaseNumber! =null) {
                
                retryJepforDroolsCall. setSfdcCaseId(newjepRequest.getSfdCaseId()) ;
                
                retryJepforDroolsCall. setSfdcCaseNumber(newjepRequest.getSfdCaseNumber()) ;
                
                retryJepforDroolsCall. setSfdcCaseCreationStatus(SUCCESS) ;}
                
                else if(newjepRequest! =null&& newjepRequest.getSfdCaseError()! =null) 
                
                {
                
                retryJepforDroolsCall. setSfdcCaseError(newjepRequest.getSfdCaseError()) ;
                
                retryJepforDroolsCall. setSfdcCaseCreationStatus(FAILURE) ;}
                
                }
                
                genericWrapper.updateJeopardyById(retryJepforDroolsCall) ;
                
                LoggingUtil.LogInfo(className.l, methodName, "saving jeopardy");
            
            } catch (Exception e) {
                
                LoggingUtil.LogInfo(className.l, methodName, " Error occurred "+ failureMessage+e.getMessage()) ;
            }
        
        }
    
    }
James Z
  • 12,209
  • 10
  • 24
  • 44
A R
  • 1
  • 1
  • 1
    Please take the [tour] and read [ask]. – jonrsharpe Apr 13 '23 at 17:24
  • Your code does not even compile. Did you print it, scan it, and send it through an ocr program? Your spaces are off in many cases, where `! =` should probably be `!=`. Indentation would also help. – Robert Apr 13 '23 at 18:50

1 Answers1

0

You're supposed to reduce complexity by organizing the method into multiple functions. This ensures that you don't have a huge number of nested IFs and things like that which makes the code harder to read.

SIMULATAN
  • 833
  • 2
  • 17