gurudatta kr
gurudatta kr

Reputation: 27

How to do following operation in Dao implementation?

I have got an array response from the following code.

I am able to return the array result as above but how do i return a json object using one of the values from that array? I am very much new to Java, springboot and hibernate. Any help would be appretiated!

GoalPlanController

 @RequestMapping(method = RequestMethod.GET, path="/calculateRecurringAmount")
    public ResponseEntity<Object> getCalculateRecurringAmount( String accountID) {

        try {
            logger.info("get recurring amount by accountid:->", accountID);
            AccountsDTO[] goalPlan =  goalPlanService.getCalculateRecurringAmount(accountID);
            return new ResponseEntity<>(goalPlan, HttpStatus.OK);
        }catch(Exception ex) {
            logger.error("Exception raised retriving recurring amount using accountId:->" + ex);
            ErrorDTO errors = new ErrorDTO();           
            errors.setError(ex.getMessage());
            errors.setStatus(HttpStatus.SERVICE_UNAVAILABLE.value());

            return new ResponseEntity<>(errors, HttpStatus.SERVICE_UNAVAILABLE);                 
        } 
    }

This is GoalPlanDaoImplementation

@Autowired
private GoalPlanRepository goalPlanRepository;


@Override
public List<Accounts> getCalculateRecurringAmount(String accountID) {
    // TODO Auto-generated method stub
    List<Accounts> goalPlan = null;
     goalPlan = goalPlanRepository.getCalculateRecurringAmount(accountID);

    return   goalPlan.subList(0, 1);                

} 

GoalPlanRepository ->

public interface GoalPlanRepository extends JpaRepository<GoalPlan, String>{    

@Query("select ac from Accounts ac where ac.userId = :accountID")
public List<Accounts> getCalculateRecurringAmount(@Param("accountID") String accountID);

}

I get a array result as follows

{
       "accountID": "acc12345",
       "accountName": "hellooee",
       "accountType": "goalPlanner",
       "userId": "abcd",
       "bankName": null,
       "bankId": null,
       "debiitCardNumber": null,
       "availableBalance": null,
}
]```


Now using accountID I need to return a json object like this


   {
   "calculatedGoalAmount": [
       {
           "goalFrequency": "Monthly",
           "goalAmount": 0.4166666666666667,
           "fromAccount": "acc12345"
       },
       {
           "goalFrequency": "Quarterly",
           "goalAmount": 1.25,
           "fromAccount": "acc12345"
       }
   ]
}



My AccountsDTO has folllowing

   public class AccountsDTO {
private String accountID;   
private String accountName;
private String accountType;
private String userId;
private String bankName;
private String bankId;
private String debitCardNumber;


//getters and setters
}


And initilAmount, goalTimePeriod, goalAmount are the values entered by user. 
then i need to calculate    
monthly = (goalAmount-initialAmount)/(12*goalTimePeriod)
quarterly = (goalAmount-initialAmount)/(4*goalTimePeriod)
accountId = (got from the response array above)

Upvotes: 0

Views: 302

Answers (1)

Vimukthi
Vimukthi

Reputation: 880

First You need to create two classes.

CustomResponse Class

public class CustomResponse {
    private List<CalculatedGoalAmount> calculatedGoalAmount;

    //getters and setters
}

CalculatedGoalAmount Class

public class CalculatedGoalAmount {
    private String goalFrequency;
    private double goalAmount;
    private String fromAccount;

    //getters and setters
}

Then inside your getCalculateRecurringAmount method write below code. Note that I have no idea about your AccountsDTO class.

@RequestMapping(method = RequestMethod.GET, path="/calculateRecurringAmount")
public ResponseEntity<Object> getCalculateRecurringAmount( String accountID) {
    CalculatedGoalAmount calculatedGoalAmount = null;
    CustomResponse customResponse = null;
    try {
        customResponse = new CustomResponse();
        AccountsDTO[] goalPlan =  goalPlanService.getCalculateRecurringAmount(accountID);

        for (AccountsDTO accountsDTO : goalPlan) {
            calculatedGoalAmount = new CalculatedGoalAmount();
            calculatedGoalAmount.setFromAccount(accountsDTO.getFromAccount());
            calculatedGoalAmount.setGoalAmount(accountsDTO.getGoalAmount());
            calculatedGoalAmount.setGoalFrequency(accountsDTO.getFrequency());

            customResponse.getCalculatedGoalAmount().add(calculatedGoalAmount);
        }
        return new ResponseEntity<>(customResponse, HttpStatus.OK);
    }catch(Exception ex) {
        logger.error("Exception raised retriving recurring amount using accountId:->" + ex);
        ErrorDTO errors = new ErrorDTO();           
        errors.setError(ex.getMessage());
        errors.setStatus(HttpStatus.SERVICE_UNAVAILABLE.value());

        return new ResponseEntity<>(errors, HttpStatus.SERVICE_UNAVAILABLE);                 
    } 
}

Upvotes: 1

Related Questions